-
Notifications
You must be signed in to change notification settings - Fork 47
/
nex.go
1230 lines (1179 loc) · 27.1 KB
/
nex.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Substantial copy-and-paste from src/pkg/regexp.
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
)
import (
"go/format"
"go/parser"
"go/printer"
"go/token"
)
type rule struct {
regex []rune
code string
startCode string
endCode string
kid []*rule
id string
}
var (
ErrInternal = errors.New("internal error")
ErrUnmatchedLpar = errors.New("unmatched '('")
ErrUnmatchedRpar = errors.New("unmatched ')'")
ErrUnmatchedLbkt = errors.New("unmatched '['")
ErrUnmatchedRbkt = errors.New("unmatched ']'")
ErrBadRange = errors.New("bad range in character class")
ErrExtraneousBackslash = errors.New("extraneous backslash")
ErrBareClosure = errors.New("closure applies to nothing")
ErrBadBackslash = errors.New("illegal backslash escape")
ErrExpectedLBrace = errors.New("expected '{'")
ErrUnmatchedLBrace = errors.New("unmatched '{'")
ErrUnexpectedEOF = errors.New("unexpected EOF")
ErrUnexpectedNewline = errors.New("unexpected newline")
ErrUnexpectedLAngle = errors.New("unexpected '<'")
ErrUnmatchedLAngle = errors.New("unmatched '<'")
ErrUnmatchedRAngle = errors.New("unmatched '>'")
)
func ispunct(c rune) bool {
for _, r := range "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" {
if c == r {
return true
}
}
return false
}
var escapes = []rune("abfnrtv")
var escaped = []rune("\a\b\f\n\r\t\v")
func escape(c rune) rune {
for i, b := range escapes {
if b == c {
return escaped[i]
}
}
return -1
}
const (
kNil = iota
kRune
kClass
kWild
kStart
kEnd
)
type edge struct {
kind int // Rune/Class/Wild/Nil.
r rune // Rune for rune edges.
lim []rune // Pairs of limits for character class edges.
negate bool // True if the character class is negated.
dst *node // Destination node.
}
type node struct {
e edges // Outedges.
n int // Index number. Scoped to a family.
accept bool // True if this is an accepting state.
set []int // The NFA nodes represented by a DFA node.
}
type edges []*edge
func (e edges) Len() int {
return len(e)
}
func (e edges) Less(i, j int) bool {
return e[i].r < e[j].r
}
func (e edges) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
type RuneSlice []rune
func (p RuneSlice) Len() int { return len(p) }
func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p RuneSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Print a graph in DOT format given the start node.
//
// $ dot -Tps input.dot -o output.ps
func writeDotGraph(outf *os.File, start *node, id string) {
done := make(map[*node]bool)
var show func(*node)
show = func(u *node) {
if u.accept {
fmt.Fprintf(outf, " %v[style=filled,color=green];\n", u.n)
}
done[u] = true
for _, e := range u.e {
// We use -1 to denote the dead end node in DFAs.
if e.dst.n == -1 {
continue
}
label := ""
runeToDot := func(r rune) string {
if strconv.IsPrint(r) {
return fmt.Sprintf("%v", string(r))
}
return fmt.Sprintf("U+%X", int(r))
}
switch e.kind {
case kRune:
label = fmt.Sprintf("[label=%q]", runeToDot(e.r))
case kWild:
label = "[color=blue]"
case kClass:
label = "[label=\"["
if e.negate {
label += "^"
}
for i := 0; i < len(e.lim); i += 2 {
label += runeToDot(e.lim[i])
if e.lim[i] != e.lim[i+1] {
label += "-" + runeToDot(e.lim[i+1])
}
}
label += "]\"]"
}
fmt.Fprintf(outf, " %v -> %v%v;\n", u.n, e.dst.n, label)
}
for _, e := range u.e {
if !done[e.dst] {
show(e.dst)
}
}
}
fmt.Fprintf(outf, "digraph %v {\n 0[shape=box];\n", id)
show(start)
fmt.Fprintln(outf, "}")
}
func inClass(r rune, lim []rune) bool {
for i := 0; i < len(lim); i += 2 {
if lim[i] <= r && r <= lim[i+1] {
return true
}
}
return false
}
var dfadot, nfadot *os.File
func gen(out *bufio.Writer, x *rule) {
s := x.regex
// Regex -> NFA
// We cannot have our alphabet be all Unicode characters. Instead,
// we compute an alphabet for each regex:
//
// 1. Singles: we add single runes used in the regex: any rune not in a
// range. These are held in `sing`.
//
// 2. Ranges: entire ranges become elements of the alphabet. If ranges in
// the same expression overlap, we break them up into non-overlapping
// ranges. The generated code checks singles before ranges, so there's no
// need to break up a range if it contains a single. These are maintained
// in sorted order in `lim`.
//
// 3. Wild: we add an element representing all other runes.
//
// e.g. the alphabet of /[0-9]*[Ee][2-5]*/ is sing: { E, e },
// lim: { [0-1], [2-5], [6-9] } and the wild element.
sing := make(map[rune]bool)
var lim []rune
var insertLimits func(l, r rune)
// Insert a new range [l-r] into `lim`, breaking it up if it overlaps, and
// discarding it if it coincides with an existing range. We keep `lim`
// sorted.
insertLimits = func(l, r rune) {
var i int
for i = 0; i < len(lim); i += 2 {
if l <= lim[i+1] {
break
}
}
if len(lim) == i || r < lim[i] {
lim = append(lim, 0, 0)
copy(lim[i+2:], lim[i:])
lim[i] = l
lim[i+1] = r
return
}
if l < lim[i] {
lim = append(lim, 0, 0)
copy(lim[i+2:], lim[i:])
lim[i+1] = lim[i] - 1
lim[i] = l
insertLimits(lim[i], r)
return
}
if l > lim[i] {
lim = append(lim, 0, 0)
copy(lim[i+2:], lim[i:])
lim[i+1] = l - 1
lim[i+2] = l
insertLimits(l, r)
return
}
// l == lim[i]
if r == lim[i+1] {
return
}
if r < lim[i+1] {
lim = append(lim, 0, 0)
copy(lim[i+2:], lim[i:])
lim[i] = l
lim[i+1] = r
lim[i+2] = r + 1
return
}
insertLimits(lim[i+1]+1, r)
}
pos := 0
n := 0
newNode := func() *node {
res := new(node)
res.n = n
n++
return res
}
newEdge := func(u, v *node) *edge {
res := new(edge)
res.dst = v
u.e = append(u.e, res)
sort.Sort(u.e)
return res
}
newStartEdge := func(u, v *node) *edge {
res := newEdge(u, v)
res.kind = kStart
return res
}
newEndEdge := func(u, v *node) *edge {
res := newEdge(u, v)
res.kind = kEnd
return res
}
newWildEdge := func(u, v *node) *edge {
res := newEdge(u, v)
res.kind = kWild
return res
}
newRuneEdge := func(u, v *node, r rune) *edge {
res := newEdge(u, v)
res.kind = kRune
res.r = r
sing[r] = true
return res
}
newNilEdge := func(u, v *node) *edge {
res := newEdge(u, v)
res.kind = kNil
return res
}
newClassEdge := func(u, v *node) *edge {
res := newEdge(u, v)
res.kind = kClass
res.lim = make([]rune, 0, 2)
return res
}
maybeEscape := func() rune {
c := s[pos]
if '\\' == c {
pos++
if len(s) == pos {
panic(ErrExtraneousBackslash)
}
c = s[pos]
switch {
case ispunct(c):
case escape(c) >= 0:
c = escape(s[pos])
default:
panic(ErrBadBackslash)
}
}
return c
}
pcharclass := func() (start, end *node) {
start, end = newNode(), newNode()
e := newClassEdge(start, end)
// Ranges consisting of a single element are a special case:
singletonRange := func(c rune) {
// 1. The edge-specific 'lim' field always expects endpoints in pairs,
// so we must give 'c' as the beginning and the end of the range.
e.lim = append(e.lim, c, c)
// 2. Instead of updating the regex-wide 'lim' interval set, we add a singleton.
sing[c] = true
}
if len(s) > pos && '^' == s[pos] {
e.negate = true
pos++
}
var left rune
leftLive := false
justSawDash := false
first := true
// Allow '-' at the beginning and end, and in ranges.
for pos < len(s) && s[pos] != ']' {
switch c := maybeEscape(); c {
case '-':
if first {
singletonRange('-')
break
}
justSawDash = true
default:
if justSawDash {
if !leftLive || left > c {
panic(ErrBadRange)
}
e.lim = append(e.lim, left, c)
if left == c {
sing[c] = true
} else {
insertLimits(left, c)
}
leftLive = false
} else {
if leftLive {
singletonRange(left)
}
left = c
leftLive = true
}
justSawDash = false
}
first = false
pos++
}
if leftLive {
singletonRange(left)
}
if justSawDash {
singletonRange('-')
}
return
}
isNested := false
var pre func() (start, end *node)
pterm := func() (start, end *node) {
if len(s) == pos || s[pos] == '|' {
end = newNode()
start = end
return
}
switch s[pos] {
case '*', '+', '?':
panic(ErrBareClosure)
case ')':
if !isNested {
panic(ErrUnmatchedRpar)
}
end = newNode()
start = end
return
case '(':
pos++
oldIsNested := isNested
isNested = true
start, end = pre()
isNested = oldIsNested
if len(s) == pos || ')' != s[pos] {
panic(ErrUnmatchedLpar)
}
case '.':
start, end = newNode(), newNode()
newWildEdge(start, end)
case '^':
start, end = newNode(), newNode()
newStartEdge(start, end)
case '$':
start, end = newNode(), newNode()
newEndEdge(start, end)
case ']':
panic(ErrUnmatchedRbkt)
case '[':
pos++
start, end = pcharclass()
if len(s) == pos || ']' != s[pos] {
panic(ErrUnmatchedLbkt)
}
default:
start, end = newNode(), newNode()
newRuneEdge(start, end, maybeEscape())
}
pos++
return
}
pclosure := func() (start, end *node) {
start, end = pterm()
if start == end {
return
}
if len(s) == pos {
return
}
switch s[pos] {
case '*':
newNilEdge(end, start)
nend := newNode()
newNilEdge(end, nend)
start, end = end, nend
case '+':
newNilEdge(end, start)
nend := newNode()
newNilEdge(end, nend)
end = nend
case '?':
nstart := newNode()
newNilEdge(nstart, start)
start = nstart
newNilEdge(start, end)
default:
return
}
pos++
return
}
pcat := func() (start, end *node) {
for {
nstart, nend := pclosure()
if start == nil {
start, end = nstart, nend
} else if nstart != nend {
end.e = make([]*edge, len(nstart.e))
copy(end.e, nstart.e)
end = nend
}
if nstart == nend {
return
}
}
panic("unreachable")
}
pre = func() (start, end *node) {
start, end = pcat()
for pos < len(s) && s[pos] != ')' {
if s[pos] != '|' {
panic(ErrInternal)
}
pos++
nstart, nend := pcat()
tmp := newNode()
newNilEdge(tmp, start)
newNilEdge(tmp, nstart)
start = tmp
tmp = newNode()
newNilEdge(end, tmp)
newNilEdge(nend, tmp)
end = tmp
}
return
}
start, end := pre()
end.accept = true
// Compute shortlist of nodes (reachable nodes), as we may have discarded
// nodes left over from parsing. Also, make short[0] the start node.
short := make([]*node, 0, n)
{
var visit func(*node)
mark := make([]bool, n)
newn := make([]int, n)
visit = func(u *node) {
mark[u.n] = true
newn[u.n] = len(short)
short = append(short, u)
for _, e := range u.e {
if !mark[e.dst.n] {
visit(e.dst)
}
}
}
visit(start)
for _, v := range short {
v.n = newn[v.n]
}
}
n = len(short)
if nfadot != nil {
writeDotGraph(nfadot, start, "NFA_"+x.id)
}
// NFA -> DFA
nilClose := func(st []bool) {
visited := make([]bool, n)
var do func(int)
do = func(i int) {
visited[i] = true
v := short[i]
for _, e := range v.e {
if e.kind == kNil && !visited[e.dst.n] {
st[e.dst.n] = true
do(e.dst.n)
}
}
}
for i := 0; i < n; i++ {
if st[i] && !visited[i] {
do(i)
}
}
}
var todo []*node
tab := make(map[string]*node)
var buf []byte
dfacount := 0
{ // Construct the node of no return.
for i := 0; i < n; i++ {
buf = append(buf, '0')
}
tmp := new(node)
tmp.n = -1
tab[string(buf)] = tmp
}
newDFANode := func(st []bool) (res *node, found bool) {
buf = nil
accept := false
for i, v := range st {
if v {
buf = append(buf, '1')
accept = accept || short[i].accept
} else {
buf = append(buf, '0')
}
}
res, found = tab[string(buf)]
if !found {
res = new(node)
res.n = dfacount
res.accept = accept
dfacount++
for i, v := range st {
if v {
res.set = append(res.set, i)
}
}
tab[string(buf)] = res
}
return res, found
}
get := func(states []bool) *node {
nilClose(states)
node, old := newDFANode(states)
if !old {
todo = append(todo, node)
}
return node
}
getcb := func(v *node, cb func(*edge) bool) *node {
states := make([]bool, n)
for _, i := range v.set {
for _, e := range short[i].e {
if cb(e) {
states[e.dst.n] = true
}
}
}
return get(states)
}
states := make([]bool, n)
// The DFA start state is the state representing the nil-closure of the start
// node in the NFA. Recall it has index 0.
states[0] = true
dfastart := get(states)
for len(todo) > 0 {
v := todo[len(todo)-1]
todo = todo[0 : len(todo)-1]
// Singles.
var runes []rune
for r, _ := range sing {
runes = append(runes, r)
}
sort.Sort(RuneSlice(runes))
for _, r := range runes {
newRuneEdge(v, getcb(v, func(e *edge) bool {
return e.kind == kRune && e.r == r ||
e.kind == kWild ||
e.kind == kClass && e.negate != inClass(r, e.lim)
}), r)
}
// Character ranges.
for j := 0; j < len(lim); j += 2 {
e := newClassEdge(v, getcb(v, func(e *edge) bool {
return e.kind == kWild ||
e.kind == kClass && e.negate != inClass(lim[j], e.lim)
}))
e.lim = append(e.lim, lim[j], lim[j+1])
}
// Wild.
newWildEdge(v, getcb(v, func(e *edge) bool {
return e.kind == kWild || (e.kind == kClass && e.negate)
}))
// ^ and $.
newStartEdge(v, getcb(v, func(e *edge) bool { return e.kind == kStart }))
newEndEdge(v, getcb(v, func(e *edge) bool { return e.kind == kEnd }))
}
n = dfacount
if dfadot != nil {
writeDotGraph(dfadot, dfastart, "DFA_"+x.id)
}
// DFA -> Go
sorted := make([]*node, n)
for _, v := range tab {
if -1 != v.n {
sorted[v.n] = v
}
}
fmt.Fprintf(out, "\n// %v\n", string(x.regex))
for i, v := range sorted {
if i == 0 {
out.WriteString("{[]bool{")
} else {
out.WriteString(", ")
}
if v.accept {
out.WriteString("true")
} else {
out.WriteString("false")
}
}
out.WriteString("}, []func(rune) int{ // Transitions\n")
for _, v := range sorted {
out.WriteString("func(r rune) int {\n")
var runeCases, classCases string
var wildDest int
for _, e := range v.e {
m := e.dst.n
switch e.kind {
case kRune:
runeCases += fmt.Sprintf("\t\tcase %d: return %d\n", e.r, m)
case kClass:
classCases += fmt.Sprintf("\t\tcase %d <= r && r <= %d: return %d\n",
e.lim[0], e.lim[1], m)
case kWild:
wildDest = m
}
}
if runeCases != "" {
out.WriteString("\tswitch(r) {\n" + runeCases + "\t}\n")
}
if classCases != "" {
out.WriteString("\tswitch {\n" + classCases + "\t}\n")
}
fmt.Fprintf(out, "\treturn %v\n},\n", wildDest)
}
out.WriteString("}, []int{ /* Start-of-input transitions */ ")
for _, v := range sorted {
s := " -1,"
for _, e := range v.e {
if e.kind == kStart {
s = fmt.Sprintf(" %d,", e.dst.n)
break
}
}
out.WriteString(s)
}
out.WriteString("}, []int{ /* End-of-input transitions */ ")
for _, v := range sorted {
s := " -1,"
for _, e := range v.e {
if e.kind == kEnd {
s = fmt.Sprintf(" %d,", e.dst.n)
break
}
}
out.WriteString(s)
}
out.WriteString("},")
if len(x.kid) == 0 {
out.WriteString("nil")
} else {
out.WriteString("[]dfa{")
for _, kid := range x.kid {
gen(out, kid)
}
out.WriteString("}")
}
out.WriteString("},\n")
}
func writeFamily(out *bufio.Writer, node *rule, lvl int) {
tab := func() {
for i := 0; i <= lvl; i++ {
out.WriteByte('\t')
}
}
if node.startCode != "" {
tab()
prefixReplacer.WriteString(out, "if !yylex.stale {\n")
tab()
out.WriteString("\t" + node.startCode + "\n")
tab()
out.WriteString("}\n")
}
tab()
fmt.Fprintf(out, "OUTER%s%d:\n", node.id, lvl)
tab()
prefixReplacer.WriteString(out,
fmt.Sprintf("for { switch yylex.next(%v) {\n", lvl))
for i, x := range node.kid {
tab()
fmt.Fprintf(out, "\tcase %d:\n", i)
lvl++
if x.kid != nil {
writeFamily(out, x, lvl)
} else {
tab()
out.WriteString("\t" + x.code + "\n")
}
lvl--
}
tab()
out.WriteString("\tdefault:\n")
tab()
fmt.Fprintf(out, "\t\t break OUTER%s%d\n", node.id, lvl)
tab()
out.WriteString("\t}\n")
tab()
out.WriteString("\tcontinue\n")
tab()
out.WriteString("}\n")
tab()
prefixReplacer.WriteString(out, "yylex.pop()\n")
tab()
out.WriteString(node.endCode + "\n")
}
var lexertext = `import ("bufio";"io";"strings")
type frame struct {
i int
s string
line, column int
}
type Lexer struct {
// The lexer runs in its own goroutine, and communicates via channel 'ch'.
ch chan frame
ch_stop chan bool
// We record the level of nesting because the action could return, and a
// subsequent call expects to pick up where it left off. In other words,
// we're simulating a coroutine.
// TODO: Support a channel-based variant that compatible with Go's yacc.
stack []frame
stale bool
// The 'l' and 'c' fields were added for
// https://github.com/wagerlabs/docker/blob/65694e801a7b80930961d70c69cba9f2465459be/buildfile.nex
// Since then, I introduced the built-in Line() and Column() functions.
l, c int
parseResult interface{}
// The following line makes it easy for scripts to insert fields in the
// generated code.
// [NEX_END_OF_LEXER_STRUCT]
}
// NewLexerWithInit creates a new Lexer object, runs the given callback on it,
// then returns it.
func NewLexerWithInit(in io.Reader, initFun func(*Lexer)) *Lexer {
yylex := new(Lexer)
if initFun != nil {
initFun(yylex)
}
yylex.ch = make(chan frame)
yylex.ch_stop = make(chan bool, 1)
var scan func(in *bufio.Reader, ch chan frame, ch_stop chan bool, family []dfa, line, column int)
scan = func(in *bufio.Reader, ch chan frame, ch_stop chan bool, family []dfa, line, column int) {
// Index of DFA and length of highest-precedence match so far.
matchi, matchn := 0, -1
var buf []rune
n := 0
checkAccept := func(i int, st int) bool {
// Higher precedence match? DFAs are run in parallel, so matchn is at most len(buf), hence we may omit the length equality check.
if family[i].acc[st] && (matchn < n || matchi > i) {
matchi, matchn = i, n
return true
}
return false
}
var state [][2]int
for i := 0; i < len(family); i++ {
mark := make([]bool, len(family[i].startf))
// Every DFA starts at state 0.
st := 0
for {
state = append(state, [2]int{i, st})
mark[st] = true
// As we're at the start of input, follow all ^ transitions and append to our list of start states.
st = family[i].startf[st]
if -1 == st || mark[st] { break }
// We only check for a match after at least one transition.
checkAccept(i, st)
}
}
atEOF := false
stopped := false
for {
if n == len(buf) && !atEOF {
r,_,err := in.ReadRune()
switch err {
case io.EOF: atEOF = true
case nil: buf = append(buf, r)
default: panic(err)
}
}
if !atEOF {
r := buf[n]
n++
var nextState [][2]int
for _, x := range state {
x[1] = family[x[0]].f[x[1]](r)
if -1 == x[1] { continue }
nextState = append(nextState, x)
checkAccept(x[0], x[1])
}
state = nextState
} else {
dollar: // Handle $.
for _, x := range state {
mark := make([]bool, len(family[x[0]].endf))
for {
mark[x[1]] = true
x[1] = family[x[0]].endf[x[1]]
if -1 == x[1] || mark[x[1]] { break }
if checkAccept(x[0], x[1]) {
// Unlike before, we can break off the search. Now that we're at the end, there's no need to maintain the state of each DFA.
break dollar
}
}
}
state = nil
}
if state == nil {
lcUpdate := func(r rune) {
if r == '\n' {
line++
column = 0
} else {
column++
}
}
// All DFAs stuck. Return last match if it exists, otherwise advance by one rune and restart all DFAs.
if matchn == -1 {
if len(buf) == 0 { // This can only happen at the end of input.
break
}
lcUpdate(buf[0])
buf = buf[1:]
} else {
text := string(buf[:matchn])
buf = buf[matchn:]
matchn = -1
select {
case ch <- frame{matchi, text, line, column}: {
}
case stopped = <- ch_stop: {
}
}
if stopped {
break
}
if len(family[matchi].nest) > 0 {
scan(bufio.NewReader(strings.NewReader(text)), ch, ch_stop, family[matchi].nest, line, column)
}
if atEOF {
break
}
for _, r := range text {
lcUpdate(r)
}
}
n = 0
for i := 0; i < len(family); i++ {
state = append(state, [2]int{i, 0})
}
}
}
ch <- frame{-1, "", line, column}
}
go scan(bufio.NewReader(in), yylex.ch, yylex.ch_stop, dfas, 0, 0)
return yylex
}
type dfa struct {
acc []bool // Accepting states.
f []func(rune) int // Transitions.
startf, endf []int // Transitions at start and end of input.
nest []dfa
}
var dfas = []dfa{`
var lexeroutro = `}
func NewLexer(in io.Reader) *Lexer {
return NewLexerWithInit(in, nil)
}
func (yyLex *Lexer) Stop() {
yyLex.ch_stop <- true
}
// Text returns the matched text.
func (yylex *Lexer) Text() string {
return yylex.stack[len(yylex.stack) - 1].s
}
// Line returns the current line number.
// The first line is 0.
func (yylex *Lexer) Line() int {
if len(yylex.stack) == 0 {
return 0
}
return yylex.stack[len(yylex.stack) - 1].line
}
// Column returns the current column number.
// The first column is 0.
func (yylex *Lexer) Column() int {
if len(yylex.stack) == 0 {
return 0
}
return yylex.stack[len(yylex.stack) - 1].column
}
func (yylex *Lexer) next(lvl int) int {
if lvl == len(yylex.stack) {
l, c := 0, 0
if lvl > 0 {
l, c = yylex.stack[lvl - 1].line, yylex.stack[lvl - 1].column
}
yylex.stack = append(yylex.stack, frame{0, "", l, c})
}
if lvl == len(yylex.stack) - 1 {
p := &yylex.stack[lvl]
*p = <-yylex.ch
yylex.stale = false
} else {
yylex.stale = true
}
return yylex.stack[lvl].i
}
func (yylex *Lexer) pop() {
yylex.stack = yylex.stack[:len(yylex.stack) - 1]
}
`
func writeLex(out *bufio.Writer, root rule) {
if !customError {
// TODO: I can't remember what this was for!
prefixReplacer.WriteString(out, `func (yylex Lexer) Error(e string) {
panic(e)
}`)
}
prefixReplacer.WriteString(out, `
// Lex runs the lexer. Always returns 0.
// When the -s option is given, this function is not generated;