-
Notifications
You must be signed in to change notification settings - Fork 0
/
RalphParser.g4
203 lines (156 loc) · 3.63 KB
/
RalphParser.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
parser grammar RalphParser;
options
{
tokenVocab = RalphLexer;
}
sourceFile: (txScript | contract | interface | assetScript)* EOF;
identifierList: varName (COMMA varName)*;
varDeclSingle: (CONST | (LET MUT?)) varName ASSIGN expression;
varDeclMulti: (CONST | (LET MUT?)) L_PAREN identifierList R_PAREN ASSIGN expression;
varDecl
: varDeclSingle
| varDeclMulti //# varDeclStmt
;
varName: IDENTIFIER;
//expression
expression:
primaryExpr
| callChain
| ifStmt
| L_PAREN expression R_PAREN
| (SUB | NOT) expression
| expression (
CONCAT
| ADD
| SUB
| MUL
| DIV
| MOD
| MODADD
| MODSUB
| MODMUL
| SHL
| SHR
| BITAND
| XOR
| BITOR
) expression
| expression (
EQ
| NQ
| LT
| LE
| GT
| GE
) expression
| expression (AND | OR) expression
| expression ASSIGN expression
;
expressionList: (expression COMMA?)*;
callChain: (varName | methodCall) (DOT callChain)*;
methodCall: IDENTIFIER aps? L_PAREN expressionList R_PAREN;
apsAlph: expression R_ARROW expression;
apsToken: apsAlph COLON expression;
apsBoth: apsAlph COMMA expression COLON expression;
aps: L_CURLY (apsAlph | apsToken | apsBoth) R_CURLY;
primaryExpr
: basicLit
| arrayExpr
;
primitiveType
: BOOL
| I256
| BYTE
| U256
| BYTEVEC
| ADDRESS
| arrayType
;
arrayType
: L_BRACKET typeName SEMI expression R_BRACKET // # arrayTypeDeclStmt
;
arrayExpr: IDENTIFIER? L_BRACKET expression (COMMA expression)* R_BRACKET;
typeName: primitiveType | IDENTIFIER ;
result
: L_PAREN R_PAREN
| typeName
| L_PAREN (typeName (COMMA typeName)* COMMA?)? R_PAREN
;
param: ATUNUSED? MUT? IDENTIFIER COLON typeName;
paramList: (param COMMA?)*;
// Function declarations
methodDecl
: (annotation)? PUB? FN IDENTIFIER L_PAREN paramList R_PAREN (R_ARROW result)? block?
;
basicLit
: integer
| string_
| ADDRESS_LIT
| ALPH_LIT
| BOOL_LIT
;
integer
: DECIMAL_LIT
| BINARY_LIT
| OCTAL_LIT
| HEX_LIT
| IMAGINARY_LIT
| RUNE_LIT
;
string_: RAW_STRING_LIT | INTERPRETED_STRING_LIT;
varNameAssign: varName ASSIGN basicLit;
enum: ENUM IDENTIFIER L_CURLY varNameAssign* R_CURLY;
typeStructBody: L_CURLY (statement | event | methodDecl | enum)* R_CURLY;
txScript
: TXSCRIPT IDENTIFIER (L_PAREN paramList R_PAREN)? typeStructBody // # txScriptDeclStmt
;
assetScript
: ASSETSCRIPT IDENTIFIER (L_PAREN paramList R_PAREN)? typeStructBody
;
contract
: ABSTRACT? CONTRACT IDENTIFIER (L_PAREN paramList R_PAREN)? extends? implements? typeStructBody // # contractDeclStmt
;
extends
: EXTENDS contractExtends (COMMA contractExtends)*
;
contractExtends: IDENTIFIER L_PAREN expressionList R_PAREN;
implements
: IMPLEMENTS IDENTIFIER
;
interface
: INTERFACE IDENTIFIER (EXTENDS IDENTIFIER)? typeStructBody // # interfaceDeclStmt
;
event: EVENT IDENTIFIER L_PAREN paramList R_PAREN;
emit
: EMIT IDENTIFIER L_PAREN expressionList R_PAREN // # emitStmt
;
// [@using(preapprovedAssets = <Bool>, assetsInContract = <Bool>)]
annotation
: ATUSING L_PAREN expressionList R_PAREN
;
block
: L_CURLY (statement)* R_CURLY
| statement
;
statement:
simpleStmt
| ifStmt
| whileStmt
| forStmt
;
simpleStmt
: emptyStmt
| returnStmt
| varDecl
| expression
| emit
;
emptyStmt: eos;
returnStmt: RETURN expressionList;
ifStmt: IF L_PAREN expression R_PAREN block elseStmt?;
elseStmt: ELSE (block | ifStmt);
whileStmt
: WHILE L_PAREN expression? R_PAREN block
;
forStmt: FOR L_PAREN LET MUT varName ASSIGN expression SEMI expression SEMI expression R_PAREN block;
eos: EOS;