@@ -29,8 +29,8 @@ class Gram
29
29
do
30
30
var res = new FlatBuffer
31
31
for p in prods do
32
- if p . spe != null then
33
- res .append ("{p .name } \{-> {p .spe . name } \}=\n " )
32
+ if not p . is_ast then
33
+ res .append ("{p .name } \{-> {p .ast_type } \}=\n " )
34
34
else
35
35
res .append ("{p .name } =\n " )
36
36
end
@@ -339,12 +339,14 @@ class Gram
339
339
var cname = "_plus {plusizes .length } "
340
340
var prod = new Production ("{e } + " )
341
341
prod .cname = cname
342
- prod .acname = " Nodes[ { e . acname } ] "
342
+ prod .ast_type = e . ast_type . as_array
343
343
prods .add (prod )
344
344
var alt1 = prod .new_alt ("{cname } _one " , e )
345
+ alt1 .trans = true
345
346
alt1 .codes = [new CodeNewNodes (alt1 ), new CodeGet (0 ), new CodeAdd : Code ]
346
347
var alt2 = prod .new_alt ("{cname } _more " , prod , e )
347
348
alt2 .codes = [new CodeGet (0 ), new CodeGet (1 ), new CodeAdd : Code ]
349
+ alt2 .trans = true
348
350
plusizes [e ] = prod
349
351
return prod
350
352
end
@@ -370,12 +372,14 @@ class Gram
370
372
var cname = "_opt {quesizes .length } "
371
373
var prod = new Production ("{e } ? " )
372
374
prod .cname = cname
373
- prod .acname = " nullable { e . acname } "
375
+ prod .ast_type = e . ast_type . as_nullable
374
376
prods .add (prod )
375
377
var a1 = prod .new_alt ("{cname } _one " , e )
378
+ a1 .trans = true
376
379
a1 .codes = [new CodeGet (0 )]
377
380
var a0 = prod .new_alt0 ("{cname } _none " )
378
381
a0 .codes = [new CodeNull ]
382
+ a0 .trans = true
379
383
quesizes [e ] = prod
380
384
return prod
381
385
end
@@ -409,9 +413,9 @@ class Gram
409
413
410
414
for t in tokens do
411
415
if t .name == "Eof " then
412
- g .add "redef class { t . acname } "
416
+ g .add "redef class NEof "
413
417
else
414
- g .add "class {t .acname } "
418
+ g .add "class {t .ast_type . to_nit } "
415
419
g .add "\tsuper NToken "
416
420
end
417
421
g .add "\tredef fun node_name do return \" {t .name .escape_to_nit } \" "
@@ -421,10 +425,9 @@ class Gram
421
425
var ps = prods .to_a
422
426
ps .add_all (ast_prods )
423
427
for p in ps do
424
- if p .spe == null and not p .altone then
425
- if p .name .has_suffix ("? " ) or p .name .has_suffix ("+ " ) then continue
428
+ if p .is_ast and not p .altone then
426
429
g .add "# Production {p } "
427
- g .add "class {p .acname } "
430
+ g .add "class {p .ast_type . to_nit } "
428
431
g .add "\tsuper NProd "
429
432
g .add "\tredef fun node_name do return \" {p .name .escape_to_nit } \" "
430
433
g .add "end "
@@ -439,14 +442,16 @@ class Gram
439
442
if p .altone then
440
443
g .add "\tsuper NProd "
441
444
else
442
- g .add "\tsuper {p .acname } "
445
+ g .add "\tsuper {p .ast_type . to_nit } "
443
446
end
444
447
g .add "\tredef fun node_name do return \" {a .name .escape_to_nit } \" "
445
448
var initarg = new Array [String ]
446
449
for i in [0 ..a .elems .length [ do
447
450
g .add "\t# Children {i } : {a .elems [i ]} "
448
- g .add "\tvar n_ {a .elemname (i )} : {a .elems [i ].acname } "
449
- initarg .add ("n_ {a .elemname (i )} : {a .elems [i ].acname } " )
451
+ var t = a .elems [i ].ast_type .to_nit
452
+ var n = a .elemname (i )
453
+ g .add "\tvar n_ {n } : {t } "
454
+ initarg .add ("n_ {n } : {t } " )
450
455
end
451
456
g .add "\tredef fun number_of_children do return {a .elems .length } "
452
457
g .add "\tredef fun child(i) do "
@@ -474,21 +479,24 @@ class Production
474
479
# Is self the accept production
475
480
var accept = false
476
481
477
- # Is self transformed to a other production for the AST
478
- # FIXME: cleanup AST
479
- var spe : nullable Production = null is writable
482
+ # Is self present as a distinct AST class
483
+ fun is_ast : Bool do return parent_production == self
484
+
485
+ # The production AST where non transformed alternative are attached to.
486
+ # If no such production exists, return null
487
+ fun parent_production : nullable Production
488
+ do
489
+ var res = ast_type .element
490
+ if not res isa Production then return null # it is a token
491
+ if ast_type .is_plain then return res
492
+ return null # is is nullable and/or array
493
+ end
494
+
480
495
481
496
# Is self contains only a single alternative (then no need for a abstract production class in the AST)
482
497
# FIXME cleanup AST
483
498
var altone = false is writable
484
499
485
- # The cname of the class in the AST
486
- # FIXME: cleanup AST
487
- redef fun acname do
488
- if spe != null then return spe .acname
489
- return super
490
- end
491
-
492
500
# Is the production nullable
493
501
var is_nullable = false
494
502
@@ -553,6 +561,64 @@ class Production
553
561
end
554
562
end
555
563
564
+ # static types of AST elements (AST productions and tokens)
565
+ class ASTType
566
+ var element : Element
567
+ var is_nullable : Bool = false
568
+ var is_array : Bool = false
569
+
570
+ fun is_plain : Bool
571
+ do
572
+ return not is_nullable and not is_array
573
+ end
574
+
575
+ redef fun to_s
576
+ do
577
+ var res = element .name
578
+ if is_nullable then
579
+ if is_array then
580
+ return "{res } * "
581
+ else
582
+ return "{res } ? "
583
+ end
584
+ else
585
+ if is_array then
586
+ return "{res } + "
587
+ else
588
+ return res
589
+ end
590
+ end
591
+ end
592
+
593
+ fun as_nullable : ASTType
594
+ do
595
+ if is_nullable then return self
596
+ var res = new ASTType (element )
597
+ res .is_nullable = true
598
+ res .is_array = is_array
599
+ return res
600
+ end
601
+ fun as_array : ASTType
602
+ do
603
+ if is_array then return self
604
+ var res = new ASTType (element )
605
+ res .is_nullable = is_nullable
606
+ res .is_array = true
607
+ return res
608
+ end
609
+ fun to_nit : String
610
+ do
611
+ var res = "N " + element .cname
612
+ if is_array then
613
+ res = "Nodes[ {res } ] "
614
+ end
615
+ if is_nullable then
616
+ res = "nullable {res } "
617
+ end
618
+ return res
619
+ end
620
+ end
621
+
556
622
# An alternative of a production
557
623
class Alternative
558
624
# The production
@@ -664,31 +730,18 @@ abstract class Element
664
730
var name : String
665
731
redef fun to_s do return name
666
732
733
+ var ast_type : ASTType = new ASTType (self ) is writable
734
+
667
735
# An example of a string
668
736
fun sample_to_s : String is abstract
669
737
670
- private var acname_cache : nullable String = null
671
-
672
738
# The mangled name of the element
673
739
var cname : String is noinit , writable
674
740
675
741
init
676
742
do
677
743
cname = name
678
744
end
679
-
680
- # The name of the class in the AST
681
- fun acname : String do
682
- var res = acname_cache
683
- if res == null then
684
- res = "N {cname } "
685
- acname_cache = res
686
- end
687
- return res
688
- end
689
-
690
- # The name of the class in the AST
691
- fun acname =(s : String ) do acname_cache = s
692
745
end
693
746
694
747
# A terminal element
0 commit comments