forked from pzbitskiy/tealang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TealangParser.g4
185 lines (152 loc) · 5.82 KB
/
TealangParser.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
parser grammar TealangParser;
options {
tokenVocab = TealangLexer;
}
program
: declaration* (main) EOF
;
module
: declaration* EOF
;
onelinecond
: expr EOF
;
statement
: decl (NEWLINE|SEMICOLON)
| condition
| termination
| assignment
| builtinVarStatement
| NEWLINE|SEMICOLON
;
block
: LEFTFIGURE statement* RIGHTFIGURE
;
main
: FUNC MAINFUNC LEFTPARA RIGHTPARA block NEWLINE*
;
declaration
: decl (NEWLINE|SEMICOLON)
| IMPORT MODULENAME MODULENAMEEND
| INLINE? FUNC IDENT LEFTPARA (IDENT (COMMA IDENT)* )? RIGHTPARA block NEWLINE
| NEWLINE|SEMICOLON
;
// named rules for tree-walking only
condition
: IF condIfExpr condTrueBlock (NEWLINE? ELSE condFalseBlock)? # IfStatement
| FOR condForExpr condTrueBlock # ForStatement
;
condTrueBlock
: block # IfStatementTrue
;
condFalseBlock
: block # IfStatementFalse
;
termination
: ERR (NEWLINE|SEMICOLON) # TermError
| RET expr (NEWLINE|SEMICOLON) # TermReturn
| ASSERT LEFTPARA expr RIGHTPARA # TermAssert
| BREAK (NEWLINE|SEMICOLON) # Break
;
decl
: LET IDENT EQ expr # DeclareVar
| LET IDENT COMMA IDENT EQ tupleExpr # DeclareVarTupleExpr
| LET IDENT COMMA IDENT COMMA IDENT COMMA IDENT EQ tupleExpr # DeclareQuadrupleExpr
| CONST IDENT EQ NUMBER # DeclareNumberConst
| CONST IDENT EQ STRING # DeclareStringConst
;
assignment
: IDENT EQ expr # Assign
| IDENT COMMA IDENT EQ tupleExpr # AssignTuple
| IDENT COMMA IDENT COMMA IDENT COMMA IDENT EQ tupleExpr # AssignQuadruple
;
expr
: IDENT # Identifier
| NUMBER # NumberLiteral
| STRING # StringLiteral
| LEFTPARA expr RIGHTPARA # Group
| functionCall # FunctionCallExpr
| builtinVarExpr # BuiltinObject
| op=LNOT expr # Not
| op=BNOT expr # BitNot
| expr op=(MUL|DIV|MOD) expr # MulDivMod
| expr op=(PLUS|MINUS) expr # AddSub
| expr op=(LESS|LE|GREATER|GE|EE|NE) expr # Relation
| expr op=(BOR|BXOR|BAND) expr # BitOp
| expr op=(LAND|LOR) expr # AndOr
| condExpr # IfExpr
;
tupleExpr
: MULW LEFTPARA ( expr COMMA expr ) RIGHTPARA
| ADDW LEFTPARA ( expr COMMA expr ) RIGHTPARA
| EXPW LEFTPARA ( expr COMMA expr ) RIGHTPARA
| DIVMODW LEFTPARA ( expr COMMA expr COMMA expr COMMA expr ) RIGHTPARA
| builtinVarTupleExpr
;
builtinVarTupleExpr
: ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT (APPGETEX|ASSETHLDBALANCE|ASSETHLDFROZEN) LEFTPARA expr (COMMA expr)? RIGHTPARA
| APPS LEFTSQUARE expr RIGHTSQUARE DOT APPGETEX LEFTPARA expr RIGHTPARA
| ASSETS LEFTSQUARE expr RIGHTSQUARE DOT ASSETPARAMSFIELDS
;
builtinVarStatement
: ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT (APPPUT|APPDEL) LEFTPARA expr (COMMA expr)? RIGHTPARA
| APPS LEFTSQUARE expr RIGHTSQUARE DOT (APPPUT|APPDEL) LEFTPARA expr (COMMA expr)? RIGHTPARA
;
functionCall
: BUILTINFUNC LEFTPARA (expr (COMMA expr)* )? RIGHTPARA # BuiltinFunCall
| IDENT LEFTPARA (expr (COMMA expr)* )? RIGHTPARA # FunCall
;
builtinVarExpr
: GLOBAL DOT GLOBALFIELD # GlobalFieldExpr
| txn # TxnFieldExpr
| gtxn # GroupTxnFieldExpr
| args # ArgsExpr
| accounts # AccountsExpr
| apps # AppsExpr
| gaid # GaidExpr
;
txn
: TXN DOT TXNFIELD # TxnSingleFieldExpr
| TXN DOT TXNARRAYFIELD LEFTSQUARE (IDENT|NUMBER) RIGHTSQUARE # TxnArrayFieldExpr
;
gtxn
: GTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNFIELD # GroupTxnSingleFieldExpr
| GTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNARRAYFIELD LEFTSQUARE (IDENT|NUMBER) RIGHTSQUARE # GroupTxnArrayFieldExpr
;
args
: ARGS LEFTSQUARE NUMBER RIGHTSQUARE # ArgsNumberExpr
| ARGS LEFTSQUARE IDENT RIGHTSQUARE # ArgsIdentExpr
;
accounts
: ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT (BALANCE|MINIMUMBALANCE) # AccountsBalanceExpr
| ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT (OPTEDIN|APPGET) LEFTPARA expr RIGHTPARA # AccountsSingleMethodsExpr
;
apps
: APPS LEFTSQUARE expr RIGHTSQUARE DOT APPGET LEFTPARA expr RIGHTPARA # AppsSingleMethodsExpr
;
compoundElem
: IDENT DOT IDENT
| arrayElem DOT IDENT
;
arrayElem
: IDENT LEFTSQUARE NUMBER RIGHTSQUARE
;
gaid
: GAID LEFTPARA NUMBER RIGHTPARA # GaidNumberExpr
;
// named rules for tree-walking only
condExpr
: IF condIfExpr LEFTFIGURE condTrueExpr RIGHTFIGURE ELSE LEFTFIGURE condFalseExpr RIGHTFIGURE
;
condTrueExpr
: expr # IfExprTrue
;
condFalseExpr
: expr # IfExprFalse
;
condIfExpr
: expr # IfExprCond
;
condForExpr
: expr # ForExprCond
;