antlr/runtime/Cpp/demo/TLexer.g4

87 lines
2.0 KiB
ANTLR

lexer grammar TLexer;
// These are all supported lexer sections:
// Lexer file header. Appears at the top of h + cpp files. Use e.g. for copyrights.
@lexer::header {/* lexer header section */}
// Appears before any #include in h + cpp files.
@lexer::preinclude {/* lexer precinclude section */}
// Follows directly after the standard #includes in h + cpp files.
@lexer::postinclude {
/* lexer postinclude section */
#ifndef _WIN32
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
}
// Directly preceds the lexer class declaration in the h file (e.g. for additional types etc.).
@lexer::context {/* lexer context section */}
// Appears in the public part of the lexer in the h file.
@lexer::members {/* public lexer declarations section */
bool canTestFoo() { return true; }
bool isItFoo() { return true; }
bool isItBar() { return true; }
void myFooLexerAction() { /* do something*/ };
void myBarLexerAction() { /* do something*/ };
}
// Appears in the private part of the lexer in the h file.
@lexer::declarations {/* private lexer declarations/members section */}
// Appears in line with the other class member definitions in the cpp file.
@lexer::definitions {/* lexer definitions section */}
channels { CommentsChannel, DirectiveChannel }
tokens {
DUMMY
}
Return: 'return';
Continue: 'continue';
INT: Digit+;
Digit: [0-9];
ID: LETTER (LETTER | '0'..'9')*;
fragment LETTER : [a-zA-Z\u0080-\u{10FFFF}];
LessThan: '<';
GreaterThan: '>';
Equal: '=';
And: 'and';
Colon: ':';
Semicolon: ';';
Plus: '+';
Minus: '-';
Star: '*';
OpenPar: '(';
ClosePar: ')';
OpenCurly: '{' -> pushMode(Mode1);
CloseCurly: '}' -> popMode;
QuestionMark: '?';
Comma: ',' -> skip;
Dollar: '$' -> more, mode(Mode1);
Ampersand: '&' -> type(DUMMY);
String: '"' .*? '"';
Foo: {canTestFoo()}? 'foo' {isItFoo()}? { myFooLexerAction(); };
Bar: 'bar' {isItBar()}? { myBarLexerAction(); };
Any: Foo Dot Bar? DotDot Baz;
Comment : '#' ~[\r\n]* '\r'? '\n' -> channel(CommentsChannel);
WS: [ \t\r\n]+ -> channel(99);
fragment Baz: 'Baz';
mode Mode1;
Dot: '.';
mode Mode2;
DotDot: '..';