prog8/il65/antlr/il65.g4

116 lines
2.5 KiB
Plaintext
Raw Normal View History

2018-08-08 23:54:43 +00:00
/*
IL65 lexer and parser grammar
*/
grammar il65;
2018-08-09 22:26:41 +00:00
COMMENT : ';' ~[\r\n]* -> channel(1) ;
WS : [ \t] -> skip ;
EOL : [\r\n]+ -> skip ;
2018-08-08 23:54:43 +00:00
NAME : [a-zA-Z_][a-zA-Z0-9_]* ;
2018-08-09 22:26:41 +00:00
DEC_INTEGER : ('0'..'9') | (('1'..'9')('0'..'9')+);
HEX_INTEGER : '$' (('a'..'f') | ('A'..'F') | ('0'..'9'))+ ;
BIN_INTEGER : '%' ('0' | '1')+ ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
FLOAT_NUMBER : FNUMBER (('E'|'e') ('+' | '-')? FNUMBER)? ; // sign comes later from unary expression
fragment FNUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
fragment STRING_ESCAPE_SEQ : '\\' . | '\\' EOL;
STRING :
'"' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f"] )* '"'
{
// get rid of the enclosing quotes
String s = getText();
setText(s.substring(1, s.length() - 1));
}
2018-08-08 23:54:43 +00:00
;
2018-08-09 22:26:41 +00:00
module : statement* EOF ;
statement :
2018-08-08 23:54:43 +00:00
directive
2018-08-09 22:26:41 +00:00
| constdecl
| memoryvardecl
2018-08-08 23:54:43 +00:00
| vardecl
2018-08-09 22:26:41 +00:00
| varinitializer
2018-08-08 23:54:43 +00:00
| assignment
| augassignment
;
2018-08-09 22:26:41 +00:00
directive : '%' singlename (directivearg? | directivearg (',' directivearg)*) ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
directivearg : singlename | integerliteral ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
vardecl: datatype arrayspec? singlename ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
varinitializer : datatype arrayspec? singlename '=' expression ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
constdecl: 'const' varinitializer ;
memoryvardecl: 'memory' varinitializer;
datatype: 'byte' | 'word' | 'float' | 'str' | 'str_p' | 'str_s' | 'str_ps' ;
arrayspec: '[' expression (',' expression)? ']' ;
assignment : assign_target '=' expression ;
2018-08-08 23:54:43 +00:00
augassignment :
assign_target ('+=' | '-=' | '/=' | '//=' | '*=' | '**=' |
'<<=' | '>>=' | '<<@=' | '>>@=' | '&=' | '|=' | '^=') expression
;
2018-08-09 22:26:41 +00:00
assign_target:
register
| singlename
| dottedname
;
2018-08-08 23:54:43 +00:00
expression :
unary_expression
| '(' expression ')'
| expression '**' expression
| expression ('*' | '/' | '//' | '**') expression
| expression ('+' | '-' | '%') expression
| expression ('<<' | '>>' | '<<@' | '>>@' | '&' | '|' | '^') expression
| expression ('and' | 'or' | 'xor') expression
| expression ('==' | '!=' | '<' | '>' | '<=' | '>=') expression
| literalvalue
| register
| dottedname
| singlename
;
2018-08-09 22:26:41 +00:00
unary_expression :
2018-08-08 23:54:43 +00:00
'~' expression
| ('+' | '-') expression
| 'not' expression
;
2018-08-09 22:26:41 +00:00
singlename : NAME ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
dottedname : NAME ('.' NAME)+ ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
register : 'A' | 'X' | 'Y' | 'AX' | 'AY' | 'XY' | 'SC' | 'SI' | 'SZ' ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
integerliteral : DEC_INTEGER | HEX_INTEGER | BIN_INTEGER ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
booleanliteral : 'true' | 'false' ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
arrayliteral : '[' expression (',' expression)* ']' ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
stringliteral : STRING ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
floatliteral : FLOAT_NUMBER ;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
literalvalue :
integerliteral
| booleanliteral
| arrayliteral
| stringliteral
| floatliteral
2018-08-08 23:54:43 +00:00
;