forked from shvetsgroup/squirrel-lang-idea-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquirrel.bnf
363 lines (313 loc) · 12.9 KB
/
Squirrel.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
{
parserClass="com.sqide.SquirrelParser"
parserUtilClass="com.sqide.parser.SquirrelParserUtil"
extends="com.intellij.extapi.psi.ASTWrapperPsiElement"
psiClassPrefix="Squirrel"
psiImplClassSuffix="Impl"
psiPackage="com.sqide.psi"
psiImplPackage="com.sqide.psi.impl"
psiImplUtilClass="com.sqide.psi.impl.SquirrelPsiImplUtil"
elementTypeHolderClass="com.sqide.SquirrelTokenTypes"
elementTypeClass="com.sqide.psi.SquirrelElementType"
tokenTypeClass="com.sqide.psi.SquirrelTokenType"
tokens = [
INCLUDE_DIRECTIVE = '@include'
SEMICOLON_SYNTHETIC = '<NL>'
RBRACE = '}'
RBRACKET = ']'
RPAREN = ')'
PLUS_PLUS = '++'
MINUS_MINUS = '--'
LBRACE = '{'
LBRACKET = '['
LPAREN = '('
DOUBLE_COLON = '::'
COLON = ':'
SEMICOLON = ';'
COMMA = ','
MULTI_ARGS = '...'
CLASS_ATTR_START = '</'
CLASS_ATTR_END = '/>'
SHIFT_LEFT = '<<'
SHIFT_RIGHT = '>>'
UNSIGNED_SHIFT_RIGHT = '>>>'
CMP = '<=>'
EQ_EQ = '=='
NOT_EQ = '!='
LESS_OR_EQ = '<='
GREATER_OR_EQ = '>='
SEND_CHANNEL = '<-'
PLUS_EQ = '+='
MINUS_EQ = '-='
MUL_EQ = '*='
DIV_EQ = '/='
REMAINDER_EQ = '%='
OR_OR = '||'
AND_AND = '&&'
EQ = '='
NOT = '!'
BIT_NOT = '~'
BIT_OR = '|'
BIT_XOR = '^'
BIT_AND = '&'
LESS = '<'
GREATER = '>'
PLUS = '+'
MINUS = '-'
MUL = '*'
DIV = '/'
REMAINDER = '%'
QUESTION = '?'
AT = '@'
DOT = '.'
SINGLE_LINE_COMMENT = "regexp:(//|#)[^\r\n]*"
MULTI_LINE_COMMENT = "regexp:/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*(\*+/)"
IDENTIFIER = "regexp:[a-zA-Z_]+[a-zA-Z_0-9]*"
INT = "regexp:((0[1-9][0-7]*)|(0x[0-9a-fA-F]*)|('\p{Alpha}')|(0|([1-9][0-9]*)))"
FLOAT = "regexp:((([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?)|([0-9]+([eE][+-]?[0-9]+))"
STRING = "regexp:(@\"([^\"]|\"\")*\"|\"(\\.|[^\"\n\r])*\")"
NL="NL"
WS="WS"
NL="regexp:[\r\n]|\r\n"
WS="regexp:[ \t\f]"
]
implements("(ClassDeclaration|ClassExpression)")="com.sqide.psi.SquirrelClass"
implements("(FunctionDeclaration|ConstructorDeclaration|MethodDeclaration|FunctionExpression)")="com.sqide.psi.SquirrelFunction"
extends(".*Expression")=Expression
extends(".*(Statement|Declaration|Block)")=Statement
name(".*Expression")="expression"
}
// ---------
// PREVIEW MODE GOTCHAS
// ---------
// The optional semicolon logic is placed inside lexer. Therefore in order to test stuff in preview mode, you should
// keep in mind that:
//
// 1. In the preview mode, you should use <NL> instead of new lines in cases where you want the new line to work as
// semicolon. This is a fix for a live preview feature where it can't distinguish between whitespace line breaks and the
// useful ones.
//
// 2. Also, you should put semicolons after } in places where you expect the virtual semicolon to be placed.
//// TEST DATA:
//
//i = 2;
//i = 02;
//i = 0x222;
//i = 'c';
//f = 0.52;
//f = 1.e2;
//f = 1.e-2;
//s = "I'm a string"<NL>
//s = @"I'm a string verbatim string"<NL>
//s = @"I'm a multiline
//string verbatim string"<NL>
//b = true;
//b = false;
//n = null;
//exp = a ? i + 2 : b > -3 + +23 * i++ / 34 % ~ 3
//nc = i++ + ++i - --i - i-- + +i - -i - -(-i) + -i-- + errors_on_right + ---i + --(-i) + --i--i
//
//const a=-2;
//const b=2;
//enum c {};
//enum c {a,b,c=4, h="23", h=23, h=-2};
//local o, a=2, b=23, c = (1,2), d = function(a,b=2,c=2){}, e = @(a,b,...) a + (b, f = 2), g=[123, function(){}, @()(1+2)], c = class {};
//function a(a, b, a=2, a=2) {};
//a(a, 2, g[1]);
//class Test.B extends Test {
// constructor (a) {
// }
// a = function (asd){
// }
// function sds() {
// }
// g = "234"
//}
SquirrelFile ::= !<<eof>> Statements
private semi ::= SEMICOLON_SYNTHETIC | ';' {name="semicolon"}
private multisemi ::= semi + {name="semicolon"}
private delimiter ::= multisemi | &'}' | <<eof>> | <<prevIsBrace>>
private Statements ::= multisemi? StatementWithSemi*
private StatementWithSemi ::= Statement delimiter { pin=1 recoverWhile=statement_recover }
private statement_recover ::= !(<<eof>> | delimiter | '{' | '}' | const | enum | local | function | class | return | break | continue | yield | throw | for | foreach | while | do | if | else | switch | case | default | try | catch | '@include')
Statement ::=
IncludeDeclaration
| Block
| ExpressionStatement
| ConstDeclaration
| EnumDeclaration
| LocalDeclaration
| FunctionDeclaration
| ClassDeclaration
| ForStatement
| ForeachStatement
| WhileStatement
| DoWhileStatement
| IfStatement
| SwitchStatement
| TryStatement
| ReturnStatement
| BreakStatement
| ContinueStatement
| YieldStatement
| ThrowStatement
private StatementOrEmpty ::= Statement | ';'
Block ::= '{' Statements? '}' {pin=1}
private OnceDirective ::= once
private IncludeDirective ::= INCLUDE_DIRECTIVE [OnceDirective]
IncludeDeclaration ::= IncludeDirective STRING {pin=1 name="include" methods=[getReference]}
ConstDeclaration ::= const Id '=' Literal {pin=1}
EnumDeclaration ::= enum Id '{' EnumItemDeclarations? '}' {pin=1}
private EnumItemDeclarations ::= EnumItem (',' EnumItem)* ','?
EnumItem ::= Id [ '=' Literal ]
LocalDeclaration ::= local VariableOrFunctionDeclarations {pin=1}
private VariableOrFunctionDeclarations ::= VarDeclarationList | FunctionDeclaration { name="variable or function declarations" }
VarDeclarationList ::= VarItem (',' VarItem)*
VarItem ::= Id VarInit?
VarInit ::= '=' ExpressionWithNoComma
FunctionDeclaration ::= function FunctionName Parameters FunctionBody {pin=1}
FunctionName ::= Id ('::' (Id | constructor))*
FunctionBody ::= StatementOrEmpty
Parameters ::= '(' ParameterList? ')'
ParameterList ::=
Parameter (',' Parameter)* ((',' DefaultParameter)+ | (',' VariableLengthParameter))?
| DefaultParameter (',' DefaultParameter)*
| VariableLengthParameter?
Parameter ::= Id (!'=')
DefaultParameter ::= Id '=' ExpressionWithNoComma
VariableLengthParameter ::= '...'
ClassDeclaration ::= class ClassName [extends ClassName] ClassBody {pin=1}
ClassName ::= Id ('.' Id)*
ClassBody ::= '{' ClassMembers? '}'
private ClassMembers ::= ClassMember (semi* ClassMember)* semi?
ClassMember ::= ClassAttribute? static? (ConstructorDeclaration | RegularClassMember)
private RegularClassMember ::= KeyValuePair | MethodDeclaration
ConstructorDeclaration ::= constructor Parameters FunctionBody
MethodDeclaration ::= function FunctionName Parameters FunctionBody
ClassAttribute ::= '</' KeyValuePairs? '/>'
ReturnStatement ::= return Expression? {pin=1}
BreakStatement ::= break
ContinueStatement ::= continue
YieldStatement ::= yield Expression {pin=1}
ThrowStatement ::= throw Expression {pin=1}
ForStatement ::= for '(' ForLoopParts ')' StatementOrEmpty {pin=1}
ForLoopParts::= [LocalDeclaration | Expression] ';' Expression? ';' Expression?
ForeachStatement ::= foreach '(' [Id ','] Id in Expression ')' StatementOrEmpty {pin=1}
WhileStatement ::= while '(' ExpressionWithRecoverUntilParen ')' StatementOrEmpty {pin=1}
DoWhileStatement ::= do StatementOrEmpty while '(' ExpressionWithRecoverUntilParen ')' {pin=1}
IfStatement ::= if '(' ExpressionWithRecoverUntilParen ')' StatementOrEmpty [semi? else StatementOrEmpty] {pin=1}
SwitchStatement ::= switch '(' ExpressionWithRecoverUntilParen ')' '{' SwitchCase* DefaultCase? '}' {pin=1}
SwitchCase ::= case Expression ':' Statements? {pin=1}
DefaultCase::= default ':' Statements? {pin=2}
TryStatement ::= try StatementOrEmpty CatchPart {pin=1}
CatchPart ::= catch '(' Id ')' StatementOrEmpty {pin=1}
private ExpressionWithRecoverUntilParen ::= Expression {recoverWhile=parenthesesRecovery}
private parenthesesRecovery ::= !')'
ExpressionStatement ::= Expression;
Expression ::=
CommaExpression
| AssignExpression
| TernaryExpression
| LogicOrExpression
| second_expr_group
| BitwiseOrExpression
| BitwiseXorExpression
| BitwiseAndExpression
| CompareExpression
| RelationalExpression
| ShiftExpression
| AdditiveExpression
| MultiplicativeExpression
| first_expr_group
| c_expr_group
| b_expr_group
| a_expr_group
private ExpressionWithNoComma ::=
AssignExpression
| TernaryExpression
| LogicOrExpression
| second_expr_group
| BitwiseOrExpression
| BitwiseXorExpression
| BitwiseAndExpression
| CompareExpression
| RelationalExpression
| ShiftExpression
| AdditiveExpression
| MultiplicativeExpression
| first_expr_group
| c_expr_group
| b_expr_group
| a_expr_group {name="expression"}
private a_expr_group ::= (SimpleRefExpression | LiteralExpression | JsonExpression | ParenthesizedExpression)
private b_expr_group ::= (ArrayItemExpression | ReferenceExpression | CallExpression)
private c_expr_group ::= (ClassExpression | TableExpression | ArrayExpression | FunctionExpression | LambdaFunctionExpression )
private first_expr_group ::= (UnaryExpression | PrefixExpression | InstanceOfExpression )
private second_expr_group ::= (LogicAndExpression | InExpression)
MultiplicativeOperator ::= '*' | '/' | '%'
AdditiveOperator ::= '+' | '-'
ShiftOperator ::= '<<' | '>>>' | '>>'
RelationalOperator ::= '<' | '<=' | '>' | '>='
EqualityOperator ::= '==' | '!=' | '<=>'
AssignmentOperator ::= '=' | '<-' | '*=' | '/=' | '%=' | '+=' | '-='
UnaryOperator ::= '-' | '!' | '~'
UnaryAction ::= typeof | clone | delete | resume
PrefixOperator ::= '++' | '--'
CommaExpression ::= Expression ',' Expression
AssignExpression ::= Expression AssignmentOperator Expression { rightAssociative=true }
TernaryExpression ::= Expression '?' Expression ':' Expression
LogicOrExpression ::= Expression '||' Expression
LogicAndExpression ::= Expression '&&' Expression
InExpression ::= Expression in Expression
BitwiseOrExpression ::= Expression '|' Expression
BitwiseXorExpression ::= Expression '^' Expression
BitwiseAndExpression ::= Expression '&' Expression
CompareExpression ::= Expression EqualityOperator Expression
RelationalExpression ::= Expression RelationalOperator Expression
ShiftExpression ::= Expression ShiftOperator Expression
AdditiveExpression ::= Expression AdditiveOperator Expression
MultiplicativeExpression ::= Expression MultiplicativeOperator Expression
UnaryExpression ::= (UnaryOperator | UnaryAction) Expression
PrefixExpression ::= PrefixOperator NamedElementExpression
InstanceOfExpression ::= Expression instanceof Expression
ParenthesizedExpression ::= '(' Expression ')'
ClassExpression ::= class [extends ClassName] ClassBody
FunctionExpression ::= function Parameters FunctionBody
LambdaFunctionExpression ::= '@' Parameters ExpressionWithNoComma
CallExpression ::= Expression Arguments
Arguments ::= '(' ArgumentList? ')'
ArgumentList ::= ArgumentListPart (',' ArgumentListPart)* {recoverWhile="argument_list_recover"}
private argument_list_recover ::= !(')')
private ArgumentListPart ::= ExpressionWithNoComma {recoverWhile="argument_list_part_recover"}
private argument_list_part_recover ::= !(')' | ',')
ArrayExpression ::= '[' ArrayElementsList? ']'
private ArrayElementsList ::= ArrayListPart (',' ArrayListPart)* ','? {recoverWhile="array_list_recover"}
private array_list_recover ::= !(']')
private ArrayListPart ::= ExpressionWithNoComma {recoverWhile="array_list_part_recover"}
private array_list_part_recover ::= !(']' | ',')
ArrayItemExpression ::= Expression '[' ExpressionWithNoComma ']' PrefixOperator?
TableExpression ::= '{' KeyValuePairs? '}'
private TableItemSeparator ::= ',' | '<NL>'
private KeyValuePairs ::= TableItem (TableItemSeparator* TableItem)* TableItemSeparator?
TableItem ::= KeyValuePair | FunctionDeclaration
private KeyValuePair ::= Key '=' ExpressionWithNoComma
Key ::= Id | '[' Expression ']'
private NamedElementExpression ::= SimpleRefExpression | ReferenceExpression | ArrayItemExpression
ReferenceExpression ::= Expression '.' (Id | constructor)
SimpleRefExpression ::= Id PrefixOperator? {elementType=ReferenceExpression}
LiteralExpression ::= Literal
JsonExpression ::= Json
Literal ::=
'-'? INT
| '-'? FLOAT
| StringLiteral
| true | false
| null
StringLiteral ::= STRING {methods =[getReference]}
Json ::= JsonObject | JsonArray
JsonArray ::= '[' [!']' JsonValue (!']' ',' JsonValue) *] ']' {pin(".*")=1}
JsonObject ::= '{' [!'}' JsonProp (!'}' ',' JsonProp) *] '}' {pin(".*")=1}
JsonProp ::= STRING ':' JsonValue {pin=1 recoverWhile=JsonRecover}
JsonValue ::= Literal | Expression | Json
private JsonRecover ::= !(',' | ']' | '}' | '[' | '{')
Id ::= '::'? IDENTIFIER { name="identifier" methods=[getReference]}