-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vyper.bnf
421 lines (363 loc) · 15.8 KB
/
Vyper.bnf
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
{
parserClass="org.vyperlang.plugin.parser.BaseVyperParser"
extends="com.intellij.extapi.psi.ASTWrapperPsiElement"
implements="org.vyperlang.plugin.psi.VyperElement"
psiClassPrefix="Vyper"
psiImplClassSuffix="Impl"
psiPackage="org.vyperlang.plugin.psi"
psiImplPackage="org.vyperlang.plugin.psi.impl"
elementTypeHolderClass="org.vyperlang.plugin.psi.VyperTypes"
elementTypeClass="org.vyperlang.plugin.psi.VyperElementType"
tokenTypeClass="org.vyperlang.plugin.psi.VyperTokenType"
parserUtilClass = "org.vyperlang.plugin.parser.ParserUtil"
tokens = [
DECORATOR = '@'
comment='regexp:(#.*)'
SEMICOLON=';'
COMMA=','
DOT='.'
COLON=':'
LBRACKET='['
RBRACKET=']'
LBRACE='{'
RBRACE='}'
LPAREN='('
RPAREN=')'
ELLIPSIS='...'
PLUS='+'
MINUS='-'
MULT='*'
DIV='/'
EXPONENT='**'
NOT='not'
ASSIGN='='
TO='=>'
EQ='=='
NEQ='!='
PLUS_ASSIGN='+='
MINUS_ASSIGN='-='
MULT_ASSIGN='*='
DIV_ASSIGN='/='
PERCENT_ASSIGN='%='
LESS='<'
LESSEQ='<='
MORE='>'
MOREEQ='>='
AND='and'
OR='or'
QUESTION='?'
PERCENT='%'
TILDE='~'
LSHIFT='<<'
RSHIFT='>>'
AMPERSAND='&'
PIPE='|'
CARET='^'
hexLiteral='regexp:hex"([0-9a-fA-F]+)"'
stringLiteralDouble='regexp:("([^"\r\n\\]|\\.)*")'
stringLiteralDoubleB='regexp:(b"([^"\r\n\\]|\\.)*")'
stringLiteralSingle="regexp:('([^'\r\n\\]|\\.)*')"
stringLiteralSingleB="regexp:(b'([^'\r\n\\]|\\.)*')"
MultiLineStringToken = 'regexp:("""([^"]|"[^"]|""[^"])*""")'
decimalNumber='regexp:([0-9][_0-9]*)'
fixedNumber='regexp:(([0-9][_0-9]*)+\.[_0-9]*|([0-9][_0-9]*)*\.([0-9][_0-9]*))'
booleanLiteral='regexp:True|False'
scientificNumber='regexp:((([0-9][_0-9]*)+|([0-9][_0-9]*)+\.[_0-9]*|([0-9][_0-9]*|[0-9])*\.[_0-9]+)[Ee][+-]?[_0-9]+)'
hexNumber='regexp:(0[xX][_0-9a-fA-F]+)'
// keywords
// def='def'
// if='if'
// elif='elif'
// else='else'
// for='for'
// in='in'
// return='return'
// raise='raise'
// pass='pass'
// assert='assert'
// log='log'
// range='range'
// enum='enum'
// flag='flag'
// struct='struct'
// interface='interface'
// event='event'
// implements='implements'
// external='external'
// internal='internal'
// deploy='deploy'
// pure='pure'
// view='view'
// nonpayable='nonpayable'
// payable='payable'
// self='self'
// indexed='indexed'
BOUND='bound'
intM = 'regexp:(int[0-9]+)'
uintM = 'regexp:(uint[0-9]+)'
bytesM = 'regexp:(bytes[0-9]+)'
string = 'String'
bytes = 'Bytes'
DynArray = 'DynArray'
HashMap = 'HashMap'
newline = 'regexp:(\r?\n)'
BREAK_LINE = 'regexp:(\s*\\\s*\n)'
Identifier='regexp:([A-Za-z_]\w*)'
]
//to handle left recursion
extends(".*Expression") = Expression
}
VyperFile ::= ((!<<eof>> MultiLineString? SourceUnit) (&INDEQ SourceUnit)*)?
external INDEQ ::= indEq
external INDLT ::= indLt
external INDGT ::= indGt
external INDNONE ::= indNone
// Order is handled by `VyperDeclarationsOrderAnnotator`
private SourceUnit ::= ( ImportDirective
| ConstantDefinitionExpression
| ImmutableDefinitionExpression
| ImplementsDirective
| StructDeclaration
| InterfaceDeclaration
| EventDeclaration
| FlagDeclaration
| FunctionDefinition
| StateVariableDeclaration)
{ recoverWhile = recoverIndent }
// constant type and immutable type are the same
private ConstantType ::= (&INDNONE constant &INDNONE '(' TYPE ')') | (public &INDNONE '(' &INDNONE ConstantType &INDNONE ')')
private ImmutableType ::= (&INDNONE immutable &INDNONE '(' TYPE ')') | (public &INDNONE '(' &INDNONE ImmutableType &INDNONE ')')
// constant type requires setting a value (semantic)
ConstantDefinitionExpression ::= Identifier &INDNONE ':' ConstantType &INDNONE '=' &INDNONE Expression {
pin=5
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
// immutable type requires setting a value in constructor
ImmutableDefinitionExpression ::= Identifier &INDNONE ':' ImmutableType {
pin=5
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
ImportDirective ::= ( import &INDNONE Identifier )
| ( import &INDNONE ImportPath &INDNONE as &INDNONE Identifier )
| ( from &INDNONE ImportPath &INDNONE import &INDNONE Identifier ) {
pin(".*")=1
mixin = "org.vyperlang.plugin.psi.VyperImportImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
ImportPath ::= Identifier (&INDNONE '.' &INDNONE Identifier)*
// implements statements
ImplementsDirective ::= implements &INDNONE ':' &INDNONE VarLiteral (&INDNONE ',' &INDNONE VarLiteral)* {
pin=1
}
// external interfaces ok
InterfaceDeclaration ::= interface &INDNONE Identifier &INDNONE ':'
<<indented (InterfaceFunction+) >> {
pin=1
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
InterfaceFunction ::= ( def &INDNONE Identifier FunctionArgs FunTypeAnnotation? &INDNONE ':' &INDNONE InterfaceFunctionModifier ) {
pin=1
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
InterfaceFunctionModifier ::= pure | view | nonpayable | payable
// Trailing commas: https://github.com/JetBrains/Grammar-Kit/blob/master/HOWTO.md#41-trailing-commas
FunctionArgs ::= '(' (ParamDef (',' (ParamDef | &')'))*)? ')' {pin(".*")=1}
ParamDef ::= Identifier ':' TYPE ('=' Expression)? {
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
// could be default assignment
StructDeclaration ::= struct &INDNONE Identifier &INDNONE COLON StructBody {
pin = 1
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
private StructBody ::= StructSingleDef | <<indented (StructMultipleDef) >>
private StructSingleDef ::= (&INDNONE StructLocalVariableDefinition)
private StructMultipleDef ::= StructLocalVariableDefinition ( &INDEQ StructLocalVariableDefinition )*
private StructLocalVariableDefinition ::= LocalVariableDefinition | Identifier | StringLiteral {recoverWhile=recoverIndent}
private recoverIndent ::= !(&INDEQ | &INDLT |<<eof>>)
FlagDeclaration ::= (enum|flag) &INDNONE Identifier &INDNONE COLON (<<indented FlagOptions >>) {
pin = 1
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
private FlagOptions ::= FlagOption ( &INDEQ FlagOption )*
FlagOption ::= Identifier {
recoverWhile=recoverIndent
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
//ok
//Events must all come before global declarations and function definitions
EventDeclaration ::= event &INDNONE Identifier &INDNONE ':' ((&INDGT EventProperty?) | (&INDNONE PassStatement)) {
pin=5
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
EventProperty ::= Identifier ':' (IndexedData | TYPE) (Identifier ':' (IndexedData |TYPE))*
IndexedData ::= indexed '(' TYPE ')'
// VARIABLES
// state variables May not assign value whilst defining type
StateVariableType ::= TYPE | public &INDNONE '(' &INDNONE StateVariableType &INDNONE ')'
StateVariableDeclaration ::= Identifier &INDNONE ':' StateVariableType (&INDNONE '=' &INDNONE Expression)?
{
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
// semantic : HashMap may only be state variable
LocalVariableDefinition ::= Identifier &INDNONE ':' &INDNONE TYPE ((&INDNONE '=') (&INDNONE Expression))?
{
mixin = "org.vyperlang.plugin.psi.VyperNamedElementImpl"
implements = "org.vyperlang.plugin.psi.VyperNamedElement"
}
MultiLineString ::= MultiLineStringToken
// FUNCTIONS
private FunDef ::= def (&INDNONE Identifier) FunctionArgs FunTypeAnnotation? (&INDNONE ':')
{
pin = 1
}
FunctionImplementation ::= (&INDNONE Statement) | <<indented ((MultiLineString)? &INDEQ Statement (&INDEQ Statement)*) >>
FunctionBody ::= (ELLIPSIS|FunctionImplementation) // ELLIPSIS is used for vyi files
FunTypeAnnotation ::= &INDNONE '->' &INDNONE ( TYPE | '(' TYPE (',' TYPE)* ')' )
FunctionDefinition ::= FunctionModifier (&INDEQ FunctionModifier)* FunDef FunctionBody
{
pin=1
implements = "org.vyperlang.plugin.psi.VyperFunctionDefElement"
mixin = "org.vyperlang.plugin.psi.VyperFunctionDefMixin"
}
private FunctionVisibility ::= external | internal | deploy
private FunctionMutability ::= pure | view | nonpayable | payable
FunctionDecorator ::= FunctionVisibility | FunctionMutability | (nonreentrant ((&INDNONE '(') FunctionEntrancyKey ')')?)
FunctionModifier ::= (DECORATOR (&INDNONE FunctionDecorator)) { pin = 1 }
FunctionEntrancyKey ::= stringLiteralSingle | stringLiteralDouble
// statements
Statement ::= PassStatement
| IfStatement
| ForStatement
| Continue
| Break
| Return
| Raise
| SimpleStatement
| MultiLineString // docstring
{recoverWhile = recoverStatement}
private recoverStatement ::= !(&INDEQ|&INDLT)
private CondStmt ::= Expression &INDNONE ':' ((&INDNONE Statement)
| <<indented (Statement (&INDEQ Statement)*)>>)
private ElifStmt ::= elif &INDNONE CondStmt
{pin = 1}
private ElseStmt ::= else &INDNONE ':' ((&INDNONE Statement)
| <<indented (Statement (&INDEQ Statement)*)>>)
{pin = 1}
IfStatement ::= if &INDNONE CondStmt (&INDEQ ElifStmt)* (&INDEQ ElseStmt)? {pin=1}
private SimpleStatement ::= LocalVariableDefinition | ExpressionStatement
private ExpressionStatement ::= Expression
ForStatement ::= for &INDNONE Identifier (&INDNONE ':' TYPE)? &INDNONE in &INDNONE Expression &INDNONE ':'
(&INDNONE Statement | <<indented (Statement (&INDEQ Statement)*)>>) {
pin=1
}
private Continue ::= continue
private Break ::= break
private Return ::= return (&INDNONE Expression (',' Expression)*)? {
pin=1
}
private Raise ::= raise StringLiteral?
private PassStatement ::= pass
Expression ::=
TernaryExpression
| AssignmentExpression
| TupleAssignmentExpression
| RangeExpression
| IndexAccessExpression
| UnaryExpression
| CallExpression
| StructExpression
| AssertExpression
| EqExpression
| OrExpression
| AndExpression
| CompExpression
| PlusMinExpression
| MultDivExpression
| ExponentExpression
| BinExpression
| MemberAccessExpression
| InlineArrayExpression
| EventLogExpression
| ExtCallExpression
| StaticCallExpression
| InExpression
| PrimaryExpression
| ParenthesizedExpression
EventLogExpression ::= log (&INDNONE Identifier '.')? &INDNONE VarLiteral FunctionCallArguments
InExpression ::= Expression (&INDNONE not)? &INDNONE in &INDNONE Expression
TernaryExpression ::= Expression &INDNONE if &INDNONE Expression &INDNONE else &INDNONE Expression { pin = 1 }
ExtCallExpression ::= extcall Expression
StaticCallExpression ::= staticcall Expression
RangeExpression ::= range &INDNONE '(' Expression (',' Expression)? RangeBound? ')'
RangeBound ::= ',' BOUND '=' (VarLiteral | NumberLiteral)
StructExpressionMember ::= VarLiteral &INDNONE ':' &INDNONE Expression
private StructExpressionMembers ::= '{' StructExpressionMember (',' (StructExpressionMember | &'}'))* '}' {pin(".*")=1}
StructExpression ::= VarLiteral (&INDNONE'(') StructExpressionMembers ')'
AssignmentExpression ::=
Expression (&INDNONE ('=' | '+=' | '-=' | '*=' | '/=' | '%='))
Expression { rightAssociative=true }
TupleAssignmentExpression ::= VarLiteral (',' VarLiteral)+ &INDNONE '=' Expression
CallExpression ::= Expression &INDNONE FunctionCallArguments
MemberAccessExpression ::= Expression &INDNONE '.' &INDNONE VarLiteral {
name = "member access"
}
IndexAccessExpression ::= Expression &INDNONE '[' Expression ']'
ParenthesizedExpression ::= '(' Expression ')' {pin = 1}
UnaryExpression ::= (not | '~' | '+' | '-') &INDNONE Expression {
rightAssociative=true
pin = 1
}
InlineArrayExpression ::= '[' Expression (',' (Expression | &']'))* ']' {pin(".*")=1}
AndExpression ::= Expression AND Expression
OrExpression ::= Expression OR Expression
EqExpression ::= Expression ('==' | '!=') Expression
CompExpression ::= Expression ('<' | '>' | '<=' | '>=') Expression
PlusMinExpression ::= Expression ('+' | '-') Expression
MultDivExpression ::= Expression ('*' | '/' | '%') Expression
ExponentExpression ::= Expression '**' Expression
BinExpression ::= Expression (PIPE | AMPERSAND | RSHIFT | LSHIFT | CARET) Expression
AssertExpression ::= assert &INDNONE Expression (&INDNONE AssertMessage)? {
pin = 1
name = "assert expression"
}
private AssertMessage ::= ',' ( stringLiteralDouble | stringLiteralSingle | MultiLineString )
FunctionCallArgument ::= Expression | ( VarLiteral '=' Expression )
FunctionCallArguments ::= '(' (FunctionCallArgument (',' (FunctionCallArgument | &')'))*)? ')' {pin(".*")=1}
PrimaryExpression ::= VarLiteral | BooleanLiteral | NumberLiteral | HexLiteral | StringLiteral | self
| TYPE // type is accepted as parameter in some built-in functions
VarLiteral ::= Identifier {
implements = "org.vyperlang.plugin.psi.VyperReferenceElement"
mixin = "org.vyperlang.plugin.psi.VyperVarLiteralMixin"
name = "variable"
}
// TODO #29: BuiltIn ::= 'bitwise_and' | 'bitwise_not' | 'bitwise_or' | 'bitwise_xor' | 'shift' | 'create_minimal_proxy_to' | 'create_copy_of' | 'create_from_blueprint' | 'raw_call' | 'raw_log' | 'raw_revert' | 'selfdestruct' | 'send' | 'ecadd' | 'ecmul' | 'ecrecover' | 'keccak256' | 'sha256' | 'concat' | 'convert' | 'uint2str' | 'extract32' | 'slice' | 'abs' | 'ceil' | 'epsilon' | 'floor' | 'max' | 'max_value' | 'min' | 'min_value' | 'pow_mod256' | 'sqrt' | 'isqrt' | 'uint256_addmod' | 'uint256_mulmod' | 'unsafe_add' | 'unsafe_sub' | 'unsafe_mul' | 'unsafe_div' | 'as_wei_value' | 'blockhash' | 'empty' | 'len' | 'method_id' | '_abi_encode' | '_abi_decode' | 'abi_encode' | 'abi_decode' | 'print'
// TYPES seems okay
TYPE ::= ListType | MapType | ValueType | StructType
ValueType ::= intM | uintM | bytesM | address | bool | ((string | bytes) &INDNONE '[' Expression ']')
ListType ::= ( ValueType | StructType ) &INDNONE '[' Expression ']' (&INDNONE '[' Expression ']')*
StructType ::= Identifier
{
implements = "org.vyperlang.plugin.psi.VyperReferenceElement"
mixin = "org.vyperlang.plugin.psi.VyperVarLiteralMixin"
}
private DynArrayType ::= DynArray &INDNONE '[' TYPE ',' Expression ']'
private HashMapType ::= HashMap &INDNONE '[' Expression ',' TYPE ']'
MapType ::= DynArrayType | HashMapType { pin=1 }
private BooleanLiteral ::= booleanLiteral
private NumberLiteral ::= HexNumber | DecimalNumber | FixedNumber
private StringLiteral ::= stringLiteralDouble | stringLiteralDoubleB | stringLiteralSingle | stringLiteralSingleB | MultiLineString
private DecimalNumber ::= decimalNumber
private HexNumber ::= hexNumber
private HexLiteral ::= hexLiteral
private FixedNumber ::=fixedNumber