-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjp.go
226 lines (211 loc) · 4.76 KB
/
jp.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
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
package jp
import (
"bufio"
"errors"
"io"
"strings"
"unicode"
)
type dict struct {
indented string
objEmpty string
objOpen string
objClose string
arrEmpty string
arrOpen string
arrClose string
colon string
comma string
strOpen string
strClose string
otherSpace string
otherOpen string
otherClose string
end string
}
var dicts = map[string]dict{
"pretty16": {"\n ", "\033[0;32m{ }", "\033[0;32m{\n", "\033[0;32m\n}", "\033[0;32m[ ]", "\033[0;32m[\n", "\033[0;32m\n]", "\033[0;32m: ", "\033[0;32m,\n", "\033[0m\"\033[1m", "\033[0m\"", "\033[0;41m \033[0m\033[1;33m", "\033[1;33m", "", "\033[0m\n"},
"compact16": {"\n", "\033[0;32m{}", "\033[0;32m{", "\033[0;32m}", "\033[0;32m[]", "\033[0;32m[", "\033[0;32m]", "\033[0;32m:", "\033[0;32m,", "\033[0m\"\033[1m", "\033[0m\"", "\033[0;41m \033[0m\033[1;33m", "\033[1;33m", "", "\033[0m\n"},
"pretty": {"\n ", "{ }", "{\n", "\n}", "[ ]", "[\n", "\n]", ": ", ",\n", `"`, `"`, " ", "", "", "\n"},
"compact": {"\n", "{}", "{", "}", "[]", "[", "]", ":", ",", `"`, `"`, " ", "", "", "\n"},
}
func (d dict) indent() dict {
return dict{
d.indented,
strings.Replace(d.objEmpty, "\n", d.indented, 1),
strings.Replace(d.objOpen, "\n", d.indented, 1),
strings.Replace(d.objClose, "\n", d.indented, 1),
strings.Replace(d.arrEmpty, "\n", d.indented, 1),
strings.Replace(d.arrOpen, "\n", d.indented, 1),
strings.Replace(d.arrClose, "\n", d.indented, 1),
strings.Replace(d.colon, "\n", d.indented, 1),
strings.Replace(d.comma, "\n", d.indented, 1),
strings.Replace(d.strOpen, "\n", d.indented, 1),
strings.Replace(d.strClose, "\n", d.indented, 1),
d.otherSpace,
strings.Replace(d.otherOpen, "\n", d.indented, 1),
strings.Replace(d.otherClose, "\n", d.indented, 1),
d.end,
}
}
type scanner struct {
r *bufio.Reader
w *bufio.Writer
indentSize int
indentDicts []dict
dict *dict
}
func (s scanner) writeRune(r rune) (e error) {
_, e = s.w.WriteRune(r)
return e
}
func (s scanner) writeString(str string) (e error) {
_, e = s.w.WriteString(str)
return e
}
func (s *scanner) indent(d int) {
s.indentSize += d
if len(s.indentDicts) <= s.indentSize {
s.indentDicts = append(s.indentDicts, s.indentDicts[len(s.indentDicts)-1].indent())
}
s.dict = &s.indentDicts[s.indentSize]
}
func (s scanner) unread() {
e := s.r.UnreadRune()
if e != nil {
panic(e.Error()) // we only ever read runes, so this shouldn't happen
}
}
func (s scanner) read() (r rune, e error) {
for e == nil {
r, _, e = s.r.ReadRune()
if !unicode.IsSpace(r) {
break
}
}
return r, e
}
func (s scanner) copyString() (e error) {
var r rune
var last rune
e = s.writeString(s.dict.strOpen)
for e == nil {
r, _, e = s.r.ReadRune()
if e != nil {
break
}
if r == '"' && last != '\\' {
e = s.writeString(s.dict.strClose)
break
} else if last != '\\' {
last = r
e = s.writeRune(r)
} else {
last = 0
e = s.writeRune(r)
}
}
return e
}
func (s scanner) copyOther() (e error) {
var r rune
var space bool
e = s.writeString(s.dict.otherOpen)
for e == nil {
r, _, e = s.r.ReadRune()
if e != nil {
break
}
switch r {
case '{', '}', '[', ']', ',', ':', '"':
s.unread()
e = s.writeString(s.dict.otherClose)
return e
default:
if unicode.IsSpace(r) {
space = true
break
}
if space {
space = false
e = s.writeString(s.dict.otherSpace)
if e != nil {
return e
}
}
e = s.writeRune(r)
}
}
return e
}
func (s scanner) expand() (e error) {
var r rune
for e == nil {
r, e = s.read()
if e != nil {
break
}
switch r {
case '{':
r, e = s.read()
if e != nil {
break
}
if r == '}' {
e = s.writeString(s.dict.objEmpty)
} else {
s.unread()
s.indent(1)
e = s.writeString(s.dict.objOpen)
}
case '}':
s.indent(-1)
e = s.writeString(s.dict.objClose)
case '[':
r, e = s.read()
if e != nil {
break
}
if r == ']' {
e = s.writeString(s.dict.arrEmpty)
} else {
s.unread()
s.indent(1)
e = s.writeString(s.dict.arrOpen)
}
case ']':
s.indent(-1)
e = s.writeString(s.dict.arrClose)
case ',':
e = s.writeString(s.dict.comma)
case ':':
e = s.writeString(s.dict.colon)
case '"':
e = s.copyString()
// todo unicode.ReplacementChar
default:
s.unread()
e = s.copyOther()
}
}
s.writeString(s.dict.end)
s.w.Flush()
if e == io.EOF {
return nil
}
return e
}
func Expand(reader io.Reader, writer io.Writer, format string) error {
d, ok := dicts[format]
if !ok {
return errors.New("unknown format")
}
indentDicts := []dict{d}
s := &scanner{
r: bufio.NewReader(reader),
w: bufio.NewWriter(writer),
indentDicts: indentDicts,
dict: &indentDicts[0],
}
return s.expand()
}