This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprintFragment.go
164 lines (151 loc) · 3.11 KB
/
printFragment.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
package parser
import (
"fmt"
"io"
"os"
"reflect"
)
var snipBlkStart = []byte(" {")
var snipBlkEnd = []byte("}")
var snipLineBreak = []byte("\n")
var snipSpace = []byte(" ")
// FragPrintOptions defines the AST printing options
type FragPrintOptions struct {
Out io.Writer
Indentation []byte
Prefix []byte
LineBreak []byte
Format func(Fragment) (head, body []byte)
}
// PrintFragment prints the fragment structure recursively
func PrintFragment(
fragment Fragment,
options FragPrintOptions,
) (bytesWritten int, err error) {
if options.Out == nil {
// Use stdout by default
options.Out = os.Stdout
}
// write returns true if there was an error,
// otherwise returns false
write := func(str []byte) bool {
var bw int
bw, err = options.Out.Write(str)
if err != nil {
// Abort printing
return true
}
bytesWritten += bw
// Continue printing
return false
}
writePrefix := func() bool {
// Write the prefix if any
if len(options.Prefix) > 0 {
return write(options.Prefix)
}
return false
}
writeLnBrk := func() bool {
if len(options.Indentation) < 1 {
// Write whitespace instead when indentation is disabled
return write(snipSpace)
}
// Write line-break
if options.LineBreak != nil {
// Use specified line-breaks
if write(options.LineBreak) {
return true
}
} else {
// Fallback to the default line-breaks
if write(snipLineBreak) {
return true
}
}
return writePrefix()
}
printIndent := func(ind uint) bool {
// Write the indentation
if len(options.Indentation) > 0 {
for ix := uint(0); ix < ind; ix++ {
if write(options.Indentation) {
return true
}
}
}
return false
}
var printFrag func(ind uint, frag Fragment) bool
printFrag = func(ind uint, frag Fragment) bool {
if printIndent(ind) {
return true
}
// Write the actual line
switch frag := frag.(type) {
case *Construct:
// Print construct recursively
var head, body []byte
if options.Format != nil {
// Use specified stringification method
head, body = options.Format(frag)
}
if head == nil {
// Fallback to the default stringification method
head = []byte(frag.String())
}
if write(head) {
return true
}
if body != nil {
if write(body) {
return true
}
break
}
if write(snipBlkStart) {
return true
}
if writeLnBrk() {
return true
}
for _, subFrag := range frag.VElements {
if printFrag(ind+1, subFrag) {
return true
}
if writeLnBrk() {
return true
}
}
if printIndent(ind) {
return true
}
return write(snipBlkEnd)
case *Token:
// Print leave fragment
var head []byte
if options.Format != nil {
// Use specified stringification method
head, _ = options.Format(frag)
}
if head == nil {
// Fallback to the default stringification method
head = []byte(frag.String())
}
if write(head) {
return true
}
default:
panic(fmt.Errorf(
"unsupported fragment type: %s",
reflect.TypeOf(frag),
))
}
return false
}
if writePrefix() {
return
}
printFrag(0, fragment)
return
}