prog8/il65/antlr/il65.g4

173 lines
4.0 KiB
Plaintext
Raw Normal View History

2018-08-08 23:54:43 +00:00
/*
2018-08-10 21:56:30 +00:00
IL65 combined lexer and parser grammar
NOTES:
- whitespace is ignored. (tabs/spaces)
2018-08-11 12:06:43 +00:00
- every position can be empty, be a comment, or contain ONE statement.
2018-08-10 21:56:30 +00:00
2018-08-08 23:54:43 +00:00
*/
grammar il65;
2018-08-11 12:06:43 +00:00
LINECOMMENT : [\r\n][ \t]* COMMENT -> channel(HIDDEN);
2018-08-10 21:56:30 +00:00
COMMENT : ';' ~[\r\n]* -> channel(HIDDEN) ;
2018-08-09 22:26:41 +00:00
WS : [ \t] -> skip ;
2018-08-10 21:56:30 +00:00
EOL : [\r\n]+ ;
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-10 21:56:30 +00:00
INLINEASMBLOCK :
'{{' .+? '}}'
{
// get rid of the enclosing double braces
String s = getText();
setText(s.substring(2, s.length() - 2));
}
;
2018-08-08 23:54:43 +00:00
2018-08-09 22:26:41 +00:00
2018-08-11 12:06:43 +00:00
module : (modulestatement | EOL)* EOF ;
2018-08-10 21:56:30 +00:00
modulestatement: directive | block ;
2018-08-11 12:06:43 +00:00
block:
'~' identifier integerliteral? '{' EOL
(statement | EOL)*
'}' EOL
;
2018-08-09 22:26:41 +00:00
statement :
2018-08-08 23:54:43 +00:00
directive
2018-08-10 00:58:41 +00:00
| varinitializer
| vardecl
2018-08-09 22:26:41 +00:00
| constdecl
| memoryvardecl
2018-08-08 23:54:43 +00:00
| assignment
| augassignment
2018-08-10 21:56:30 +00:00
| unconditionaljump
| postincrdecr
| inlineasm
2018-08-12 15:16:36 +00:00
| labeldef
| returnstmt
2018-08-10 21:56:30 +00:00
// @todo forloop, whileloop, repeatloop, ifelse
2018-08-08 23:54:43 +00:00
;
2018-08-12 15:16:36 +00:00
labeldef : identifier ':' ;
2018-08-10 21:56:30 +00:00
call_location : integerliteral | identifier | scoped_identifier ;
unconditionaljump : 'goto' call_location ;
2018-08-08 23:54:43 +00:00
2018-08-10 21:56:30 +00:00
directive :
directivename=('%output' | '%launcher' | '%zp' | '%address' | '%import' |
'%breakpoint' | '%asminclude' | '%asmbinary')
(directivearg? | directivearg (',' directivearg)*)
;
2018-08-08 23:54:43 +00:00
2018-08-10 21:56:30 +00:00
directivearg : stringliteral | identifier | integerliteral ;
2018-08-08 23:54:43 +00:00
2018-08-10 00:58:41 +00:00
vardecl: datatype arrayspec? identifier ;
2018-08-08 23:54:43 +00:00
2018-08-10 00:58:41 +00:00
varinitializer : datatype arrayspec? identifier '=' 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 :
2018-08-11 17:15:39 +00:00
assign_target operator=('+=' | '-=' | '/=' | '*=' | '**=' |
2018-08-08 23:54:43 +00:00
'<<=' | '>>=' | '<<@=' | '>>@=' | '&=' | '|=' | '^=') expression
;
2018-08-09 22:26:41 +00:00
assign_target:
register
2018-08-10 00:58:41 +00:00
| identifier
| scoped_identifier
2018-08-09 22:26:41 +00:00
;
2018-08-08 23:54:43 +00:00
2018-08-10 21:56:30 +00:00
postincrdecr : assign_target operator = ('++' | '--') ;
2018-08-08 23:54:43 +00:00
expression :
2018-08-10 21:56:30 +00:00
'(' expression ')'
| expression arrayspec
| functioncall
2018-08-11 17:15:39 +00:00
| prefix = ('+'|'-'|'~') expression
2018-08-10 21:56:30 +00:00
| left = expression bop = '**' right = expression
| left = expression bop = ('*' | '/' ) right = expression
2018-08-10 21:56:30 +00:00
| left = expression bop = ('+' | '-' ) right = expression
| left = expression bop = ('<<' | '>>' | '<<@' | '>>@' ) right = expression
| left = expression bop = ('<' | '>' | '<=' | '>=') right = expression
| left = expression bop = ('==' | '!=') right = expression
| left = expression bop = '&' right = expression
| left = expression bop = '^' right = expression
| left = expression bop = '|' right = expression
| left = expression bop = 'and' right = expression
| left = expression bop = 'or' right = expression
| left = expression bop = 'xor' right = expression
2018-08-11 17:15:39 +00:00
| prefix = 'not' expression
2018-08-10 21:56:30 +00:00
| rangefrom = expression 'to' rangeto = expression
2018-08-08 23:54:43 +00:00
| literalvalue
| register
2018-08-10 00:58:41 +00:00
| identifier
| scoped_identifier
2018-08-08 23:54:43 +00:00
;
2018-08-10 21:56:30 +00:00
functioncall :
2018-08-12 15:16:36 +00:00
call_location '(' expression_list? ')'
;
2018-08-12 15:16:36 +00:00
expression_list :
expression (',' expression)*
2018-08-08 23:54:43 +00:00
;
2018-08-12 15:16:36 +00:00
returnstmt : 'return' expression_list? ;
2018-08-10 00:58:41 +00:00
identifier : NAME ;
2018-08-08 23:54:43 +00:00
2018-08-10 00:58:41 +00:00
scoped_identifier : 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
;
2018-08-10 21:56:30 +00:00
inlineasm : '%asm' INLINEASMBLOCK;