Skip to content

Commit bb1845e

Browse files
committed
Minor fixes
1 parent 4d87f9c commit bb1845e

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

grammar.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,7 @@ func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness in
264264

265265
// Return only the appropriate results.
266266
outpaths := []*Path{}
267-
for i := 1; i <= len(ppaths); i++ {
268-
// Don't add under minimum repetition
269-
if i < rep.Min {
270-
continue
271-
}
267+
for i := max(1, rep.Min); i <= len(ppaths); i++ {
272268
outpaths = append(outpaths, ppaths[i-1]...)
273269
}
274270
// If the empty solution if possible, keep track of it
@@ -754,19 +750,7 @@ func lexABNF(input []byte, path *Path) (any, error) {
754750
}, nil
755751
}
756752

757-
if len(path.Subpaths) == 1 && path.MatchRule == "" {
758-
return lexABNF(input, path.Subpaths[0])
759-
}
760-
761-
from := path.Start - 10
762-
if from < 0 {
763-
from = 0
764-
}
765-
to := path.End + 10
766-
if to > len(input) {
767-
to = len(input)
768-
}
769-
panic(fmt.Sprintf("unhandlable path from %d to %d: \"%s\" ; sneek peak around \"%s\"", path.Start, path.End, input[path.Start:path.End], input[from:to]))
753+
panic(fmt.Sprintf("unhandlable path from %d to %d: \"%s\" ; sneek peak around \"%s\"", path.Start, path.End, input[path.Start:path.End], input[max(path.Start-10, 0):min(path.End+10, 0)]))
770754
}
771755

772756
// SemvalABNF proceed to semantic validations of an ABNF grammar.

grammar_fuzz_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,35 @@ func FuzzParseABNF(f *testing.F) {
1818
if path != nil {
1919
t.Fatal("Expected no path when error")
2020
}
21+
if err, ok := err.(*goabnf.ErrMultipleSolutionsFound); ok {
22+
t.Fatalf("For input %s, got error %s", input, err)
23+
}
2124
} else {
2225
if path == nil {
2326
t.Fatal("Expected a path when no error")
2427
}
2528
}
2629
})
2730
}
31+
32+
func FuzzParseABNF_Generate(f *testing.F) {
33+
f.Fuzz(func(t *testing.T, seed int64) {
34+
input, _ := goabnf.ABNF.Generate(seed, "rulelist")
35+
36+
g, err := goabnf.ParseABNF(input, goabnf.WithValidation(false))
37+
38+
if err != nil {
39+
if g != nil {
40+
t.Fatal("Expected no path when error")
41+
}
42+
if err, ok := err.(*goabnf.ErrMultipleSolutionsFound); ok {
43+
t.Fatalf("For input %s, got error %s", input, err)
44+
}
45+
return
46+
} else {
47+
if g == nil {
48+
t.Fatal("Expected a path when no error")
49+
}
50+
}
51+
})
52+
}

0 commit comments

Comments
 (0)