Skip to content

Commit

Permalink
format grammar files with Antlr4Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
albertocavalcante committed Sep 11, 2024
1 parent f8fbbb7 commit c01bba7
Show file tree
Hide file tree
Showing 35 changed files with 2,008 additions and 1,328 deletions.
201 changes: 147 additions & 54 deletions examples/antlr4/Cpp/src/antlr4/TLexer.g4
Original file line number Diff line number Diff line change
@@ -1,87 +1,180 @@
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 */}

@ lexer :: header
{/* lexer header section */}
// Appears before any #include in h + cpp files.
@lexer::preinclude {/* lexer precinclude section */}

@ lexer :: preinclude
{/* lexer precinclude section */}
// Follows directly after the standard #includes in h + cpp files.
@lexer::postinclude {

@ 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 */}

@ lexer :: context
{/* lexer context section */}
// Appears in the public part of the lexer in the h file.
@lexer::members {/* public lexer declarations section */

@ 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 */}

@ 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);
@ 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(); }
;

fragment Baz: 'Baz';
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: '.';
Dot
: '.'
;

mode Mode2;
DotDot: '..';
DotDot
: '..'
;

Loading

0 comments on commit c01bba7

Please sign in to comment.