parser: extends grammar to support annotations
[nit.git] / src / parser / nit.sablecc3xx
1
2 /* This file is part of NIT ( http://www.nitlanguage.org ).
3  *
4  * Copyright 2008-2009 Jean Privat <jean@pryen.org>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /* This grammar defines the NIT language. */
20 Package org.nitlanguage.gen;
21
22 /*****************************************************************************/
23 Helpers
24 /*****************************************************************************/
25
26 all = [0 .. 0xFF];
27 lowercase = ['a' .. 'z'];
28 uppercase = ['A' .. 'Z'];
29 digit = ['0' .. '9'];
30 letter = lowercase | uppercase | digit | '_';
31
32 tab = 9;
33 cr = 13;
34 lf = 10;
35 any = [all - [cr + lf]];
36
37 // characers inside strings and super-strings (atomaton powaa)
38 str_char
39         = [any - [['"' + '{'] + '\']] 
40         | '\' any 
41         ;
42 str_body = str_char*;
43
44 extern_code_char
45         = [all - ['`' + '\']]
46         | '\' all
47         ;
48 extern_code_body = extern_code_char*;
49
50 eol_helper = cr lf | cr | lf; // This takes care of different platforms
51
52 /*****************************************************************************/
53 States
54 /*****************************************************************************/
55 initial;
56
57
58 /*****************************************************************************/
59 Tokens
60 /*****************************************************************************/
61
62 blank = (' ' | tab)+;
63
64 eol = eol_helper;
65 comment = '#' any* eol_helper?;
66
67 kwmodule = 'package'|'module';
68 kwimport = 'import';
69 kwclass = 'class';
70 kwabstract = 'abstract';
71 kwinterface = 'interface';
72 kwenum = 'universal'|'enum';
73 kwend = 'end';
74 kwmeth = 'fun';
75 kwtype = 'type';
76 kwinit = 'init';
77 kwredef = 'redef';
78 kwis = 'is';
79 kwdo = 'do';
80 kwreadable = 'readable';
81 kwwritable = 'writable';
82 kwvar = 'var';
83 kwintern = 'intern';
84 kwextern = 'extern';
85 kwprotected = 'protected';
86 kwprivate = 'private';
87 kwintrude = 'intrude';
88 kwif = 'if';
89 kwthen = 'then';
90 kwelse = 'else';
91 kwwhile = 'while';
92 kwloop = 'loop';
93 kwfor = 'for';
94 kwin = 'in';
95 kwand = 'and';
96 kwor = 'or';
97 kwnot = 'not';
98 kwreturn = 'return';
99 kwcontinue = 'continue';
100 kwbreak = 'break';
101 kwabort = 'abort';
102 kwassert = 'assert';
103 kwnew = 'new';
104 kwisa = 'isa';
105 kwonce = 'once';
106 kwsuper = 'super';
107 kwself = 'self';
108 kwtrue = 'true';
109 kwfalse = 'false';
110 kwnull = 'null';
111 kwas = 'as';
112 kwnullable = 'nullable';
113 kwisset = 'isset';
114 kwlabel = 'label';
115 kwdebug = '__debug__';
116
117 opar = '(';
118 cpar = ')';
119 obra = '[';
120 cbra = ']';
121 comma = ',';
122 column = ':';
123 quad = '::';
124 assign = '=';
125 pluseq = '+=';
126 minuseq = '-=';
127 dotdotdot = '...';
128 dotdot = '..';
129 dot = '.';
130 plus = '+';
131 minus = '-';
132 star = '*';
133 slash = '/';
134 percent = '%';
135 eq = '==';
136 ne = '!=';
137 lt = '<';
138 le = '<=';
139 ll = '<<';
140 gt = '>';
141 ge = '>=';
142 gg = '>>';
143 starship = '<=>';
144 bang='!';
145 at='@';
146
147 classid = uppercase letter*;
148 id = lowercase letter*;
149 attrid = '_' lowercase letter*;
150
151 number = digit+;
152 float = digit* '.' digit+;
153 char = (''' [[any - '''] - '\'] ''') | (''' '\' any ''');
154 string = '"' str_body '"';
155 start_string = '"' str_body '{';
156 mid_string = '}' str_body '{';
157 end_string = '}' str_body '"';
158 bad_char = ''' '\'? any;
159 bad_string = ('"'|'}') str_body;
160
161 extern_code_segment = '`' '{' extern_code_body '`' '}';
162
163 /*****************************************************************************/
164 Ignored Tokens
165 /*****************************************************************************/
166
167 blank;
168
169 /*****************************************************************************/
170 Productions
171 /*****************************************************************************/
172
173 /* MODULES *******************************************************************/
174 module
175         = moduledecl? [imports]:import* [extern_bodies]:extern_code_body* [classdefs]:classdef* implicit_top_class? implicit_main_class? {-> New module(moduledecl, [imports.import], [extern_bodies.extern_code_block], [classdefs.classdef,implicit_top_class.classdef,implicit_main_class.classdef])};
176
177 moduledecl
178         = [doc]:no kwmodule no module_name annotation_withend? [n2]:n1 {-> New moduledecl(doc.doc, kwmodule, module_name, annotation_withend.annotations)};
179
180 import
181         = {std} [doc]:no visibility kwimport no module_name annotation_withend? [n2]:n1 {-> New import.std(visibility, kwimport, module_name, annotation_withend.annotations)}
182         | {no} [doc]:no visibility kwimport no kwend [n2]:n1 {-> New import.no(visibility, kwimport, kwend)}
183         ;
184
185 implicit_top_class {-> classdef}
186         = propdefs_toplevel+ {-> New classdef.top([propdefs_toplevel.propdef])};
187
188 implicit_main_class {-> classdef?}
189         = implicit_main_meth {-> New classdef.main([implicit_main_meth.propdef])}
190         | {null} n {-> Null}
191         ;
192 implicit_main_meth {-> propdef}
193         = [doc]:no stmts {-> New propdef.main_meth(Null, stmts.expr)}
194         | {n} [doc]:no stmtsn {-> New propdef.main_meth(Null, stmtsn.expr)}
195         ;
196
197 /* CLASSES *******************************************************************/
198 classdef
199         = [doc]:no redef visibility classkind no classid formaldefs? line_annotations_forclass? extern_code_block? [superclasses]:superclass* propdefs* [n2]:no kwend {-> New classdef.std(doc.doc, redef.kwredef, visibility, classkind, classid, [formaldefs.formaldef], line_annotations_forclass.annotations, extern_code_block, [superclasses.superclass], [propdefs.propdef], kwend)};
200 redef {-> kwredef?}
201         = kwredef? {-> kwredef};
202 classkind
203         = {concrete} kwclass
204         | {abstract} kwabstract kwclass
205         | {interface} kwinterface
206         | {enum} kwenum
207         | {extern} kwextern kwclass?
208         ;
209
210 formaldefs {-> formaldef*}
211         = obra no formaldef formaldefs_tail* [n2]:no cbra {-> [formaldef, formaldefs_tail.formaldef]};
212 formaldefs_tail {-> formaldef}
213         = comma no formaldef {-> formaldef};
214 formaldef
215         = classid annotations? typing? {-> New formaldef(classid, typing.type, annotations)};
216
217 superclass {-> superclass}
218         = {super} no kwsuper [n2]:no type annotation_withend? {-> New superclass(kwsuper, type, annotation_withend.annotations)}
219         ;
220
221 propdefs~toplevel {-> propdef}
222         = propdef~toplevel n1 {-> propdef~toplevel.propdef}
223         ;
224 propdef~toplevel {-> propdef}
225         = {meth} [doc]:no redef visibility kwmeth methid signature annotation_noend? kwdo stmtso kwend? {-> New propdef.concrete_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature, annotation_noend.annotations, stmtso.expr)}
226         | {assign_return} [doc]:no redef visibility kwmeth methid signature_withret assign no assign_return {-> New propdef.concrete_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature_withret.signature, Null, assign_return.expr)}
227         | {nobody} [doc]:no redef visibility kwmeth methid signature annotation_withend {-> New propdef.deferred_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, annotation_withend.annotations)}
228 !toplevel| {deferred} [doc]:no redef visibility kwmeth methid signature kwis kwabstract {-> New propdef.deferred_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, Null)}
229 !toplevel| {intern} [doc]:no redef visibility kwmeth methid signature kwis kwintern {-> New propdef.intern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature)}
230         | {extern} [doc]:no redef visibility kwmeth methid signature kwis kwextern string? extern_calls? extern_code_block? {-> New propdef.extern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, string, extern_calls, extern_code_block)}
231         | {extern_implicit} [doc]:no redef visibility kwmeth methid signature extern_calls? extern_code_block {-> New propdef.extern_meth(doc.doc, redef.kwredef, visibility, kwmeth, methid, signature.signature, Null, extern_calls, extern_code_block)}
232 !toplevel| {var} [doc]:no readable? writable? redef visibility kwvar attrid typing? {-> New propdef.attr(doc.doc, readable.able, writable.able, redef.kwredef, visibility, kwvar, attrid, Null, typing.type, Null, Null)}
233 !toplevel| {var2} [doc]:no readable? writable? redef visibility kwvar attrid typing? assign [n2]:no expr {-> New propdef.attr(doc.doc, readable.able, writable.able, redef.kwredef, visibility, kwvar, attrid, Null, typing.type, Null, expr)}
234 !toplevel| {var3} [doc]:no redef visibility kwvar id typing? writable2? annotation_withend? {-> New propdef.attr(doc.doc, Null, writable2.able, redef.kwredef, visibility, kwvar, Null, id, typing.type, annotation_withend.annotations, Null)}
235 !toplevel| {var4} [doc]:no redef visibility kwvar id typing? writable2? assign [n2]:no expr_nois annotation_withend? {-> New propdef.attr(doc.doc, Null, writable2.able, redef.kwredef, visibility, kwvar, Null, id, typing.type, annotation_withend.annotations, expr_nois.expr)}
236 !toplevel| {init} [doc]:no redef visibility kwinit methid? signature annotation_noend? kwdo stmtso kwend? {-> New propdef.concrete_init(doc.doc, redef.kwredef, visibility, kwinit, methid, signature, annotation_noend.annotations, stmtso.expr)}
237 !toplevel| {type} [doc]:no redef visibility kwtype classid typing annotation_withend? {-> New propdef.type(doc.doc, redef.kwredef, visibility, kwtype, classid, typing.type, annotation_withend.annotations)}
238 !toplevel| {extern_init} [doc]:no redef visibility kwnew methid? signature kwis kwextern string? extern_calls? extern_code_block? {-> New propdef.extern_init(doc.doc, redef.kwredef, visibility, kwnew, methid, signature, string, extern_calls, extern_code_block)}
239 !toplevel| {extern_init_implicit} [doc]:no redef visibility kwnew methid? signature string? extern_calls? extern_code_block {-> New propdef.extern_init(doc.doc, redef.kwredef, visibility, kwnew, methid, signature, string, extern_calls, extern_code_block)}
240         ;
241 annotation_withend {-> annotations}
242         = {oneliner} kwis many_annotations {-> many_annotations.annotations}
243         | {more} kwis n1 line_annotations kwend {-> line_annotations.annotations}
244         ;
245 annotation_noend {-> annotations}
246         = {oneliner} kwis many_annotations {-> many_annotations.annotations}
247         | {more} kwis n1 line_annotations {-> line_annotations.annotations}
248         ;
249 readable {-> able}
250         = redef kwreadable {-> New able.read(redef.kwredef, kwreadable)}
251         ;
252 writable {-> able}
253         = redef kwwritable {-> New able.write(redef.kwredef, Null, kwwritable)}
254         ;
255 writable2 {-> able}
256         = redef visibility kwwritable {-> New able.write(redef.kwredef, visibility, kwwritable)}
257         ;
258
259 visibility
260         = {public} {-> New visibility.public()}
261         | {private} kwprivate no {-> New visibility.private(kwprivate)}
262         | {protected} kwprotected no {-> New visibility.protected(kwprotected)}
263         | {intrude} kwintrude no {-> New visibility.intrude(kwintrude)}
264         ;
265
266 methid {-> methid}
267         = {id} id {-> New methid.id(id)}
268         | {plus} plus {-> New methid.plus(plus)}
269         | {minus} minus {-> New methid.minus(minus)}
270         | {star} star {-> New methid.star(star)}
271         | {slash} slash {-> New methid.slash(slash)}
272         | {percent} percent {-> New methid.percent(percent)}
273         | {eq} eq {-> New methid.eq(eq)}
274         | {ne} ne {-> New methid.ne(ne)}
275         | {le} le {-> New methid.le(le)}
276         | {ge} ge {-> New methid.ge(ge)}
277         | {lt} lt {-> New methid.lt(lt)}
278         | {gt} gt {-> New methid.gt(gt)}
279         | {ll} ll {-> New methid.ll(ll)}
280         | {gg} gg {-> New methid.gg(gg)}
281         | {bra} obra cbra {-> New methid.bra(obra, cbra)}
282         | {starship} starship {-> New methid.starship(starship)}
283         | {assign} id assign {-> New methid.assign(id, assign)}
284         | {braassign} obra cbra assign {-> New methid.braassign(obra, cbra, assign)}
285         ;
286
287 signature~withret {-> signature}
288         = opar no params? cpar typing [no2]:no closure_decls? {-> New signature(opar, [params.param], cpar, typing.type, [closure_decls.closure_decl])}
289 !withret| {noret} opar no params? cpar [no2]:no closure_decls? {-> New signature(opar, [params.param], cpar, Null, [closure_decls.closure_decl])}
290         | {nopar} typing no closure_decls? {-> New signature(Null, [], Null, typing.type, [closure_decls.closure_decl])}
291 !withret| {noparnoret} no closure_decls? {-> New signature(Null, [], Null, Null, [closure_decls.closure_decl])}
292         ;
293
294 signature_noclosures {-> signature}
295         = opar no params? cpar typing? {-> New signature(opar, [params.param], cpar, typing.type, [])}
296         | {nopar} typing? {-> New signature(Null, [], Null, typing.type, [])}
297         ;
298
299 params {-> param*} 
300         = param params_tail* [n2]:no {-> [param, params_tail.param] }
301         ;
302 params_tail {-> param}
303         = comma no param {-> param};
304 param
305         = {untyped} id annotations? {-> New param(id, Null, Null, annotations)}
306         | id annotations? typing dotdotdot? {-> New param(id, typing.type, dotdotdot, annotations)}
307         ;
308
309 closure_decls {->closure_decl*}
310         = closure_decl+ {-> [closure_decl]};
311 closure_decl
312         = kwbreak? bang id signature_noclosures n {-> New closure_decl(kwbreak, bang, id, signature_noclosures.signature, Null)}
313         | {optionnal} kwbreak? bang id signature_noclosures kwdo stmtso n {-> New closure_decl(kwbreak, bang, id, signature_noclosures.signature, stmtso.expr)}
314         | {assign} kwbreak? bang id signature_noclosures assign no assign_continue n {-> New closure_decl(kwbreak, bang, id, signature_noclosures.signature, assign_continue.expr)}
315         ;
316
317 assign_return{-> expr}
318         = expr_final {-> New expr.return(Null, expr_final.expr)}
319         ;
320
321 extern_calls {-> extern_calls}
322         = kwimport no extern_call extern_call_tail* {-> New extern_calls( kwimport, [extern_call, extern_call_tail.extern_call] )}
323         ;
324 extern_call_tail {-> extern_call}
325         = comma no extern_call {-> extern_call};
326 extern_call {-> extern_call}
327     = {prop} extern_call_prop {-> extern_call_prop.extern_call}
328     | {cast} extern_call_cast {-> extern_call_cast.extern_call}
329         | {super} kwsuper {-> New extern_call.super( kwsuper )}
330     ;
331 extern_call_prop {-> extern_call}
332     = {local} methid {-> New extern_call.local_prop( methid )}
333         | {full} classid quad methid {-> New extern_call.full_prop( classid, quad, methid )}
334         | {init} classid {-> New extern_call.init_prop( classid )}
335         ;
336 extern_call_cast {-> extern_call}
337     = {as_cast} [from_type]:type kwas [n2]:no opar [n3]:no [to_type]:type [n4]:no cpar {-> New extern_call.cast_as(from_type, kwas, to_type)}
338         | {as_nullable} type kwas [n2]:no kwnullable {-> New extern_call.as_nullable( type, kwas, kwnullable)}
339         | {as_not_nullable} type kwas [n2]:no kwnot [n3]:no kwnullable {-> New extern_call.as_not_nullable( type, kwas, kwnot, kwnullable)}
340         ;
341
342 in_language = kwin string;
343 extern_code_block = in_language? extern_code_segment;
344 extern_code_body {-> extern_code_block} = no extern_code_block {-> extern_code_block};
345
346 /* TYPES *********************************************************************/
347 type~nobra~nopar {-> type}
348         = {simple} kwnullable? classid annotations~nopar? {-> New type(kwnullable, classid, [], annotations~nopar.annotations)}
349 !nobra  | {generic} kwnullable? classid obra no types [n2]:no cbra annotations~nopar? {-> New type(kwnullable, classid, [types.type], annotations~nopar.annotations)}
350         ;
351 types {-> type*} 
352         = type types_tail* {-> [type, types_tail.type]};
353 types_tail {-> type}
354         = comma no type {-> type};
355 typing {-> type}
356         = column no type {-> type};
357
358 /* STATMENTS *****************************************************************/
359 stmtso~withelse~withend {-> expr?}
360         = {block} n stmtsnend {-> stmtsnend.expr}
361         | {emptyblock} n kwend {-> New expr.block([], kwend)}
362         | {emptyoneline} kwend {-> New expr.block([], kwend)}
363 !withend| {oneline} stmt~withelse {-> stmt~withelse.expr}
364         ;
365 stmts {-> expr}
366         = stmt stmts_tail* {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
367 stmtsn {-> expr}
368         = stmt stmts_tail* n {-> New expr.block([stmt.expr, stmts_tail.expr], Null)};
369 stmtsnend {-> expr}
370         = stmt stmts_tail* n kwend {-> New expr.block([stmt.expr, stmts_tail.expr], kwend)};
371 stmts_tail {-> expr}
372         = n stmt {-> stmt.expr};
373 stmt~withelse~noexpr~nopar {-> expr}
374         = {vardecl} vardecl~withelse {-> vardecl~withelse.expr}
375         | {assign} assignment~withelse~nopar {-> assignment~withelse~nopar.expr}
376         | {return} kwreturn expr_final~withelse? {-> New expr.return(kwreturn, expr_final~withelse.expr)}
377         | {break} kwbreak label? expr_final~withelse? {-> New expr.break(kwbreak, label, expr_final~withelse.expr)}
378         | {abort} kwabort {-> New expr.abort(kwabort)}
379         | {continue} kwcontinue label? expr_final~withelse? {-> New expr.continue(kwcontinue, label, expr_final~withelse.expr)}
380         | {do} do~withelse {-> do~withelse.expr}
381 !noexpr | {if} if~withelse {-> if~withelse.expr}
382         | {while} while~withelse {-> while~withelse.expr}
383         | {loop} loop~withelse {-> loop~withelse.expr}
384         | {for} for~withelse {-> for~withelse.expr}
385         | {assert} assert~withelse {-> assert~withelse.expr}
386 !noexpr | {call} recv id args_nopar closure_defs~withelse? {-> New expr.call(recv.expr, id, args_nopar.exprs, [closure_defs~withelse.closure_def])}
387 !noexpr | {super} qualified? kwsuper args_nopar {-> New expr.super(qualified, kwsuper, args_nopar.exprs)}
388 !noexpr | {init} recv kwinit args_nopar {-> New expr.init(recv.expr, kwinit, args_nopar.exprs)}
389         | {debug_type_is} kwdebug kwtype type column expr_final~withelse {-> New expr.debug_type(kwdebug, kwtype, expr_final~withelse.expr, type) }
390         ;
391
392 label= kwlabel id;
393
394 closure_defs~withelse {-> closure_def*}
395         = {one} closure_def_last~withelse {-> [closure_def_last~withelse.closure_def]}
396         | closure_def closure_defs~withelse {-> [closure_def, closure_defs~withelse.closure_def]}
397         ;
398
399 closure_def_last~withelse {-> closure_def}
400         = bang [id]:closure_id idlist? kwdo stmtso_withend label {-> New closure_def(bang, id, [idlist.id], kwdo, stmtso_withend.expr, label)}
401         | {nolabel} bang [id]:closure_id idlist? kwdo stmtso~withelse {-> New closure_def(bang, id, [idlist.id], kwdo, stmtso~withelse.expr, Null)}
402         | {assign} bang [id]:closure_id idlist? assign no assign_continue~withelse {-> New closure_def(bang, id, [idlist.id], Null, assign_continue~withelse.expr, Null)}
403         ;
404
405 closure_def {-> closure_def}
406         = bang [id]:closure_id idlist? kwdo n stmtsn {-> New closure_def(bang, id, [idlist.id], kwdo, stmtsn.expr, Null)}
407         | {empty} bang [id]:closure_id idlist? kwdo n {-> New closure_def(bang, id, [idlist.id], kwdo, Null, Null)}
408         ;
409 closure_id
410         = {simple} id
411         | {break} kwbreak
412         ;
413
414 assign_continue~withelse{-> expr}
415         = expr_final~withelse {-> New expr.continue(Null, Null, expr_final~withelse.expr)}
416         ;
417
418 vardecl~withelse{-> expr}
419         = kwvar id annotations? typing? {-> New expr.vardecl(kwvar, id, typing.type, Null, Null, annotations)}
420         | {assign} kwvar id annotations? typing? assign no expr_final~withelse {-> New expr.vardecl(kwvar, id, typing.type, assign, expr_final~withelse.expr, annotations)}
421         ;
422
423 assignment~withelse~nopar {-> expr}
424         = {attr} recv~nopar attrid assign expr_final~withelse {-> New expr.attr_assign(recv~nopar.expr, attrid, assign, expr_final~withelse.expr)}
425         | {call} recv~nopar id args assign expr_final~withelse {-> New expr.call_assign(recv~nopar.expr, id, args.exprs, assign,  expr_final~withelse.expr)}
426         | {bra} expr_atom~nopar braargs assign expr_final~withelse {-> New expr.bra_assign(expr_atom~nopar.expr, braargs.exprs, assign,  expr_final~withelse.expr)}
427         | {attr_re} recv~nopar attrid assign_op expr_final~withelse {-> New expr.attr_reassign(recv~nopar.expr, attrid, assign_op,  expr_final~withelse.expr)}
428         | {call_re} recv~nopar id args assign_op expr_final~withelse {-> New expr.call_reassign(recv~nopar.expr, id, args.exprs, assign_op,  expr_final~withelse.expr)}
429         | {bra_re} expr_atom~nopar braargs assign_op expr_final~withelse {-> New expr.bra_reassign(expr_atom~nopar.expr, braargs.exprs, assign_op,  expr_final~withelse.expr)}
430         ;
431 assign_op
432         = {plus} pluseq
433         | {minus} minuseq
434         ;
435
436 do~withelse {-> expr}
437         = kwdo stmtso_withend label {-> New expr.do(kwdo, stmtso_withend.expr, label)}
438         | {nolabel} kwdo stmtso~withelse {-> New expr.do(kwdo, stmtso~withelse.expr, Null)}
439         ;
440
441 if~withelse {-> expr}
442         = {onelineelse} kwif no expr [n2]:no kwthen stmt_withelse kwelse stmtso~withelse {-> New expr.if(kwif, expr, stmt_withelse.expr, stmtso~withelse.expr)}
443 !withelse       | {oneline} kwif no expr [n2]:no kwthen stmt {-> New expr.if(kwif, expr, stmt.expr, Null)}
444 !withelse       | {block} kwif no expr [n2]:no kwthen [n3]:n stmtsn elsepartblock {-> New expr.if(kwif, expr, stmtsn.expr, elsepartblock.expr)}
445 !withelse       | {emptyblock} kwif no expr [n2]:no kwthen [n3]:n? elsepartblock {-> New expr.if(kwif, expr, Null, elsepartblock.expr)}
446         ;
447 elsepartblock {-> expr?}
448         = {else} kwelse stmtso {-> stmtso.expr}
449         | {empty} kwend {-> New expr.block([], kwend)}
450         ;
451
452 loop~withelse {-> expr}
453         = kwloop stmtso_withend label {-> New expr.loop(kwloop, stmtso_withend.expr, label)}
454         | {nolabel} kwloop stmtso~withelse {-> New expr.loop(kwloop, stmtso~withelse.expr, Null)}
455         ;
456
457 while~withelse {-> expr}
458         = kwwhile no expr [n2]:no kwdo stmtso_withend label {-> New expr.while(kwwhile, expr, kwdo, stmtso_withend.expr, label)}
459         | {nolabel} kwwhile no expr [n2]:no kwdo stmtso~withelse {-> New expr.while(kwwhile, expr, kwdo, stmtso~withelse.expr, Null)}
460         ;
461
462 for~withelse {-> expr}
463         = kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso_withend label {-> New expr.for(kwfor, [ids.id], expr, kwdo, stmtso_withend.expr, label)}
464         | {nolabel} kwfor no [ids]:idlist [n2]:no kwin [n3]:no expr [n4]:no kwdo stmtso~withelse {-> New expr.for(kwfor, [ids.id], expr, kwdo, stmtso~withelse.expr, Null)}
465         ;
466
467 assert~withelse {-> expr}
468         = {else} kwassert assertid? expr_final_withelse kwelse stmtso~withelse {-> New expr.assert(kwassert, assertid.id, expr_final_withelse.expr, stmtso~withelse.expr)}
469 !withelse| {noelse} kwassert assertid? expr_final {-> New expr.assert(kwassert, assertid.id, expr_final.expr, Null)}
470         ;
471 assertid {-> id}
472         = id column {-> id};
473
474 /* EXPRESSIONS ***************************************************************/
475 expr_final~nopar~withelse~nobra {-> expr}
476         = expr~nopar~nobra {-> expr~nopar~nobra.expr}
477         | {closure_call} recv~nobra id args closure_defs~withelse {-> New expr.call(recv~nobra.expr, id, args.exprs, [closure_defs~withelse.closure_def])}
478 !nobra!nopar    | {closure_bra} expr_atom braargs closure_defs~withelse {-> New expr.bra(expr_atom.expr, braargs.exprs, [closure_defs~withelse.closure_def])}
479         ;
480
481 expr~nopar~nobra~nois {-> expr}
482         = expr_and~nopar~nobra~nois {-> expr_and~nopar~nobra~nois.expr}
483         | {ifexpr} kwif [n1]:no expr [n2]:no kwthen [n3]:no [then]:expr [n4]:no kwelse [n5]:no [else]:expr~nopar~nobra~nois {-> New expr.ifexpr(kwif, expr, kwthen, then, kwelse, else.expr)}
484         ;
485
486 expr_and~nopar~nobra~nois {-> expr}
487         = expr_not~nopar~nobra~nois {-> expr_not~nopar~nobra~nois.expr}
488         | {:or} expr_and~nopar~nobra~nois :kwor :no expr_not~nopar~nobra~nois
489         | {:and} expr_and~nopar~nobra~nois :kwand :no expr_not~nopar~nobra~nois
490         | {:or_else} expr_and~nopar~nobra~nois :kwor :kwelse :no expr_not~nopar~nobra~nois
491         ;
492
493 expr_not~nopar~nobra~nois {-> expr}
494         = expr_eq~nopar~nobra~nois {-> expr_eq~nopar~nobra~nois.expr}
495         | {not} kwnot no expr_not~nopar~nobra~nois {-> New expr.not(kwnot, expr_not~nopar~nobra~nois.expr)}
496         ;
497
498 expr_eq~nopar~nobra~nois {-> expr}
499         = expr_add~nopar~nobra {-> expr_add~nopar~nobra.expr}
500         | {:eq} expr_add~nopar~nobra :eq :no [expr2]:expr_add~nopar~nobra
501 !nois   | {ee} [expr]:expr_add~nopar~nobra kwis no [expr2]:expr_add~nopar~nobra {-> New expr.ee(expr.expr, expr2.expr)}
502         | {:ne} expr_add~nopar~nobra :ne :no [expr2]:expr_add~nopar~nobra
503         | {:lt} expr_add~nopar~nobra :lt :no [expr2]:expr_add~nopar~nobra
504         | {:le} expr_add~nopar~nobra :le :no [expr2]:expr_add~nopar~nobra
505         | {:ll} expr_eq~nopar~nobra~nois :ll :no [expr2]:expr_add~nopar~nobra
506         | {:gt} expr_add~nopar~nobra :gt :no [expr2]:expr_add~nopar~nobra
507         | {:ge} expr_add~nopar~nobra :ge :no [expr2]:expr_add~nopar~nobra
508         | {:gg} expr_eq~nopar~nobra~nois :gg :no [expr2]:expr_add~nopar~nobra
509         | {:starship} expr_add~nopar~nobra :starship :no [expr2]:expr_add~nopar~nobra
510         | {:isa} expr_add~nopar~nobra :kwisa :no type~nobra
511         ;
512
513 expr_add~nopar~nobra {-> expr}
514         =  expr_mul~nopar~nobra {-> expr_mul~nopar~nobra.expr}
515         | {:plus} expr_add~nopar~nobra :plus :no [expr2]:expr_mul~nopar~nobra
516         | {:minus} expr_add~nopar~nobra :minus :no [expr2]:expr_mul~nopar~nobra
517         ;
518
519 expr_mul~nopar~nobra {-> expr}
520         = expr_minus~nopar~nobra {-> expr_minus~nopar~nobra.expr}
521         | {:star} expr_mul~nopar~nobra :star :no [expr2]:expr_minus~nopar~nobra
522         | {:slash} expr_mul~nopar~nobra :slash :no [expr2]:expr_minus~nopar~nobra
523         | {:percent} expr_mul~nopar~nobra :percent :no [expr2]:expr_minus~nopar~nobra
524         ;
525
526 expr_minus~nopar~nobra {-> expr}
527         = expr_new~nopar~nobra {-> expr_new~nopar~nobra.expr}
528         | {:uminus} minus :no expr_minus~nopar~nobra
529         | {:once} kwonce :no expr_minus~nopar~nobra
530         ;
531
532 expr_new~nopar~nobra {-> expr}
533         = expr_atom~nopar~nobra {-> expr_atom~nopar~nobra.expr}
534         | {new} kwnew no type~nobra_nopar args {-> New expr.new(kwnew, type~nobra_nopar.type, Null, args.exprs)}
535         | {isset_attr} kwisset recv~nopar~nobra attrid {-> New expr.isset_attr(kwisset, recv~nopar~nobra.expr, attrid)}
536         ;
537
538 expr_atom~nopar~nobra {-> expr}
539         = {attr} recv~nopar~nobra attrid {-> New expr.attr(recv~nopar~nobra.expr, attrid)}
540         | {call} recv~nopar~nobra id args {-> New expr.call(recv~nopar~nobra.expr, id, args.exprs, [])}
541         | {super} qualified? kwsuper args {-> New expr.super(qualified, kwsuper, args.exprs)}
542         | {init} recv~nopar~nobra kwinit args {-> New expr.init(recv~nopar~nobra.expr, kwinit, args.exprs)}
543 !nobra  | {bra} expr_atom~nopar braargs {-> New expr.bra(expr_atom~nopar.expr, braargs.exprs, [])}
544         | {new} kwnew no type~nobra_nopar dot [n2]:no id args {-> New expr.new(kwnew, type~nobra_nopar.type, id, args.exprs)}
545 // !nopar to unambiguise 'foo[5].bar' between '(foo[5]).bar' and 'foo([5].bar),
546 !nobra!nopar    | {range} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no cbra annotations? {-> New expr.crange(obra, expr, expr2.expr, cbra, annotations)}
547 !nobra!nopar    | {orange} obra no expr [n2]:no dotdot [n3]:no [expr2]:expr_nobra [n4]:no [cbra]:obra annotations? {-> New expr.orange(obra, expr, expr2.expr, cbra, annotations)}
548 !nobra!nopar    | {array} braargs annotations? {-> New expr.array(braargs.exprs, annotations)}
549         | {self} kwself annotations? {-> New expr.self(kwself, annotations)}
550         | {true} kwtrue annotations? {-> New expr.true(kwtrue, annotations)}
551         | {false} kwfalse annotations? {-> New expr.false(kwfalse, annotations)}
552         | {null} kwnull annotations? {-> New expr.null(kwnull, annotations)}
553         | {int} number annotations? {-> New expr.int(number, annotations)}
554         | {float} float annotations? {-> New expr.float(float, annotations)}
555         | {char} char annotations? {-> New expr.char(char, annotations)}
556         | {string} string annotations? {-> New expr.string(string, annotations)}
557         | {superstring} superstring  {-> superstring.expr}
558 !nopar  | {par} opar expr cpar annotations? {-> New expr.par(opar, expr, cpar, annotations)}
559         | {as_cast} expr_atom~nopar~nobra dot no kwas [n2]:no opar [n3]:no type [n4]:no cpar {-> New expr.as_cast(expr_atom~nopar~nobra.expr, kwas, opar, type, cpar)}
560         | {as_notnull} expr_atom~nopar~nobra dot no kwas [n2]:no opar [n3]:no kwnot [n4]:no kwnull [n5]:no cpar {-> New expr.as_notnull(expr_atom~nopar~nobra.expr, kwas, opar, kwnot, kwnull, cpar)}
561         ;
562
563 superstring {-> expr} 
564         = superstring_start superstring_middle* superstring_end annotations? {-> New expr.superstring([superstring_start.expr, superstring_middle.expr, superstring_end.expr], annotations)};
565 superstring_start {-> expr*}
566         = start_string_p no expr [n2]:no {-> [start_string_p.expr, expr]};
567 start_string_p {-> expr}
568         = start_string {-> New expr.start_string(start_string)};
569 superstring_middle {-> expr*}
570         = mid_string_p no expr [n2]:no {-> [mid_string_p.expr, expr]};
571 mid_string_p {-> expr}
572         = mid_string {-> New expr.mid_string(mid_string)};
573 superstring_end {-> expr}
574         = end_string {-> New expr.end_string(end_string)};
575
576 /* ANNOTATIONS *******************************************************************/
577
578 annotations~nopar {-> annotations}
579         = {one} at one_annotation~nopar {-> New annotations(at, Null, [one_annotation~nopar.annotation], Null)}
580         | {many} at opar annotation_list cpar {-> New annotations(at, opar, [annotation_list.annotation], cpar)}
581         ;
582
583 one_annotation~nopar {-> annotation}
584         = {alone} atid annotations~nopar? {-> New annotation(atid, Null, [], Null, annotations~nopar.annotations)}
585 // !nopar to unambiguise 'new T@foo(bar)' between 'new T@(foo(bar))' and 'new (T@foo)(bar)'
586 !nopar  | {args} atid opar at_args cpar annotations~nopar? {-> New annotation(atid, opar, [at_args.at_arg], cpar, annotations~nopar.annotations)}
587         ;
588
589 many_annotations {-> annotations}
590         = {many} annotation_list {-> New annotations(Null, Null, [annotation_list.annotation], Null)}
591         ;
592
593 annotation_list {-> annotation*}
594         = {many} one_annotation annotations_tail* {-> [one_annotation.annotation, annotations_tail.annotation] }
595         ;
596
597 line_annotations~forclass {-> annotations}
598         = line_annotation~forclass+ {-> New annotations(Null, Null, [line_annotation~forclass.annotation], Null) }
599         ;
600 line_annotation~forclass {-> annotation}
601         = [doc]:no atid~forclass annotations? n1 {-> New annotation(atid~forclass.atid, Null, [], Null, annotations)}
602         | {args} [doc]:no atid~forclass opar at_args cpar annotations? n1 {-> New annotation(atid~forclass.atid, opar, [at_args.at_arg], cpar, annotations)}
603         | {nopar} [doc]:no atid~forclass at_args_nopar n1 {-> New annotation(atid~forclass.atid, Null, [at_args_nopar.at_arg], Null, Null)}
604         ;
605
606 annotations_tail {-> annotation}
607         = comma no one_annotation {-> one_annotation.annotation}
608         ;
609
610 at_args~nopar {-> at_arg* }
611         = {many} at_arg~nopar at_args_tail* {-> [at_arg~nopar.at_arg, at_args_tail.at_arg]}
612         ;
613
614 at_args_tail {-> at_arg}
615         = comma no at_arg {-> at_arg}
616         ;
617
618 at_arg~nopar {-> at_arg}
619         = {type} type {-> New at_arg.type(type)}
620         | {expr} expr~nopar {-> New at_arg.expr(expr~nopar.expr)}
621         | {stmt} stmt_noexpr~nopar {-> New at_arg.expr(stmt_noexpr~nopar.expr)}
622 !nopar  | {at} annotations {-> New at_arg.at(annotations.annotations)}
623         ;
624
625 atid~forclass {-> atid}
626         = {id} id {-> New atid.id(id)}
627 //!forclass     | {kwextern} kwextern {-> New atid.kwextern(kwextern)}
628 //!forclass     | {kwintern} kwintern {-> New atid.kwintern(kwintern)}
629 !forclass       | {kwreadable} kwreadable {-> New atid.kwreadable(kwreadable)}
630 !forclass       | {kwwritable} kwwritable {-> New atid.kwwritable(kwwritable)}
631         | {kwimport} kwimport {-> New atid.kwimport(kwimport)}
632         ;
633
634 /* MISC **********************************************************************/
635
636 recv~nopar~nobra {-> expr}
637         = expr_atom~nopar~nobra dot no {-> expr_atom~nopar~nobra.expr}
638         | {implicit} {-> New expr.implicit_self()}
639         ;
640
641 args {-> exprs}
642         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
643         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
644         | {empty} {-> New exprs.list([])}
645         ;
646 args_nopar {-> exprs}
647         = opar no expr_list cpar {-> New exprs.par(opar, [expr_list.expr], cpar) }
648         | {onearg} expr_nopar {-> New exprs.list([expr_nopar.expr])}
649         | {emptypar} opar no cpar {-> New exprs.par(opar, [], cpar) }
650         | {empty} {-> New exprs.list([])}
651         ;
652 braargs {-> exprs}
653         = obra no expr_list cbra {-> New exprs.bra(obra, [expr_list.expr], cbra)};
654 expr_list {-> expr*}
655         = expr [n2]:no expr_tail* {-> [expr, expr_tail.expr]};
656 expr_tail {-> expr} 
657         = comma no expr [n2]:no {-> expr};
658 idlist {-> id*}
659         = opar idlist_nopar cpar {-> [idlist_nopar.id]}
660         | {nopar} idlist_nopar {-> [idlist_nopar.id]}
661         ;
662 idlist_nopar {-> id*}
663         = {single} id {-> [id]}
664         | {more} idlist_nopar no comma [n2]:no id {-> [idlist_nopar.id, id]}
665         ;
666
667 module_name {-> module_name}
668         = {mod} modquad* id {-> New module_name(Null, [modquad.id], id)}
669         | {root} quad no modquad* id {-> New module_name(quad, [modquad.id], id)}
670         ;
671
672 qualified 
673         = {cla} modquad* classquad {-> New qualified([modquad.id], classquad.classid)}
674         | {mod} modquad+ {-> New qualified([modquad.id], Null)}
675         ; 
676 modquad {-> id}
677         = id quad no {-> id};
678 classquad {-> classid} 
679         = classid quad no {-> classid};
680
681 n1      = {a} comment | {b} eol;
682 n {-> doc?}
683         = {a} n2? comment+ {-> New doc([comment])}
684         | {b} n2 {-> Null}
685         ;
686 no {-> doc?}
687         = {empty} {-> Null}
688         | n {-> n.doc}
689         ;
690
691 n2
692         = {a} n2? comment+ eol+
693         | {b} eol+
694         ;
695
696 /*****************************************************************************/
697 Abstract Syntax Tree
698 /*****************************************************************************/
699
700 module  = moduledecl? [imports]:import* [extern_code_blocks]:extern_code_block* [classdefs]:classdef*;
701
702 moduledecl
703         = doc? kwmodule [name]:module_name annotations?;
704
705 import  = {std} visibility kwimport [name]:module_name annotations?
706         | {no} visibility kwimport kwend
707         ;
708
709 visibility
710         = {public}
711         | {private} kwprivate
712         | {protected} kwprotected
713         | {intrude} kwintrude
714         ;
715
716 classdef= {std} doc? kwredef? visibility classkind [id]:classid? [formaldefs]:formaldef* annotations? extern_code_block? [superclasses]:superclass* [propdefs]:propdef* kwend
717         | {top} [propdefs]:propdef*
718         | {main} [propdefs]:propdef*
719         ;
720 classkind
721         = {concrete} kwclass
722         | {abstract} kwabstract kwclass
723         | {interface} kwinterface
724         | {enum} kwenum
725         | {extern} kwextern kwclass?
726         ;
727 formaldef = [id]:classid type? annotations?;
728 superclass = kwsuper type annotations?;
729
730
731 propdef = {attr} doc? [readable]:able? [writable]:able? kwredef? visibility kwvar [id]:attrid? [id2]:id? type? annotations? expr?
732         | {meth} doc? kwredef? visibility methid signature
733         | {deferred_meth} doc? kwredef? visibility kwmeth methid signature annotations?
734         | {intern_meth} doc? kwredef? visibility kwmeth methid signature
735         | {extern_meth} doc? kwredef? visibility kwmeth methid signature [extern]:string? extern_calls? extern_code_block?
736         | {concrete_meth} doc? kwredef? visibility kwmeth methid signature annotations? [block]:expr?
737         | {concrete_init} doc? kwredef? visibility kwinit methid? signature annotations? [block]:expr?
738         //| {concrete_new} doc? kwredef? visibility kwnew methid? signature [block]:expr?
739         | {extern_init} doc? kwredef? visibility kwnew methid? signature [extern]:string? extern_calls? extern_code_block?
740         | {main_meth} kwredef? [block]:expr?
741         | {type} doc? kwredef? visibility kwtype [id]:classid type annotations?
742         ;
743
744 able    = {read} kwredef? kwreadable
745         | {write} kwredef? visibility? kwwritable
746         ;
747
748 methid = {id} id | {plus} plus | {minus} minus | {star} star | {slash} slash | {percent} percent | {eq} eq | {ne} ne | {le} le | {ge} ge | {lt} lt | {gt} gt |  {ll} ll | {gg} gg | {bra} obra cbra | {starship} starship | {assign} id assign | {braassign} obra cbra assign;
749
750 signature = opar? [params]:param* cpar? type? [closure_decls]:closure_decl*;
751
752 param   = id type? dotdotdot? annotations?
753         ;
754
755 closure_decl = kwbreak? bang id signature expr?
756         ;
757
758 type    = kwnullable? [id]:classid [types]:type* annotations?;
759
760 label = kwlabel id;
761
762 expr    = {block} expr* kwend? 
763         | {vardecl} kwvar id type? assign? expr? annotations?
764         | {return} kwreturn? expr?
765         | {break} kwbreak label? expr?
766         | {abort} kwabort
767         | {continue} kwcontinue? label? expr?
768         | {do} kwdo [block]:expr? label?
769         | {if} kwif expr [then]:expr? [else]:expr? 
770         | {ifexpr} kwif expr kwthen [then]:expr kwelse [else]:expr
771         | {while} kwwhile expr kwdo [block]:expr? label?
772         | {loop} kwloop [block]:expr? label?
773         | {for} kwfor [ids]:id* expr kwdo [block]:expr? label?
774         | {assert} kwassert id? expr [else]:expr?
775         | {once} kwonce expr 
776         | {send} expr 
777         | {binop} expr [expr2]:expr 
778         | {or} expr [expr2]:expr 
779         | {and} expr [expr2]:expr 
780         | {or_else} expr [expr2]:expr
781         | {not} kwnot expr 
782         | {eq} expr [expr2]:expr 
783         | {ee} expr [expr2]:expr 
784         | {ne} expr [expr2]:expr 
785         | {lt} expr [expr2]:expr 
786         | {le} expr [expr2]:expr 
787         | {ll} expr [expr2]:expr
788         | {gt} expr [expr2]:expr 
789         | {ge} expr [expr2]:expr 
790         | {gg} expr [expr2]:expr
791         | {isa} expr type 
792         | {plus} expr [expr2]:expr 
793         | {minus} expr [expr2]:expr 
794         | {starship} expr [expr2]:expr 
795         | {star} expr [expr2]:expr 
796         | {slash} expr [expr2]:expr 
797         | {percent} expr [expr2]:expr 
798         | {uminus} minus expr 
799         | {new} kwnew type id? [args]:exprs
800         | {attr} expr [id]:attrid 
801         | {attr_assign} expr [id]:attrid assign [value]:expr 
802         | {attr_reassign} expr [id]:attrid assign_op [value]:expr 
803         | {call} expr id [args]:exprs [closure_defs]:closure_def*
804         | {call_assign} expr id [args]:exprs assign [value]:expr 
805         | {call_reassign} expr id [args]:exprs assign_op [value]:expr 
806         | {super} qualified? kwsuper [args]:exprs
807         | {init} expr kwinit [args]:exprs 
808         | {bra} expr [args]:exprs [closure_defs]:closure_def* 
809         | {bra_assign} expr [args]:exprs assign [value]:expr 
810         | {bra_reassign} expr [args]:exprs assign_op [value]:expr 
811         | {closure_call} id [args]:exprs [closure_defs]:closure_def*
812         | {var} id
813         | {var_assign} id assign [value]:expr 
814         | {var_reassign} id assign_op [value]:expr 
815         | {range} expr [expr2]:expr annotations?
816         | {crange} obra expr [expr2]:expr cbra annotations?
817         | {orange} obra expr [expr2]:expr [cbra]:obra annotations?
818         | {array} [exprs]:exprs annotations?
819         | {self} kwself annotations?
820         | {implicit_self} 
821         | {true} kwtrue annotations?
822         | {false} kwfalse annotations?
823         | {null} kwnull annotations?
824         | {int} number annotations?
825         | {float} float annotations?
826         | {char} char annotations?
827         | {string} string annotations?
828         | {start_string} [string]:start_string 
829         | {mid_string} [string]:mid_string 
830         | {end_string} [string]:end_string 
831         | {superstring} [exprs]:expr* annotations?
832         | {par} opar expr cpar annotations?
833         | {as_cast} expr kwas opar type cpar
834         | {as_notnull} expr kwas opar kwnot kwnull cpar
835         | {isset_attr} kwisset expr [id]:attrid
836         | {debug_type} kwdebug kwtype expr type
837         ;
838 exprs
839         = {list} [exprs]:expr*
840         | {par} opar [exprs]:expr* cpar
841         | {bra} obra [exprs]:expr* cbra
842         ;
843 assign_op
844         = {plus} pluseq
845         | {minus} minuseq
846         ;
847
848 closure_def
849         = bang [id]:closure_id [ids]:id* kwdo? expr? label?
850         ;
851 closure_id
852         = {simple} id
853         | {break} kwbreak
854         ;
855
856 module_name = quad? [path]:id* id;
857 extern_calls = kwimport [extern_calls]:extern_call*
858         ;
859 extern_call =
860         | {super} kwsuper
861         | {local_prop} methid
862         | {full_prop} classid quad? methid
863         | {init_prop} classid
864         | {cast_as} [from_type]:type kwas [to_type]:type
865         | {as_nullable} type kwas kwnullable
866         | {as_not_nullable} type kwas kwnot kwnullable
867         ;
868 in_language = kwin string;
869 extern_code_block = in_language? extern_code_segment;
870
871 qualified = id* classid? ;
872
873 doc = comment+;
874
875 annotations = at? opar? [items]:annotation* cpar?;
876
877 annotation = atid opar? [args]:at_arg* cpar? annotations?;
878
879 at_arg
880         = {type} type
881         | {expr} expr
882         | {at} annotations
883         ;
884 atid = {id} id | {kwextern} [id]:kwextern | {kwintern} [id]:kwintern | {kwreadable} [id]:kwreadable | {kwwritable} [id]:kwwritable | {kwimport} [id]:kwimport;
885
886 /*****************************************************************************/
887