@@ -15,15 +15,15 @@ type Grammar struct {
15
15
// IsValid checks there exist at least a path that completly consumes
16
16
// input, hence is valide given this gramma and especially one of its
17
17
// 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 ) {
19
19
lt , err := g .IsLeftTerminating (rulename )
20
20
if err != nil {
21
21
return false , err
22
22
}
23
23
if ! lt {
24
24
return false , fmt .Errorf ("rule %s is not left terminating thus can't be validated without the risk of infinite recursion" , rulename )
25
25
}
26
- paths , err := Parse (input , g , rulename , opts ... )
26
+ paths , err := Parse (input , g , rulename )
27
27
return len (paths ) != 0 && err == nil , nil
28
28
}
29
29
@@ -101,7 +101,7 @@ func ParseABNF(input []byte, opts ...ParseABNFOption) (*Grammar, error) {
101
101
// Parse input with ABNF grammar
102
102
// Don't need to transmit deepness option, as we can be sure ABNF won't
103
103
// recurse indefinitely.
104
- paths , err := Parse (input , ABNF , "rulelist" , WithDeepnessThreshold ( - 1 ) )
104
+ paths , err := Parse (input , ABNF , "rulelist" )
105
105
if err != nil {
106
106
return nil , err
107
107
}
@@ -138,15 +138,7 @@ func ParseABNF(input []byte, opts ...ParseABNFOption) (*Grammar, error) {
138
138
// order to look for solutions. If many are found, it raises an error.
139
139
// If the input is invalid (gramatically, incomplete...) it returns
140
140
// 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 ) {
150
142
// Select root rule to begin with
151
143
rootRule := GetRule (rootRulename , grammar .Rulemap )
152
144
if rootRule == nil {
@@ -156,7 +148,7 @@ func Parse(input []byte, grammar *Grammar, rootRulename string, opts ...ParseOpt
156
148
}
157
149
158
150
// 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 )
160
152
161
153
// Look for solutions that consumed the whole input
162
154
outPoss := []* Path {}
@@ -171,14 +163,14 @@ func Parse(input []byte, grammar *Grammar, rootRulename string, opts ...ParseOpt
171
163
return outPoss , nil
172
164
}
173
165
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 {
175
167
altPossibilities := []* Path {}
176
168
177
169
for _ , concat := range alt .Concatenations {
178
170
cntPossibilities := []* Path {}
179
171
180
172
// 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 )
182
174
for _ , poss := range possibilities {
183
175
cntPossibilities = append (cntPossibilities , & Path {
184
176
Subpaths : []* Path {poss },
@@ -195,7 +187,7 @@ func solveAlt(grammar *Grammar, alt Alternation, input []byte, index, deepness i
195
187
196
188
tmpPossibilities := []* Path {}
197
189
for _ , cntPoss := range cntPossibilities {
198
- possibilities := solveRep (grammar , rep , input , cntPoss .End , deepness , opts )
190
+ possibilities := solveRep (grammar , rep , input , cntPoss .End )
199
191
for _ , poss := range possibilities {
200
192
// If the possibility is the empty path, don't append the empty one
201
193
if poss .Start == poss .End {
@@ -227,13 +219,13 @@ func solveAlt(grammar *Grammar, alt Alternation, input []byte, index, deepness i
227
219
return altPossibilities
228
220
}
229
221
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 {
231
223
// Initiate repetition solve
232
224
if ! solveKeepGoing (rep , input , index , 0 ) {
233
225
return []* Path {}
234
226
}
235
227
ppaths := [][]* Path {}
236
- ppaths = append (ppaths , solveElem (grammar , rep .Element , input , index , deepness , opts ))
228
+ ppaths = append (ppaths , solveElem (grammar , rep .Element , input , index ))
237
229
238
230
// Other repetition solves
239
231
for i := 1 ; i != rep .Max ; i ++ {
@@ -243,7 +235,7 @@ func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness in
243
235
if ! solveKeepGoing (rep , input , prevPath .End , i ) {
244
236
continue
245
237
}
246
- elemPossibilities := solveElem (grammar , rep .Element , input , prevPath .End , deepness , opts )
238
+ elemPossibilities := solveElem (grammar , rep .Element , input , prevPath .End )
247
239
for _ , elemPoss := range elemPossibilities {
248
240
subs := make ([]* Path , len (prevPath .Subpaths ), len (prevPath .Subpaths )+ 1 )
249
241
copy (subs , prevPath .Subpaths )
@@ -289,18 +281,13 @@ func solveRep(grammar *Grammar, rep Repetition, input []byte, index, deepness in
289
281
return outpaths
290
282
}
291
283
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 {
293
285
paths := []* Path {}
294
286
295
- // Check don't go too deep
296
- if opts .deepnessThreshold != - 1 && deepness > opts .deepnessThreshold {
297
- return paths
298
- }
299
-
300
287
switch v := elem .(type ) {
301
288
case ElemRulename :
302
289
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 )
304
291
for _ , poss := range possibilities {
305
292
poss .MatchRule = v .Name
306
293
paths = append (paths , poss )
@@ -311,10 +298,10 @@ func solveElem(grammar *Grammar, elem ElemItf, input []byte, index, deepness int
311
298
Min : 0 ,
312
299
Max : 1 ,
313
300
Element : ElemGroup (v ),
314
- }, input , index , deepness + 1 , opts )
301
+ }, input , index )
315
302
316
303
case ElemGroup :
317
- paths = solveAlt (grammar , v .Alternation , input , index , deepness + 1 , opts )
304
+ paths = solveAlt (grammar , v .Alternation , input , index )
318
305
319
306
case ElemNumVal :
320
307
switch v .Status {
0 commit comments