This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
432 lines (383 loc) · 9.25 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package parser
import (
"errors"
"fmt"
"reflect"
)
// Parser represents a parser
type Parser struct {
grammar *Rule
errGrammar *Rule
recursionRegister recursionRegister
// MaxRecursionLevel defines the maximum tolerated recursion level.
// The limitation is disabled when MaxRecursionLevel is set to 0
MaxRecursionLevel uint
}
// NewParser creates a new parser instance
func NewParser(grammar *Rule, errGrammar *Rule) (*Parser, error) {
if grammar == nil {
return nil, errors.New("missing grammar")
}
if err := ValidatePattern(grammar); err != nil {
return nil, fmt.Errorf("invalid grammar: %w", err)
}
if errGrammar != nil {
if err := ValidatePattern(errGrammar); err != nil {
return nil, fmt.Errorf("invalid error-grammar: %w", err)
}
}
recRegister := recursionRegister{}
findRules(grammar, recRegister)
findRules(errGrammar, recRegister)
return &Parser{
grammar: grammar,
errGrammar: errGrammar,
recursionRegister: recRegister,
// Disable recursion limitation by default
MaxRecursionLevel: uint(0),
}, nil
}
func (pr Parser) handlePattern(
debug *DebugProfile,
scan *scanner,
pattern Pattern,
level uint,
) (frag Fragment, err error) {
switch pt := pattern.(type) {
case *Rule:
frag, err = pr.parseRule(debug, scan.New(), pt, level)
if err, ok := err.(*ErrUnexpectedToken); ok {
// Override expected pattern to the higher-order rule
err.Expected = pt
}
case *Exact:
frag, err = pr.parseExact(debug, scan, pt, level)
case *Lexed:
frag, err = pr.parseLexed(debug, scan, pt, level)
case *Repeated:
err = pr.parseRepeated(debug, scan, pt.Min, pt.Max, pt, level)
case Sequence:
err = pr.parseSequence(debug, scan, pt, level)
case Either:
frag, err = pr.parseEither(debug, scan, pt, level)
case Not:
err = pr.parseNot(debug, scan, pt, level)
default:
panic(fmt.Errorf(
"unsupported pattern type: %s",
reflect.TypeOf(pattern),
))
}
return
}
func (pr Parser) parseNot(
debug *DebugProfile,
scan *scanner,
ptr Not,
level uint,
) error {
debugIndex := debug.record(ptr, scan.Lexer.cr, level)
beforeCr := scan.Lexer.cr
_, err := pr.handlePattern(debug, scan, ptr.Pattern, level+1)
switch err := err.(type) {
case *ErrUnexpectedToken:
scan.Set(beforeCr)
return nil
case errEOF:
scan.Set(beforeCr)
return nil
case nil:
debug.markMismatch(debugIndex)
return &ErrUnexpectedToken{
At: beforeCr,
Expected: ptr,
}
default:
return err
}
}
func (pr Parser) parseLexed(
debug *DebugProfile,
scanner *scanner,
expected *Lexed,
level uint,
) (Fragment, error) {
debugIndex := debug.record(expected, scanner.Lexer.cr, level)
if scanner.Lexer.reachedEOF() {
debug.markMismatch(debugIndex)
return nil, errEOF{}
}
beforeCr := scanner.Lexer.cr
tk, err := scanner.ReadUntil(expected.Fn, expected.Kind)
if err != nil {
return nil, err
}
if tk == nil || tk.VEnd.Index-tk.VBegin.Index < expected.MinLen {
debug.markMismatch(debugIndex)
return nil, &ErrUnexpectedToken{
At: beforeCr,
Expected: expected,
}
}
return tk, nil
}
func (pr Parser) parseRepeated(
debug *DebugProfile,
scanner *scanner,
min uint,
max uint,
repeated *Repeated,
level uint,
) error {
debugIndex := debug.record(repeated, scanner.Lexer.cr, level)
num := uint(0)
lastPosition := scanner.Lexer.cr
for {
if max != 0 && num >= max {
break
}
frag, err := pr.handlePattern(
debug,
scanner,
repeated.Pattern,
level+1,
)
switch err := err.(type) {
case *ErrUnexpectedToken:
if min != 0 && num < min {
// Mismatch before the minimum is read
return err
}
// Reset scanner to the last match
scanner.Set(lastPosition)
return nil
case errEOF:
if min != 0 && num < min {
// Mismatch before the minimum is read
debug.markMismatch(debugIndex)
return &ErrUnexpectedToken{
At: scanner.Lexer.cr,
Expected: repeated,
}
}
// Reset scanner to the last match
scanner.Set(lastPosition)
return nil
case nil:
num++
lastPosition = scanner.Lexer.cr
// Append rule patterns, other patterns are appended automatically
if !repeated.Pattern.Container() {
scanner.Append(repeated.Pattern, frag)
}
default:
return err
}
}
return nil
}
func (pr Parser) parseSequence(
debug *DebugProfile,
scanner *scanner,
patterns Sequence,
level uint,
) error {
debugIndex := debug.record(patterns, scanner.Lexer.cr, level)
for _, pt := range patterns {
frag, err := pr.handlePattern(debug, scanner, pt, level+1)
if err != nil {
debug.markMismatch(debugIndex)
return err
}
// Append rule patterns, other patterns are appended automatically
if !pt.Container() {
scanner.Append(pt, frag)
}
}
return nil
}
func (pr Parser) parseEither(
debug *DebugProfile,
scanner *scanner,
patternOptions Either,
level uint,
) (Fragment, error) {
debugIndex := debug.record(patternOptions, scanner.Lexer.cr, level)
beforeCr := scanner.Lexer.cr
for ix, pt := range patternOptions {
lastOption := ix >= len(patternOptions)-1
frag, err := pr.handlePattern(debug, scanner, pt, level+1)
if err != nil {
if er, ok := err.(*ErrUnexpectedToken); ok {
if lastOption {
// Set actual expected pattern
er.Expected = patternOptions
debug.markMismatch(debugIndex)
} else {
// Reset scanner to the initial position
scanner.Set(beforeCr)
// Continue checking other options
continue
}
} else {
// Unexpected error
debug.markMismatch(debugIndex)
}
return nil, err
}
// Append rule patterns, other patterns are appended automatically
if !pt.Container() {
scanner.Append(pt, frag)
}
return frag, nil
}
return nil, nil
}
func (pr Parser) parseExact(
debug *DebugProfile,
scanner *scanner,
exact *Exact,
level uint,
) (Fragment, error) {
debugIndex := debug.record(exact, scanner.Lexer.cr, level)
if scanner.Lexer.reachedEOF() {
debug.markMismatch(debugIndex)
return nil, errEOF{}
}
beforeCr := scanner.Lexer.cr
tk, match, err := scanner.ReadExact(
exact.Expectation,
exact.Kind,
)
if err != nil {
return nil, err
}
if !match {
debug.markMismatch(debugIndex)
return nil, &ErrUnexpectedToken{
At: beforeCr,
Expected: exact,
}
}
return tk, nil
}
func (pr Parser) parseRule(
debug *DebugProfile,
scanner *scanner,
rule *Rule,
level uint,
) (frag Fragment, err error) {
debugIndex := debug.record(rule, scanner.Lexer.cr, level)
if pr.MaxRecursionLevel > 0 {
pr.recursionRegister[rule]++
if pr.recursionRegister[rule] > pr.MaxRecursionLevel {
// Max recursion level exceeded
return nil, &Err{
Err: fmt.Errorf(
"max recursion level exceeded at rule %p (%q)",
rule,
rule.Designation,
),
At: scanner.Lexer.cr,
}
}
}
frag, err = pr.handlePattern(debug, scanner, rule.Pattern, level+1)
if err != nil {
debug.markMismatch(debugIndex)
return
}
if !rule.Pattern.Container() {
scanner.Append(rule.Pattern, frag)
}
frag = scanner.Fragment(rule.Kind)
if rule.Action != nil {
// Execute rule action callback
if err := rule.Action(frag); err != nil {
return nil, &Err{Err: err, At: frag.Begin()}
}
}
return
}
func (pr Parser) tryErrRule(
debug *DebugProfile,
lex *lexer,
errRule *Rule,
previousUnexpErr error,
) error {
if errRule != nil {
_, err := pr.parseRule(debug, newScanner(lex), errRule, 0)
if err == nil {
// Return the previous error when no error was returned
return previousUnexpErr
}
if err, ok := err.(*ErrUnexpectedToken); ok {
// Reset expected token for the error-rule
err.Expected = nil
}
return err
}
return nil
}
// Debug parses the given source file in debug mode generating a debug profile
func (pr *Parser) Debug(source *SourceFile) (*DebugProfile, Fragment, error) {
debug := newDebugProfile()
mainFrag, err := pr.parse(source, debug)
return debug, mainFrag, err
}
// Parse parses the given source file.
//
// WARNING: Parse isn't safe for concurrent use and shall therefore
// not be executed by multiple goroutines concurrently!
func (pr *Parser) Parse(source *SourceFile) (Fragment, error) {
return pr.parse(source, nil)
}
func (pr *Parser) parse(
source *SourceFile,
debug *DebugProfile,
) (Fragment, error) {
if pr.MaxRecursionLevel > 0 {
// Reset the recursion register when recursion limitation is enabled
pr.recursionRegister.Reset()
}
cr := NewCursor(source)
lex := &lexer{cr: cr}
mainFrag, err := pr.parseRule(debug, newScanner(lex), pr.grammar, 0)
if err != nil {
if err, ok := err.(*ErrUnexpectedToken); ok {
// Reset the lexer to the start position of the error
lex.cr = err.At
}
if err := pr.tryErrRule(debug, lex, pr.errGrammar, err); err != nil {
return nil, err
}
return nil, err
}
// Ensure EOF
last, err := lex.ReadUntil(
func(uint, Cursor) bool { return true },
0,
)
switch err := err.(type) {
case errEOF:
// Ignore EOF errors
return mainFrag, nil
case nil:
default:
// Report unexpected errors
return nil, err
}
if last != nil {
if pr.errGrammar != nil {
// Try to match an error-pattern
lex.cr = last.VBegin
}
unexpErr := &ErrUnexpectedToken{At: last.VBegin}
if err := pr.tryErrRule(debug, lex, pr.errGrammar, unexpErr); err != nil {
return nil, err
}
// Fallback to default unexpected-token error
return nil, unexpErr
}
return mainFrag, nil
}