Skip to content

Commit

Permalink
nitcc: add groupize to create anonymous productions
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Jun 23, 2024
1 parent 3aae1d0 commit 8c08ada
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions contrib/nitcc/src/nitcc.sablecc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ elem =
{star:} elem '*' |
{ques:} elem '?' |
{plus:} elem '+' |
{group:} '(' elem+ ')' |
{empty:} 'Empty' ;

priority =
Expand Down
27 changes: 27 additions & 0 deletions contrib/nitcc/src/nitcc_semantic.nit
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ private class CheckNameVisitor
return prod
end

# Pool for anoymous grouped production (reuse them!)
var groupizes = new HashMap[Array[Element], Production]

# Create an anonymous production that groups some elements.
# Note: an anonymous production is always returned, even if `es` is empty or single.
fun groupize(es: Array[Element]): Production
do
if groupizes.has_key(es) then return groupizes[es]
var name = "_group{groupizes.length}"
var prod = new Production(name)
v1.gram.prods.add(prod)
var a1 = prod.new_alt2("{name}_single", es)
groupizes[es] = prod
return prod
end

# The current nexpr, used to track dependency on named expressions (see `Nexpr::precs`)
var nexpr: nullable Nexpr = null

Expand Down Expand Up @@ -703,6 +719,17 @@ redef class Nelem_plus
end
end

redef class Nelem_group
redef fun accept_check_name_visitor(v) do
var old = v.elems
v.elems = new Array[Element]
super
var elem = v.groupize(v.elems)
v.elems = old
set_elem(v, null, elem)
end
end

redef class Ntree_part
redef fun accept_collect_prod(v) do
print "NOT YET IMPLEMENTED: `Tree` part; it is ignored"
Expand Down

0 comments on commit 8c08ada

Please sign in to comment.