Files
bds.mr.dpg/doc/grammars/pascal/wpLexer.g
T
2026-01-03 18:31:15 +01:00

238 lines
5.1 KiB
Plaintext

unit wpLexer;
lexer TwpLexer;
options
{
exportVocab=wpLexer;
caseSensitive=false;
testLiterals=false;
k=2;
}
tokens
{
"do";
"if";
"in";
"of";
"or";
"to";
"and";
"div";
"end";
"for";
"mod";
"nil";
"not";
"set";
"var";
"xor";
"case";
"else";
"file";
"goto";
"then";
"type";
"uses";
"with";
"array";
"begin";
"const";
"label";
"until";
"while";
"downto";
"packed";
"record";
"repeat";
"program";
"function";
"procedure";
STRING;
CHAR;
}
// ============================================================================
// Simple tokens
// ============================================================================
LPAREN : '(';
RPAREN : ')';
LBRACKET : '[';
RBRACKET : ']';
COMMA : ',';
COLON : ':';
SEMI : ';';
DOT : '.';
RANGE : "..";
ASSIGN : ":=";
EQ : '=';
GT : '>';
LT : '<';
GE : ">=";
LE : "<=";
NE : "<>";
PLUS : '+';
MINUS : '-';
STAR : '*';
SLASH : '/';
PTR : '^';
// ============================================================================
// Identifier
// ============================================================================
ID
options
{
testLiterals=true;
}
: LETTER (LETTER | DIGIT)* ;
// ============================================================================
// Int or real
// ============================================================================
UINT_OR_REAL
: (UINT RANGE) => UINT { _ttype := TT_UINT; }
| (UINT DOT) => UREAL { _ttype := TT_UREAL; }
| (UINT ('E'|'e')) => UREAL { _ttype := TT_UREAL; }
| UINT { _ttype := TT_UINT; }
;
// ============================================================================
// Protected rules
// ============================================================================
protected
LETTER : 'a'..'z' | 'A'..'Z' | '_';
// ============================================================================
// Int
// ============================================================================
protected
UINT
: (DIGIT)+
;
// ============================================================================
// Real
// ============================================================================
protected
UREAL
: UINT ('.' UINT)? ( ('e' | 'E') ('+'|'-')? UINT)?
;
// ============================================================================
// Digit
// ============================================================================
protected
DIGIT
: '0'..'9'
;
// ============================================================================
// String or char
// ============================================================================
STRING_OR_CHAR
: '\'' (~'\'' | '\'' '\'')* '\''
{
if TokenText = '''''' then _ttype := TT_STRING
else if TokenText = '''''''''' then _ttype := TT_CHAR
else if Length( TokenText) > 3 then _ttype := TT_STRING
else _ttype := TT_CHAR;
}
;
// ============================================================================
// Single line comment
// ============================================================================
SLCOMMENT
:
"//"
( ~( '\r' | '\n') )*
(
'\r' '\n' { newLine; }
| '\r' { newLine; }
| '\n' { newLine; }
)
{
_ttype := TT_SKIP;
}
;
// ============================================================================
// Multi line comment version 1
// Nested comments aren't allowed!
// ============================================================================
MLCOMMENT1
:
"(*"
(
options
{
greedy = false;
generateAmbigWarnings = false;
}
: '\r' '\n' { newLine; }
| '\r' { newLine; }
| '\n' { newLine; }
| .
)*
"*)"
{
_ttype := TT_SKIP;
}
;
// ============================================================================
// Multi line comment version 2
// Nested comments aren't allowed!
// ============================================================================
MLCOMMENT2
:
"{"
(
options
{
greedy = false;
generateAmbigWarnings = false;
}
: '\r' '\n' { newLine; }
| '\r' { newLine; }
| '\n' { newLine; }
| .
)*
"}"
{
_ttype := TT_SKIP;
}
;
// ============================================================================
// White space
// ============================================================================
WS
:
(
'\r' '\n' { newLine; }
| '\r' { newLine; }
| '\n' { newLine; }
| '\t' { tab; }
| ' '
)
{
_ttype := TT_SKIP;
}
;