forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
54 lines (43 loc) · 1.08 KB
/
printer.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
package participle
import (
"fmt"
"reflect"
"strings"
)
func dumpNode(v node) string {
seen := map[reflect.Value]bool{}
return nodePrinter(seen, v)
}
func nodePrinter(seen map[reflect.Value]bool, v node) string {
if seen[reflect.ValueOf(v)] {
return "<>"
}
seen[reflect.ValueOf(v)] = true
switch n := v.(type) {
case disjunction:
out := []string{}
for _, n := range n {
out = append(out, nodePrinter(seen, n))
}
return strings.Join(out, "|")
case *strct:
return fmt.Sprintf("strct(type=%s, expr=%s)", n.typ, nodePrinter(seen, n.expr))
case sequence:
out := []string{}
for _, n := range n {
out = append(out, nodePrinter(seen, n))
}
return fmt.Sprintf("(%s)", strings.Join(out, " "))
case *reference:
return fmt.Sprintf("@(field=%s, node=%s)", n.field.Name, nodePrinter(seen, n.node))
case *tokenReference:
return fmt.Sprintf("token(%q)", n.identifier)
case *optional:
return fmt.Sprintf("[%s]", nodePrinter(seen, n.node))
case *repetition:
return fmt.Sprintf("{ %s }", nodePrinter(seen, n.node))
case *literal:
return n.String()
}
return "?"
}