-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprod.go
537 lines (486 loc) · 12.1 KB
/
prod.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package parse
import (
"fmt"
"io"
"reflect"
"regexp"
"strings"
"github.com/ohait/forego/ctx"
)
type Prod struct {
g *Grammar
// what to ignore before any text matching
WS *regexp.Regexp
// override the above
wsFrom *Prod
// Set by Add()
Name string
// Set by Add()
Directive string
// file:line where this production was Add()-ed
src string
// each directive part will generate
actions []action
// function to be used at the end of the production
ret func(from int, at *pos, in []any) (any, error)
retType reflect.Type
}
type action struct {
// if true, results won't be added to the output
silent bool
commit bool
p *Prod
prod string
re *regexp.Regexp
negative bool // if true, make into a negative lookahead
argType reflect.Type // if set, means a return function expect this to be of the given type
}
func (this action) String() string {
s := ""
if this.commit {
return "+"
}
if this.silent {
s = "~"
}
if this.re != nil {
return s + "/" + this.re.String() + "/"
}
if this.prod != "" {
return s + this.prod
}
return s
}
func (this *Prod) ws() *regexp.Regexp {
if this.wsFrom != nil {
return this.wsFrom.ws()
}
return this.WS
}
func (this action) exec(p *pos) (any, *Error) {
if this.commit {
p.Log("commit %p", p)
p.commit = true
return nil, nil
}
if this.re != nil {
ws := this.p.ws()
if ws != nil {
err := p.IgnoreRE(ws, false)
if err != nil {
return nil, p.NewErrorf("can't consume whitespace: %v", err)
}
}
out, err := p.ConsumeRE(this.re, this.negative)
return out, err
}
if this.prod != "" {
alt := this.p.g.alts[this.prod]
if len(alt.prods) == 0 {
return nil, p.NewErrorf("no prod with name %q", this.prod)
}
return p.consumeProds(alt.prods...)
}
return nil, p.NewErrorf("empty action")
}
// helper
func (this *Prod) Parse(fname string, in []byte, end *regexp.Regexp) (any, error) {
p := &pos{
g: this.g,
file: fname,
src: &Src{bytes: in},
stats: &Stats{},
}
out, err := p.consumeProds(this)
if err != nil {
return nil, err
}
if end != nil {
p.IgnoreRE(end, false)
}
if p.Rem(10) != "" {
return out, ctx.NewErrorf(nil, "rem: %q", p.Rem(80))
}
return out, nil
}
func parseText(d string) (*regexp.Regexp, int, error) {
re := regexp.MustCompile(`^"(([^"\\]|\\.)*)"`)
m := re.FindStringSubmatch(d)
if m == nil {
return nil, 0, ctx.NewErrorf(nil, "invalid directive `%s`", d)
}
re = regexp.MustCompile("^" + regexp.QuoteMeta(m[1]))
return re, len(m[0]), nil
}
func parseRE(d string) (*regexp.Regexp, int, error) {
reEnd := regexp.MustCompile(`^/(([^/\\]|\\.)*)/`)
m := reEnd.FindStringSubmatch(d)
if m == nil {
return nil, 0, ctx.NewErrorf(nil, "invalid directive `%s`", d)
}
d = d[len(m[0]):]
re, err := regexp.Compile("^" + m[1])
if err != nil {
return nil, 0, ctx.NewErrorf(nil, "invalid directive `%s`: %v", m[1], err)
}
return re, len(m[0]), nil
}
func (this *Prod) mustBuild(term string) int {
ct, err := this.build(term)
if err != nil {
panic(err)
}
return ct
}
func (this *Prod) build(term string) (int, error) {
//log.Printf("prod[%q]...", this.Name)
this.Directive = strings.TrimSpace(this.Directive)
if this.Directive == "" {
//this.act = func(p *Pos) ([]any, error) {
// return nil, nil
//}
return 0, nil
}
negative := false
silent := false
d := this.Directive
for {
//log.Debugf(nil, "REM: `%s`", d)
switch term {
case "":
if d == "" {
return 0, nil
}
default:
if strings.HasPrefix(d, term) {
return len(this.Directive) - len(d), nil
}
}
if len(d) == 0 {
return 0, io.EOF
}
//log.Printf("parsing %q", d)
switch d[0] {
case '~':
silent = true
d = d[1:]
case '!': // negative look ahead
negative = true
d = d[1:]
case '+': // commit to this production
this.actions = append(this.actions, action{
p: this,
commit: true,
silent: true,
})
d = d[1:]
case '[': // TODO
re := regexp.MustCompile(`\[(.*)\]`)
m := re.FindString(d)
panic(m[1])
case '"':
re, ct, err := parseText(d)
if err != nil {
return len(this.Directive) - len(d), nil
}
d = d[ct:]
this.actions = append(this.actions, action{
p: this,
re: re,
negative: negative,
silent: true,
})
negative = false
silent = false
case '/':
re, l, err := parseRE(d)
if err != nil {
return len(this.Directive) - len(d), nil
}
d = d[l:]
//log.Printf("prod[%q]: /%s/", this.Name, re)
this.actions = append(this.actions, action{
p: this,
re: re,
negative: negative,
silent: silent,
})
negative = false
silent = false
case ' ', '\t', '\n', '\r': // ignore whitespace
d = d[1:]
default: // by default, we assume it's the production name
re := regexp.MustCompile(`(\w+)`)
m := re.FindStringSubmatch(d)
if m == nil {
return len(this.Directive) - len(d), ctx.NewErrorf(nil, "invalid directive: %q", d)
}
d = d[len(m[0]):]
name := m[1]
if len(d) > 2 {
//log.Debugf(nil, "REPEAT: `%s`", d)
switch d[0:1] {
case "(":
d = d[1:]
if negative {
return 0, ctx.NewErrorf(nil, "can't do a negative lookahead with repetition")
}
temp := &Prod{
Directive: d,
}
ct, err := temp.build(")")
if err != nil {
return len(this.Directive) - len(d), ctx.NewErrorf(nil, "invalid repetition: %v", err)
}
// internal names for the new alternations
repName := fmt.Sprintf("%s,rep%d", this.Name, this.g.repCt.Add(1))
repRep := repName + "_"
rep := d[0:ct]
d = d[ct+1:]
//log.Debugf(nil, "REPEAT: %+v", temp)
var sepAction *action
switch len(temp.actions) {
case 1: // simple
case 2: // with separator
sepAction = &temp.actions[1]
default:
return 0, ctx.NewErrorf(nil, "invalid repetition: `%s`", rep)
}
// TODO(oha) allow for other options like `?` or `s?` or `3` or `3..5` or `..5` etc
switch temp.actions[0].prod {
case "s":
default:
return 0, ctx.NewErrorf(nil, "expected valid repetition, got `%s`", rep)
}
// external prod, catches `name` and then repetitions
p1 := &Prod{
g: this.g,
Directive: name + " " + repRep,
Name: repName,
src: this.src,
wsFrom: this,
}
p1.actions = append(p1.actions, action{p: p1, prod: name})
p1.actions = append(p1.actions, action{p: p1, prod: repRep})
p1.Return(func(l any, r []any) []any {
return append([]any{l}, r...)
})
// internal prod, catches `sep` and `name` and then itself
p2 := &Prod{
g: this.g,
Name: repRep,
src: this.src,
wsFrom: this,
}
p2.Directive = name + " " + repRep
if sepAction != nil {
sepAction.p = p2
p2.actions = append(p2.actions, *sepAction)
p2.Directive = sepAction.prod + " " + p2.Directive
}
p2.actions = append(p2.actions, action{p: p2, prod: name})
p2.actions = append(p2.actions, action{p: p2, prod: repRep})
if sepAction != nil && !sepAction.silent {
p2.Return(func(sep any, l any, r []any) []any {
return append([]any{sep, l}, r...)
})
} else {
p2.Return(func(l any, r []any) []any {
return append([]any{l}, r...)
})
}
// empty fallback, when reaching the end
p3 := &Prod{
g: this.g,
Directive: "",
Name: repRep,
src: this.src,
wsFrom: this,
}
p3.mustBuild("")
p3.Return(func() []any { return []any{} })
this.g.Alt(repName).prods = []*Prod{p1}
this.g.Alt(repRep).prods = []*Prod{p2, p3}
name = repName // replace name with repName to use the above
default:
}
}
this.actions = append(this.actions, action{
p: this,
prod: name,
negative: negative,
silent: silent,
})
negative = false
silent = false
}
}
}
func (this *Prod) verify() error {
for _, act := range this.actions {
if act.prod != "" {
alt := this.g.alts[act.prod]
if alt == nil || len(alt.prods) == 0 {
return ctx.NewErrorf(nil, "production %q `%s` refers to empty %q", this.Name, this.Directive, act.prod)
}
if act.argType != nil {
for _, p := range this.g.alts[act.prod].prods {
if p.retType != nil && !p.retType.AssignableTo(act.argType) {
return ctx.NewErrorf(nil, "production %q at %s: action `%s` expect %v but %s returns %v",
this.Name, this.src, act, act.argType, p.src, p.retType)
}
}
}
}
}
return nil
}
func (this *Prod) exec(p *pos) (any, *Error) {
p.p = this
list := make([]any, 0, len(this.actions))
var err *Error
from := p.at
for _, act := range this.actions {
//if act.negative {
// rev := p.at
// _, err := act.exec(p)
// p.at = rev
// if err == nil {
// return nil, p.NewErrorf("negative lookahead: %s", p.Rem(20))
// }
// p.Log("negative lookahead ok: %v", err)
//} else {
out, err := act.exec(p)
if err != nil {
if err.commit {
// committed error must return directly
return nil, err
}
if p.commit {
// if we are committed, but the error isn't, wrap it so it's easier to see where the commit happened
err = p.NewErrorf("expected %s got %q", act.String(), p.Rem(10))
err.commit = true // and commit!
}
// return a non-committed error
return nil, err
}
if !act.silent {
list = append(list, out)
}
//}
}
if this.ret == nil {
switch len(list) {
case 0:
return nil, err
case 1:
return list[0], err
default:
return list, err
}
} else {
//if this.G.Log != nil { this.G.Log("ret(%v, %v)", in, out) }
out, err := this.ret(from, p, list)
if err != nil {
return out, &Error{err, p.at, p.commit}
}
p.Log("return %v", out)
return out, nil
}
}
// set a new return
func (this *Prod) Return(action any) *Prod {
if action == nil {
this.ret = func(from int, p *pos, in []any) (any, error) {
switch len(in) {
case 0:
return nil, nil
case 1:
return in[0], nil
default:
return in, nil
}
}
return this
}
f := reflect.ValueOf(action)
t := f.Type()
actNum := 0
for _, act := range this.actions {
if !act.silent {
actNum++
}
}
wantPos := t.NumIn() > 0 && t.In(0) == reflect.TypeOf(Pos{})
if wantPos {
actNum++
}
if t.NumIn() != actNum {
panic(fmt.Sprintf("%s: %v expects %d args, but %d are in the directive (%+v)", this.src, t, t.NumIn(), actNum, this.actions))
}
switch t.NumOut() {
case 1:
this.retType = t.Out(0)
case 2:
if t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
panic(fmt.Sprintf("%s: %v should return (X, error) or (X)", this.src, t))
}
this.retType = t.Out(0)
default:
panic(fmt.Sprintf("%s: %v should return (X, error) or (X)", this.src, t))
}
j := 0
if wantPos {
j = 1
}
for i, act := range this.actions {
if !act.silent {
this.actions[i].argType = t.In(j)
j++
}
}
this.ret = func(from int, p *pos, in []any) (any, error) {
if this.g.Log != nil {
ins := []string{}
for _, in := range in {
ins = append(ins, fmt.Sprintf("%T", in))
}
p.Log("calling `%v` with (%s)", t, strings.Join(ins, ", "))
}
var list []reflect.Value
if wantPos {
list = append(list, reflect.ValueOf(Pos{from, p.at, p.file, p.src}))
}
for _, in := range in {
t := t.In(len(list)) // expected type
v, err := coerce(reflect.ValueOf(in), t)
if err != nil {
panic(fmt.Sprintf("%s: can't coerce arg #%d: %v", this.src, len(list), err))
}
//if this.g.Log != nil { this.g.Log("ARG: %v", v) }
list = append(list, v)
}
if len(list) != t.NumIn() {
panic(fmt.Sprintf("%s: action `%v` expects %d arguments, but got %+v (wantPos: %v)", this.src, t, t.NumOut(), len(list), wantPos))
}
//log.Printf("CALL[%v](%v)", f, list)
out := f.Call(list)
switch len(out) {
case 1:
return out[0].Interface(), nil
case 2:
first := out[0].Interface()
second := out[1].Interface()
if second == nil {
return first, nil
}
if this.g.Log != nil {
this.g.Log("ERR: %T (%+v)", second, second.(error))
}
return first, second.(error)
default:
panic("can only return (any) or (any, error)")
}
}
return this
}