Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes, improvements, optimization & refactoring before parser generation #288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Include explicit disjunction in the union node
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
commit bf3580029785fcb8cdf909d54ba5cd6c16878ec1
2 changes: 1 addition & 1 deletion ebnf.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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