Skip to content

Commit b38b222

Browse files
committed
refactor: remove option that is no longer needed thanks to *Grammar.IsLeftTerminating
1 parent e41ea54 commit b38b222

File tree

2 files changed

+16
-57
lines changed

2 files changed

+16
-57
lines changed

grammar.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ type Grammar struct {
1515
// IsValid checks there exist at least a path that completly consumes
1616
// input, hence is valide given this gramma and especially one of its
1717
// rule.
18-
func (g *Grammar) IsValid(rulename string, input []byte, opts ...ParseOption) (bool, error) {
18+
func (g *Grammar) IsValid(rulename string, input []byte) (bool, error) {
1919
lt, err := g.IsLeftTerminating(rulename)
2020
if err != nil {
2121
return false, err
2222
}
2323
if !lt {
2424
return false, fmt.Errorf("rule %s is not left terminating thus can't be validated without the risk of infinite recursion", rulename)
2525
}
26-
paths, err := Parse(input, g, rulename, opts...)
26+
paths, err := Parse(input, g, rulename)
2727
return len(paths) != 0 && err == nil, nil
2828
}
2929

@@ -101,7 +101,7 @@ func ParseABNF(input []byte, opts ...ParseABNFOption) (*Grammar, error) {
101101
// Parse input with ABNF grammar
102102
// Don't need to transmit deepness option, as we can be sure ABNF won't
103103
// recurse indefinitely.
104-
paths, err := Parse(input, ABNF, "rulelist", WithDeepnessThreshold(-1))
104+
paths, err := Parse(input, ABNF, "rulelist")
105105
if err != nil {
106106
return nil, err
107107
}
@@ -138,15 +138,7 @@ func ParseABNF(input []byte, opts ...ParseABNFOption) (*Grammar, error) {
138138
// order to look for solutions. If many are found, it raises an error.
139139
// If the input is invalid (gramatically, incomplete...) it returns
140140
// an error of type *ErrParse.
141-
func Parse(input []byte, grammar *Grammar, rootRulename string, opts ...ParseOption) ([]*Path, error) {
142-
// Process functional options
143-
o := &parseOptions{
144-
deepnessThreshold: defaultDeepnessThreshold,
145-
}
146-
for _, opt := range opts {
147-
opt.applyParse(o)
148-
}
149-
141+
func Parse(input []byte, grammar *Grammar, rootRulename string) ([]*Path, error) {
150142
// Select root rule to begin with
151143
rootRule := GetRule(rootRulename, grammar.Rulemap)
152144
if rootRule == nil {
@@ -156,7 +148,7 @@ func Parse(input []byte, grammar *Grammar, rootRulename string, opts ...ParseOpt
156148
}
157149

158150
// Parse input with grammar's initial rule
159-
possibilites := solveAlt(grammar, rootRule.Alternation, input, 0, 0, o)
151+
possibilites := solveAlt(grammar, rootRule.Alternation, input, 0)
160152

161153
// Look for solutions that consumed the whole input
162154
outPoss := []*Path{}
@@ -171,14 +163,14 @@ func Parse(input []byte, grammar *Grammar, rootRulename string, opts ...ParseOpt
171163
return outPoss, nil
172164
}
173165

174-
func solveAlt(grammar *Grammar, alt Alternation, input []byte, index, deepness int, opts *parseOptions) []*Path {
166+
func solveAlt(grammar *Grammar, alt Alternation, input []byte, index int) []*Path {
175167
altPossibilities := []*Path{}
176168

177169
for _, concat := range alt.Concatenations {
178170
cntPossibilities := []*Path{}
179171

180172
// Init with first repetition (guarantee of at least 1 repetition)
181-
possibilities := solveRep(grammar, concat.Repetitions[0], input, index, deepness, opts)
173+
possibilities := solveRep(grammar, concat.Repetitions[0], input, index)
182174
for _, poss := range possibilities {
183175
cntPossibilities = append(cntPossibilities, &Path{
184176
Subpaths: []*Path{poss},
@@ -195,7 +187,7 @@ func solveAlt(grammar *Grammar, alt Alternation, input []byte, index, deepness i
195187

196188
tmpPossibilities := []*Path{}
197189
for _, cntPoss := range cntPossibilities {
198-
possibilities := solveRep(grammar, rep, input, cntPoss.End, deepness, opts)
190+
possibilities := solveRep(grammar, rep, input, cntPoss.End)
199191
for _, poss := range possibilities {
200192
// If the possibility is the empty path, don't append the empty one
201193
if poss.Start == poss.End {
@@ -227,13 +219,13 @@ func solveAlt(grammar *Grammar, alt Alternation, input []byte, index, deepness i
227219
return altPossibilities
228220
}
229221

230-
func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness int, opts *parseOptions) []*Path {
222+
func solveRep(grammar *Grammar, rep Repetition, input []byte, index int) []*Path {
231223
// Initiate repetition solve
232224
if !solveKeepGoing(rep, input, index, 0) {
233225
return []*Path{}
234226
}
235227
ppaths := [][]*Path{}
236-
ppaths = append(ppaths, solveElem(grammar, rep.Element, input, index, deepness, opts))
228+
ppaths = append(ppaths, solveElem(grammar, rep.Element, input, index))
237229

238230
// Other repetition solves
239231
for i := 1; i != rep.Max; i++ {
@@ -243,7 +235,7 @@ func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness in
243235
if !solveKeepGoing(rep, input, prevPath.End, i) {
244236
continue
245237
}
246-
elemPossibilities := solveElem(grammar, rep.Element, input, prevPath.End, deepness, opts)
238+
elemPossibilities := solveElem(grammar, rep.Element, input, prevPath.End)
247239
for _, elemPoss := range elemPossibilities {
248240
subs := make([]*Path, len(prevPath.Subpaths), len(prevPath.Subpaths)+1)
249241
copy(subs, prevPath.Subpaths)
@@ -289,18 +281,13 @@ func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness in
289281
return outpaths
290282
}
291283

292-
func solveElem(grammar *Grammar, elem ElemItf, input []byte, index, deepness int, opts *parseOptions) []*Path {
284+
func solveElem(grammar *Grammar, elem ElemItf, input []byte, index int) []*Path {
293285
paths := []*Path{}
294286

295-
// Check don't go too deep
296-
if opts.deepnessThreshold != -1 && deepness > opts.deepnessThreshold {
297-
return paths
298-
}
299-
300287
switch v := elem.(type) {
301288
case ElemRulename:
302289
rule := GetRule(v.Name, grammar.Rulemap)
303-
possibilities := solveAlt(grammar, rule.Alternation, input, index, deepness+1, opts)
290+
possibilities := solveAlt(grammar, rule.Alternation, input, index)
304291
for _, poss := range possibilities {
305292
poss.MatchRule = v.Name
306293
paths = append(paths, poss)
@@ -311,10 +298,10 @@ func solveElem(grammar *Grammar, elem ElemItf, input []byte, index, deepness int
311298
Min: 0,
312299
Max: 1,
313300
Element: ElemGroup(v),
314-
}, input, index, deepness+1, opts)
301+
}, input, index)
315302

316303
case ElemGroup:
317-
paths = solveAlt(grammar, v.Alternation, input, index, deepness+1, opts)
304+
paths = solveAlt(grammar, v.Alternation, input, index)
318305

319306
case ElemNumVal:
320307
switch v.Status {

option.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,18 @@
11
package goabnf
22

33
const (
4-
defaultDeepnessThreshold = 1024
5-
defaultValidate = true
4+
defaultValidate = true
65
)
76

8-
// ParseOption defines the interface for Parse functional options.
9-
type ParseOption interface {
10-
applyParse(*parseOptions)
11-
}
12-
137
// ParseABNFOption defines the interface for ParseABNF functional options.
148
type ParseABNFOption interface {
159
applyParseABNF(*parseABNFOptions)
1610
}
1711

18-
type parseOptions struct {
19-
deepnessThreshold int
20-
}
21-
2212
type parseABNFOptions struct {
2313
validate bool
2414
}
2515

26-
// Defines recursive deepness threshold to stop at.
27-
// Count all unfinished "element" alternatives i.e. rule / group / option.
28-
type deepnessThresholdOption int
29-
30-
var _ ParseOption = (*deepnessThresholdOption)(nil)
31-
32-
func (o deepnessThresholdOption) applyParse(opts *parseOptions) {
33-
opts.deepnessThreshold = int(o)
34-
}
35-
36-
// WithDeepnessThreshold returns a functional option to define at
37-
// which resursive deepness threshold to stop parsing at.
38-
// Modifying it can lead to panic, use with extreme caution.
39-
// Default is 1024. Setting it to -1 sets no limit to recursion.
40-
func WithDeepnessThreshold(deepnessThreshold int) ParseOption {
41-
return deepnessThresholdOption(deepnessThreshold)
42-
}
43-
4416
// Defines if proceed to semantic validation.
4517
type validateOption bool
4618

0 commit comments

Comments
 (0)