-
Notifications
You must be signed in to change notification settings - Fork 8
/
TealangParser.g4
218 lines (181 loc) · 7.61 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
parser grammar TealangParser;
options {
tokenVocab = TealangLexer;
}
program
: globalStatement* (main) EOF
;
module
: declaration* EOF
;
onelinecond
: expr EOF
;
statement
: decl (NEWLINE|SEMICOLON)
| condition
| termination
| assignment
| builtinVarStatement
| innertxn
| functionCallStatement
| NEWLINE|SEMICOLON
;
block
: LEFTFIGURE statement* RIGHTFIGURE
;
main
: FUNC MAINFUNC LEFTPARA RIGHTPARA block NEWLINE*
;
globalStatement
: declaration
| functionCallStatement
;
declaration
: decl (NEWLINE|SEMICOLON)
| IMPORT MODULENAME MODULENAMEEND
| INLINE? FUNC IDENT LEFTPARA (IDENT (COMMA IDENT)* )? RIGHTPARA VOID? 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
;
innertxn
: INNERTXN DOT ITXNBEGIN LEFTPARA RIGHTPARA # InnerTxnBegin
| INNERTXN DOT ITXNNEXT LEFTPARA RIGHTPARA # InnerTxnNext
| INNERTXN DOT ITXNEND LEFTPARA RIGHTPARA # InnerTxnEnd
| INNERTXN DOT TXNFIELD EQ expr # InnerTxnAssign
| INNERTXN DOT TXNARRAYFIELD DOT ITXNPUSH LEFTPARA expr RIGHTPARA # InnerTxnArrayAssign
;
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
| functionCallExpresion # 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
| (TOINT|TOBYTE) LEFTPARA (expr) RIGHTPARA # TypeCastExpr
;
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
| ECDSADECOMPRESS LEFTPARA ( ECDSACURVE COMMA expr ) RIGHTPARA
| ECDSARECOVER LEFTPARA ( ECDSACURVE COMMA expr COMMA expr COMMA expr COMMA expr ) RIGHTPARA
| builtinVarTupleExpr
;
builtinVarTupleExpr
: ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT (APPGETEX|ASSETHLDBALANCE|ASSETHLDFROZEN) LEFTPARA expr (COMMA expr)? RIGHTPARA
| ACCOUNTS LEFTSQUARE expr RIGHTSQUARE DOT ACCTPARAMS LEFTPARA RIGHTPARA
| APPS LEFTSQUARE expr RIGHTSQUARE DOT (APPGETEX|APPPARAMSFIELDS) LEFTPARA expr RIGHTPARA
| APPS LEFTSQUARE expr RIGHTSQUARE DOT APPPARAMSFIELDS LEFTPARA 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
;
functionCallExpresion
: BUILTINFUNC LEFTPARA ( expr (COMMA expr)* )? RIGHTPARA # BuiltinFunCall
| functionCall # FunCall
| ECDSAVERIFY LEFTPARA ( ECDSACURVE COMMA expr COMMA expr COMMA expr COMMA expr COMMA expr ) RIGHTPARA # EcDsaFunCall
| EXTRACT LEFTPARA ( (EXTRACTOPT COMMA)? expr COMMA expr (COMMA expr)? ) RIGHTPARA # ExtractFunCall
;
functionCallStatement
: functionCall # VoidFunCall
| LOG LEFTPARA expr RIGHTPARA # DoLog
;
functionCall
: IDENT LEFTPARA ( expr (COMMA expr)* )? RIGHTPARA
;
builtinVarExpr
: GLOBAL DOT GLOBALFIELD # GlobalFieldExpr
| txn # TxnFieldExpr
| gtxn # GroupTxnFieldExpr
| ARGS LEFTSQUARE expr RIGHTSQUARE # ArgsExpr
| accounts # AccountsExpr
| apps # AppsExpr
| itxn # InnerTxnFieldExpr
| gitxn # GroupInnerTxnFieldExpr
;
txn
: TXN DOT TXNFIELD # TxnSingleFieldExpr
| TXN DOT TXNARRAYFIELD LEFTSQUARE (expr) RIGHTSQUARE # TxnArrayFieldExpr
;
gtxn
: GTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNFIELD # GroupTxnSingleFieldExpr
| GTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNARRAYFIELD LEFTSQUARE expr RIGHTSQUARE # GroupTxnArrayFieldExpr
;
itxn
: INNERTXN DOT TXNFIELD # InnerTxnSingleFieldExpr
| INNERTXN DOT TXNARRAYFIELD LEFTSQUARE (expr) RIGHTSQUARE # InnerTxnArrayFieldExpr
;
gitxn
: GINNERTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNFIELD # GroupInnerTxnSingleFieldExpr
| GINNERTXN LEFTSQUARE expr RIGHTSQUARE DOT TXNARRAYFIELD LEFTSQUARE expr RIGHTSQUARE # GroupInnerTxnArrayFieldExpr
;
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
;
// 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
;