2018-08-09 01:54:43 +02:00
|
|
|
/*
|
2018-09-15 16:21:05 +02:00
|
|
|
Prog8 combined lexer and parser grammar
|
2018-08-10 23:56:30 +02:00
|
|
|
|
|
|
|
NOTES:
|
|
|
|
|
|
|
|
- whitespace is ignored. (tabs/spaces)
|
2018-09-13 22:31:59 +02:00
|
|
|
- every position can be empty, be a comment, or contain ONE statement.
|
2018-08-10 23:56:30 +02:00
|
|
|
|
2018-08-09 01:54:43 +02:00
|
|
|
*/
|
|
|
|
|
2018-09-15 16:21:05 +02:00
|
|
|
grammar prog8;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-01-29 12:06:33 +01:00
|
|
|
@header {
|
|
|
|
package prog8.parser;
|
|
|
|
}
|
2018-08-11 14:06:43 +02:00
|
|
|
|
|
|
|
LINECOMMENT : [\r\n][ \t]* COMMENT -> channel(HIDDEN);
|
2018-08-10 23:56:30 +02:00
|
|
|
COMMENT : ';' ~[\r\n]* -> channel(HIDDEN) ;
|
2018-08-10 00:26:41 +02:00
|
|
|
WS : [ \t] -> skip ;
|
2018-08-10 23:56:30 +02:00
|
|
|
EOL : [\r\n]+ ;
|
2019-01-26 17:32:26 +01:00
|
|
|
// WS2 : '\\' EOL -> skip;
|
2018-08-09 01:54:43 +02:00
|
|
|
NAME : [a-zA-Z_][a-zA-Z0-9_]* ;
|
2018-08-10 00:26:41 +02:00
|
|
|
DEC_INTEGER : ('0'..'9') | (('1'..'9')('0'..'9')+);
|
|
|
|
HEX_INTEGER : '$' (('a'..'f') | ('A'..'F') | ('0'..'9'))+ ;
|
|
|
|
BIN_INTEGER : '%' ('0' | '1')+ ;
|
2019-04-12 22:00:32 +02:00
|
|
|
ADDRESS_OF: '&';
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
FLOAT_NUMBER : FNUMBER (('E'|'e') ('+' | '-')? FNUMBER)? ; // sign comes later from unary expression
|
|
|
|
fragment FNUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 00:26:41 +02: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-09 01:54:43 +02:00
|
|
|
;
|
2018-08-10 23:56:30 +02:00
|
|
|
INLINEASMBLOCK :
|
|
|
|
'{{' .+? '}}'
|
|
|
|
{
|
|
|
|
// get rid of the enclosing double braces
|
|
|
|
String s = getText();
|
|
|
|
setText(s.substring(2, s.length() - 2));
|
|
|
|
}
|
|
|
|
;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-09-30 23:07:26 +02:00
|
|
|
SINGLECHAR :
|
|
|
|
'\'' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f"] ) '\''
|
|
|
|
{
|
|
|
|
// get rid of the enclosing quotes
|
|
|
|
String s = getText();
|
|
|
|
setText(s.substring(1, s.length() - 1));
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2019-01-26 22:46:01 +01:00
|
|
|
ZEROPAGE :
|
|
|
|
'@zp'
|
|
|
|
;
|
|
|
|
|
2019-04-16 01:19:51 +02:00
|
|
|
ARRAYSIG :
|
|
|
|
'[]'
|
|
|
|
;
|
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
|
2018-08-11 14:06:43 +02:00
|
|
|
module : (modulestatement | EOL)* EOF ;
|
2018-08-10 23:56:30 +02:00
|
|
|
|
|
|
|
modulestatement: directive | block ;
|
|
|
|
|
2018-08-14 14:33:36 +02:00
|
|
|
block: '~' identifier integerliteral? statement_block EOL ;
|
2018-08-10 00:26:41 +02:00
|
|
|
|
|
|
|
statement :
|
2018-08-09 01:54:43 +02:00
|
|
|
directive
|
2018-08-10 02:58:41 +02:00
|
|
|
| varinitializer
|
2019-07-15 22:28:05 +02:00
|
|
|
| structvarinitializer
|
2018-08-10 02:58:41 +02:00
|
|
|
| vardecl
|
2019-07-15 22:28:05 +02:00
|
|
|
| structvardecl
|
2018-08-10 00:26:41 +02:00
|
|
|
| constdecl
|
|
|
|
| memoryvardecl
|
2019-07-12 06:14:59 +02:00
|
|
|
| structdecl
|
2018-08-09 01:54:43 +02:00
|
|
|
| assignment
|
|
|
|
| augassignment
|
2018-08-10 23:56:30 +02:00
|
|
|
| unconditionaljump
|
|
|
|
| postincrdecr
|
2018-08-14 02:22:59 +02:00
|
|
|
| functioncall_stmt
|
2018-08-14 14:33:36 +02:00
|
|
|
| if_stmt
|
2018-09-02 18:32:48 +02:00
|
|
|
| branch_stmt
|
2018-08-13 01:30:33 +02:00
|
|
|
| subroutine
|
2018-10-01 20:05:32 +02:00
|
|
|
| asmsubroutine
|
2018-08-10 23:56:30 +02:00
|
|
|
| inlineasm
|
2018-08-12 17:16:36 +02:00
|
|
|
| returnstmt
|
2018-09-16 03:00:32 +02:00
|
|
|
| forloop
|
2018-09-23 02:04:45 +02:00
|
|
|
| whileloop
|
|
|
|
| repeatloop
|
2019-07-09 00:02:38 +02:00
|
|
|
| whenstmt
|
2018-09-18 23:14:32 +02:00
|
|
|
| breakstmt
|
|
|
|
| continuestmt
|
2018-09-19 02:41:35 +02:00
|
|
|
| labeldef
|
2018-08-09 01:54:43 +02:00
|
|
|
;
|
|
|
|
|
2018-09-16 03:00:32 +02:00
|
|
|
|
2018-08-12 17:16:36 +02:00
|
|
|
labeldef : identifier ':' ;
|
2018-08-10 23:56:30 +02:00
|
|
|
|
2019-01-02 23:32:41 +01:00
|
|
|
unconditionaljump : 'goto' (integerliteral | scoped_identifier) ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 23:56:30 +02:00
|
|
|
directive :
|
2018-10-09 00:01:53 +02:00
|
|
|
directivename=('%output' | '%launcher' | '%zeropage' | '%zpreserved' | '%address' | '%import' |
|
2018-08-13 10:51:05 +02:00
|
|
|
'%breakpoint' | '%asminclude' | '%asmbinary' | '%option')
|
2018-08-10 23:56:30 +02:00
|
|
|
(directivearg? | directivearg (',' directivearg)*)
|
|
|
|
;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 23:56:30 +02:00
|
|
|
directivearg : stringliteral | identifier | integerliteral ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-07-15 22:28:05 +02:00
|
|
|
vardecl: datatype ZEROPAGE? (arrayindex | ARRAYSIG) ? varname=identifier ;
|
|
|
|
|
|
|
|
structvardecl: structname=identifier varname=identifier ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-01-26 22:46:01 +01:00
|
|
|
varinitializer : vardecl '=' expression ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-07-15 22:28:05 +02:00
|
|
|
structvarinitializer : structvardecl '=' expression ;
|
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
constdecl: 'const' varinitializer ;
|
|
|
|
|
2019-04-12 22:00:32 +02:00
|
|
|
memoryvardecl: ADDRESS_OF varinitializer;
|
2018-08-10 00:26:41 +02:00
|
|
|
|
2019-07-12 06:14:59 +02:00
|
|
|
structdecl: 'struct' identifier '{' EOL vardecl ( EOL vardecl)* EOL? '}' EOL;
|
|
|
|
|
2019-03-10 18:11:26 +01:00
|
|
|
datatype: 'ubyte' | 'byte' | 'uword' | 'word' | 'float' | 'str' | 'str_s' ;
|
2018-08-10 00:26:41 +02:00
|
|
|
|
2019-04-16 01:19:51 +02:00
|
|
|
arrayindex: '[' expression ']' ;
|
2018-08-10 00:26:41 +02:00
|
|
|
|
2019-07-10 09:33:19 +02:00
|
|
|
assignment : assign_target '=' expression ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
|
|
|
augassignment :
|
2019-01-09 22:01:47 +01:00
|
|
|
assign_target operator=('+=' | '-=' | '/=' | '*=' | '**=' | '&=' | '|=' | '^=' | '%=' | '<<=' | '>>=' ) expression
|
2018-08-09 01:54:43 +02:00
|
|
|
;
|
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
assign_target:
|
|
|
|
register
|
2018-08-10 02:58:41 +02:00
|
|
|
| scoped_identifier
|
2018-10-02 03:07:46 +02:00
|
|
|
| arrayindexed
|
2018-12-30 21:40:27 +01:00
|
|
|
| directmemory
|
2018-08-10 00:26:41 +02:00
|
|
|
;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 23:56:30 +02:00
|
|
|
postincrdecr : assign_target operator = ('++' | '--') ;
|
|
|
|
|
2018-08-09 01:54:43 +02:00
|
|
|
expression :
|
2018-12-31 04:48:26 +01:00
|
|
|
functioncall
|
|
|
|
| <assoc=right> prefix = ('+'|'-'|'~') expression
|
2019-01-26 17:32:26 +01:00
|
|
|
| left = expression EOL? bop = '**' EOL? right = expression
|
|
|
|
| left = expression EOL? bop = ('*' | '/' | '%' ) EOL? right = expression
|
|
|
|
| left = expression EOL? bop = ('+' | '-' ) EOL? right = expression
|
|
|
|
| left = expression EOL? bop = ('<<' | '>>' ) EOL? right = expression
|
|
|
|
| left = expression EOL? bop = ('<' | '>' | '<=' | '>=') EOL? right = expression
|
|
|
|
| left = expression EOL? bop = ('==' | '!=') EOL? right = expression
|
|
|
|
| left = expression EOL? bop = '&' EOL? right = expression
|
|
|
|
| left = expression EOL? bop = '^' EOL? right = expression
|
|
|
|
| left = expression EOL? bop = '|' EOL? right = expression
|
2018-09-16 15:40:28 +02:00
|
|
|
| rangefrom = expression 'to' rangeto = expression ('step' rangestep = expression)? // can't create separate rule due to mutual left-recursion
|
2019-01-26 17:32:26 +01:00
|
|
|
| left = expression EOL? bop = 'and' EOL? right = expression
|
|
|
|
| left = expression EOL? bop = 'or' EOL? right = expression
|
|
|
|
| left = expression EOL? bop = 'xor' EOL? right = expression
|
2018-08-11 19:15:39 +02:00
|
|
|
| prefix = 'not' expression
|
2018-08-09 01:54:43 +02:00
|
|
|
| literalvalue
|
|
|
|
| register
|
2018-08-10 02:58:41 +02:00
|
|
|
| scoped_identifier
|
2018-10-01 22:23:16 +02:00
|
|
|
| arrayindexed
|
2018-12-30 21:40:27 +01:00
|
|
|
| directmemory
|
2019-04-11 20:55:20 +02:00
|
|
|
| addressof
|
2018-12-19 03:51:22 +01:00
|
|
|
| expression typecast
|
2018-12-31 04:48:26 +01:00
|
|
|
| '(' expression ')'
|
2018-08-09 01:54:43 +02:00
|
|
|
;
|
|
|
|
|
2018-08-10 23:56:30 +02:00
|
|
|
|
2018-12-19 03:51:22 +01:00
|
|
|
typecast : 'as' datatype;
|
|
|
|
|
|
|
|
|
2019-04-16 01:19:51 +02:00
|
|
|
arrayindexed : scoped_identifier arrayindex ;
|
2018-10-01 22:23:16 +02:00
|
|
|
|
2018-12-31 04:48:26 +01:00
|
|
|
directmemory : '@' '(' expression ')';
|
2018-12-30 21:40:27 +01:00
|
|
|
|
2019-04-12 22:00:32 +02:00
|
|
|
addressof : <assoc=right> ADDRESS_OF scoped_identifier ;
|
2019-04-04 21:02:24 +02:00
|
|
|
|
2018-10-01 22:23:16 +02:00
|
|
|
|
2019-01-02 23:32:41 +01:00
|
|
|
functioncall : scoped_identifier '(' expression_list? ')' ;
|
2018-08-14 02:22:59 +02:00
|
|
|
|
|
|
|
|
2019-01-02 23:32:41 +01:00
|
|
|
functioncall_stmt : scoped_identifier '(' expression_list? ')' ;
|
2018-08-14 02:22:59 +02:00
|
|
|
|
2018-08-12 17:16:36 +02:00
|
|
|
expression_list :
|
2018-12-31 01:52:18 +01:00
|
|
|
expression (',' EOL? expression)* // you can split the expression list over several lines
|
2018-08-09 01:54:43 +02:00
|
|
|
;
|
|
|
|
|
2019-07-10 19:14:41 +02:00
|
|
|
returnstmt : 'return' expression? ;
|
2018-08-12 17:16:36 +02:00
|
|
|
|
2018-09-16 03:00:32 +02:00
|
|
|
breakstmt : 'break';
|
|
|
|
|
|
|
|
continuestmt: 'continue';
|
|
|
|
|
2018-08-10 02:58:41 +02:00
|
|
|
identifier : NAME ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-01-02 23:32:41 +01:00
|
|
|
scoped_identifier : NAME ('.' NAME)* ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-11-02 22:06:57 +01:00
|
|
|
register : 'A' | 'X' | 'Y' ;
|
2018-09-02 18:32:48 +02:00
|
|
|
|
2019-07-09 04:09:29 +02:00
|
|
|
registerorpair : 'A' | 'X' | 'Y' | 'AX' | 'AY' | 'XY' ; // pairs can only be used in subroutine params and returnvalues
|
2018-11-10 00:53:50 +01:00
|
|
|
|
2018-10-01 20:05:32 +02:00
|
|
|
statusregister : 'Pc' | 'Pz' | 'Pn' | 'Pv' ;
|
|
|
|
|
2018-09-13 22:31:59 +02:00
|
|
|
integerliteral : intpart=(DEC_INTEGER | HEX_INTEGER | BIN_INTEGER) wordsuffix? ;
|
|
|
|
|
|
|
|
wordsuffix : '.w' ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
booleanliteral : 'true' | 'false' ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-07-16 00:08:28 +02:00
|
|
|
arrayliteral : '[' EOL? expression (',' EOL? expression)* EOL? ']' ; // you can split the values over several lines
|
|
|
|
|
|
|
|
structliteral : '{' EOL? expression (',' EOL? expression)* EOL? '}' ; // you can split the values over several lines
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
stringliteral : STRING ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2018-09-30 23:07:26 +02:00
|
|
|
charliteral : SINGLECHAR ;
|
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
floatliteral : FLOAT_NUMBER ;
|
2018-08-09 01:54:43 +02:00
|
|
|
|
2019-07-16 00:08:28 +02:00
|
|
|
|
2018-08-10 00:26:41 +02:00
|
|
|
literalvalue :
|
|
|
|
integerliteral
|
|
|
|
| booleanliteral
|
|
|
|
| arrayliteral
|
|
|
|
| stringliteral
|
2018-09-30 23:07:26 +02:00
|
|
|
| charliteral
|
2018-08-10 00:26:41 +02:00
|
|
|
| floatliteral
|
2019-07-16 00:08:28 +02:00
|
|
|
| structliteral
|
2018-08-09 01:54:43 +02:00
|
|
|
;
|
2018-08-10 23:56:30 +02:00
|
|
|
|
|
|
|
inlineasm : '%asm' INLINEASMBLOCK;
|
2018-08-13 01:30:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
subroutine :
|
2018-10-01 20:05:32 +02:00
|
|
|
'sub' identifier '(' sub_params? ')' sub_return_part? (statement_block EOL)
|
2018-08-13 04:12:42 +02:00
|
|
|
;
|
|
|
|
|
2018-09-30 18:49:58 +02:00
|
|
|
sub_return_part : '->' sub_returns ;
|
|
|
|
|
2018-08-14 14:33:36 +02:00
|
|
|
statement_block :
|
2018-08-13 04:12:42 +02:00
|
|
|
'{' EOL
|
2018-08-13 01:30:33 +02:00
|
|
|
(statement | EOL) *
|
2018-08-14 14:33:36 +02:00
|
|
|
'}'
|
2018-08-13 01:30:33 +02:00
|
|
|
;
|
|
|
|
|
2018-08-13 04:12:42 +02:00
|
|
|
|
2018-12-19 02:51:22 +01:00
|
|
|
sub_params : vardecl (',' EOL? vardecl)* ;
|
2018-10-01 20:05:32 +02:00
|
|
|
|
2018-10-02 22:52:05 +02:00
|
|
|
sub_returns : datatype (',' EOL? datatype)* ;
|
2018-10-01 20:05:32 +02:00
|
|
|
|
|
|
|
asmsubroutine :
|
2019-07-08 23:00:18 +02:00
|
|
|
'asmsub' identifier '(' asmsub_params? ')' EOL?
|
|
|
|
asmsub_clobbers? asmsub_returns? (asmsub_address | statement_block )
|
2018-10-01 20:05:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
asmsub_address : '=' address=integerliteral ;
|
|
|
|
|
2018-10-02 22:52:05 +02:00
|
|
|
asmsub_params : asmsub_param (',' EOL? asmsub_param)* ;
|
2018-10-01 20:05:32 +02:00
|
|
|
|
2019-07-08 23:00:18 +02:00
|
|
|
asmsub_param : vardecl '@' (registerorpair | statusregister | stack='stack') ;
|
|
|
|
|
|
|
|
asmsub_clobbers : 'clobbers' '(' clobber? ')' ;
|
2018-10-01 20:05:32 +02:00
|
|
|
|
|
|
|
clobber : register (',' register)* ;
|
2018-08-13 01:30:33 +02:00
|
|
|
|
2019-07-08 23:00:18 +02:00
|
|
|
asmsub_returns : '->' asmsub_return (',' EOL? asmsub_return)* ;
|
2018-08-13 01:30:33 +02:00
|
|
|
|
2019-01-04 22:11:46 +01:00
|
|
|
asmsub_return : datatype '@' (registerorpair | statusregister | stack='stack') ;
|
2018-08-14 14:33:36 +02:00
|
|
|
|
|
|
|
|
2019-01-01 18:45:21 +01:00
|
|
|
if_stmt : 'if' expression EOL? (statement | statement_block) EOL? else_part? ; // statement is constrained later
|
2018-08-14 14:33:36 +02:00
|
|
|
|
|
|
|
else_part : 'else' EOL? (statement | statement_block) ; // statement is constrained later
|
2018-09-02 18:32:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
branch_stmt : branchcondition EOL? (statement | statement_block) EOL? else_part? EOL ;
|
|
|
|
|
2018-09-22 00:33:25 +02:00
|
|
|
branchcondition: 'if_cs' | 'if_cc' | 'if_eq' | 'if_z' | 'if_ne' | 'if_nz' | 'if_pl' | 'if_pos' | 'if_mi' | 'if_neg' | 'if_vs' | 'if_vc' ;
|
2018-09-16 03:00:32 +02:00
|
|
|
|
|
|
|
|
2019-01-26 22:46:01 +01:00
|
|
|
forloop : 'for' datatype? ZEROPAGE? (register | identifier) 'in' expression EOL? (statement | statement_block) ;
|
2018-09-23 02:04:45 +02:00
|
|
|
|
|
|
|
whileloop: 'while' expression EOL? (statement | statement_block) ;
|
|
|
|
|
|
|
|
repeatloop: 'repeat' (statement | statement_block) EOL? 'until' expression ;
|
2019-07-09 00:02:38 +02:00
|
|
|
|
|
|
|
whenstmt: 'when' expression '{' EOL (when_choice | EOL) * '}' EOL? ;
|
|
|
|
|
2019-07-10 00:25:21 +02:00
|
|
|
when_choice: (expression_list | 'else' ) '->' (statement | statement_block ) ;
|
2019-07-12 06:14:59 +02:00
|
|
|
|