Skip to content

Commit

Permalink
fix(#32): place ABNF-based root rule assumption on group in the evalu…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
pandatix committed Jul 17, 2024
1 parent 92e4a6e commit fe76257
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func Parse(input []byte, grammar *Grammar, rootRulename string) ([]*Path, error)
outPoss := []*Path{}
for _, poss := range possibilites {
if poss.End == len(input) {
pth := poss.Subpaths[0]
pth.MatchRule = rootRulename
poss.MatchRule = rootRulename
outPoss = append(outPoss, poss)
}
}
Expand Down Expand Up @@ -423,6 +422,7 @@ func lexABNF(input []byte, path *Path) (any, error) {
case abnfRulelist.Name:
mp := map[string]*Rule{}

path := path.Subpaths[0]
sub := path.Subpaths[0]
for i := 0; i < len(path.Subpaths); i++ {
// Only work on rules (i.e. skip empty lines)
Expand Down
27 changes: 27 additions & 0 deletions grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,30 @@ func Test_U_ABNFParseItself(t *testing.T) {
assert.NotNil(sol)
assert.Nil(err)
}

func Test_U_ParseRootNonGroup(t *testing.T) {
// Issue #32 use case is a root rule that does not start
// with a group as its first root alternation element.
assert := assert.New(t)

// First we build our grammar
g, err := goabnf.ParseABNF(platypusAbnf)
if !assert.Nil(err) {
return
}

// Then we consider an input, valid according to our grammar.
input := []byte("a")
p, err := goabnf.Parse(input, g, "b")
if !assert.Nil(err) {
return
}

// Then we make sure there is only 1 possibility, and all
// subpaths have the proper name.
if !assert.Len(p, 1) {
return
}
assert.Equal("b", p[0].MatchRule)
assert.Equal("a", p[0].Subpaths[0].MatchRule)
}

0 comments on commit fe76257

Please sign in to comment.