-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyScriptParser.g4
78 lines (66 loc) · 2.12 KB
/
FlyScriptParser.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** Simple statically-typed programming language with functions and variables
* taken from "Language Implementation Patterns" book.
*/
parser grammar FlyScriptParser;
options {
tokenVocab = 'FlyScriptLexer';
}
file: (function | field | stmt)+ ;
//varDecl
// : Type ID ('=' expr)? ';' #varStmt
// ;
//type: 'string' | 'float' | 'int' | 'void' ;
type: Type_void | Type_string | Type_float | Type_int | Type_boolean ;
field
: type ID ('=' expr)? ';'
;
function
: type ID '(' functionParameters? ')' block // "void f(int x) {...}"
;
functionParameters
: functionParameter (',' functionParameter)*
;
functionParameter
: type ID
;
block: '{' stmt* '}' ; // possibly empty statement block
stmt
:
block #codeBlock
| type ID ('=' expr)? ';' #varStmt
| 'if' '(' expr ')' block ('else' block)? #ifElseStmt
| 'while' '(' expr ')' block #whileStmt
| 'break' ';' #breakStmt
| 'return' expr? ';' #returnStmt
| expr '=' expr ';' #assignStmt // assignment
| expr ';' #invokeStmt // func call
;
judgeSymbol: '==' #judgeEqualSymbol
| '!=' #notEqualSymbol
| '>=' #greaterOrEqualSymbol
| '>' #greaterSymbol
| '<=' #lesserOrEqualSymbol
| '<' #lesserSymbol
;
expr: ID '(' exprList? ')' #invokeExpr // func call like f(), f(x), f(1,2)
| ID '[' expr ']' #arrayGetExpr // array index like a[i], a[i][j]
| '!' expr #notExpr // boolean not
| expr ('*'|'/') expr #mulDivExpr
| expr ('+'|'-') expr #plusSubExpr
| expr ('|') expr #orExpr
| expr ('&') expr #andExpr
| expr judgeSymbol expr #booleanExpr
| expr '||' expr #orBooleanExpr
| expr '&&' expr #andBooleanExpr
| ID #identifierExpr // variable reference
| INT #intExpr
| Kv_True #booleanTrueExpr
| Kv_False #booleanFalseExpr
| DQUOTE stringContents* DQUOTE #stringExpr
| '(' expr ')' #parensExpr
;
exprList : expr (',' expr)* ; // arg list
stringContents : TEXT
| ESCAPE_SEQUENCE
| '\\(' expr ')'
;