Skip to content

Commit

Permalink
nitcc: use parent_production instead of spe
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Aug 28, 2024
1 parent fa953eb commit b180b98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
7 changes: 2 additions & 5 deletions contrib/nitcc/src/grammar.nit
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Gram
do
var res = new FlatBuffer
for p in prods do
if p.spe != null then
res.append("{p.name} \{-> {p.spe.name}\}=\n")
if not p.is_ast then
res.append("{p.name} \{-> {p.ast_type}\}=\n")
else
res.append("{p.name} =\n")
end
Expand Down Expand Up @@ -492,9 +492,6 @@ class Production
return null # is is nullable and/or array
end

# Is self transformed to a other production for the AST
# FIXME: cleanup AST
var spe: nullable Production = null is writable

# Is self contains only a single alternative (then no need for a abstract production class in the AST)
# FIXME cleanup AST
Expand Down
26 changes: 14 additions & 12 deletions contrib/nitcc/src/nitcc_semantic.nit
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ redef class Nprod
# The main production will be used for the last priority class
var spe = prod
prod = new Production(name + "0")
prod.spe = spe
prod.ast_type = spe.ast_type
v.gram.prods.add(prod)
end
self.sub_prod = prod
Expand All @@ -343,8 +343,9 @@ redef class Nptrans

var node = v.v1.names[name]
if node isa Nprod then
v.prod.spe = node.prod.as(not null)
if v.prod.spe.spe != null then
var p = node.prod.as(not null)
v.prod.ast_type = p.ast_type
if not p.is_ast then
print "Cannot transform into {name}, {name} is already transformed."
exit(1)
abort
Expand All @@ -370,21 +371,23 @@ redef class Npriority
# The associated production
var prod: nullable Production

fun main_prod: nullable Production do return prod.parent_production

# The production in the with the next less priority class.
# `null` if there is no priority or if the first priority class.
var next: nullable Production

redef fun accept_collect_prod(v) do
var old = v.prod
assert old != null
var spe = old.spe
var spe = old.parent_production
assert spe != null
if is_last then
prod = spe
else
v.pricpt -= 1
prod = new Production(spe.name + "{v.pricpt}")
prod.spe = spe
prod.ast_type = spe.ast_type
v.gram.prods.add(prod.as(not null))
end
next = old
Expand Down Expand Up @@ -413,7 +416,7 @@ end

redef class Npriority_left
redef fun check_priority(v) do
var p = prod.spe or else prod
var p = main_prod
assert p != null
if v.elems.length < 2 or v.elems.first != p or v.elems.last != p then
print("Error: in a Left priority class, left and right must be the production")
Expand All @@ -426,7 +429,7 @@ end

redef class Npriority_right
redef fun check_priority(v) do
var p = prod.spe or else prod
var p = main_prod
assert p != null
if v.elems.length < 2 or v.elems.first != p or v.elems.last != p then
print("Error: in a Right priority class, left and right must be the production")
Expand All @@ -439,7 +442,7 @@ end

redef class Npriority_unary
redef fun check_priority(v) do
var p = prod.spe or else prod
var p = main_prod
assert p != null
if v.elems.length < 2 or (v.elems.first != p and v.elems.last != p) then
print("Error: in a Unary priority class, left or right must be the production")
Expand Down Expand Up @@ -473,13 +476,12 @@ redef class Nalt
if pri != null then pri.check_priority(v)

var prod = v.prod.as(not null)
var prodabs = prod.spe
if prodabs == null then prodabs = prod
var prodabs = prod.parent_production
var name = v.altname
if name == null then
if v.trans then
name = prod.name + "_" + prod.alts.length.to_s
else if prod.spe == null and prod.alts.is_empty and pri == null then
else if prod.is_ast and prod.alts.is_empty and pri == null then
name = prod.name
prod.altone = true
else
Expand Down Expand Up @@ -515,7 +517,7 @@ redef class Naltid
var name = id.text
v.altname = name
var prod = v.prod.as(not null)
var prodabs = prod.spe
var prodabs = prod.parent_production
if prodabs == null then prodabs = prod
for x in prodabs.alts do
if x.short_name == name then
Expand Down

0 comments on commit b180b98

Please sign in to comment.