-
Notifications
You must be signed in to change notification settings - Fork 0
/
earley_stringer.go
147 lines (139 loc) · 3.01 KB
/
earley_stringer.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
package fmr
import (
"fmt"
"io"
"math/big"
"strconv"
"strings"
)
func (ts *TableState) String() string {
s := ""
switch ts.Term.Type {
case Nonterminal:
if ts.Rb != nil {
for i, term := range ts.Rb.Terms {
if i == ts.Dot {
s += DOT + " "
}
switch term.Type {
case Nonterminal:
s += "<" + term.Value + "> "
case Terminal:
s += strconv.Quote(term.Value) + " "
case Any:
s += "(any) "
case List:
s += "(list<" + term.Value + ">) "
}
}
if ts.Dot == len(ts.Rb.Terms) {
s += DOT
}
return fmt.Sprintf("<%s> -> %s [%d-%d] {%s}",
ts.Term.Value, s, ts.Start, ts.End, ts.Rb.F)
}
case Any:
for i := ts.Start; i < ts.End; i++ {
s += "# "
}
s += DOT + " * "
return fmt.Sprintf("(any) -> %s [%d-%d]", s, ts.Start, ts.End)
case List:
f := "fmr.list("
for i := 0; i < ts.Dot; i++ {
s += "<" + ts.Term.Value + "> "
f += fmt.Sprintf("$%d", i+1)
if i != ts.Dot-1 {
f += ","
}
}
f += ")"
s += DOT + " * "
return fmt.Sprintf("(list<%s>) -> %s [%d-%d] {%s}", ts.Term.Value, s, ts.Start, ts.End, f)
}
return fmt.Sprintf("%s [%d-%d]", strconv.Quote(ts.Term.Value), ts.Start, ts.End)
}
func (tc *TableColumn) String() (out string) {
if tc.index == 0 {
out = "[0] ''\n"
} else {
out = fmt.Sprintf("[%d] '%s' position:[%d-%d]\n",
tc.index, tc.token, tc.token.StartByte, tc.token.EndByte)
}
out += "=======================================\n"
for _, s := range tc.states {
out += s.String() + "\n"
}
return out
}
func (p *Parse) String() string {
out := ""
for _, c := range p.columns {
out += c.String() + "\n"
}
return out
}
// Print this tree to out
func (n *Node) Print(out io.Writer) {
n.printLevel(out, 0)
}
func (n *Node) printLevel(out io.Writer, level int) {
indentation := ""
for i := 0; i < level; i++ {
indentation += " "
}
fmt.Fprintf(out, "%s%v\n", indentation, n.Value)
for _, child := range n.Children {
child.printLevel(out, level+1)
}
}
func (n *Node) String() string {
if len(n.Children) > 0 {
return fmt.Sprintf("%+v %+v", n.Value, n.Children)
}
return fmt.Sprintf("%+v", n.Value)
}
func (f *FMR) String() string {
if f == nil {
return "nf.I($0)"
}
var args []string
invalid := "invalid_fmr"
for _, arg := range f.Args {
switch arg.Type {
case "string":
if s, ok := arg.Value.(string); ok {
args = append(args, strconv.Quote(s))
} else {
return invalid
}
case "int":
if i, ok := arg.Value.(*big.Int); ok {
args = append(args, i.String())
} else {
return invalid
}
case "float":
if f, ok := arg.Value.(*big.Float); ok {
args = append(args, f.String())
} else {
return invalid
}
case "func":
if fmr, ok := arg.Value.(*FMR); ok {
args = append(args, fmr.String())
} else {
return invalid
}
case "index":
if i, ok := arg.Value.(int); ok {
args = append(args, fmt.Sprintf("$%d", i))
} else {
return invalid
}
default:
return invalid
}
}
return fmt.Sprintf("%s(%s)", f.Fn, strings.Join(args, ","))
}