2022-09-26 09:43:23 +02:00

81 lines
1.1 KiB
ANTLR

grammar Waifu2;
WS: ' ';
NL: '\u000A';
ID
: (Letter | '_') (Letter | '_' | DEC_DIGIT)*
;
fragment Letter: [a-zA-Z] ;
// comments
MultiLineComment
: '##' ( MultiLineComment | . )*? '##'
-> channel(HIDDEN)
;
SingleLineComment
: '#' ~[\u000A]*
-> channel(HIDDEN)
;
// operators
MULT: '*' ;
MOD: '%' ;
DIV: '/' ;
ADD: '+' ;
SUB: '-' ;
ASSIGN: '<-';
// RESERVED
LPAREN: '(' ;
RPAREN: ')' ;
COLON: ':' ;
// numerics
fragment DEC_DIGIT:
[0-9]
;
INT: DEC_DIGIT+ ;
DEC: DEC_DIGIT* '.' DEC_DIGIT+ ;
BOOL: [01] ;
STR: '\'' .*? '\'' ;
waifuFile
: NL* func* NL* EOF
;
// todo not right
type: COLON '#' ;
sign
: ADD
| SUB
;
operator: SUB | ADD | MULT | DIV | MOD ;
literal: INT | DEC | BOOL | STR ;
// func
func: functionDeclaration NL functionBody ;
functionDeclaration: LPAREN functionParam RPAREN WS type? WS ID NL ;
functionParam: ID WS type ;
functionBody: (expr NL+)+ ;
// expr
expr: literal
| ID
| expr WS operator WS expr
| sign expr
| MultiLineComment
| SingleLineComment
;