Lexical Structure

This is the Free Pascal language diagrams page. The diagrams below present visual representations of the Free Pascal language rules. At this time not all rule diagrams have been posted. If you find an error or would like to add new diagrams, you can use the edit this page function to alter or add new diagrams.

Legend

These are common shapes and their meanings used used in the diagrams. The link shape navigates to other diagrams when clicked.

EOL

End of line which varies by platform.

Symbol

The following single and paired symbols have special. In the diagrams to follow the open and close parenthesis symbols are not always shown. For example in the (7 + 4) * 2 expression the number constant diagram omits these types the parenthesis.

Operator

An operator is any of the following symbols or keywords.

Letter

Letters include all upper and lower case characters A to Z along with the underscore character.

Digit

Digits include the characters 0 to 9.

Hex Digit

Hexadecimal digits include the 0 to 9 digits plus the case insensitive characters A to F.

Integer Literal

An integer literal is a series of digits beginning with an optional sign.

Hex Literal

A hexadecimal begins with a dollar sign character and is followed by one or more hex digits.

Number Literal

A number literal is used in places where either an integer or hex literal is valid.

Float Literal

A float literal begins with an optional sign. There must be at least one digit before the decimal point. An optional mantissa can be used to move the decimal point to the right with a positive sign and to the left with a negative sign.

Mantissa

The mantissa is used to raise or lower a floating point literal numbers by factors of 10.

Char Literal

A char literal can be used to specify characters which are either unprintable or easier to remember by character code.

String Literal

A string is a chain of on or more characters. In Free Pascal literal strings begin and end with a ' single quote character or a series of one or more char literals. When spanning multiple lines in source code that can be joined using the + operator.

Boolean Literal

A boolean literal signifies a value or true or false.

Enumeration

An enumeration a collection of enumeration values surrounded by parenthesis.

Enumeration Value

A enumeration value is named identifier belonging to an enumeration which can be evaluated to a number. Enumeration values are assigned a number by the compiler unless specifically assigned a value by the programmer.

Ordinal

An ordinal is any of the following.

Subrange

A subrange is the inclusive list of values between two ordinals. When defining a subrange the two underlying types must be matched. For example the subrange 'A'..'Z' is valid while 65..'Z' is not.

Identifier

An identifier is must begin with at least one letter and can then be followed by any combination of letters or digits, but if cannot conflict with a keyword.

Identifier Dot

A dotted identifier an identifier that is optionally followed by several more identifiers separated by . dots.

Identifier List

A list of dotted identifiers that is optionally followed by several more dotted identifiers separated by , commas.

Comment

A comment is treated by the compiler like white space. Three comment styles are available. The single line comment style begins with // two forward slashes and is terminated by the first EOL marker. The other two styles can space across multiple lines. When comments are nested, the first comment style terminates the nested child comments.

Program Header

A program starts with the keyword program and is followed by a dotted identifier.

Unit Header

A unit starts with the unit keyword and is followed by a dotted identifier.

Program Structure

A program is structured starting with a header, followed by a program block, and ending with a begin and end. with statements in between forming the main program entry point.

Unit Structure

A unit is structured starting with a header followed by interface and implementation sections. The initialization section is optional.

Uses Clause

The uses clause is an optional section that can be placed at the top of a program block or a unit's interface and implementation sections. It begins with the uses keyword and is followed by one or more unit names. The compiler searches it's include path to find unit files matching the names in the uses clause, then add their declarations to the current program or unit's namespace.

Uses Unit

Each unit in a uses clause contains a dotted identifier and may optionally specify a file path.

Program Block

The program block starts with an optional uses clause which is followed by implementation code.

Interface Section

A unit's interface section starts with an optional uses clause which is followed by declarations.

Implementation Section

A unit's implementation section follows the interface section. It may have it's own uses clause which is followed by implementation code.

Initialization Section

A unit may contain both initialization and finalization sections at its end. Each section may contain zero or more statements. The initialization section must precede the finalization section.

Declarations

A unit's interface section can contain many declarations. Each declaration starts with a block from one of seven possible keywords: var threadvar const resourcestring type function procedure.

Implementations

A unit's implementation section may contain new declarations which become private to the unit. It is required to provide implementation code for any functions or procedure in the previous section which haven't been marked with the external directive.

Local Implementations

A local implementation can be used inside a function or procedure implementation. I follows the same rules as regular implementations, but threadvar and resourcestring are disallowed.

Char Constant

A char constant can be build using a char literal or a dotted identifiers which references another char constant by name.

String Constant

A string constant can be build using a string literal or a dotted identifiers which references another string constant by name.

Number Constant

A number constant can be build using a number literal or a dotted identifiers that references another number constant by name. Integer math operations which can be fully evaluated at compile time are allowed.

Float Constant

A floating point constant can be build using a number literal, floating literal, or a dotted identifiers that references another float constant by name. Math operations which can be fully evaluated at compile time are allowed. Please note, this includes certain math functions not shown in the diagram.

Boolean Constant

A boolean constant can be build using a boolean literal or a dotted identifier which references another boolean constant by name. Boolean operatators can be applied to the constant.

Subrange Constant

A subrange constant is a collection of one or more subranges or ordinals of the same type separated by commas.

Pointer Constant

The only possible pointer constant is nil.

Constant

All of constants mentioned above can be group under the single name constant. A constant can be surrounded by an optional typecast when appropriate.

Identifier List

An identifier list is a series of one or more identifiers separated by commas.

Var Block

A var block contains one or more variable declarations. Each variable has an identifier and a type. When a single identifier is used it can be assigned an initial value using a constant.

Const Block

A const block contains one or more constant declarations. Each constant has an identifier and a constant value. When a type identifier is used it's possible to reassign the constant at runtime if assignable typed constants is enabled in the compiler.

Resource String Block

Resource strings have an identifier and must be assigned a string value at compile time.

Function Header

A function header includes the name of the function, optional generic arguments, an optional list of parameter, and returns a value of a given type name. It may have directives at the end such as calling convention or an external marker.

Procedure Header

A procedure header includes the name of the procedure, optional generic arguments, and an optional list of parameter. Procedures do not declare a return value. They have the same options for directives as a function header.

Generic Block

A generic block is an optional argument to functions, procedure, and types. When used with functions or procedures it precedes the parameter list. When used with a type it follows the type identifier.

Generic Constraint

Constraints can be applied to a generic argument. The constraint can either be a list of heap allocated identifier names, or class which is short for the TObject identifier. When the constructor keyword is used the identifier type must provide a single constructor with no arguments. The keyword record can be used to constraint a generic argument to stack based types.

Function Implementation

A function implementation can either appear in the program block or in a unit's implementation section. Variables, constants, types, and other procedures and functions can be nested inside a function. These items are known as local implementations and they are private to the function. A function implementation may contain statements between begin and end;
Note: the external directive cannot be used inside a function implementation.

Procedure Implementation

Other than the procedure header, a procedure implementation is identical to a function implementation.

Procedure Implementation

Other than the procedure header, a procedure implementation is identical to a function implementation.

Statement

A statement's trailing ; semicolon is optional when a single statement is used. When an if statement has an else clause the ; semicolon cannot be used at the close of the then statement. In all other situations the ; semicolon must be placed at the end of a statement.

Boolean Expression

A boolean expression one which ultimately resolves to a boolean value.

Ordinal Expression

An ordinal expression one which ultimately resolves to an ordinal value.

Case Condition

A case statement contains one or more case conditions between the keywords of and end.

Case Statement

A case statement is a flow controls structure requiring an ordinal expression. Program flow is directed through a series of case conditions. When the result of the expression falls within the range of a case condition the flow moved to the statement attached to the condition. If no case condition matches the result of the expression flow can optionally be moved to an else statement.

For Statement

A looping statement beginning for the for keyword and ending with either to or downto. Identifier must be local, and ordinal expression must evaluate to the same type as the identifier. The to or downto keyword must be followed by a statement.

For In Statement

A looping statement beginning for the for keyword and ending with do. Expression between the for and do must evaluate to an enumerable type. The do keyword must be followed by a statement.

If Statement

The if statement is a flow control structure requiring a boolean expression. When the expression evaluates to True the flow moved to the then statement. When an optional else clause is provided flow is moved to it when the expression evaluates to False.
In situations where else is used, the then Statement must start with begin and close with end and the then statement's trailing ; semicolon must be omitted.

While Statement

The while statement is a flow control structure requiring a boolean expression. Flow is repeatedly moved to the statement following do while the expression evaluates to True. If a Break procedure is encountered inside a while statement flow immediately stops and moves to the next statement following the while statement. If a Break procedure is encountered inside a while statement flow immediately moves to the beginning of the statement after the do keyword.

Repeat Statement

The repeat statement is a flow control structure requiring a boolean expression. Flow is moves to the statement after the repeat keyword and moves back the expression evaluates to True. If a Break procedure is encountered inside a while statement flow immediately stops and moves to the next statement following the while statement. If a Break procedure is encountered inside a while statement flow immediately moves to the beginning of the statement after the do keyword.

Assignment

An assignment statement is one which copies a right value to a left value. The type of the right value must be compatible with that of the left. The expression on the left must resolve to a writable value. Writeable values are those which are variables, fields, properties, dereferened pointers, and when enabled writable typed constants.

Expression

A collection of dotted identifiers with optional, access and operators that does not contain an assignment := symbol.

Access

Brackets [ < or up caret ^ used to access a property or data structure.

Expression List

One or more expressions separated by a , command.

Generic Arguments

One or more identifier lists, opening with a $lt; angle bracket and closing with > angle bracket. Identifier lists may open other generic arguments.

Type Alias

A type alias creates another optionally strongly typed identifier based on a type identifier.