Skip to content

Commit

Permalink
Include explicit disjunction in the union node
Browse files Browse the repository at this point in the history
The generated parser would be the second place that would construct a
temporary disjunction in the implementation, I think it might as well
contain the disjunction directly.
  • Loading branch information
petee-d committed Dec 5, 2022
1 parent bb4e748 commit bf35800
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ebnf.go
Expand Up @@ -62,7 +62,7 @@ func buildEBNF(root bool, n node, seen map[node]bool, p *ebnfp, outp *[]*ebnfp)
p = &ebnfp{name: name}
*outp = append(*outp, p)
seen[n] = true
for i, next := range n.nodeMembers {
for i, next := range n.disjunction.nodes {
if i > 0 {
p.out += " | "
}
Expand Down
4 changes: 2 additions & 2 deletions grammar.go
Expand Up @@ -30,7 +30,7 @@ func (g *generatorContext) addUnionDefs(defs []unionDef) error {
}
unionNode := &union{
unionDef: def,
nodeMembers: make([]node, 0, len(def.members)),
disjunction: disjunction{nodes: make([]node, 0, len(def.members))},
}
g.typeNodes[def.typ], unionNodes[i] = unionNode, unionNode
}
Expand All @@ -41,7 +41,7 @@ func (g *generatorContext) addUnionDefs(defs []unionDef) error {
if err != nil {
return err
}
unionNode.nodeMembers = append(unionNode.nodeMembers, memberNode)
unionNode.disjunction.nodes = append(unionNode.disjunction.nodes, memberNode)
}
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions nodes.go
Expand Up @@ -97,16 +97,15 @@ func (c *custom) Parse(ctx *parseContext, parent reflect.Value) (out []reflect.V
// @@ (for a union)
type union struct {
unionDef
nodeMembers []node
disjunction disjunction
}

func (u *union) String() string { return ebnf(u) }
func (u *union) GoString() string { return u.typ.Name() }

func (u *union) Parse(ctx *parseContext, parent reflect.Value) (out []reflect.Value, err error) {
defer ctx.printTrace(u)()
temp := disjunction{u.nodeMembers}
vals, err := temp.Parse(ctx, parent)
vals, err := u.disjunction.Parse(ctx, parent)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion visit.go
Expand Up @@ -20,7 +20,7 @@ func visit(n node, visitor func(n node, next func() error) error) error {
case *custom:
return nil
case *union:
for _, member := range n.nodeMembers {
for _, member := range n.disjunction.nodes {
if err := visit(member, visitor); err != nil {
return err
}
Expand Down

0 comments on commit bf35800

Please sign in to comment.