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.
|
|
|
|
@parser::postinclude {/* parser postinclude section */}
|
|
|
|
|
|
|
|
// Appears in the public part of the parser in the h file.
|
|
|
|
@parser::declarations {/* public parser declarations section */}
|
|
|
|
|
|
|
|
// Appears in the private part of the parser in the h file.
|
2016-03-18 21:22:42 +08:00
|
|
|
@parser::members {
|
2016-05-06 18:22:08 +08:00
|
|
|
/* private parser declarations/members section */
|
|
|
|
bool myAction();
|
|
|
|
bool doesItBlend();
|
|
|
|
void cleanUp();
|
|
|
|
void doInit();
|
|
|
|
void doAfter();
|
2016-03-18 21:22:42 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
// Appears in line with the other class member definitions in the cpp file.
|
|
|
|
@parser::definitions {
|
|
|
|
/* parser definitions section */
|
|
|
|
bool TParser::myAction() {
|
2016-03-18 21:22:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
bool TParser::doesItBlend() {
|
|
|
|
return true;
|
2016-03-18 21:22:42 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
void TParser::cleanUp() {
|
2016-03-18 21:22:42 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
void TParser::doInit() {
|
2016-03-18 21:22:42 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 18:22:08 +08:00
|
|
|
void TParser::doAfter() {
|
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-01 23:48:18 +08:00
|
|
|
main: stat+;
|
2016-04-14 01:05:56 +08:00
|
|
|
divide : ID (and_ GreaterThan)? {doesItBlend()}?;
|
|
|
|
and_ @init{ doInit(); } @after { doAfter(); } : And ;
|
2016-03-18 21:22:42 +08:00
|
|
|
|
|
|
|
conquer:
|
|
|
|
divide+
|
|
|
|
| {doesItBlend()}? and_ { myAction(); }
|
2016-04-14 01:05:56 +08:00
|
|
|
| ID (LessThan* divide)??
|
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-03-31 02:11:05 +08:00
|
|
|
| <assoc = right> expr QuestionMark expr Colon expr
|
|
|
|
| <assoc = right> expr Equal expr
|
|
|
|
| identifier = id
|
|
|
|
| flowControl
|
2016-05-04 00:12:04 +08:00
|
|
|
| INT
|
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;
|