Skip to content

Commit 17eaca0

Browse files
authored
NitCC use ASTType to represent AST information #2855 from privat/nitcc_asttype
2 parents 2fcb457 + beea28f commit 17eaca0

File tree

10 files changed

+169
-81
lines changed

10 files changed

+169
-81
lines changed

contrib/nitcc/src/autom.nit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ private class DFAGenerator
10911091
add("\t\tvar t = new MyNToken\n")
10921092
add("\t\tt.text = position.extract(source)\n")
10931093
else
1094-
add("\t\tvar t = new {token.acname}\n")
1094+
add("\t\tvar t = new {token.ast_type.to_nit}\n")
10951095
var ttext = token.text
10961096
if ttext == null then
10971097
add("\t\tt.text = position.extract(source)\n")

contrib/nitcc/src/grammar.nit

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class Gram
2929
do
3030
var res = new FlatBuffer
3131
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")
3434
else
3535
res.append("{p.name} =\n")
3636
end
@@ -339,12 +339,14 @@ class Gram
339339
var cname = "_plus{plusizes.length}"
340340
var prod = new Production("{e}+")
341341
prod.cname = cname
342-
prod.acname = "Nodes[{e.acname}]"
342+
prod.ast_type = e.ast_type.as_array
343343
prods.add(prod)
344344
var alt1 = prod.new_alt("{cname}_one", e)
345+
alt1.trans = true
345346
alt1.codes = [new CodeNewNodes(alt1), new CodeGet(0), new CodeAdd: Code]
346347
var alt2 = prod.new_alt("{cname}_more", prod, e)
347348
alt2.codes = [new CodeGet(0), new CodeGet(1), new CodeAdd: Code]
349+
alt2.trans = true
348350
plusizes[e] = prod
349351
return prod
350352
end
@@ -370,12 +372,14 @@ class Gram
370372
var cname = "_opt{quesizes.length}"
371373
var prod = new Production("{e}?")
372374
prod.cname = cname
373-
prod.acname = "nullable {e.acname}"
375+
prod.ast_type = e.ast_type.as_nullable
374376
prods.add(prod)
375377
var a1 = prod.new_alt("{cname}_one", e)
378+
a1.trans = true
376379
a1.codes = [new CodeGet(0)]
377380
var a0 = prod.new_alt0("{cname}_none")
378381
a0.codes = [new CodeNull]
382+
a0.trans = true
379383
quesizes[e] = prod
380384
return prod
381385
end
@@ -409,9 +413,9 @@ class Gram
409413

410414
for t in tokens do
411415
if t.name == "Eof" then
412-
g.add "redef class {t.acname}"
416+
g.add "redef class NEof"
413417
else
414-
g.add "class {t.acname}"
418+
g.add "class {t.ast_type.to_nit}"
415419
g.add "\tsuper NToken"
416420
end
417421
g.add "\tredef fun node_name do return \"{t.name.escape_to_nit}\""
@@ -421,10 +425,9 @@ class Gram
421425
var ps = prods.to_a
422426
ps.add_all(ast_prods)
423427
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
426429
g.add "# Production {p}"
427-
g.add "class {p.acname}"
430+
g.add "class {p.ast_type.to_nit}"
428431
g.add "\tsuper NProd"
429432
g.add "\tredef fun node_name do return \"{p.name.escape_to_nit}\""
430433
g.add "end"
@@ -439,14 +442,16 @@ class Gram
439442
if p.altone then
440443
g.add "\tsuper NProd"
441444
else
442-
g.add "\tsuper {p.acname}"
445+
g.add "\tsuper {p.ast_type.to_nit}"
443446
end
444447
g.add "\tredef fun node_name do return \"{a.name.escape_to_nit}\""
445448
var initarg = new Array[String]
446449
for i in [0..a.elems.length[ do
447450
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}")
450455
end
451456
g.add "\tredef fun number_of_children do return {a.elems.length}"
452457
g.add "\tredef fun child(i) do"
@@ -474,21 +479,24 @@ class Production
474479
# Is self the accept production
475480
var accept = false
476481

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+
480495

481496
# Is self contains only a single alternative (then no need for a abstract production class in the AST)
482497
# FIXME cleanup AST
483498
var altone = false is writable
484499

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-
492500
# Is the production nullable
493501
var is_nullable = false
494502

@@ -553,6 +561,64 @@ class Production
553561
end
554562
end
555563

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+
556622
# An alternative of a production
557623
class Alternative
558624
# The production
@@ -664,31 +730,18 @@ abstract class Element
664730
var name: String
665731
redef fun to_s do return name
666732

733+
var ast_type: ASTType = new ASTType(self) is writable
734+
667735
# An example of a string
668736
fun sample_to_s: String is abstract
669737

670-
private var acname_cache: nullable String = null
671-
672738
# The mangled name of the element
673739
var cname: String is noinit, writable
674740

675741
init
676742
do
677743
cname = name
678744
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
692745
end
693746

694747
# A terminal element

contrib/nitcc/src/lrautomaton.nit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ redef class Generator
381381
add "end"
382382

383383
for t in gram.tokens do
384-
add "redef class {t.acname}"
384+
add "redef class {t.ast_type.to_nit}"
385385
for s in t.shifts do
386386
if not s.need_guard then continue
387387
add "\tredef fun action_s{s.number}(parser) do"
@@ -484,7 +484,7 @@ redef class Generator
484484
add "\t\t# REDUCE {alt}"
485485
var i = alt.elems.length - 1
486486
for e in alt.elems.to_a.reversed do
487-
add "\t\tvar n{i} = parser.pop.as({e.acname})"
487+
add "\t\tvar n{i} = parser.pop.as({e.ast_type.to_nit})"
488488
i -= 1
489489
end
490490

@@ -520,7 +520,7 @@ redef class Generator
520520
st.add("p{cpt}")
521521
else if c isa CodeNewNodes then
522522
cpt += 1
523-
add "\t\tvar p{cpt} = new {c.alt.prod.acname}"
523+
add "\t\tvar p{cpt} = new {c.alt.prod.ast_type.to_nit}"
524524
st.add("p{cpt}")
525525
else if c isa CodeAdd then
526526
var a1 = st.pop

0 commit comments

Comments
 (0)