-
Notifications
You must be signed in to change notification settings - Fork 20
/
prettyprinter.go
171 lines (152 loc) · 4.67 KB
/
prettyprinter.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
package quamina
import (
"fmt"
"math/rand"
"strings"
)
// printer is an interface used to generate representations of Quamina data structures to facilitate
// debugging and optimization. It's an interface rather than a type so that a null implementation can
// be provided for production that should incur very little performance cost.
type printer interface {
labelTable(table *smallTable, label string)
printNFA(table *smallTable) string
shortPrintNFA(table *smallTable) string
}
// nullPrinter is what the name says, a do-nothing implementation of the printer interface which ideally
// should consume close to zero CPU cycles.
type nullPrinter struct{}
const noPP = "prettyprinting not enabled"
func (*nullPrinter) labelTable(_ *smallTable, _ string) {
}
func (*nullPrinter) printNFA(_ *smallTable) string {
return noPP
}
func (*nullPrinter) shortPrintNFA(_ *smallTable) string {
return noPP
}
var sharedNullPrinter = &nullPrinter{}
// prettyPrinter makes a human-readable representation of a NFA; each smallTable may be
// given a label and as a side effect will get a random 3-digit serial number. For an example
// of the output, see the functions TestPP and TestNullPP in prettyprinter_test.go
type prettyPrinter struct {
randInts rand.Source
tableLabels map[*smallTable]string
tableSerials map[*smallTable]uint
}
func newPrettyPrinter(seed int) *prettyPrinter {
return &prettyPrinter{
randInts: rand.NewSource(int64(seed)),
tableLabels: make(map[*smallTable]string),
tableSerials: make(map[*smallTable]uint),
}
}
func (pp *prettyPrinter) tableSerial(t *smallTable) uint {
return pp.tableSerials[t]
}
func (pp *prettyPrinter) tableLabel(t *smallTable) string {
return pp.tableLabels[t]
}
func (pp *prettyPrinter) labelTable(table *smallTable, label string) {
pp.tableLabels[table] = label
newSerial := pp.randInts.Int63()%500 + 500
//nolint:gosec
pp.tableSerials[table] = uint(newSerial)
}
func (pp *prettyPrinter) printNFA(t *smallTable) string {
return pp.printNFAStep(&faState{table: t}, 0, make(map[*smallTable]bool))
}
func (pp *prettyPrinter) printNFAStep(fas *faState, indent int, already map[*smallTable]bool) string {
t := fas.table
trailer := "\n"
if len(fas.fieldTransitions) != 0 {
trailer = fmt.Sprintf(" [%d transition(s)]\n", len(fas.fieldTransitions))
}
s := " " + pp.printTable(t) + trailer
for _, step := range t.steps {
if step != nil {
for _, state := range step.states {
_, ok := already[state.table]
if !ok {
already[state.table] = true
s += pp.printNFAStep(state, indent+1, already)
}
}
}
}
return s
}
func (pp *prettyPrinter) printTable(t *smallTable) string {
// going to build a string rep of a smallTable based on the unpacked form
// each line is going to be a range like
// 'c' .. 'e' => %X
// lines where the *faNext is nil are omitted
// TODO: Post-nfa-rationalization, I don't think the whole defTrans thing is necessary any more?
var rows []string
unpacked := unpackTable(t)
var rangeStart int
var b int
defTrans := unpacked[0]
// TODO: Try to generate an NFA with a state with multiple epsilons
if len(t.epsilon) != 0 {
fas := ""
for i, eps := range t.epsilon {
ep := &faNext{states: []*faState{eps}}
if i != 0 {
fas += ", "
}
fas += pp.nextString(ep)
}
rows = append(rows, "ε → "+fas)
}
for {
for b < len(unpacked) && unpacked[b] == nil {
b++
}
if b == len(unpacked) {
break
}
rangeStart = b
lastN := unpacked[b]
for b < len(unpacked) && unpacked[b] == lastN {
b++
}
if lastN != defTrans {
row := ""
if b == rangeStart+1 {
row += fmt.Sprintf("'%s'", branchChar(byte(rangeStart)))
} else {
row += fmt.Sprintf("'%s'…'%s'", branchChar(byte(rangeStart)), branchChar(byte(b-1)))
}
row += " → " + pp.nextString(lastN)
rows = append(rows, row)
}
}
serial := pp.tableSerial(t)
label := pp.tableLabel(t)
if defTrans != nil {
dtString := "★ → " + pp.nextString(defTrans)
return fmt.Sprintf("%d[%s] ", serial, label) + strings.Join(rows, " / ") + " / " + dtString
} else {
return fmt.Sprintf("%d[%s] ", serial, label) + strings.Join(rows, " / ")
}
}
func (pp *prettyPrinter) nextString(n *faNext) string {
var snames []string
for _, step := range n.states {
snames = append(snames, fmt.Sprintf("%d[%s]",
pp.tableSerial(step.table), pp.tableLabel(step.table)))
}
return strings.Join(snames, " · ")
}
func branchChar(b byte) string {
switch b {
// TODO: Figure out how to test commented-out cases
case valueTerminator:
return "ℵ"
default:
return fmt.Sprintf("%c", b)
}
}
func (pp *prettyPrinter) shortPrintNFA(table *smallTable) string {
return fmt.Sprintf("%d[%s]", pp.tableSerials[table], pp.tableLabels[table])
}