@@ -84,12 +84,19 @@ module.exports = grammar({
84
84
[ $ . self_type , $ . _simple_expression ] ,
85
85
// 'package' package_identifier '{' operator_identifier '=>' • 'enum' …
86
86
[ $ . self_type , $ . lambda_expression ] ,
87
+ // 'class' _class_constructor • _automatic_semicolon …
88
+ [ $ . _class_definition ] ,
89
+ // 'class' operator_identifier • _automatic_semicolon …
90
+ [ $ . _class_constructor ] ,
91
+ // 'enum' _class_constructor '{' 'case' operator_identifier _full_enum_def_repeat1 • _automatic_semicolon …
92
+ [ $ . _full_enum_def ] ,
87
93
] ,
88
94
89
95
word : $ => $ . _alpha_identifier ,
90
96
91
97
rules : {
92
- compilation_unit : $ => repeat ( $ . _top_level_definition ) ,
98
+ // TopStats ::= TopStat {semi TopStat}
99
+ compilation_unit : $ => optional ( trailingSep1 ( $ . _semicolon , $ . _top_level_definition ) ) ,
93
100
94
101
_top_level_definition : $ => choice (
95
102
$ . package_clause ,
@@ -161,7 +168,7 @@ module.exports = grammar({
161
168
)
162
169
) ,
163
170
164
- simple_enum_case : $ => seq ( field ( 'name' , $ . _identifier ) , field ( 'extend' , optional ( $ . extends_clause ) ) ) ,
171
+ simple_enum_case : $ => prec . left ( seq ( field ( 'name' , $ . _identifier ) , field ( 'extend' , optional ( $ . extends_clause ) ) ) ) ,
165
172
166
173
full_enum_case : $ => seq ( field ( 'name' , $ . _identifier ) , $ . _full_enum_def ) ,
167
174
@@ -171,16 +178,15 @@ module.exports = grammar({
171
178
field ( 'extend' , optional ( $ . extends_clause ) )
172
179
) ,
173
180
174
- package_clause : $ => seq (
181
+ package_clause : $ => prec . right ( seq (
175
182
'package' ,
176
183
field ( 'name' , $ . package_identifier ) ,
177
- optional ( $ . _semicolon ) ,
178
184
// This is slightly more permissive than the EBNF in that it allows any
179
185
// kind of delcaration inside of the package blocks. As we're more
180
186
// concerned with the structure rather than the validity of the program
181
187
// we'll allow it.
182
188
field ( 'body' , optional ( $ . template_body ) )
183
- ) ,
189
+ ) ) ,
184
190
185
191
package_identifier : $ => prec . right ( sep1 (
186
192
'.' , $ . _identifier
@@ -263,43 +269,48 @@ module.exports = grammar({
263
269
field ( 'name' , $ . _identifier ) ,
264
270
field ( 'extend' , optional ( $ . extends_clause ) ) ,
265
271
field ( 'derive' , optional ( $ . derives_clause ) ) ,
266
- field ( 'body' , optional ( $ . template_body ) ) ,
272
+ field ( 'body' , optional ( $ . _definition_body ) ) ,
267
273
) ) ,
268
274
269
- class_definition : $ => prec . left ( seq (
275
+ class_definition : $ => seq (
270
276
repeat ( $ . annotation ) ,
271
277
optional ( $ . modifiers ) ,
272
278
optional ( 'case' ) ,
273
279
'class' ,
274
280
$ . _class_definition ,
275
- ) ) ,
281
+ ) ,
276
282
277
283
_class_definition : $ => seq (
278
284
$ . _class_constructor ,
279
285
field ( 'extend' , optional ( $ . extends_clause ) ) ,
280
286
field ( 'derive' , optional ( $ . derives_clause ) ) ,
281
- field ( 'body' , optional ( $ . template_body ) )
287
+ optional ( $ . _definition_body ) ,
288
+ ) ,
289
+
290
+ _definition_body : $ => seq (
291
+ optional ( $ . _automatic_semicolon ) ,
292
+ field ( 'body' , $ . template_body )
282
293
) ,
283
294
284
295
/**
285
296
* ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
286
297
* ConstrMods ::= {Annotation} [AccessModifier]
287
298
*/
288
- _class_constructor : $ => prec . right ( seq (
299
+ _class_constructor : $ => seq (
289
300
field ( 'name' , $ . _identifier ) ,
290
301
field ( 'type_parameters' , optional ( $ . type_parameters ) ) ,
291
302
optional ( $ . annotation ) ,
292
303
optional ( $ . access_modifier ) ,
293
304
field ( 'class_parameters' , repeat ( $ . class_parameters ) ) ,
294
- ) ) ,
305
+ ) ,
295
306
296
307
trait_definition : $ => prec . left ( seq (
297
308
repeat ( $ . annotation ) ,
298
309
optional ( $ . modifiers ) ,
299
310
'trait' ,
300
311
$ . _class_constructor ,
301
312
field ( 'extend' , optional ( $ . extends_clause ) ) ,
302
- field ( 'body' , optional ( $ . template_body ) )
313
+ field ( 'body' , optional ( $ . _definition_body ) )
303
314
) ) ,
304
315
305
316
// The EBNF makes a distinction between function type parameters and other
@@ -633,6 +644,7 @@ module.exports = grammar({
633
644
) ) ,
634
645
635
646
class_parameters : $ => prec ( 1 , seq (
647
+ optional ( $ . _automatic_semicolon ) ,
636
648
'(' ,
637
649
optional ( choice ( 'implicit' , 'using' ) ) ,
638
650
trailingCommaSep ( $ . class_parameter ) ,
0 commit comments