2018-08-08 23:54:43 +00:00
|
|
|
/*
|
2018-09-15 14:21:05 +00:00
|
|
|
Prog8 combined lexer and parser grammar
|
2018-08-10 21:56:30 +00:00
|
|
|
|
|
|
|
NOTES:
|
|
|
|
|
|
|
|
- whitespace is ignored. (tabs/spaces)
|
2018-09-13 20:31:59 +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
|
|
|
*/
|
|
|
|
|
2021-10-09 14:56:39 +00:00
|
|
|
// -> java classes Prog8ANTLRParser and Prog8ANTLRLexer,
|
|
|
|
// both NOT to be used from Kotlin code, but ONLY through Kotlin class Prog8Parser
|
|
|
|
grammar Prog8ANTLR;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2019-01-29 11:06:33 +00:00
|
|
|
@header {
|
|
|
|
package prog8.parser;
|
|
|
|
}
|
2018-08-11 12:06:43 +00:00
|
|
|
|
2021-06-14 20:04:22 +00:00
|
|
|
LINECOMMENT : ('\r'? '\n' | '\r') [ \t]* COMMENT -> channel(HIDDEN);
|
|
|
|
COMMENT : ';' ~[\r\n]* -> channel(HIDDEN) ;
|
|
|
|
EOL : ('\r'? '\n' | '\r')+ ;
|
2021-06-13 20:49:54 +00:00
|
|
|
|
|
|
|
WS : [ \t] -> skip ;
|
2019-01-26 16:32:26 +00:00
|
|
|
// WS2 : '\\' EOL -> skip;
|
2020-02-09 00:21:23 +00:00
|
|
|
VOID: 'void';
|
2020-08-22 15:03:40 +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')+ ;
|
2022-04-02 00:08:01 +00:00
|
|
|
ADDRESS_OF: '&' ;
|
|
|
|
INVALID_AND_COMPOSITE: '&&' ;
|
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
|
|
|
|
2021-12-05 17:11:40 +00:00
|
|
|
fragment STRING_ESCAPE_SEQ : '\\' . | '\\x' . . | '\\u' . . . .;
|
2018-08-09 22:26:41 +00:00
|
|
|
STRING :
|
|
|
|
'"' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f"] )* '"'
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
2018-08-10 21:56:30 +00:00
|
|
|
INLINEASMBLOCK :
|
|
|
|
'{{' .+? '}}'
|
|
|
|
;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2018-09-30 21:07:26 +00:00
|
|
|
SINGLECHAR :
|
2021-12-05 17:11:40 +00:00
|
|
|
'\'' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f'] ) '\''
|
2018-09-30 21:07:26 +00:00
|
|
|
;
|
|
|
|
|
2022-01-14 22:16:05 +00:00
|
|
|
ZEROPAGE : '@zp' ;
|
2019-01-26 21:46:01 +00:00
|
|
|
|
2022-01-14 22:16:05 +00:00
|
|
|
ZEROPAGEREQUIRE : '@requirezp' ;
|
|
|
|
|
|
|
|
SHARED : '@shared' ;
|
2021-05-18 23:19:25 +00:00
|
|
|
|
2019-04-15 23:19:51 +00:00
|
|
|
ARRAYSIG :
|
|
|
|
'[]'
|
|
|
|
;
|
|
|
|
|
2020-12-21 18:19:53 +00:00
|
|
|
cpuregister: 'A' | 'X' | 'Y';
|
|
|
|
register: 'A' | 'X' | 'Y' | 'AX' | 'AY' | 'XY' | 'Pc' | 'Pz' | 'Pn' | 'Pv' | 'R0' | 'R1' | 'R2' | 'R3' | 'R4' | 'R5' | 'R6' | 'R7' | 'R8' | 'R9' | 'R10' | 'R11' | 'R12' | 'R13' | 'R14' | 'R15';
|
|
|
|
|
2021-06-14 20:17:30 +00:00
|
|
|
// A module (file) consists of zero or more directives or blocks, in any order.
|
|
|
|
// If there are more than one, then they must be separated by EOL (one or more newlines).
|
|
|
|
// However, trailing EOL is NOT required.
|
2021-06-18 19:55:03 +00:00
|
|
|
// Note: the parser may see *several* consecutive EOLs - this happens when EOL and comments are interleaved (see #47)
|
|
|
|
module: EOL* ((directive | block) (EOL+ (directive | block))*)? EOL* EOF;
|
2018-08-10 21:56:30 +00:00
|
|
|
|
2021-06-14 20:17:30 +00:00
|
|
|
block: identifier integerliteral? '{' EOL (block_statement | EOL)* '}';
|
2020-03-14 13:20:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
block_statement:
|
|
|
|
directive
|
|
|
|
| variabledeclaration
|
|
|
|
| subroutinedeclaration
|
|
|
|
| inlineasm
|
2021-04-18 21:03:18 +00:00
|
|
|
| labeldef
|
2020-03-14 13:20:55 +00:00
|
|
|
;
|
|
|
|
|
2018-08-09 22:26:41 +00:00
|
|
|
|
|
|
|
statement :
|
2018-08-08 23:54:43 +00:00
|
|
|
directive
|
2020-03-14 12:23:13 +00:00
|
|
|
| variabledeclaration
|
2018-08-08 23:54:43 +00:00
|
|
|
| assignment
|
|
|
|
| augassignment
|
2018-08-10 21:56:30 +00:00
|
|
|
| unconditionaljump
|
|
|
|
| postincrdecr
|
2018-08-14 00:22:59 +00:00
|
|
|
| functioncall_stmt
|
2018-08-14 12:33:36 +00:00
|
|
|
| if_stmt
|
2018-09-02 16:32:48 +00:00
|
|
|
| branch_stmt
|
2020-03-14 12:23:13 +00:00
|
|
|
| subroutinedeclaration
|
2018-08-10 21:56:30 +00:00
|
|
|
| inlineasm
|
2018-08-12 15:16:36 +00:00
|
|
|
| returnstmt
|
2018-09-16 01:00:32 +00:00
|
|
|
| forloop
|
2018-09-23 00:04:45 +00:00
|
|
|
| whileloop
|
2020-07-25 14:25:02 +00:00
|
|
|
| untilloop
|
2018-09-23 00:04:45 +00:00
|
|
|
| repeatloop
|
2019-07-08 22:02:38 +00:00
|
|
|
| whenstmt
|
2018-09-18 21:14:32 +00:00
|
|
|
| breakstmt
|
2018-09-19 00:41:35 +00:00
|
|
|
| labeldef
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
|
|
|
|
2018-09-16 01:00:32 +00:00
|
|
|
|
2020-03-14 12:23:13 +00:00
|
|
|
variabledeclaration :
|
|
|
|
varinitializer
|
|
|
|
| vardecl
|
|
|
|
| constdecl
|
|
|
|
| memoryvardecl
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
subroutinedeclaration :
|
|
|
|
subroutine
|
|
|
|
| asmsubroutine
|
|
|
|
| romsubroutine
|
|
|
|
;
|
|
|
|
|
|
|
|
|
2018-08-12 15:16:36 +00:00
|
|
|
labeldef : identifier ':' ;
|
2018-08-10 21:56:30 +00:00
|
|
|
|
2019-01-02 22:32:41 +00:00
|
|
|
unconditionaljump : 'goto' (integerliteral | scoped_identifier) ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2018-08-10 21:56:30 +00:00
|
|
|
directive :
|
2018-10-08 22:01:53 +00:00
|
|
|
directivename=('%output' | '%launcher' | '%zeropage' | '%zpreserved' | '%address' | '%import' |
|
2021-09-04 13:01:16 +00:00
|
|
|
'%breakpoint' | '%asminclude' | '%asmbinary' | '%option' )
|
2018-08-10 21:56:30 +00:00
|
|
|
(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
|
|
|
|
2022-01-14 22:16:05 +00:00
|
|
|
vardecl: datatype (arrayindex | ARRAYSIG)? decloptions varname=identifier ;
|
|
|
|
|
|
|
|
decloptions: (SHARED | ZEROPAGE | ZEROPAGEREQUIRE)* ;
|
2019-07-15 20:28:05 +00:00
|
|
|
|
2019-01-26 21:46:01 +00:00
|
|
|
varinitializer : vardecl '=' expression ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2018-08-09 22:26:41 +00:00
|
|
|
constdecl: 'const' varinitializer ;
|
|
|
|
|
2019-04-12 20:00:32 +00:00
|
|
|
memoryvardecl: ADDRESS_OF varinitializer;
|
2018-08-09 22:26:41 +00:00
|
|
|
|
2020-02-07 19:47:38 +00:00
|
|
|
datatype: 'ubyte' | 'byte' | 'uword' | 'word' | 'float' | 'str' ;
|
2018-08-09 22:26:41 +00:00
|
|
|
|
2019-04-15 23:19:51 +00:00
|
|
|
arrayindex: '[' expression ']' ;
|
2018-08-09 22:26:41 +00:00
|
|
|
|
2019-07-10 07:33:19 +00:00
|
|
|
assignment : assign_target '=' expression ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
|
|
|
augassignment :
|
2022-03-27 10:12:57 +00:00
|
|
|
assign_target operator=('+=' | '-=' | '/=' | '*=' | '&=' | '|=' | '^=' | '%=' | '<<=' | '>>=' ) expression
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
|
|
|
|
2018-08-09 22:26:41 +00:00
|
|
|
assign_target:
|
2020-07-24 20:57:19 +00:00
|
|
|
scoped_identifier
|
2018-10-02 01:07:46 +00:00
|
|
|
| arrayindexed
|
2018-12-30 20:40:27 +00:00
|
|
|
| directmemory
|
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 :
|
2022-01-08 12:45:00 +00:00
|
|
|
'(' expression ')'
|
|
|
|
| functioncall
|
2018-12-31 03:48:26 +00:00
|
|
|
| <assoc=right> prefix = ('+'|'-'|'~') expression
|
2019-01-26 16:32:26 +00: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
|
2020-10-11 17:02:53 +00:00
|
|
|
| left = expression EOL? bop = ('==' | '!=') EOL? right = expression
|
2020-03-11 19:47:42 +00:00
|
|
|
| rangefrom = expression rto = ('to'|'downto') rangeto = expression ('step' rangestep = expression)? // can't create separate rule due to mutual left-recursion
|
2021-12-29 15:21:37 +00:00
|
|
|
| left = expression EOL? bop = 'in' EOL? right = expression
|
2021-11-08 17:38:04 +00:00
|
|
|
| prefix = 'not' expression
|
2019-01-26 16:32:26 +00: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-08 23:54:43 +00:00
|
|
|
| literalvalue
|
2018-08-10 00:58:41 +00:00
|
|
|
| scoped_identifier
|
2018-10-01 20:23:16 +00:00
|
|
|
| arrayindexed
|
2018-12-30 20:40:27 +00:00
|
|
|
| directmemory
|
2019-04-11 18:55:20 +00:00
|
|
|
| addressof
|
2018-12-19 02:51:22 +00:00
|
|
|
| expression typecast
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
|
|
|
|
2018-12-19 02:51:22 +00:00
|
|
|
typecast : 'as' datatype;
|
|
|
|
|
2019-04-15 23:19:51 +00:00
|
|
|
arrayindexed : scoped_identifier arrayindex ;
|
2018-10-01 20:23:16 +00:00
|
|
|
|
2018-12-31 03:48:26 +00:00
|
|
|
directmemory : '@' '(' expression ')';
|
2018-12-30 20:40:27 +00:00
|
|
|
|
2019-04-12 20:00:32 +00:00
|
|
|
addressof : <assoc=right> ADDRESS_OF scoped_identifier ;
|
2019-04-04 19:02:24 +00:00
|
|
|
|
2018-10-01 20:23:16 +00:00
|
|
|
|
2020-02-09 00:21:23 +00:00
|
|
|
functioncall : scoped_identifier '(' expression_list? ')' ;
|
2018-08-14 00:22:59 +00:00
|
|
|
|
2020-02-09 00:21:23 +00:00
|
|
|
functioncall_stmt : VOID? scoped_identifier '(' expression_list? ')' ;
|
2018-08-14 00:22:59 +00:00
|
|
|
|
2018-08-12 15:16:36 +00:00
|
|
|
expression_list :
|
2018-12-31 00:52:18 +00:00
|
|
|
expression (',' EOL? expression)* // you can split the expression list over several lines
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
|
|
|
|
2019-07-10 17:14:41 +00:00
|
|
|
returnstmt : 'return' expression? ;
|
2018-08-12 15:16:36 +00:00
|
|
|
|
2018-09-16 01:00:32 +00:00
|
|
|
breakstmt : 'break';
|
|
|
|
|
2018-08-10 00:58:41 +00:00
|
|
|
identifier : NAME ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2019-01-02 22:32:41 +00:00
|
|
|
scoped_identifier : NAME ('.' NAME)* ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2020-09-19 21:23:32 +00:00
|
|
|
integerliteral : intpart=(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
|
|
|
|
2019-07-15 22:08:28 +00:00
|
|
|
arrayliteral : '[' EOL? expression (',' EOL? expression)* EOL? ']' ; // you can split the values over several lines
|
|
|
|
|
2022-02-11 20:56:33 +00:00
|
|
|
stringliteral : (encoding=NAME ':')? STRING ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2022-02-11 20:56:33 +00:00
|
|
|
charliteral : (encoding=NAME ':')? SINGLECHAR ;
|
2018-09-30 21:07:26 +00:00
|
|
|
|
2018-08-09 22:26:41 +00:00
|
|
|
floatliteral : FLOAT_NUMBER ;
|
2018-08-08 23:54:43 +00:00
|
|
|
|
2019-07-15 22:08:28 +00:00
|
|
|
|
2018-08-09 22:26:41 +00:00
|
|
|
literalvalue :
|
|
|
|
integerliteral
|
|
|
|
| booleanliteral
|
|
|
|
| arrayliteral
|
|
|
|
| stringliteral
|
2018-09-30 21:07:26 +00:00
|
|
|
| charliteral
|
2018-08-09 22:26:41 +00:00
|
|
|
| floatliteral
|
2018-08-08 23:54:43 +00:00
|
|
|
;
|
2018-08-10 21:56:30 +00:00
|
|
|
|
|
|
|
inlineasm : '%asm' INLINEASMBLOCK;
|
2018-08-12 23:30:33 +00:00
|
|
|
|
2020-12-26 04:33:00 +00:00
|
|
|
inline: 'inline';
|
2018-08-12 23:30:33 +00:00
|
|
|
|
|
|
|
subroutine :
|
2022-05-09 13:42:58 +00:00
|
|
|
'sub' identifier '(' sub_params? ')' sub_return_part? (statement_block EOL)
|
2018-08-13 02:12:42 +00:00
|
|
|
;
|
|
|
|
|
2022-03-12 13:12:06 +00:00
|
|
|
sub_return_part : '->' datatype ;
|
2018-09-30 16:49:58 +00:00
|
|
|
|
2018-08-14 12:33:36 +00:00
|
|
|
statement_block :
|
2018-08-13 02:12:42 +00:00
|
|
|
'{' EOL
|
2018-08-12 23:30:33 +00:00
|
|
|
(statement | EOL) *
|
2018-08-14 12:33:36 +00:00
|
|
|
'}'
|
2018-08-12 23:30:33 +00:00
|
|
|
;
|
|
|
|
|
2018-08-13 02:12:42 +00:00
|
|
|
|
2018-12-19 01:51:22 +00:00
|
|
|
sub_params : vardecl (',' EOL? vardecl)* ;
|
2018-10-01 18:05:32 +00:00
|
|
|
|
|
|
|
asmsubroutine :
|
2020-12-26 04:33:00 +00:00
|
|
|
inline? 'asmsub' asmsub_decl statement_block
|
2018-10-01 18:05:32 +00:00
|
|
|
;
|
|
|
|
|
2020-03-10 22:09:31 +00:00
|
|
|
romsubroutine :
|
|
|
|
'romsub' integerliteral '=' asmsub_decl
|
|
|
|
;
|
|
|
|
|
|
|
|
asmsub_decl : identifier '(' asmsub_params? ')' asmsub_clobbers? asmsub_returns? ;
|
2018-10-01 18:05:32 +00:00
|
|
|
|
2018-10-02 20:52:05 +00:00
|
|
|
asmsub_params : asmsub_param (',' EOL? asmsub_param)* ;
|
2018-10-01 18:05:32 +00:00
|
|
|
|
2020-12-21 18:19:53 +00:00
|
|
|
asmsub_param : vardecl '@' register ; // A,X,Y,AX,AY,XY,Pc,Pz,Pn,Pv allowed.
|
2019-07-08 21:00:18 +00:00
|
|
|
|
|
|
|
asmsub_clobbers : 'clobbers' '(' clobber? ')' ;
|
2018-10-01 18:05:32 +00:00
|
|
|
|
2020-12-21 18:19:53 +00:00
|
|
|
clobber : cpuregister (',' cpuregister)* ; // A,X,Y allowed
|
2018-08-12 23:30:33 +00:00
|
|
|
|
2019-07-08 21:00:18 +00:00
|
|
|
asmsub_returns : '->' asmsub_return (',' EOL? asmsub_return)* ;
|
2018-08-12 23:30:33 +00:00
|
|
|
|
2020-12-21 18:19:53 +00:00
|
|
|
asmsub_return : datatype '@' register ; // A,X,Y,AX,AY,XY,Pc,Pz,Pn,Pv allowed
|
2018-08-14 12:33:36 +00:00
|
|
|
|
|
|
|
|
2019-01-01 17:45:21 +00:00
|
|
|
if_stmt : 'if' expression EOL? (statement | statement_block) EOL? else_part? ; // statement is constrained later
|
2018-08-14 12:33:36 +00:00
|
|
|
|
|
|
|
else_part : 'else' EOL? (statement | statement_block) ; // statement is constrained later
|
2018-09-02 16:32:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
branch_stmt : branchcondition EOL? (statement | statement_block) EOL? else_part? EOL ;
|
|
|
|
|
2018-09-21 22:33:25 +00: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 01:00:32 +00:00
|
|
|
|
|
|
|
|
2021-12-10 13:59:04 +00:00
|
|
|
forloop : 'for' scoped_identifier 'in' expression EOL? (statement | statement_block) ;
|
2018-09-23 00:04:45 +00:00
|
|
|
|
|
|
|
whileloop: 'while' expression EOL? (statement | statement_block) ;
|
|
|
|
|
2020-07-25 14:25:02 +00:00
|
|
|
untilloop: 'do' (statement | statement_block) EOL? 'until' expression ;
|
2019-07-08 22:02:38 +00:00
|
|
|
|
2020-07-25 14:25:02 +00:00
|
|
|
repeatloop: 'repeat' expression? EOL? (statement | statement_block) ;
|
2020-03-14 17:11:04 +00:00
|
|
|
|
2019-07-08 22:02:38 +00:00
|
|
|
whenstmt: 'when' expression '{' EOL (when_choice | EOL) * '}' EOL? ;
|
|
|
|
|
2019-07-09 22:25:21 +00:00
|
|
|
when_choice: (expression_list | 'else' ) '->' (statement | statement_block ) ;
|