-
Notifications
You must be signed in to change notification settings - Fork 31
/
type.peg.go
613 lines (569 loc) · 12.1 KB
/
type.peg.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
package stdlib
// Code generated by peg -output=type.peg.go type.peg DO NOT EDIT.
import (
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
const endSymbol rune = 1114112
/* The rule types inferred from the grammar are below. */
type pegRule uint8
const (
ruleUnknown pegRule = iota
ruleMainRule
ruleTypes
rule_
ruleEOF
ruleType
ruleGenericType
ruleTupleType
)
var rul3s = [...]string{
"Unknown",
"MainRule",
"Types",
"_",
"EOF",
"Type",
"GenericType",
"TupleType",
}
type token32 struct {
pegRule
begin, end uint32
}
func (t *token32) String() string {
return fmt.Sprintf("\x1B[34m%v\x1B[m %v %v", rul3s[t.pegRule], t.begin, t.end)
}
type node32 struct {
token32
up, next *node32
}
func (node *node32) print(w io.Writer, pretty bool, buffer string) {
var print func(node *node32, depth int)
print = func(node *node32, depth int) {
for node != nil {
for c := 0; c < depth; c++ {
fmt.Fprintf(w, " ")
}
rule := rul3s[node.pegRule]
quote := strconv.Quote(string(([]rune(buffer)[node.begin:node.end])))
if !pretty {
fmt.Fprintf(w, "%v %v\n", rule, quote)
} else {
fmt.Fprintf(w, "\x1B[36m%v\x1B[m %v\n", rule, quote)
}
if node.up != nil {
print(node.up, depth+1)
}
node = node.next
}
}
print(node, 0)
}
func (node *node32) Print(w io.Writer, buffer string) {
node.print(w, false, buffer)
}
func (node *node32) PrettyPrint(w io.Writer, buffer string) {
node.print(w, true, buffer)
}
type tokens32 struct {
tree []token32
}
func (t *tokens32) Trim(length uint32) {
t.tree = t.tree[:length]
}
func (t *tokens32) Print() {
for _, token := range t.tree {
fmt.Println(token.String())
}
}
func (t *tokens32) AST() *node32 {
type element struct {
node *node32
down *element
}
tokens := t.Tokens()
var stack *element
for _, token := range tokens {
if token.begin == token.end {
continue
}
node := &node32{token32: token}
for stack != nil && stack.node.begin >= token.begin && stack.node.end <= token.end {
stack.node.next = node.up
node.up = stack.node
stack = stack.down
}
stack = &element{node: node, down: stack}
}
if stack != nil {
return stack.node
}
return nil
}
func (t *tokens32) PrintSyntaxTree(buffer string) {
t.AST().Print(os.Stdout, buffer)
}
func (t *tokens32) WriteSyntaxTree(w io.Writer, buffer string) {
t.AST().Print(w, buffer)
}
func (t *tokens32) PrettyPrintSyntaxTree(buffer string) {
t.AST().PrettyPrint(os.Stdout, buffer)
}
func (t *tokens32) Add(rule pegRule, begin, end, index uint32) {
tree, i := t.tree, int(index)
if i >= len(tree) {
t.tree = append(tree, token32{pegRule: rule, begin: begin, end: end})
return
}
tree[i] = token32{pegRule: rule, begin: begin, end: end}
}
func (t *tokens32) Tokens() []token32 {
return t.tree
}
type Types struct {
Buffer string
buffer []rune
rules [8]func() bool
parse func(rule ...int) error
reset func()
Pretty bool
tokens32
}
func (p *Types) Parse(rule ...int) error {
return p.parse(rule...)
}
func (p *Types) Reset() {
p.reset()
}
type textPosition struct {
line, symbol int
}
type textPositionMap map[int]textPosition
func translatePositions(buffer []rune, positions []int) textPositionMap {
length, translations, j, line, symbol := len(positions), make(textPositionMap, len(positions)), 0, 1, 0
sort.Ints(positions)
search:
for i, c := range buffer {
if c == '\n' {
line, symbol = line+1, 0
} else {
symbol++
}
if i == positions[j] {
translations[positions[j]] = textPosition{line, symbol}
for j++; j < length; j++ {
if i != positions[j] {
continue search
}
}
break search
}
}
return translations
}
type parseError struct {
p *Types
max token32
}
func (e *parseError) Error() string {
tokens, err := []token32{e.max}, "\n"
positions, p := make([]int, 2*len(tokens)), 0
for _, token := range tokens {
positions[p], p = int(token.begin), p+1
positions[p], p = int(token.end), p+1
}
translations := translatePositions(e.p.buffer, positions)
format := "parse error near %v (line %v symbol %v - line %v symbol %v):\n%v\n"
if e.p.Pretty {
format = "parse error near \x1B[34m%v\x1B[m (line %v symbol %v - line %v symbol %v):\n%v\n"
}
for _, token := range tokens {
begin, end := int(token.begin), int(token.end)
err += fmt.Sprintf(format,
rul3s[token.pegRule],
translations[begin].line, translations[begin].symbol,
translations[end].line, translations[end].symbol,
strconv.Quote(string(e.p.buffer[begin:end])))
}
return err
}
func (p *Types) PrintSyntaxTree() {
if p.Pretty {
p.tokens32.PrettyPrintSyntaxTree(p.Buffer)
} else {
p.tokens32.PrintSyntaxTree(p.Buffer)
}
}
func (p *Types) WriteSyntaxTree(w io.Writer) {
p.tokens32.WriteSyntaxTree(w, p.Buffer)
}
func (p *Types) SprintSyntaxTree() string {
var bldr strings.Builder
p.WriteSyntaxTree(&bldr)
return bldr.String()
}
func Pretty(pretty bool) func(*Types) error {
return func(p *Types) error {
p.Pretty = pretty
return nil
}
}
func Size(size int) func(*Types) error {
return func(p *Types) error {
p.tokens32 = tokens32{tree: make([]token32, 0, size)}
return nil
}
}
func (p *Types) Init(options ...func(*Types) error) error {
var (
max token32
position, tokenIndex uint32
buffer []rune
)
for _, option := range options {
err := option(p)
if err != nil {
return err
}
}
p.reset = func() {
max = token32{}
position, tokenIndex = 0, 0
p.buffer = []rune(p.Buffer)
if len(p.buffer) == 0 || p.buffer[len(p.buffer)-1] != endSymbol {
p.buffer = append(p.buffer, endSymbol)
}
buffer = p.buffer
}
p.reset()
_rules := p.rules
tree := p.tokens32
p.parse = func(rule ...int) error {
r := 1
if len(rule) > 0 {
r = rule[0]
}
matches := p.rules[r]()
p.tokens32 = tree
if matches {
p.Trim(tokenIndex)
return nil
}
return &parseError{p, max}
}
add := func(rule pegRule, begin uint32) {
tree.Add(rule, begin, position, tokenIndex)
tokenIndex++
if begin != position && position > max.end {
max = token32{rule, begin, position}
}
}
matchDot := func() bool {
if buffer[position] != endSymbol {
position++
return true
}
return false
}
/*matchChar := func(c byte) bool {
if buffer[position] == c {
position++
return true
}
return false
}*/
/*matchRange := func(lower byte, upper byte) bool {
if c := buffer[position]; c >= lower && c <= upper {
position++
return true
}
return false
}*/
_rules = [...]func() bool{
nil,
/* 0 MainRule <- <(Types EOF)> */
func() bool {
position0, tokenIndex0 := position, tokenIndex
{
position1 := position
if !_rules[ruleTypes]() {
goto l0
}
if !_rules[ruleEOF]() {
goto l0
}
add(ruleMainRule, position1)
}
return true
l0:
position, tokenIndex = position0, tokenIndex0
return false
},
/* 1 Types <- <((GenericType / TupleType / Type) (_ '|' _ Types)?)> */
func() bool {
position2, tokenIndex2 := position, tokenIndex
{
position3 := position
{
position4, tokenIndex4 := position, tokenIndex
if !_rules[ruleGenericType]() {
goto l5
}
goto l4
l5:
position, tokenIndex = position4, tokenIndex4
if !_rules[ruleTupleType]() {
goto l6
}
goto l4
l6:
position, tokenIndex = position4, tokenIndex4
if !_rules[ruleType]() {
goto l2
}
}
l4:
{
position7, tokenIndex7 := position, tokenIndex
if !_rules[rule_]() {
goto l7
}
if buffer[position] != rune('|') {
goto l7
}
position++
if !_rules[rule_]() {
goto l7
}
if !_rules[ruleTypes]() {
goto l7
}
goto l8
l7:
position, tokenIndex = position7, tokenIndex7
}
l8:
add(ruleTypes, position3)
}
return true
l2:
position, tokenIndex = position2, tokenIndex2
return false
},
/* 2 _ <- <(' ' / '\t')*> */
func() bool {
{
position10 := position
l11:
{
position12, tokenIndex12 := position, tokenIndex
{
position13, tokenIndex13 := position, tokenIndex
if buffer[position] != rune(' ') {
goto l14
}
position++
goto l13
l14:
position, tokenIndex = position13, tokenIndex13
if buffer[position] != rune('\t') {
goto l12
}
position++
}
l13:
goto l11
l12:
position, tokenIndex = position12, tokenIndex12
}
add(rule_, position10)
}
return true
},
/* 3 EOF <- <!.> */
func() bool {
position15, tokenIndex15 := position, tokenIndex
{
position16 := position
{
position17, tokenIndex17 := position, tokenIndex
if !matchDot() {
goto l17
}
goto l15
l17:
position, tokenIndex = position17, tokenIndex17
}
add(ruleEOF, position16)
}
return true
l15:
position, tokenIndex = position15, tokenIndex15
return false
},
/* 4 Type <- <(([A-Z] / [a-z]) ([A-Z] / [a-z] / [0-9])*)> */
func() bool {
position18, tokenIndex18 := position, tokenIndex
{
position19 := position
{
position20, tokenIndex20 := position, tokenIndex
if c := buffer[position]; c < rune('A') || c > rune('Z') {
goto l21
}
position++
goto l20
l21:
position, tokenIndex = position20, tokenIndex20
if c := buffer[position]; c < rune('a') || c > rune('z') {
goto l18
}
position++
}
l20:
l22:
{
position23, tokenIndex23 := position, tokenIndex
{
position24, tokenIndex24 := position, tokenIndex
if c := buffer[position]; c < rune('A') || c > rune('Z') {
goto l25
}
position++
goto l24
l25:
position, tokenIndex = position24, tokenIndex24
if c := buffer[position]; c < rune('a') || c > rune('z') {
goto l26
}
position++
goto l24
l26:
position, tokenIndex = position24, tokenIndex24
if c := buffer[position]; c < rune('0') || c > rune('9') {
goto l23
}
position++
}
l24:
goto l22
l23:
position, tokenIndex = position23, tokenIndex23
}
add(ruleType, position19)
}
return true
l18:
position, tokenIndex = position18, tokenIndex18
return false
},
/* 5 GenericType <- <(Type _ '[' _ Types? _ ']')> */
func() bool {
position27, tokenIndex27 := position, tokenIndex
{
position28 := position
if !_rules[ruleType]() {
goto l27
}
if !_rules[rule_]() {
goto l27
}
if buffer[position] != rune('[') {
goto l27
}
position++
if !_rules[rule_]() {
goto l27
}
{
position29, tokenIndex29 := position, tokenIndex
if !_rules[ruleTypes]() {
goto l29
}
goto l30
l29:
position, tokenIndex = position29, tokenIndex29
}
l30:
if !_rules[rule_]() {
goto l27
}
if buffer[position] != rune(']') {
goto l27
}
position++
add(ruleGenericType, position28)
}
return true
l27:
position, tokenIndex = position27, tokenIndex27
return false
},
/* 6 TupleType <- <('(' _ Types _ (',' _ Types)+ _ ')')> */
func() bool {
position31, tokenIndex31 := position, tokenIndex
{
position32 := position
if buffer[position] != rune('(') {
goto l31
}
position++
if !_rules[rule_]() {
goto l31
}
if !_rules[ruleTypes]() {
goto l31
}
if !_rules[rule_]() {
goto l31
}
if buffer[position] != rune(',') {
goto l31
}
position++
if !_rules[rule_]() {
goto l31
}
if !_rules[ruleTypes]() {
goto l31
}
l33:
{
position34, tokenIndex34 := position, tokenIndex
if buffer[position] != rune(',') {
goto l34
}
position++
if !_rules[rule_]() {
goto l34
}
if !_rules[ruleTypes]() {
goto l34
}
goto l33
l34:
position, tokenIndex = position34, tokenIndex34
}
if !_rules[rule_]() {
goto l31
}
if buffer[position] != rune(')') {
goto l31
}
position++
add(ruleTupleType, position32)
}
return true
l31:
position, tokenIndex = position31, tokenIndex31
return false
},
}
p.rules = _rules
return nil
}