root/trunk/minid2.txt

Revision 580, 11.1 kB (checked in by JarrettBillingsley, 3 years ago)

The grammar and parser now accept trailing commas in array and table literals.

Line 
1 // Lexical
2
3 WhiteSpace:
4     Space {Space}
5
6 Space:
7     ' '
8     '\t'
9     '\v'
10     '\u000C'
11     EndOfLine
12     Comment
13    
14 EndOfLine:
15     '\r'
16     '\n'
17     '\r\n'
18     '\n\r'
19     EndOfFile
20
21 EndOfFile:
22     physical end of file
23     '\0'
24
25 Comment:
26     '/*' {Character} '*/'
27     '//' {Character} EndOfLine
28     NestedComment
29    
30 NestedComment:
31     '/+' {Character | NestedComment} '+/'
32
33 Token:
34     Identifier
35     Keyword
36     CharLiteral
37     StringLiteral
38     IntLiteral
39     FloatLiteral
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     EOF
96
97 Identifier:
98     IdentifierStart {IdentifierChar}
99
100 IdentifierStart:
101     '_'
102     Letter
103
104 IdentifierChar:
105     IdentifierStart
106     DecimalDigit
107
108 Keyword:
109     'as'
110     'assert'
111     'break'
112     'case'
113     'catch'
114     'class'
115     'continue'
116     'coroutine'
117     'default'
118     'do'
119     'else'
120     'false'
121     'finally'
122     'for'
123     'foreach'
124     'function'
125     'global'
126     'if'
127     'import'
128     'in'
129     'is'
130     'local'
131     'module'
132     'namespace'
133     'null'
134     'return'
135     'scope'
136     'super'
137     'switch'
138     'this'
139     'throw'
140     'true'
141     'try'
142     'vararg'
143     'while'
144     'with'
145     'yield'
146
147 CharLiteral:
148     "'" (Character | EscapeSequence) "'"
149
150 StringLiteral:
151     RegularString
152     WysiwygString
153     AltWysiwygString
154
155 RegularString:
156     '"' {Character | EscapeSequence | EndOfLine} '"'
157
158 EscapeSequence:
159     '\''
160     '\"'
161     '\\'
162     '\a'
163     '\b'
164     '\f'
165     '\n'
166     '\r'
167     '\t'
168     '\v'
169     '\x' HexDigit HexDigit
170     '\u' HexDigit HexDigit HexDigit HexDigit
171     '\U' HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit
172     '\ ' DecimalDigit [DecimalDigit [DecimalDigit]]
173
174 WysiwygString:
175     '@"' {Character | EndOfLine | '""'} '"'
176
177 AltWysiwygString:
178     '`' {Character | EndOfLine | '``'} '`'
179
180 IntLiteral:
181     Decimal
182     Binary
183     Hexadecimal
184
185 Decimal:
186     DecimalDigit {DecimalDigit | '_'}
187
188 DecimalDigit:
189     '0' .. '9'
190
191 Binary:
192     '0' ('b' | 'B') (BinaryDigit | '_') {BinaryDigit | '_'}
193
194 BinaryDigit:
195     '0'
196     '1'
197
198 Hexadecimal:
199     '0' ('x' | 'X') (HexDigit | '_') {HexDigit | '_'}
200
201 HexDigit:
202     '0' .. '9'
203     'a' .. 'f'
204     'A' .. 'F'
205
206 FloatLiteral:
207     [DecimalDigit {DecimalDigit | '_'}] '.' (DecimalDigit | '_') {DecimalDigit | '_'} [Exponent]
208     DecimalDigit {DecimalDigit | '_'} [Exponent]
209
210 Exponent:
211     ('e' | 'E')['+' | '-'] (DecimalDigit | '_') {DecimalDigit | '_'}
212
213 // -----------------------------------------------------------------------------------------------------------------
214 // -----------------------------------------------------------------------------------------------------------------
215 // -----------------------------------------------------------------------------------------------------------------
216
217 // Syntax
218
219 Module:
220     ModuleDeclaration {Statement} EOF
221
222 ModuleDeclaration:
223     [Decorators] 'module' Identifier {'.' Identifier} StatementTerminator
224
225 StatementTerminator:
226     ';'
227     EndOfLine // not consumed
228     '}' // not consumed
229     ')' // not consumed
230     ']' // not consumed
231
232 Statement:
233     AssertStatement
234     ImportStatement
235     BlockStatement
236     ExpressionStatement
237     DeclarationStatement
238     IfStatement
239     WhileStatement
240     DoWhileStatement
241     ForStatement
242     ForeachStatement
243     SwitchStatement
244     ContinueStatement
245     BreakStatement
246     ReturnStatement
247     TryCatchStatement
248     ThrowStatement
249     ScopeActionStatement
250     Assignment
251
252 AssertStatement:
253     'assert' '(' Expression [',' Expression] ')' StatementTerminator
254
255 ImportStatement:
256     'import' [Identifier '='] Identifier {'.' Identifier} [SelectiveImports] StatementTerminator
257     'import' [Identifier '='] '(' Expression ')' [SelectiveImports] StatementTerminator
258
259 SelectiveImports:
260     ':' SelectiveImport {',' SelectiveImport}
261
262 SelectiveImport:
263     [Identifier '='] Identifier
264
265 BlockStatement:
266     '{' {Statement} '}'
267
268 ExpressionStatement:
269     Expression StatementTerminator
270
271 DeclarationStatement:
272     VariableDeclaration StatementTerminator
273     [Decorators] OtherDeclaration
274    
275 Decorators:
276     Decorator {Decorator}
277
278 Decorator:
279     '@' Identifier {'.' Identifier} [ArgumentsWith]
280
281 OtherDeclaration:
282     FunctionDeclaration
283     ClassDeclaration
284     NamespaceDeclaration
285
286 VariableDeclaration:
287     LocalVarDeclaration
288     GlobalVarDeclaration
289
290 LocalVarDeclaration:
291     'local' Identifier {',' Identifier} ['=' ExpressionList]
292
293 GlobalVarDeclaration:
294     'global' Identifier {',' Identifier} ['=' ExpressionList]
295
296 FunctionDeclaration:
297     ['local' | 'global'] SimpleFunctionDeclaration
298
299 SimpleFunctionDeclaration:
300     'function' Identifier FunctionBody
301
302 FunctionBody:
303     '(' Parameters ')' (Statement | '=' Expression)
304
305 Parameters:
306     [('this' ':' Type | Parameter) {',' Parameter} [',' 'vararg']]
307     'vararg'
308
309 Parameter:
310     Identifier [':' Type] ['=' Expression]
311    
312 Type:
313     BasicType
314     '!' 'null'
315     'any'
316
317 BasicType:
318     BasicType '|' BasicType
319     'null'
320     'bool'
321     'int'
322     'float'
323     'char'
324     'string'
325     'table'
326     'array'
327     'function'
328     'class'
329     'instance' [(Identifier {'.' Identifier}) | ('(' Expression')')]
330     Identifier {'.' Identifier}
331     'namespace'
332     'thread'
333     'nativeobj'
334     'weakref'
335
336 ClassDeclaration:
337     ['local' | 'global'] 'class' Identifier [':' Expression] '{' {ClassMember} '}'
338
339 ClassMember:
340     [Decorators] SimpleFunctionDeclaration
341     [Decorators] 'this' FunctionBody
342     [Decorators] Identifier ['=' Expression] StatementTerminator
343
344 NamespaceDeclaration:
345     ['local' | 'global'] 'namespace' Identifier [':' Expression] '{' {NamespaceMember} '}'
346
347 NamespaceMember:
348     [Decorators] SimpleFunctionDeclaration
349     [Decorators] Identifier ['=' Expression] StatementTerminator
350
351 IfStatement:
352     'if' '(' ['local' Identifier '='] Expression ')' Statement ['else' Statement]
353
354 WhileStatement:
355     'while' '(' ['local' Identifier '='] Expression ')' Statement
356
357 DoWhileStatement:
358     'do' Statement 'while' '(' Expression ')'
359
360 ForStatement:
361     'for' '(' [ForInitializer] ';' [Expression] ';' [ForIncrement] ')' Statement
362     'for' '(' Identifier (':' | ';') Expression '..' Expression [',' Expression] ')' Statement
363
364 ForInitializer:
365     ForInitializer ',' ForInitializer
366     Expression
367     LocalVarDeclaration
368     Assignment
369
370 ForIncrement:
371     ForIncrement ',' ForIncrement
372     Expression
373     Assignment
374
375 ForeachStatement:
376     'foreach' '(' Identifier {',' Identifier} ';' Expression [',' Expression [',' Expression]] ')' Statement
377    
378 SwitchStatement:
379     'switch' '(' Expression ')' '{' CaseStatement {CaseStatement} [DefaultStatement] '}'
380
381 CaseStatement:
382     'case' Expression ({',' Expression} | '..' Expression) ':' {Statement}
383
384 DefaultStatement:
385     'default' ':' {Statement}
386    
387 ContinueStatement:
388     'continue' StatementTerminator
389
390 BreakStatement:
391     'break' StatementTerminator
392    
393 ReturnStatement:
394     'return' [ExpressionList] StatementTerminator
395
396 TryCatchStatement:
397     'try' Statement (('catch' '(' Identifier ')' Statement) || ('finally' Statement))
398    
399 ThrowStatement:
400     'throw' Expression StatementTerminator
401
402 ScopeActionStatement:
403     'scope' '(' ('exit' | 'success' | 'failure') ')' Statement
404
405 Assignment:
406     AssignmentLHS {',' AssignmentLHS} '=' ExpressionList
407     AssignmentLHS '+=' Expression
408     AssignmentLHS '-=' Expression
409     AssignmentLHS '~=' Expression
410     AssignmentLHS '*=' Expression
411     AssignmentLHS '/=' Expression
412     AssignmentLHS '%=' Expression
413     AssignmentLHS '<<=' Expression
414     AssignmentLHS '>>=' Expression
415     AssignmentLHS '>>>=' Expression
416     AssignmentLHS '|=' Expression
417     AssignmentLHS '^=' Expression
418     AssignmentLHS '&=' Expression
419     AssignmentLHS '?=' Expression
420     '++' PrimaryExpression
421     '--' PrimaryExpression
422     PrimaryExpression '++'
423     PrimaryExpression '--'
424
425 AssignmentLHS:
426     Identifier
427     // Note - for these, the PostfixExpression must start with Identifier, 'this', or '#'.
428     PostfixExpression '[' ']'
429     PostfixExpression '[' Expression ']'
430     PostfixExpression '[' [Expression] '..' [Expression] ']'
431     DotExpression
432
433 Expression:
434     ConditionalExpression
435    
436 ConditionalExpression:
437     OrOrExpression
438     OrOrExpression '?' Expression ':' ConditionalExpression
439
440 OrOrExpression:
441     AndAndExpression
442     OrOrExpression '||' AndAndExpression
443
444 AndAndExpression:
445     OrExpression
446     AndAndExpression '&&' OrExpression
447
448 OrExpression:
449     XorExpression
450     OrExpression '|' XorExpression
451
452 XorExpression:
453     AndExpression
454     XorExpression '^' AndExpression
455
456 AndExpression:
457     CompareExpression
458     AndExpression '&' CompareExpression
459
460 CompareExpression:
461     ShiftExpression
462     EqualExpression
463     RelExpression
464
465 EqualExpression:
466     ShiftExpression '==' ShiftExpression
467     ShiftExpression '!=' ShiftExpression
468     ShiftExpression 'is' ShiftExpression
469     ShiftExpression '!' 'is' ShiftExpression
470
471 RelExpression:
472     ShiftExpression '<' ShiftExpression
473     ShiftExpression '<=' ShiftExpression
474     ShiftExpression '>' ShiftExpression
475     ShiftExpression '>=' ShiftExpression
476     ShiftExpression '<=>' ShiftExpression
477     ShiftExpression 'as' ShiftExpression
478     ShiftExpression 'in' ShiftExpression
479     ShiftExpression '!' 'in' ShiftExpression
480
481 ShiftExpression:
482     AddExpression
483     ShiftExpression '<<' AddExpression
484     ShiftExpression '>>' AddExpression
485     ShiftExpression '>>>' AddExpression
486
487 AddExpression:
488     MulExpression
489     AddExpression '+' MulExpression
490     AddExpression '-' MulExpression
491     AddExpression '~' MulExpression
492
493 MulExpression:
494     UnaryExpression
495     MulExpression '*' UnaryExpression
496     MulExpression '/' UnaryExpression
497     MulExpression '%' UnaryExpression
498
499 UnaryExpression:
500     PostfixExpression
501     '-' UnaryExpression
502     '!' UnaryExpression
503     '~' UnaryExpression
504     '#' UnaryExpression
505     'coroutine' UnaryExpression
506
507 PostfixExpression:
508     PrimaryExpression
509     CallExpression
510     PostfixExpression '[' ']'
511     PostfixExpression '[' Expression ']'
512     PostfixExpression '[' [Expression] '..' [Expression] ']'
513     PostfixExpression '.' 'super'
514     DotExpression
515
516 DotExpression:
517     PostfixExpression '.' (Identifier | '(' Expression ')')
518
519 CallExpression:
520     PostfixExpression ArgumentsWith
521     DotExpression ArgumentsWith
522     'super' ['.' (Identifier | '(' Expression ')')] Arguments
523
524 ArgumentsWith:
525     Arguments
526     '(' 'with' Expression [',' ExpressionList] ')'
527
528 Arguments:
529     '(' [ExpressionList] ')'
530     '$' ExpressionList
531
532 ExpressionList:
533     Expression {',' Expression}
534
535 PrimaryExpression:
536     Identifier
537     'this'
538     'null'
539     'true'
540     'false'
541     'vararg'
542     IntLiteral
543     FloatLiteral
544     CharLiteral
545     StringLiteral
546     ':' (Identifier | 'super' | '(' Expression ')')
547     'function' [Identifier] FunctionBody
548     '\' Parameters (('->' Expression) | BlockStatement)
549     'class' [Identifier] [':' Expression] '{' {ClassMember} '}'
550     '(' Expression ')'
551     TableCtorExp
552     ArrayLiteral
553     'namespace' Identifier [':' Expression] '{' {NamespaceMember} '}'
554     'yield' '(' [Arguments] ')'
555
556 ArrayLiteral:
557     '[' [Expression [','] {Expression [',']}] ']'
558     '[' Expression ForComprehension ']'
559
560 TableCtorExp:
561     '{' [TableField [','] { TableField [',']}] '}'
562     '{' PlainTableField ForComprehension '}'
563
564 TableField:
565     Identifier '=' Expression
566     PlainTableField
567     SimpleFunctionDeclaration
568
569 PlainTableField:
570     '[' Expression ']' '=' Expression
571
572 ForComprehension:
573     'for' Identifier {',' Identifier} 'in' Expression [',' Expression [',' Expression]] [IfComprehension] [ForComprehension]
574     'for' Identifier 'in' Expression '..' Expression [',' Expression] [IfComprehension] [ForComprehension]
575
576 IfComprehension:
577     'if' Expression
Note: See TracBrowser for help on using the browser.