Sign in Join
Wiki copytemporary

Scanner/Tokenizer: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11back to contents [[FPC internals]]
22
33# Scanner/Tokenizer
44
55The scanner and tokenizer is used to construct an input stream of tokens which will be fed to the parser. It is in this stage that the preprocessing is done, that all read compiler directives change the internal state variables of the compiler, and that all illegal characters found in the input stream cause an error.
66
77Info about how macros work: [[Macro internals]]
88
99## Architecture
1010
1111The general architecture of the scanner is shown in the following figure:
1212
1313<http://www.pjh2.de/fpc/CompilerInternalsFigure02.png>
1414
1515Several types can be read from the input stream, a string, handled by `readstring`, a numeric value, handled by `readnumeric`, comments , compiler and preprocessor directives.
1616
1717### Input stream
1818
1919*(last updated for fpc version 1.0.x)*
2020
2121The input data is handled via the standard way of handling all the I/O in the compiler. That is to say, that it is a hook which can be overriden in **comphook.pas (do_openinputfile)**, in case where another I/O method wants to be used.
2222
2323The default hook uses a non-buffered dos stream contained in **files.pas**
2424
2525### Preprocessor
2626
2727*(last updated for fpc version 1.0.x)*
2828
2929The scanner resolves all preprocessor directives and only gives to the parser the visible parts of the code (such as those which are included in conditional compilation). Compiler switches and directives are also saved in global variables while in the preprocessor, therefore this is part is completely independent of the parser.
3030
3131#### Conditional compilation (scandir.inc, scanner.pas)
3232
3333*(last updated for fpc version 1.0.x)*
3434
3535The conditional compilation is handled via a preprocessor stack, where each directive is pushed on a stack, and popped when it is resolved. The actual implementation of the stack is a linked list of preprocessor directive items.
3636
3737#### Compiler switches (scandir.inc, switches.pas)
3838
3939*(last updated for fpc version 1.0.x)*
4040
4141The compiler switches are handled via a lookup table which is linearly searched. Then another lookup table takes care of setting the appropriate bit flags and variables in the switches for this compilation process.
4242
4343## Scanner interface
4444
4545*(last updated for fpc version 1.0.x)*
4646
4747The parser only receives tokens as its input, where a token is a enumeration which indicates the type of the token, either a reserved word, a special character, an operator, a numeric constant, string, or an identifier.
4848
4949Resolution of the string into a token is done via lookup which searches the string table to find the equivalent token. This search is done using a binary search algorithm through the string table.
5050
5151In the case of identifiers, constants (including numeric values), the value is returned in the **pattern** string variable , with the appropriate return value of the token (numeric values are also returned as non-converted strings, with any special prefix included). In the case of operators, and reserved words, only the token itself must be assumed to be preserved. The read input string is assmued to be lost.
5252
5353Therefore the interface with the parser is with the **readtoken()** routine and the **pattern** variable.
5454
5555### Routines
5656
5757#### ReadToken
5858
5959| | |
6060|----|----|
61-| Declaration: | <b>`procedure`</b>` ReadToken;` |
61+| Declaration: | `procedure`` ReadToken;` |
6262| Description: | Sets the global variable token to the current token read, and sets the pattern variable appropriately (if required). |
6363
6464### Variables
6565
6666#### Token
6767
6868| | |
6969|----|----|
70-| Declaration: | <b>`var`</b>` Token: TToken;` |
70+| Declaration: | `var`` Token: TToken;` |
7171| Description: | Contains the contain token which was last read by a call to ReadToken |
7272| See also: | ReadToken |
7373
7474#### Pattern
7575
7676| | |
7777|----|----|
78-| Declaration: | <b>`var`</b>` Pattern: String;` |
78+| Declaration: | `var`` Pattern: String;` |
7979| Description: | Contains the string of the last pattern read by a call to ReadToken |
8080| See also: | ReadToken |
8181
8282## Assembler parser interface
8383
8484*(last updated for fpc version 1.0.x)*
8585
8686The inline assembler parser is completely separate from the pascal parser, therefore its scanning process is also completely independent. The scanner only takes care of the preprocessor part and comments, all the rest is passed character per character to the assembler parser via the **AsmGetChar()** scanner routine.
8787
8888### Routines
8989
9090#### AsmGetChar
9191
9292| | |
9393|--------------|-------------------------------------------------|
94-| Declaration: | <b>`function`</b>` AsmGetChar: Char;` |
94+| Declaration: | `function`` AsmGetChar: Char;` |
9595| Description: | Returns the next character in the input stream. |
9696
9797Next chapter: [[The parse tree]]
9898
9999---
100100*From the Free Pascal wiki, page "Scanner/Tokenizer", as it stood on 2020-05-24 after a change by E-ric, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
101101Category: FPC internals

Back to history