Tokens
The Free Pascal Reference guide names five kinds of token: reserved words, identifiers,
operators, separators (white space) and constants (numbers and strings). Names and
reserved words are not case sensitive: BEGIN, Begin and
begin are the same word, and so are MyProc and
myproc.
Comments
| Form | Ends | Where |
|---|---|---|
{ … } | at the next }, over any number of lines | Turbo Pascal and every dialect after it |
(* … *) | at the next *), over any number of lines | ISO 7185 and every dialect; the form of the original Pascal report |
// … | at the end of the line | Delphi and Free Pascal (not in ISO 7185 or Turbo Pascal) |
{$ … }, (*$ … *) | as above | A compiler directive, which looks like a comment. See Compiler directives |
Free Pascal lists (* and *) as equivalent to {
and }. A comment ends with the closing delimiter of the form that opened
it, except in ISO mode ({$mode iso} and Extended Pascal mode), where the
standard lets a comment opened with { end with *) and one
opened with (* end with }.
Nesting
- A comment of one kind may hold the other two kinds as plain text:
{ (* x *) },(* { x } *)and{ // x }are each one comment. - A comment of the same kind nests only where the mode allows it. In Free Pascal’s own mode (
fpc, the default) and inobjfpcmode the compiler counts the levels, so{ a { b } c }is one comment (the mode switchnestedcommentsturns this on or off). Indelphi,tp,macpasandisomodes, and in Delphi and Turbo Pascal themselves, a comment ends at the first closing delimiter, so{ a { b }is a whole comment andc }is code. - A
//comment that holds a{or(*whose closing delimiter is on a later line is an error in Free Pascal.
The file viewer on this site follows the mode directives it meets
({$mode objfpc} and {$mode fpc} turn nesting on,
{$mode delphi}, tp, macpas turn it off,
iso and extendedpascal let either closing delimiter end a
comment, and {$modeswitch nestedcomments} switches it). A file with no
mode directive is read as Delphi or Turbo Pascal, without nesting.
Identifiers
- An identifier starts with a letter or an underscore and goes on with letters, digits and underscores. ISO 7185 allows no underscore, only letters and digits; Turbo Pascal, Delphi and Free Pascal allow it. Free Pascal counts between 1 and 127 significant characters.
- Delphi and Free Pascal also accept letters above code 127 (Unicode letters) in names.
- A reserved word may be used as a name by writing
&in front of it:procedure &do;declares a procedure calleddo, and&do;calls it (Delphi, and Free Pascal 2.5.1 and later in its Free Pascal and Delphi modes). - A name may be qualified by its unit (
SysUtils.IntToStr), its record or object (P.X) or, in Delphi and Free Pascal, a unit scope (System.SysUtils.IntToStr). - The directives (below) are not reserved. They are ordinary names except where they are used as directives, so
name,index,readandwritecan be variables.
Reserved words
A reserved word cannot be redefined. Each dialect has more than the one before.
| Dialect | Reserved words |
|---|---|
| ISO 7185 (35) | and array begin case const div do downto else end file for function goto if in label mod nil not of or packed procedure program record repeat set then to type until var while with |
| Extended Pascal (ISO/IEC 10206) adds | bindable export import module only otherwise pow protected qualified restricted value (and the operator **) |
Turbo Pascal 7 (and Free Pascal in tp mode) adds to ISO | absolute asm constructor destructor implementation inherited inline interface object shl shr string unit uses xor; Free Pascal also counts operator, reintroduce and self as reserved in its Turbo Pascal list |
| Delphi and Free Pascal’s Object Pascal modes add | as class dispinterface except exports finalization finally initialization is library on out property raise resourcestring threadvar try (this is Free Pascal’s list for its Object Pascal modes; Delphi’s own list was not read here) |
| Oxygene (RemObjects Elements) adds | method, namespace and others that its documentation lists; not checked here |
In Free Pascal’s tp mode only the Turbo Pascal words are reserved, and the
Delphi ones can be redefined.
Directives and modifiers
These words are meaningful only where a directive may stand: after a procedure,
function or method heading, in a property declaration, in a class declaration after
class, and in a few other places. Elsewhere they are plain names.
| Where | Words |
|---|---|
| After a routine heading (Delphi and Free Pascal) | abstract alias assembler cdecl cppdecl cvar deprecated dispid dynamic experimental export external far far16 final forward inline interrupt iocheck local message near noreturn nostackframe oldfpccall overload override pascal platform register reintroduce safecall saveregisters sealed softfloat static stdcall unaligned unimplemented unsafe varargs virtual winapi |
After external or in exports | name index delayed resident |
| In a property | read write stored default nodefault implements index readonly writeonly dispid |
| Visibility | private protected public published automated strict (strict private, strict protected) |
In a type or after class, record | abstract sealed helper final generic specialize reference (reference to procedure) |
| Special positions | at (after raise), on (in an except part), out (a parameter), absolute (a variable), package, requires, contains (a package file), segment and intrinsic (UCSD), result (the function result variable), self |
Free Pascal’s Reference guide lists these modifiers: absolute abstract alias assembler bitpacked break cdecl continue cppdecl cvar default deprecated dynamic enumerator experimental export external far far16 forward generic helper implements index interrupt iocheck local message name near nodefault noreturn nostackframe oldfpccall otherwise overload override pascal platform private protected public published read register reintroduce result safecall saveregisters softfloat specialize static stdcall stored strict unaligned unimplemented varargs virtual winapi write. Delphi also has at contains delayed package readonly requires resident sealed unsafe writeonly (from memory; DocWiki was not read).
Symbols and operators
The characters with a special meaning in Free Pascal are
+ - * / = < > [ ] . , ( ) : ^ @ { } $ # & %
and the pairs << >> ** <> >< <= >= := += -= *= /= (* *) (. .) //.
In a range specifier, (. and .) stand for [ and
] (the alternative tokens of ISO 7185, for keyboards without brackets).
These pairs keep their ordinary meaning inside strings.
| Token | Use |
|---|---|
:= | assignment |
+= -= *= /= | assignment with an operation (Free Pascal with {$COPERATORS ON}, Delphi never) |
+ - * / | add (and set union, string and array concatenation), subtract (set difference), multiply (set intersection), real divide |
div mod | integer divide and remainder |
and or xor not | boolean and bitwise operators |
shl shr (and << >> in Free Pascal) | shift left and right |
= <> < > <= >= | comparison; on sets <= and >= mean subset and superset |
in | set membership (and for … in) |
is as | class type test and checked type cast |
** | power (Free Pascal, Extended Pascal) |
>< | symmetric set difference (Free Pascal) |
@ | address of; @@ in a procedural variable expression takes the address of the variable |
^ | pointer type and dereference (and the start of a control character) |
., .. | field access and the end of the program; a range in a subrange, a set or a case label |
, ; : | list separator, statement separator, and the separator in declarations, labels and case arms |
( ) [ ] | grouping and calls; indexes, sets, array constructors, and Delphi attributes |
$ # & % | prefixes of hexadecimal numbers, character codes, octal numbers or escaped names, and binary numbers |
The precedence of the operators is on the Expressions page.
Numbers
| Form | Example | Notes |
|---|---|---|
| Decimal integer | 0, 1234 | The type is the smallest that holds the value |
| Hexadecimal | $FF, $deadBEEF | $ then hex digits; case does not matter |
| Octal | &17 | & then octal digits (Free Pascal 1.0.7 and later). The Reference guide says Turbo Pascal and Delphi compatibility modes do not have it, but the compiler source reads it in the Free Pascal and Delphi modes and not in tp mode |
| Binary | %11111111 | % then binary digits (Free Pascal). Not in tp mode |
| Real | 3.14, 0.314E1, 2e-3 | Digits, a point, digits and an optional scale factor: E or e, an optional sign, digits. A real always has digits on both sides of the point, or none and only a scale factor |
| Digit separator | 1_000_000, $FF_FF | An underscore between digits. Free Pascal reads it when the underscoreisseparator mode switch is on, which its Delphi mode turns on. The compiler source shows it in all four bases and not before the first digit |
| Sign | -5, +2.5 | The sign is the unary operator, not part of the number |
Hexadecimal literals have no h suffix and no 0x prefix in
Pascal (the inline assembler does accept 0FFh and 0xFF). There are no
suffixes for the type of a number and no literal for a real in hexadecimal.
Character and string literals
- Quoted string. Zero or more characters between single quotes, on one line. Two quotes in a row stand for one quote:
'it''s'.''is the empty string, and a one-character string is also a character constant. There are no backslash escapes. A quoted string cannot hold a line end. - Character code.
#followed by a number gives the character with that code:#27,#$1B. Free Pascal also reads#&33(octal) and#%11011(binary). Codes above 255 give a wide character, and above 65535 a surrogate pair; the Free Pascal compiler accepts codes up to#$10FFFF. - Control character.
^and a letter:^Ais code 1,^Mis 13,^Zis 26.^@is 0,^[is 27, and the characters@ A–Z [ \ ] ^ _give 0 to 31. The caret is a control character only where a pointer type or a dereference cannot stand, sop^and^Integerare pointer syntax. - Concatenation. Quoted strings and character codes may be written next to each other without an operator and form one string:
'Line one'#13#10'Line two','x'^G'y'. That is the only way to put a control character in a string. - Multi-line string. Delphi 12 and the development version of Free Pascal (3.3.1, behind the
multilinestringsmode switch, which its Object Pascal and Delphi modes turn on) take a string that opens with'''(three quotes, or any odd number of at least three) as the last thing on a line, and closes with a line that holds only white space and the same number of quotes. The text in between, line ends included, is the string; the indentation of the closing line is taken off every line.'''followed by text on the same line is not a multi-line string. - Backtick string. The Free Pascal compiler source also reads
`…`as a multi-line string (a doubled backtick is one backtick), behind the same mode switch. - Double quotes. Oxygene also writes strings with double quotes. Standard Pascal, Delphi and Free Pascal do not.
- Encoding. A string is 8-bit characters (ASCII or UTF-8) or 16-bit wide characters. In Free Pascal source the characters are read in the code page of the file, UTF-8 by default; Delphi 2009 and later read UTF-8 or UTF-16 files.
WriteLn('It''s ', 'a ''quoted'' word');
S := 'tab:'#9'here'#13#10;
Esc := #27 + '[0m';
Bell := ^G;
Hex := #$41#$42;
Text := '''
First line
Indented line
''';
Labels
A label in standard Pascal is an unsigned integer of up to four digits (0
to 9999). Turbo Pascal, Delphi and Free Pascal also allow identifiers as
labels. Labels are declared with label and used with goto
(see Statements). In Free Pascal the
{$GOTO} directive controls whether goto and label
are allowed.
How the viewer colours it
| Colour class | What gets it |
|---|---|
| comment | { }, (* *) and // comments |
| directive | {$…} and (*$…*) compiler directives (a letter must follow the $); the name of a Delphi attribute in [Name(…)] before a declaration |
| string | quoted strings, # character codes, ^ control characters, ''', backtick and double-quoted strings, joined into one when written side by side |
| number | decimal, $ hexadecimal, % binary and & octal numbers, reals with a scale factor, underscores between digits |
| keyword | the reserved words, and the directives where they are directives (see above) |
| type | the standard types and classes of the standard library page, except where the name is followed by a colon or follows a dot |
| literal | true false nil maxint maxlongint |
| function | a name followed by (; the name declared after procedure, function, constructor, destructor and operator; and the standard routines that need no parentheses |
| variable | Result, and the registers and tag labels inside asm blocks |
| plain | names, operators, separators |