]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/demo/TLexer.g4
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / demo / TLexer.g4
1 lexer grammar TLexer;
2
3 // These are all supported lexer sections:
4
5 // Lexer file header. Appears at the top of h + cpp files. Use e.g. for copyrights.
6 @lexer::header {/* lexer header section */}
7
8 // Appears before any #include in h + cpp files.
9 @lexer::preinclude {/* lexer precinclude section */}
10
11 // Follows directly after the standard #includes in h + cpp files.
12 @lexer::postinclude {
13 /* lexer postinclude section */
14 #ifndef _WIN32
15 #pragma GCC diagnostic ignored "-Wunused-parameter"
16 #endif
17 }
18
19 // Directly preceds the lexer class declaration in the h file (e.g. for additional types etc.).
20 @lexer::context {/* lexer context section */}
21
22 // Appears in the public part of the lexer in the h file.
23 @lexer::members {/* public lexer declarations section */
24 bool canTestFoo() { return true; }
25 bool isItFoo() { return true; }
26 bool isItBar() { return true; }
27
28 void myFooLexerAction() { /* do something*/ };
29 void myBarLexerAction() { /* do something*/ };
30 }
31
32 // Appears in the private part of the lexer in the h file.
33 @lexer::declarations {/* private lexer declarations/members section */}
34
35 // Appears in line with the other class member definitions in the cpp file.
36 @lexer::definitions {/* lexer definitions section */}
37
38 channels { CommentsChannel, DirectiveChannel }
39
40 tokens {
41         DUMMY
42 }
43
44 Return: 'return';
45 Continue: 'continue';
46
47 INT: Digit+;
48 Digit: [0-9];
49
50 ID: LETTER (LETTER | '0'..'9')*;
51 fragment LETTER : [a-zA-Z\u0080-\u{10FFFF}];
52
53 LessThan: '<';
54 GreaterThan:  '>';
55 Equal: '=';
56 And: 'and';
57
58 Colon: ':';
59 Semicolon: ';';
60 Plus: '+';
61 Minus: '-';
62 Star: '*';
63 OpenPar: '(';
64 ClosePar: ')';
65 OpenCurly: '{' -> pushMode(Mode1);
66 CloseCurly: '}' -> popMode;
67 QuestionMark: '?';
68 Comma: ',' -> skip;
69 Dollar: '$' -> more, mode(Mode1);
70 Ampersand: '&' -> type(DUMMY);
71
72 String: '"' .*? '"';
73 Foo: {canTestFoo()}? 'foo' {isItFoo()}? { myFooLexerAction(); };
74 Bar: 'bar' {isItBar()}? { myBarLexerAction(); };
75 Any: Foo Dot Bar? DotDot Baz;
76
77 Comment : '#' ~[\r\n]* '\r'? '\n' -> channel(CommentsChannel);
78 WS: [ \t\r\n]+ -> channel(99);
79
80 fragment Baz: 'Baz';
81
82 mode Mode1;
83 Dot: '.';
84
85 mode Mode2;
86 DotDot: '..';