Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grammar lang;
- // PARSER RULES
- program : func_body?;
- return_stmt : 'return' test ';';
- if_stmt
- : 'if' test '{' stmt* '}' ('else if' test '{' stmt* '}')* ('else' '{' stmt* '}')?
- | 'if' paren_test stmt ('else if' paren_test stmt)* ('else' stmt)?
- ;
- param : ID (':' ID) ('=' test)?;
- let_params : param (',' param)*;
- func_body : stmt+;
- func_def_stmt
- : 'func' ID '(' let_params? ')' (':' ID) '{' func_body? '}'
- ;
- let_def_stmt
- : 'pub'? ('final' | 'static')? 'let' let_params ';'
- ;
- func_call_stmt
- : ID ('(' (arglist)? ')' | '[' test ']')
- ;
- stmt
- : ';'
- | test '=' test ';'
- | test op_augassign test ';'
- | test ('++' | '--') ';'
- | ('++' | '--') test ';'
- | if_stmt
- | paren_test ';'
- | test ';'
- | let_def_stmt
- | func_def_stmt
- | func_call_stmt ';'
- ;
- paren_test: '(' test ')';
- test: or_test ('?' test ':' test)?
- ;
- or_test: and_test ('||' and_test)*
- ;
- and_test: not_test ('&&' not_test)*
- ;
- not_test: '!' not_test | comparison
- ;
- comparison: expr (op_comp expr)*
- ;
- op_augassign : '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=';
- op_comp : '<' | '>' | '<=' | '>=' | '!=' | '==' | 'is' | 'has';
- expr: xor_expr ('|' xor_expr)*
- ;
- xor_expr: and_expr ('^' and_expr)*
- ;
- and_expr: shift_expr ('&' shift_expr)*
- ;
- shift_expr: arith_expr (('<<'|'>>') arith_expr)*
- ;
- arith_expr: term (('+'|'-') term)*
- ;
- term: factor (('*'|'/'|'%') factor)*
- ;
- factor: ('+'|'-'|'~') factor | power
- ;
- power: atom trailer* ('**' factor)?
- ;
- atom : ID | NUMBER | STRING;
- trailer
- : '(' (arglist)? ')' | '[' test ']' | '.' ID
- ;
- arglist
- : test (',' test)*
- ;
- // LEXER RULES
- ID : [a-zA-Z_] [a-zA-Z0-9_]+;
- STRING : '"' (ESC_SEQ | ~('\\'|'"'))* '"';
- CHAR : '\'' (ESC_SEQ | ~('\\'|'\'')) '\'';
- NUMBER
- : SIGN? [0-9]+ EXPONENT?
- | SIGN? [0-9]+ '.' [0-9]+ EXPONENT? 'f'?
- | SIGN? '.' [0-9]+ EXPONENT? 'f'?
- | SIGN? HEX_NUMBER
- ;
- HEX_NUMBER
- : '0' [xX] HEX+
- ;
- fragment
- HEX
- : [a-fA-F0-9]
- ;
- EXPONENT
- : [eE] SIGN? [0-9]+
- ;
- fragment
- ESC_SEQ
- : '\\' ('b'|'t'|'n'|'f'|'r'|'"'|'\''|'\\')
- | UNICODE_ESC
- | OCTAL_ESC
- ;
- fragment
- OCTAL_ESC
- : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
- | '\\' ('0'..'7') ('0'..'7')
- | '\\' ('0'..'7')
- ;
- fragment
- UNICODE_ESC
- : '\\' 'u' HEX HEX HEX HEX
- ;
- WS
- : [ \r\n\t] + -> skip
- ;
- UNKNOWN : . -> skip;
- fragment
- SIGN
- : [+-]
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement