Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. // test.g4 file;
  2. grammar milestone_2;
  3.  
  4. INDENT: (' ')+;
  5. SKIP1:(' ')+->skip;
  6. NEWLINE2: (' ')-> skip;
  7. NEWLINE : [\r\n]-> skip;
  8. MULTICOMMENT:' '* '#'+'[' (SINGLECOMMENT|.)*? ']' '#' ' '* ->skip;
  9. SINGLECOMMENT: ' '* '#' + '[' .*? ']' '#' ' '*[\n|\r]* -> skip;
  10. COMMENT : ' '* '#'+ .*? [\n\r] ' '* -> skip;
  11. VAR : 'var' ;
  12. AND : 'and';
  13. ADDR : 'addr';
  14. AS: 'as';
  15. ASM: 'asm';
  16. BIND: 'bind';
  17. BLOCK: 'block';
  18. BREAK: 'break';
  19. CASE: 'case';
  20. CAST: 'cast';
  21. CONCEPT: 'concept';
  22. CONST: 'const';
  23. CONTINUE: 'continue';
  24. CONVERTER: 'converter';
  25. DEFER: 'defer';
  26. DISCARD:'discard';
  27. DISTINCT:'distinct';
  28. DIV:'div';
  29. DO:'do';
  30. ELIF: 'elif';
  31. ELSE:'else';
  32. ENUM:'enum';
  33. END:'end';
  34. EXCEPT:'except';
  35. EXPORT:'export';
  36. FINALLY:'finally';
  37. FOR:'for';
  38. FUNC:'func';
  39. IF:'if';
  40. IMPORT:'import';
  41. IN:'in';
  42. INCLUDE:'include';
  43. INTERFACE:'interface';
  44. IS:'is';
  45. ISNOT:'isnot';
  46. ITERATOR:'iterator';
  47. LET:'let';
  48. MACRO:'macro';
  49. METHOD:'method';
  50. MIXIN:'mixin';
  51. MOD:'mod';
  52. NIL:'nil';
  53. NOT:'not';
  54. NOTIN:'notin';
  55. OBJECT:'object';
  56. OF:'of';
  57. OR:'or';
  58. OUT:'out';
  59. PROC:'proc';
  60. PTR:'ptr';
  61. RAISE:'raise';
  62. REF:'ref';
  63. RETURN:'return';
  64. SHL:'shl';
  65. SHR:'shr';
  66. STATIC:'static';
  67. TEMPLATE:'template';
  68. TRY:'try';
  69. TUPLE:'tuple';
  70. TYPE:'type';
  71. USING:'using';
  72. WHEN:'when';
  73. WHILE:'while';
  74. XOR:'xor';
  75. YIELD:'yield';
  76. OP0:('->'|'=>');
  77. OP1:('+='|'*=');
  78. OP2:(AT|COLON|'?');
  79. OP3:(OR|XOR);
  80. OP4:AND;
  81. OP5:('=='|'<='|'<'|'>='|'!='|'>'|IN|NOTIN|IS|ISNOT|NOT|OF|'=');
  82. OP6:DOT DOT;
  83. OP7:AND_OPERATOR;
  84. OP8:ADD_OPERATOR|MINUS_OPERATOR;
  85. OP9:MUL_OPERATOR|DIV_OPERATOR|DIV|MOD|SHL|SHR|MODULUS;
  86. OP10:'$'|'^';
  87. IDENTIFIER: LETTER+ ('_'|(LETTER| DIGIT))*;
  88. LETTER :([A-Z] | [a-z] ){print('LETTER')};
  89. FLOAT32_SUFFIX : ('f' | 'F') ('32');
  90. FLOAT32_LIT : HEX_LIT '\'' FLOAT32_SUFFIX
  91. | (FLOAT_LIT | DEC_LIT | OCT_LIT | BIN_LIT) ('\'') FLOAT32_SUFFIX;
  92. FLOAT64_SUFFIX: ( ('f' | 'F') '64' ) | 'd' | 'D';
  93. FLOAT64_LIT : HEX_LIT '\'' FLOAT64_SUFFIX
  94. | (FLOAT_LIT | DEC_LIT | OCT_LIT | BIN_LIT) ('\'') FLOAT64_SUFFIX;
  95. FLOAT_LIT : DIGIT ('_' DIGIT)* (('.' DIGIT ('_' DIGIT|DIGIT)* (EXP)*) |EXP);
  96. DIGIT:[0-9];
  97. HEXDIGIT : DIGIT | [A-F] | [a-f];
  98. OCTDIGIT : [0-7];
  99. BINDIGIT : [0-1];
  100. INT_LIT : HEX_LIT
  101. | DEC_LIT
  102. | OCT_LIT
  103. | BIN_LIT;
  104. HEX_LIT : '0' ('x' | 'X' ) HEXDIGIT ('_' HEXDIGIT | HEXDIGIT)*;
  105. DEC_LIT : DIGIT ( '_' DIGIT|DIGIT )*;
  106. OCT_LIT : '0' 'o' OCTDIGIT ( '_' OCTDIGIT|OCTDIGIT )*;
  107. BIN_LIT : '0' ('b' | 'B' ) BINDIGIT ( '_' BINDIGIT | BINDIGIT )*;
  108. INT8_LIT : INT_LIT ('\'') ('i' | 'I') '8';
  109. INT16_LIT : INT_LIT ('\'') ('i' | 'I') '16';
  110. INT32_LIT : INT_LIT ('\'') ('i' | 'I') '32';
  111. INT64_LIT : INT_LIT ('\'') ('i' | 'I') '64';
  112. UINT_LIT : INT_LIT ('\'') ('u' | 'U');
  113. UINT8_LIT : INT_LIT ('\'') ('u' | 'U') '8';
  114. UINT16_LIT : INT_LIT ('\'') ('u' | 'U') '16';
  115. UINT32_LIT : INT_LIT ('\'') ('u' | 'U') '32';
  116. UINT64_LIT : INT_LIT ('\'') ('u' | 'U') '64';
  117. EXP : ('E' | 'e' ) ('+' | '-') DIGIT ( '_' DIGIT |DIGIT )*;
  118. EQUALS_OPERATOR: '='+;
  119. ADD_OPERATOR:'+';
  120. MUL_OPERATOR:'*';
  121. MINUS_OPERATOR:'-';
  122. DIV_OPERATOR:'/';
  123. BITWISE_NOT_OPERATOR:'~';
  124. AND_OPERATOR:'&';
  125. OR_OPERATOR:'|';
  126. LESS_THAN:'<';
  127. GREATER_THAN:'>';
  128. AT:'@';
  129. MODULUS:'%';
  130. NOT_OPERATOR:'!';
  131. XOR_OPERATOR:'^';
  132. DOT:'.';
  133. COLON:':';
  134. FROM:'from';
  135. OPEN_PAREN:'(';
  136. CLOSE_PAREN:')';
  137. OPEN_BRACE:'{';
  138. CLOSE_BRACE:'}';
  139. OPEN_BRACK:'[';
  140. CLOSE_BRACK:']';
  141. COMMA:',';
  142. SEMI_COLON:';';
  143. STR_LIT: '"'(.*?) '"';
  144. CHAR_LIT: '\'' .*? '\'';
  145. TRIPLESTR_LIT: '"""' .*? '"""';
  146. RSTR_LIT: ('R'|'r') '"' .*? '"';
  147.  
  148.  
  149. module : (stmt ((';' | INDENT) stmt)*)?;
  150. start: module;
  151. comma : ',' COMMENT?;
  152. semicolon : ';' COMMENT?;
  153. colon : ':' COMMENT?;
  154. colcom : ':' COMMENT?;
  155. operator : OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
  156. | 'or' | 'xor' | 'and'
  157. | 'is' | 'isnot' | 'in' | 'notin' | 'of'
  158. | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..';
  159. opr: OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
  160. | 'or' | 'xor' | 'and'
  161. | 'is' | 'isnot' | 'in' | 'notin' | 'of'
  162. | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..';
  163. prefixOperator : operator;
  164. optInd : COMMENT? INDENT?;
  165. optPar : (INDENT | INDENT)?;
  166. simpleExpr : arrowExpr (OP0 optInd arrowExpr)* pragma?;
  167. arrowExpr : assignExpr (OP1 optInd assignExpr)*;
  168. assignExpr : orExpr (OP2 optInd orExpr)*;
  169. orExpr : andExpr (OP3 optInd andExpr)*;
  170. andExpr : cmpExpr (OP4 optInd cmpExpr)*;
  171. cmpExpr : sliceExpr (OP5 optInd sliceExpr)*;
  172. sliceExpr : ampExpr (OP6 optInd ampExpr)*;
  173. ampExpr : plusExpr (OP7 optInd plusExpr)*;
  174. plusExpr : mulExpr (OP8 optInd mulExpr)*;
  175. mulExpr : dollarExpr (OP9 optInd dollarExpr)*;
  176. dollarExpr : primary (OP10 optInd primary)*;
  177. keyw:parKeyw|typeKeyw|'int'|'string';
  178. symbol : ('\'' (keyw|IDENTIFIER|literal|(operator|'('|')'|'['|']'|'{'|'}'|'='))+ '\'')
  179. | IDENTIFIER | keyw|literal|STR_LIT;
  180. exprColonEqExpr : expr (':'|'=' expr)?;
  181. exprList : expr (comma expr)*;
  182. exprColonEqExprList : exprColonEqExpr (comma exprColonEqExpr)* (comma)?;
  183. dotExpr : expr '.' optInd (symbol | '[:' exprList ']');
  184. explicitGenericInstantiation : '[:' exprList ']' ( '(' exprColonEqExpr ')' )?;
  185. qualifiedIdent : symbol ('.' optInd symbol)?;
  186. setOrTableConstr : '{' ((exprColonEqExpr comma)* | ':' ) '}';
  187. castExpr : 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')';
  188. parKeyw : 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
  189. | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
  190. | 'when' | 'var' | 'mixin';
  191. par : '(' optInd
  192. ( parKeyw complexOrSimpleStmt ( ';' complexOrSimpleStmt)*
  193. | ';' complexOrSimpleStmt( ';' complexOrSimpleStmt)*
  194. | pragmaStmt
  195. | simpleExpr ( ('=' expr (';' complexOrSimpleStmt (';' complexOrSimpleStmt)* )? )
  196. | (':' expr (',' exprColonEqExpr ( ',' exprColonEqExpr)* )? ) ) )
  197. optPar ')';
  198. literal : STR_LIT|INT_LIT | INT8_LIT | INT16_LIT | INT32_LIT | INT64_LIT
  199. | UINT_LIT | UINT8_LIT | UINT16_LIT | UINT32_LIT | UINT64_LIT
  200. | FLOAT_LIT | FLOAT32_LIT | FLOAT64_LIT
  201. | STR_LIT | RSTR_LIT | TRIPLESTR_LIT
  202. | CHAR_LIT
  203. | NIL;
  204.  
  205. generalizedLit : GENERALIZED_STR_LIT | GENERALIZED_TRIPLESTR_LIT;
  206. identOrLiteral : generalizedLit | symbol | literal
  207. | par | arrayConstr | setOrTableConstr
  208. | castExpr;
  209. tupleConstr : '(' optInd (exprColonEqExpr comma+)* optPar ')';
  210. arrayConstr : '[' optInd (exprColonEqExpr comma+)* optPar ']';
  211. primarySuffix : '(' (exprColonEqExpr comma+)* ')' doBlocks?
  212. | doBlocks
  213. | '.' optInd symbol generalizedLit?
  214. | '[' optInd exprList optPar ']'
  215. | '{' optInd exprList optPar '}'
  216. | ( '`'|IDENTIFIER|literal|'cast'|'addr'|'type') expr ;
  217. condExpr : expr colcom expr optInd
  218. ('elif' expr colcom expr optInd)*
  219. 'else' colcom expr;
  220. ifExpr : 'if' condExpr;
  221. moduleName:IDENTIFIER;
  222. whenExpr : 'when' condExpr;
  223. pragma : '{.' optInd (exprColonEqExpr comma?)* optPar ('.}' | '}');
  224. identVis : symbol opr?;
  225. identVisDot : symbol '.' optInd symbol opr?;
  226. identWithPragma : identVis pragma?;
  227. identWithPragmaDot : identVisDot pragma?;
  228. declColonEquals : identWithPragma (comma identWithPragma)* comma? firstPart secondPart;
  229. identColonEquals : IDENTIFIER (comma IDENTIFIER)* comma? firstPart secondPart;
  230. firstPart: ':'?( optInd typeDesc)?;
  231. secondPart:'='?( optInd expr)?;
  232. inlTupleDecl : 'tuple'
  233. '[' optInd (identColonEquals (comma|semicolon)?)* optPar ']';
  234. extTupleDecl : 'tuple'
  235. COMMENT? (INDENT identColonEquals (INDENT identColonEquals)*)?;
  236. tupleClass : 'tuple';
  237. paramList : '(' (declColonEquals ((comma|semicolon)? declColonEquals)*)? ')';
  238. paramListWithoutParan:(declColonEquals ((comma|semicolon)? declColonEquals)*)?;
  239. paramListArrow : paramList? ('->' optInd typeDesc)?;
  240. paramListColon : paramList? (':' optInd typeDesc)?;
  241.  
  242. procExpr : 'proc' paramListColon pragmas? ('=' COMMENT? stmt)?;
  243. distinct : 'distinct' optInd typeDesc;
  244. forStmt : 'for' (identWithPragma (comma identWithPragma)*) 'in' expr colcom stmt;
  245. forExpr : forStmt;
  246. expr : (blockExpr
  247. | ifExpr
  248. | whenExpr
  249. //removed caseExpr
  250. | forExpr
  251. | tryExpr)
  252. | simpleExpr;
  253. typeKeyw : 'var' | 'out' | 'ref' | 'ptr' | 'shared' | 'tuple'
  254. | 'proc' | 'iterator' | 'distinct' | 'object' | 'enum';
  255. primary : typeKeyw typeDesc
  256. | prefixOperator* identOrLiteral primarySuffix*
  257. | 'bind' primary;
  258. typeDesc : simpleExpr;
  259. typeDefAux : simpleExpr
  260. | 'concept' typeClass;
  261. postExprBlocks : ':' stmt? ( INDENT doBlock
  262. | INDENT 'of' exprList ':' stmt
  263. | INDENT 'elif' expr ':' stmt
  264. | INDENT 'except' exprList ':' stmt
  265. | INDENT 'else' ':' stmt )*;
  266. exprStmt : simpleExpr
  267. (( '=' optInd expr colonBody? )
  268. | ( expr (comma expr)*
  269. doBlocks
  270. //removed Macro
  271. ))?;
  272. importStmt : 'import' optInd expr
  273. ((comma expr)*
  274. | 'except' optInd (expr (comma expr))*);
  275. includeStmt : 'include' optInd expr (comma expr)*;
  276. fromStmt : 'from' moduleName 'import' optInd expr (comma expr)*;
  277. returnStmt : 'return' optInd expr?;
  278. raiseStmt : 'raise' optInd expr?;
  279. yieldStmt : 'yield' optInd expr?;
  280. discardStmt : 'discard' optInd expr?;
  281. breakStmt : 'break' optInd expr?;
  282. continueStmt : 'continue' optInd expr?;
  283. condStmt : expr colcom stmt COMMENT?
  284. (INDENT 'elif' expr colcom stmt)*
  285. (INDENT'else' colcom stmt)?;
  286. ifStmt : 'if' condStmt;
  287. whenStmt : 'when' condStmt;
  288. whileStmt : 'while' expr colcom stmt;
  289. ofBranch : 'of' exprList colcom stmt;
  290. ofBranches : ofBranch (INDENT ofBranch)*
  291. (INDENT 'elif' expr colcom stmt)*
  292. (INDENT 'else' colcom stmt)?;
  293. caseStmt : 'case' expr ':'? COMMENT?
  294. (INDENT ofBranches
  295. |INDENT ofBranches);
  296. tryStmt : 'try' colcom stmt (INDENT? 'except'|'finally')
  297. (INDENT? 'except' exprList colcom stmt)*
  298. (INDENT? 'finally' colcom stmt)?;
  299. tryExpr : 'try' colcom stmt (optInd 'except'|'finally')
  300. (optInd 'except' exprList colcom stmt)*
  301. (optInd 'finally' colcom stmt)?;
  302. exceptBlock : 'except' colcom stmt;
  303. blockStmt : 'block' symbol? colcom stmt;
  304. blockExpr : 'block' symbol? colcom stmt;
  305. staticStmt : 'static' colcom stmt;
  306. deferStmt : 'defer' colcom stmt;
  307. asmStmt : 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLESTR_LIT);
  308. genericParam : symbol (comma symbol)* (colon expr)? ('=' optInd expr)?;
  309. genericParamList : '[' optInd
  310. (genericParam ((comma|semicolon) genericParam)*)? optPar ']';
  311. pattern : '{' stmt '}';
  312. indAndComment : (INDENT COMMENT)? | COMMENT?;
  313. routine : optInd identVis pattern? genericParamList?
  314. paramListColon pragma? ('=' COMMENT? stmt)? indAndComment;
  315. commentStmt : COMMENT;
  316. section1 : COMMENT? typeDef | (INDENT (typeDef | COMMENT)(INDENT (typeDef | COMMENT))* );
  317. section2 : COMMENT? constant | (INDENT(constant | COMMENT)(INDENT (constant | COMMENT))* );
  318. section3 : COMMENT? variable | (INDENT (variable | COMMENT)(INDENT (variable | COMMENT))* );
  319. doBlock : 'do' paramListArrow pragmas? colcom stmt;
  320. doBlocks: (doBlock)+;
  321. constant : identWithPragma (colon typeDesc)? '=' optInd expr indAndComment;
  322. enum : 'enum' optInd (symbol optInd ('=' optInd expr COMMENT?)? comma?)+;
  323. objectWhen : 'when' expr colcom objectPart COMMENT?
  324. ('elif' expr colcom objectPart COMMENT?)*
  325. ('else' colcom objectPart COMMENT?)?;
  326. objectBranch : 'of' exprList colcom objectPart;
  327. objectBranches : objectBranch (INDENT objectBranch)*
  328. (INDENT'elif' expr colcom objectPart)*
  329. (INDENT 'else' colcom objectPart)?;
  330. objectCase : 'case' identWithPragma ':' typeDesc ':'? COMMENT?
  331. (INDENT objectBranches
  332. | INDENT objectBranches);
  333. objectPart : INDENT objectPart(INDENT objectPart)* //DED
  334. | objectWhen | objectCase | 'nil' | 'discard' | declColonEquals;
  335. object1 : 'object' pragma? ('of' typeDesc)? COMMENT? objectPart;
  336. typeClassParam : ('var' | 'out')? symbol;
  337. typeClass : (typeClassParam (',' typeClassParam)*)? (pragma)? ('of' (typeDesc (',' typeDesc)*)?)?
  338. INDENT stmt;
  339. typeDef : identWithPragmaDot genericParamList? '=' optInd typeDefAux
  340. indAndComment;
  341. varTuple : '(' optInd identWithPragma ( comma identWithPragma)* optPar ')' '=' optInd expr;
  342. colonBody : colcom stmt doBlocks?;
  343.  
  344. variable : (varTuple | identColonEquals) colonBody? indAndComment;
  345. bindStmt : 'bind' optInd qualifiedIdent (comma qualifiedIdent)*;
  346. mixinStmt : 'mixin' optInd qualifiedIdent ( comma qualifiedIdent)*;
  347. pragmaStmt : pragma (':' COMMENT? stmt)?;
  348. simpleStmt : ((returnStmt | raiseStmt | yieldStmt | discardStmt | breakStmt
  349. | continueStmt | pragmaStmt | importStmt | fromStmt //removed exportstmt
  350. | includeStmt | commentStmt) | exprStmt) COMMENT?;
  351. complexOrSimpleStmt : (ifStmt | whenStmt | whileStmt
  352. | tryStmt | forStmt
  353. | blockStmt | staticStmt | deferStmt | asmStmt
  354. | 'proc' routine
  355. | 'method' routine
  356. | 'iterator' routine
  357. | 'macro' routine
  358. | echoStatement
  359. | 'template' routine
  360. | 'converter' routine
  361. | 'type' section1(typeDef)
  362. | 'const' section2(constant)
  363. | ('let' | 'var' | 'using') section3(variable)
  364. | bindStmt | mixinStmt)
  365. | simpleStmt;
  366. stmt : (INDENT? complexOrSimpleStmt((INDENT? | ';') complexOrSimpleStmt)* )
  367. | simpleStmt (';' simpleStmt)*;
  368. pragmas:(pragma)+;
  369. echoParamList: paramList|paramListWithoutParan;
  370. echoStatement: 'echo' echoParamList stmt?;
  371. exprColonExpr: expr ':' expr;
  372. GENERALIZED_STR_LIT:IDENTIFIER STR_LIT;
  373. GENERALIZED_TRIPLESTR_LIT: IDENTIFIER TRIPLESTR_LIT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement