2016-03-18 21:22:42 +08:00
|
|
|
parser grammar TParser;
|
|
|
|
|
|
|
|
options {
|
|
|
|
tokenVocab = TLexer;
|
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// These are all supported parser sections:
|
|
|
|
|
|
|
|
// Parser file header. Appears at the top in all parser related files. Use e.g. for copyrights.
|
|
|
|
@parser::header {/* parser/listener/visitor header section */}
|
|
|
|
|
|
|
|
// Appears before any #include in h + cpp files.
|
|
|
|
@parser::preinclude {/* parser precinclude section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// Follows directly after the standard #includes in h + cpp files.
|
2016-05-09 20:51:46 +08:00
|
|
|
@parser::postinclude {
|
|
|
|
/* parser postinclude section */
|
|
|
|
#ifndef _WIN32
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#endif
|
|
|
|
}
|
2016-05-06 18:22:08 +08:00
|
|
|
|
2016-05-09 20:09:00 +08:00
|
|
|
// Directly preceeds the parser class declaration in the h file (e.g. for additional types etc.).
|
|
|
|
@parser::context {/* parser context section */}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// Appears in the private part of the parser in the h file.
|
2016-05-10 16:14:40 +08:00
|
|
|
// The function bodies could also appear in the definitions section, but I want to maximize
|
|
|
|
// Java compatibility, so we can also create a Java parser from this grammar.
|
|
|
|
// Still, some tweaking is necessary after the Java file generation (e.g. bool -> boolean).
|
2016-03-18 21:22:42 +08:00
|
|
|
@parser::members {
|
2016-05-10 16:14:40 +08:00
|
|
|
/* public parser declarations/members section */
|
|
|
|
bool myAction() { return true; }
|
|
|
|
bool doesItBlend() { return true; }
|
|
|
|
void cleanUp() {}
|
|
|
|
void doInit() {}
|
|
|
|
void doAfter() {}
|
2016-03-18 21:22:42 +08:00
|
|
|
}
|
|
|
|
|
2016-05-10 16:14:40 +08:00
|
|
|
// Appears in the public part of the parser in the h file.
|
|
|
|
@parser::declarations {/* private parser declarations section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-10 16:14:40 +08:00
|
|
|
// Appears in line with the other class member definitions in the cpp file.
|
|
|
|
@parser::definitions {/* parser definitions section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// Additionally there are similar sections for (base)listener and (base)visitor files.
|
|
|
|
@parser::listenerpreinclude {/* listener preinclude section */}
|
|
|
|
@parser::listenerpostinclude {/* listener postinclude section */}
|
|
|
|
@parser::listenerdeclarations {/* listener public declarations/members section */}
|
|
|
|
@parser::listenermembers {/* listener private declarations/members section */}
|
|
|
|
@parser::listenerdefinitions {/* listener definitions section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
@parser::baselistenerpreinclude {/* base listener preinclude section */}
|
|
|
|
@parser::baselistenerpostinclude {/* base listener postinclude section */}
|
|
|
|
@parser::baselistenerdeclarations {/* base listener public declarations/members section */}
|
|
|
|
@parser::baselistenermembers {/* base listener private declarations/members section */}
|
|
|
|
@parser::baselistenerdefinitions {/* base listener definitions section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
@parser::visitorpreinclude {/* visitor preinclude section */}
|
|
|
|
@parser::visitorpostinclude {/* visitor postinclude section */}
|
|
|
|
@parser::visitordeclarations {/* visitor public declarations/members section */}
|
|
|
|
@parser::visitormembers {/* visitor private declarations/members section */}
|
|
|
|
@parser::visitordefinitions {/* visitor definitions section */}
|
|
|
|
|
|
|
|
@parser::basevisitorpreinclude {/* base visitor preinclude section */}
|
|
|
|
@parser::basevisitorpostinclude {/* base visitor postinclude section */}
|
|
|
|
@parser::basevisitordeclarations {/* base visitor public declarations/members section */}
|
|
|
|
@parser::basevisitormembers {/* base visitor private declarations/members section */}
|
|
|
|
@parser::basevisitordefinitions {/* base visitor definitions section */}
|
2016-03-18 21:22:42 +08:00
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// Actual grammar start.
|
2016-05-09 20:09:00 +08:00
|
|
|
main: stat+ EOF;
|
2016-09-24 18:33:54 +08:00
|
|
|
divide : ID (and_ GreaterThan)? {doesItBlend()}?;
|
2016-04-14 01:05:56 +08:00
|
|
|
and_ @init{ doInit(); } @after { doAfter(); } : And ;
|
2016-03-18 21:22:42 +08:00
|
|
|
|
|
|
|
conquer:
|
|
|
|
divide+
|
|
|
|
| {doesItBlend()}? and_ { myAction(); }
|
2016-05-10 16:14:40 +08:00
|
|
|
| ID (LessThan* divide)?? { $ID.text; }
|
2016-03-18 21:22:42 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
// Unused rule to demonstrate some of the special features.
|
2016-05-01 23:48:18 +08:00
|
|
|
unused[double input = 111] returns [double calculated] locals [int _a, double _b, int _c] @init{ doInit(); } @after { doAfter(); } :
|
2016-04-17 19:13:15 +08:00
|
|
|
stat
|
2016-03-18 21:22:42 +08:00
|
|
|
;
|
|
|
|
catch [...] {
|
|
|
|
// Replaces the standard exception handling.
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
cleanUp();
|
|
|
|
}
|
2016-03-31 02:11:05 +08:00
|
|
|
|
|
|
|
unused2:
|
|
|
|
(unused[1] .)+ (Colon | Semicolon | Plus)? ~Semicolon
|
|
|
|
;
|
|
|
|
|
|
|
|
stat: expr Equal expr Semicolon
|
|
|
|
| expr Semicolon
|
|
|
|
;
|
|
|
|
|
|
|
|
expr: expr Star expr
|
|
|
|
| expr Plus expr
|
2016-05-04 00:12:04 +08:00
|
|
|
| OpenPar expr ClosePar
|
2016-09-24 18:33:54 +08:00
|
|
|
| <assoc = right> expr QuestionMark expr Colon expr
|
2016-03-31 02:11:05 +08:00
|
|
|
| <assoc = right> expr Equal expr
|
|
|
|
| identifier = id
|
|
|
|
| flowControl
|
2016-05-04 00:12:04 +08:00
|
|
|
| INT
|
2016-05-14 21:57:37 +08:00
|
|
|
| String
|
2016-03-31 02:11:05 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
flowControl:
|
|
|
|
Return expr # Return
|
|
|
|
| Continue # Continue
|
|
|
|
;
|
|
|
|
|
|
|
|
id: ID;
|
|
|
|
array : OpenCurly el += INT (Comma el += INT)* CloseCurly;
|
|
|
|
idarray : OpenCurly element += id (Comma element += id)* CloseCurly;
|
2016-08-04 15:27:46 +08:00
|
|
|
any: t = .;
|