File: syntax.bnf

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (483 lines) | stat: -rw-r--r-- 13,907 bytes parent folder | download | duplicates (3)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Implementation of the grammar for Java, complete with highlighting annotations.
// Source: https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html

delimiter = SDelimiter;

void SDelimiter();
SDelimiter : "[ \n\r\t]*" - (SCommentStart #comment - SDelimiter)?;

void SCommentStart();
SCommentStart : "//[^\n\r]*[\n\r]+";
SCommentStart : "/" - "\*" - [SInComment]@;

// Multiline comments.
void SInComment();
SInComment : "\*+/"; // End of the comment.
SInComment : "/\*+" - SInComment - SInComment; // Start of a nested comment.
SInComment : "[^\*/]*" - SInComment; // Everything except * and / are fine.
SInComment : "\*+[^\*/]" - SInComment;
SInComment : "/+" - SInComment;


// Identifiers.
void SIdent();
SIdent : "[A-Za-z_$][A-Za-z0-9_$]*";

// Qualified identifier.
void SQIdent();
SQIdent : SIdent #typeName - (, "\.", SIdent #typeName)*;

void SQIdentList();
SQIdentList : SQIdent - (, ",", SQIdent)*;


// Start of the grammar.
void SRoot();
SRoot : SDelimiter - SPkgName, SImportDecl, STypeDecl - SDelimiter;

void SPkgName();
SPkgName : (SAnnotation, )* "package" #keyword, SQIdent #typeName, ";";

void SImportDecl();
SImportDecl[-1] :;
SImportDecl : "import" #keyword, ("static" #keyword,)? SQIdent, SImportEnd, ";", SImportDecl;

void SImportEnd();
SImportEnd : ("\.\*")?;

void STypeDecl();
STypeDecl : SModifiers, SClassDecl;
STypeDecl : SModifiers, SEnumDecl;
STypeDecl : SModifiers, SInterfaceDecl;
STypeDecl : SModifiers, SAnnotationDecl;

void SClassDecl();
SClassDecl : "class" #keyword, SIdent #typeName, STypeParams, SExtends, SImplements, SClassBody;

void SEnumDecl();
SEnumDecl : "enum" #keyword, SIdent #typeName, SImplements, SEnumBody;

void SInterfaceDecl();
SInterfaceDecl : "interface" #keyword, SIdent #typeName, STypeParams, SExtendsList, SInterfaceBody;

void SAnnotationDecl();
SAnnotationDecl : "@" #keyword, "interface" #keyword, SIdent #typeName, SAnnotationBody;

void SExtends();
SExtends : "extends" #keyword, SType;
SExtends :;

void SExtendsList();
SExtendsList : "extends" #keyword, STypeList;
SExtendsList :;

void SImplements();
SImplements : "implements" #keyword, STypeList;
SImplements :;


// Types.
void SType();
SType : SBasicType - (, "\[\]")*;
SType : SRefType;

void SBasicType();
SBasicType : "byte" #typeName;
SBasicType : "short" #typeName;
SBasicType : "char" #typeName;
SBasicType : "int" #typeName;
SBasicType : "long" #typeName;
SBasicType : "float" #typeName;
SBasicType : "double" #typeName;
SBasicType : "boolean" #typeName;

void STypeVoid();
STypeVoid : "void" #keyword;
STypeVoid : SType;

void SRefType();
// Note: The official documentation does not allow SArrayTokens here. We do, since the official
// Java compiler seems to allow new Foo<Bar[]>();
SRefType : SIdent #typeName, STypeArgs - (, "\.", SIdent #typeName, STypeArgs)*, SArrayTokens;

void STypeList();
STypeList : SRefType - (, ",", SRefType)*;

void STypeActuals();
STypeActuals : ;
STypeActuals : "<", ">";
STypeActuals : "<", STypeList, ">";

void STypeArgs();
STypeArgs :;
STypeArgs : "<", STypeArg - (, ",", STypeArg)*, ">";

void STypeArg();
STypeArg : SRefType;
STypeArg : "\?";
STypeArg : "\?", "extends", SRefType;
STypeArg : "\?", "super", SRefType;

// Type parameters.
void STypeParams();
STypeParams :;
STypeParams : "<", STypeParam - (, ",", STypeParam)*, ">";

void STypeParam();
STypeParam : SIdent, ("extends", SBound)?;

void SBound();
SBound : SRefType - (, "&", SRefType)*;

// Modifiers.
void SModifiers();
SModifiers : (SModifier, )*;

void SModifier();
SModifier : SAnnotation;
SModifier : "public" #keyword;
SModifier : "protected" #keyword;
SModifier : "private" #keyword;
SModifier : "static" #keyword;
SModifier : "abstract" #keyword;
SModifier : "final" #keyword;
SModifier : "native" #keyword;
SModifier : "synchronized" #keyword;
SModifier : "transient" #keyword;
SModifier : "volatile" #keyword;
SModifier : "strictfp" #keyword;

// Annotations.
void SAnnotation();
SAnnotation : "@" #keyword, SQIdent #keyword - (, SAnnotationParams)?;

void SAnnotationParams();
SAnnotationParams : "(", ")";
SAnnotationParams : "(", SAnnotationElement, ")";

void SAnnotationElement();
SAnnotationElement : SElementValue;
SAnnotationElement : SElementValuePair - (, ",", SElementValuePair)*;

void SElementValuePair();
SElementValuePair : SIdent #varName, "=", SElementValue;

void SElementValue();
SElementValue : SAnnotation;
SElementValue : SExpr1;
SElementValue : "{", SElementArray, (",",)? "}";

void SElementArray();
SElementArray : SElementValue - (, ",", SElementValue)*;

// Expressions and blocks.
void SExpr();
SExpr : SExpr1 - (, SAssignOp, SExpr1)?;

void SAssignOp();
SAssignOp : "=";
SAssignOp : "\+=";
SAssignOp : "-=";
SAssignOp : "*=";
SAssignOp : "/=";
SAssignOp : "&=";
SAssignOp : "|=";
SAssignOp : "%=";
SAssignOp : "<<=";
SAssignOp : ">>=";
SAssignOp : ">>>=";

void SExpr1();
// Note: The original documentation allows an assignment between ? and :, which seems strange.
SExpr1 : SExpr2 - (, "?", SExpr1, ":", SExpr1)?;

void SExpr2();
SExpr2 : SExpr3 - (, SExpr2Rest)*;

void SExpr2Rest();
// Note: The Java grammar actually disallows 'x instanceof Foo && y'. We allow it here, since
// it seems to be allowed by the Java compiler.
SExpr2Rest : "instanceof" #keyword, SType;
SExpr2Rest : SInfixOp, SExpr3;

void SInfixOp();
SInfixOp : "||";
SInfixOp : "&&";
SInfixOp : "|";
SInfixOp : "^";
SInfixOp : "&";
SInfixOp : "==";
SInfixOp : "!=";
SInfixOp : ">";
SInfixOp : "<";
SInfixOp : "<=";
SInfixOp : ">=";
SInfixOp : "<<";
SInfixOp : ">>";
SInfixOp : ">>>";
SInfixOp : "\+";
SInfixOp : "-";
SInfixOp : "\*";
SInfixOp : "/";
SInfixOp : "%";

void SExpr3();
SExpr3 : SPrefixOp, SExpr3;
SExpr3 : SPrimary, SExpr3Tail;
// This also seems wrong in the documentation, the documentation allows (1)3 as an expression...
SExpr3 : "(", SType, ")", SExpr3;

void SExpr3Tail();
SExpr3Tail : ("\.", SSelector,)* SExpr3Tail2;

void SExpr3Tail2();
SExpr3Tail2 : (SPostfixOp,)*;

void SPrefixOp();
SPrefixOp : "\+\+";
SPrefixOp : "--";
SPrefixOp : "!";
SPrefixOp : "~";
SPrefixOp : "\+";
SPrefixOp : "-";

void SPostfixOp();
SPostfixOp : "\+\+";
SPostfixOp : "--";

void SPrimary();
SPrimary : "(", SExpr, ")";
SPrimary : "[0-9]+[Ll]?" #constant;
SPrimary : "0[Xx][0-9A-Fa-f]+[Ll]?" #constant;
SPrimary : SFloat #constant - ("[fFdD]" #constant)?;
SPrimary : "true" #constant;
SPrimary : "false" #constant;
SPrimary : "null" #constant;
SPrimary : "\"" #string - SStrContent #string - "\"" #string;
SPrimary : "'" #string - SCharContent #string - "'" #string;
SPrimary : "this" #keyword - (, SArgs)?;
SPrimary : "super" #keyword - (, SArgs)?;
SPrimary : "super" #keyword, "\.", SIdent #fnName - (, SArgs)?;
SPrimary[-1] : SIdent #varName - (, SIdentSuffix)*;
SPrimary : SBasicType #typeName, ("\[\]",)* "\.", "class" #keyword;
SPrimary : "void" #typeName, "\.", "class" #keyword;
SPrimary : "new[ \t]" #keyword, SCreator;
SPrimary : "<", STypeList, ">", "this" #keyword, SArgs;
SPrimary : "<", STypeList, ">", "super" #keyword, SSelector;
SPrimary : "<", STypeList, ">", SIdent #fnName, SArgs;

void SCreator();
SCreator : "<", STypeList, ">", SCreatedName, SCreatorRest;
SCreator : SCreatedName, SCreatorRest;
SCreator : SCreatedName, SArrayRest;

void SCreatedName();
SCreatedName : SIdent #typeName, STypeArgs - (, "\.", SIdent #typeName, STypeActuals)*;

void SCreatorRest();
SCreatorRest : SArgs - (, SClassBody)?;

void SArrayRest();
// TODO: Figure out how this should be implemented...
SArrayRest : "\[", SExpr, "\]" - (, "\[", SExpr, "\]")*, SArrayTokens;
SArrayRest : ("\[\]",)+ SArrayInit;

void SArrayTokens();
SArrayTokens : ;
SArrayTokens : "\[\]" - (, "\[\]")*;

void SFloat();
SFloat : SFloatBase - (SFloatExp)?;
SFloat : "[0-9]+" - SFloatExp;

void SFloatBase();
SFloatBase : "[0-9]+\.[0-9]*";
SFloatBase : "\.[0-9]+";

void SFloatExp();
SFloatExp : "e[+\-]?[0-9]+";

void SStrContent();
SStrContent : "[^\"\\\n]*";
SStrContent : SStrContent - "\\." - SStrContent;

void SCharContent();
SCharContent : "[^\\']";
SCharContent : "\\.";

void SIdentSuffix();
SIdentSuffix : ;
SIdentSuffix[-1] : "\.", SIdent #varName;
SIdentSuffix : ("\[\]", )* "\.", "class" #keyword;
SIdentSuffix : "\[", SExpr, "\]"; // Strange to have an expression in this context...
SIdentSuffix : SArgs;
SIdentSuffix : "\.", "this" #keyword;
SIdentSuffix : "\.", "super" #keyword, SArgs;
SIdentSuffix : "\.", "new" #keyword, STypeActuals, SIdent #typeName, STypeActuals, SArgs - (,SClassBody)?;
SIdentSuffix : "\.", STypeActuals, "super" #keyword, SSelector;
SIdentSuffix : "\.", STypeActuals, SIdent #fnName, SArgs;

void SArgs();
SArgs : "(", ")";
SArgs : "(", SExpr, (",", SExpr, )*, ")";

void SSelector();
SSelector : SIdent #fnName - (, SArgs)?;
SSelector : "this" #keyword;
SSelector[10] : "super" #keyword - (, SArgs)?;
SSelector[10] : "super" #keyword, "\.", SSelector;
SSelector : "new" #keyword, STypeActuals, SIdent #typeName, STypeActuals, SArgs - (,SClassBody)?;
SSelector : "<", STypeList, ">", SGenericSuffix;
SSelector : "\[", SExpr, "\]";

void SGenericSuffix();
SGenericSuffix : "super" #keyword, SSelector;
SGenericSuffix : SIdent #fnName, SArgs;

void SStmt();
SStmt : ";";
SStmt : SBlock;
SStmt : SIdent, ":", SStmt;
SStmt : SExpr, ";";
SStmt[10] : "assert[ \t]" #keyword, SExpr, (":", SExpr,)? ";";
SStmt[10] : "if" #keyword, "(", SExpr, ")" [, SStmt]? - (, SStmtElse)?;
SStmt[10] : "while" #keyword, "(", SExpr, ")" [, SStmt]?;
SStmt[10] : "do[ \t]" #keyword [, SStmt, ]? "while" #keyword, "(", SExpr, ")", ";";
SStmt[10] : "for" #keyword, "(", SForControl, ")" [, SStmt]?;
SStmt[10] : "break" #keyword - (, SIdent)?;
SStmt[10] : "continue" #keyword - (, SIdent)?;
SStmt[10] : "return[ \t]" #keyword, SExpr;
SStmt[10] : "throw[ \t]" #keyword, SExpr;
SStmt[10] : "synchronized", "(", SExpr, ")", SBlock;
SStmt[10] : "try" #keyword, SBlock, STryTail;
SStmt[10] : "try" #keyword, "(", SResourceSpec, (";",)? ")", SBlock, STryTail;
SStmt[10] : "switch" #keyword, "(", SExpr, ")", "{", (SSwitchStmt,)* "}";

void SSwitchStmt();
SSwitchStmt : SStmt;
SSwitchStmt : "case[ \t]" #keyword, SExpr, ":";
SSwitchStmt : "default" #keyword, ":";

void SStmtElse();
SStmtElse : "else" #keyword [, SStmt]?;

void STryTail();
STryTail : (SCatchClause, )* "finally" #keyword, SBlock;
STryTail : (SCatchClause, )+;

void SCatchClause();
SCatchClause : "catch" #keyword, "(", SVarModifier, SQIdent, ("|", SQIdent,)* SIdent #varName, ")", SBlock;

void SResourceSpec();
SResourceSpec : SVarModifier, SRefType, SIdent #varName, ("\[\]",)* "=", SExpr;
SResourceSpec : SResourceSpec, ";", SResourceSpec;

void SForControl();
SForControl : SForStmt, ";", SForStmt, ";", SForStmt;
SForControl : SVarModifier, SType, SIdent #varName, ("\[\]",)* SForControlRest;

void SForControlRest();
SForControlRest : ":", SExpr;
SForControlRest : "=", SVarInit, (",", SIdent #varName, SVarSuffix,)* ";", SForStmt, ";", SForStmt;

void SForStmt();
SForStmt : ;
SForStmt : SExpr, (",", SExpr)*;

void SBlockStmt();
SBlockStmt[10] : SStmt;
SBlockStmt : STypeDecl;
SBlockStmt : SVarModifier, SType, SIdent #varName, SVarSuffix, (",", SIdent #varName, SVarSuffix,)* ";";

void SBlock();
SBlock : "{" [, (SBlockStmt,)* ]+ "}";

// Class body.
void SClassBody();
SClassBody : "{" [, (SClassEntry,)*]+ "}";

void SClassEntry();
SClassEntry : ";";
SClassEntry : SModifiers, SMemberDecl;
SClassEntry : ("static" #keyword,)? SBlock;

void SMemberDecl();
SMemberDecl : STypeDecl;
SMemberDecl : SType, SIdent #varName, SVarSuffix, (",", SIdent #varName, SVarSuffix,)* ";";
SMemberDecl : STypeVoid, SIdent #fnName, SFormals, ("throws" #keyword, SQIdentList,)? SMethodEnd;
SMemberDecl : SIdent #typeName, SFormals, ("throws" #keyword, SQIdentList,)? SBlock;
SMemberDecl : STypeParams, STypeVoid, SIdent #fnName, SFormals, ("throws" #keyword, SQIdentList,)? SMethodEnd;
SMemberDecl : STypeParams, SIdent #typeName, SFormals, ("throws" #keyword, SQIdentList,)? SBlock;

void SMethodEnd();
SMethodEnd : ";";
SMethodEnd : SBlock;

void SFormals();
SFormals : "(", (SFormalList)?, ")";

void SFormalList();
SFormalList : SVarModifier, SType, SIdent #varName, (",", SFormalList)?;
SFormalList : SVarModifier, SType, SIdent #varName, "\[\]", (",", SFormalList)?;
SFormalList : SVarModifier, SType, "\.\.\.", SIdent #varName;

void SVarModifier();
SVarModifier : ;
SVarModifier : "final" #keyword, SVarModifier;
SVarModifier : SAnnotation, SVarModifier;

void SVarSuffix();
SVarSuffix : ("\[\]",)*;
SVarSuffix : ("\[\]",)* "=", SVarInit;

void SVarInit();
SVarInit : SExpr;
SVarInit : SArrayInit;

void SArrayInit();
SArrayInit : "{", "}";
SArrayInit : "{", ",", "}";
SArrayInit : "{", SVarInit, (",", SVarInit, )* "}";
SArrayInit : "{", SVarInit, (",", SVarInit, )* ",", "}";

// Enum body.
void SEnumBody();
SEnumBody : "{" [, SEnumConsts, SEnumDecls, ]+ "}";

void SEnumConsts();
SEnumConsts : ;
SEnumConsts : SEnumConst - (, ",")?;
SEnumConsts : SEnumConst, ",", SEnumConsts;

void SEnumConst();
SEnumConst : (SAnnotation,)* SIdent #varName, SEnumConstTail;

void SEnumConstTail();
SEnumConstTail : (SArgs,)? SClassBody;
SEnumConstTail : SArgs;
SEnumConstTail : ;

void SEnumDecls();
SEnumDecls : ;
SEnumDecls : ";" - (, SClassEntry)*;

// Interface body.
void SInterfaceBody();
SInterfaceBody : "{" [, (SInterfaceEntry,)* ]+ "}";

void SInterfaceEntry();
SInterfaceEntry : ";";
SInterfaceEntry : SModifiers, SMemberDecl;

// Annotation body.
void SAnnotationBody();
SAnnotationBody : "{" [, (SModifiers, SAnnotationEntry,)* ]+ "}";

void SAnnotationEntry();
SAnnotationEntry : SClassDecl;
SAnnotationEntry : SEnumDecl;
SAnnotationEntry : SInterfaceDecl;
SAnnotationEntry : SAnnotationDecl;
SAnnotationEntry : SType, SIdent #varName, "(", ")", "\[\]", ("default" #keyword, SElementValue,)? ";";
SAnnotationEntry : SType, SIdent #varName, "(", ")", ("default" #keyword, SElementValue,)? ";";
SAnnotationEntry : SType, SIdent #varName, SVarSuffix, (",", SIdent #varName, SVarSuffix,)* ";";