-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathprettyprinter.go
More file actions
266 lines (239 loc) · 7.84 KB
/
Copy pathprettyprinter.go
File metadata and controls
266 lines (239 loc) · 7.84 KB
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
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
tableSerial(t *smallTable) uint
// printSerial(table *smallTable) string
// printState(state *faState) 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
}
func (*nullPrinter) tableSerial(_ *smallTable) uint { return 0 }
// func (*nullPrinter) printSerial(_ *smallTable) string { return noPP }
// func (*nullPrinter) printState(_ *faState) 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()%899 + 100
//nolint:gosec
pp.tableSerials[table] = uint(newSerial)
}
/*
func (pp *prettyPrinter) printSerial(table *smallTable) string {
label := pp.tableLabels[table]
if len(label) == 0 {
label = fmt.Sprintf("%p", table)[7:]
}
return fmt.Sprintf("%d[%s]", pp.tableSerials[table], label)
}
func (pp *prettyPrinter) printState(state *`faState`) string {
return fmt.Sprintf("State @%p table %s", state, pp.printSerial(state.table))
}
*/
// This structure is necessitated by the fact that it seems possible that Quamina sometimes
// generates FAs in which more than one faState has the same smallTable. So to make sure that
// all the states show up in the prettyprint output, we have to match on both the state and
// table.
type ppPair struct {
state *faState
table *smallTable
}
type ppAlready map[ppPair]bool
func newPpAlready() ppAlready {
return make(map[ppPair]bool)
}
func (a ppAlready) sawThis(state *faState, table *smallTable) bool {
return a[ppPair{state, table}]
}
func (a ppAlready) remember(state *faState, table *smallTable) {
a[ppPair{state, table}] = true
}
func (pp *prettyPrinter) printNFA(t *smallTable) string {
// Use the caller's *smallTable pointer for label lookup. Building a
// throwaway faState{table: *t} would re-locate the smallTable in memory
// and lose the label (labels are keyed by address).
return pp.printNFAFromTable(t, newPpAlready())
}
// printNFAFromTable prints starting from a smallTable, using its address for
// label lookup. Used for the start node where there's no real *faState owning
// the requested address.
func (pp *prettyPrinter) printNFAFromTable(t *smallTable, already ppAlready) string {
tableCost := mcSmallTable(t)
stateCost := mcFaStateBase + tableCost - mcSmallTableBase
trailer := fmt.Sprintf("[s/t %d/%d] \n", tableCost, stateCost)
s := " " + pp.printTable(t) + trailer
for _, step := range t.steps {
if step != nil {
s += pp.printNFAStep(step, 1, already)
}
}
for _, step := range t.epsilons {
s += pp.printNFAStep(step, 1, already)
}
return s
}
func (pp *prettyPrinter) printNFAStep(fas *faState, indent int, already ppAlready) string {
t := &fas.table
if already.sawThis(fas, t) {
return ""
}
already.remember(fas, t)
trailer := ""
if len(fas.epsilonClosure) > 0 {
trailer += fmt.Sprintf(" [%d in closure]", len(fas.epsilonClosure))
}
if len(fas.fieldTransitions) != 0 {
trailer += fmt.Sprintf(" [%d transition(s)]", len(fas.fieldTransitions))
}
trailer += fmt.Sprintf("[s/t %d/%d] ", mcSmallTable(&fas.table), mcFaState(fas))
trailer += "\n"
s := " " + pp.printTable(t) + trailer
for _, step := range t.steps {
if step != nil {
s += pp.printNFAStep(step, indent+1, already)
}
}
for _, step := range t.epsilons {
s += pp.printNFAStep(step, indent+1, already)
}
return s
}
func shortTableAddress(p *smallTable) string {
long := fmt.Sprintf("%p", p)
return long[len(long)-4:]
}
/*
func shortStateAddress(p *faState) string {
long := fmt.Sprintf("%p", p)
return long[len(long)-4:]
}
*/
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
var rows []string
unpacked := unpackTable(t)
var rangeStart int
var b byte
defTrans := unpacked[0]
if len(t.epsilons) != 0 {
fas := ""
for i, eps := range t.epsilons {
if i != 0 {
fas += ", "
}
fas += pp.nextString(eps)
}
rows = append(rows, "ε → "+fas)
}
for {
for int(b) < len(unpacked) && unpacked[b] == nil {
b++
}
if b == byte(len(unpacked)) {
break
}
rangeStart = int(b)
lastN := unpacked[b]
for int(b) < len(unpacked) && unpacked[b] == lastN {
b++
}
if lastN != defTrans {
row := ""
if int(b) == rangeStart+1 {
row += fmt.Sprintf("'%s'", branchChar(byte(rangeStart)))
} else {
row += fmt.Sprintf("'%s'…'%s'", branchChar(byte(rangeStart)), branchChar(b-1))
}
row += " → " + "(" + pp.nextString(lastN)
rows = append(rows, row)
}
}
serial := pp.tableSerial(t)
label := pp.tableLabel(t)
if len(label) == 0 {
label = shortTableAddress(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 *faState) string {
label := pp.tableLabel(&n.table)
if len(label) == 0 {
label = shortTableAddress(&n.table)
}
return fmt.Sprintf("%d[%s]", pp.tableSerial(&n.table), label)
}
func branchChar(b byte) string {
replaceStr := []string{
"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", "vt", "np", "cr", "so", "si", "dle",
"dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em", "sub", "esc", "fs", "gs", "rs", "us", "sp",
"!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
":", ";", "<", "=", ">", "?", "@",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z",
"[", "\\", "]", "^", "_", "`",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z",
"{", "|", "}", "~", "del"}
switch b {
case valueTerminator:
return fmt.Sprintf("%x/ℵ", valueTerminator)
default:
if b < 128 {
return fmt.Sprintf("%x/%s", b, replaceStr[b])
} else {
return fmt.Sprintf("%x/", b)
}
}
}
func (pp *prettyPrinter) shortPrintNFA(table *smallTable) string {
return fmt.Sprintf("%d[%s]", pp.tableSerials[table], pp.tableLabels[table])
}