-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
268 lines (218 loc) · 3.87 KB
/
parser.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
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
267
268
// Package brashtag provides parser for parsing
// files written in brashtag notation.
package brashtag
import (
"fmt"
"strings"
)
type Node interface {
fmt.Stringer
isNode()
}
type bag struct {
tag string
kids []Node
}
// Bag has a tag and children.
type Bag struct {
*bag
}
var _ Node = Bag{}
func (Bag) isNode() {}
func NewBag(tag string, kids ...Node) Bag {
return Bag{
&bag{
tag: tag,
kids: kids,
},
}
}
func (b Bag) AddKids(kids ...Node) {
b.kids = append(b.kids, kids...)
}
func (b Bag) RemoveKid(i int) {
if i < len(b.kids) {
b.kids = append(b.kids[:i], b.kids[i+1:]...)
}
}
func (b Bag) SetTag(tag string) {
b.tag = tag
}
func (b Bag) Kids() []Node {
return b.kids
}
func (b Bag) Tag() string {
return b.tag
}
func (b Bag) String() string {
var text []string
for _, kid := range b.Kids() {
text = append(text, kid.String())
}
return fmt.Sprintf("#%s{%s}", b.tag, strings.Join(text, ""))
}
type code struct {
tag string
text string
}
type Code struct {
*code
}
var _ Node = Code{}
func (Code) isNode() {}
func NewCode(tag, text string) Code {
return Code{
&code{
tag: tag,
text: text,
},
}
}
func (c Code) SetTag(tag string) {
c.tag = tag
}
func (c Code) SetText(text string) {
c.text = text
}
func (c Code) Text() string {
return c.text
}
func (c Code) Tag() string {
return c.tag
}
func (c Code) String() string {
return fmt.Sprintf("%s%s%s", c.tag, c.text, c.tag)
}
type blob struct {
text string
}
type Blob struct {
*blob
}
var _ Node = Blob{}
func (Blob) isNode() {}
func NewBlob(text string) Blob {
return Blob{
&blob{text: text},
}
}
func (b Blob) SetText(text string) {
b.text = text
}
func (b Blob) Text() string {
return b.text
}
func (b Blob) String() string {
return fmt.Sprint(b.text)
}
// Parse parses the text and returns its brashtag tree.
func Parse(text string) (Node, error) {
root, rem, err := parseBag([]byte(text))
if len(rem) != 0 {
n := 100
if len(rem) < 100 {
n = len(rem)
}
msg := rem[:n]
return nil, fmt.Errorf("%w: %s", err, string(msg))
}
if err != nil {
return nil, err
}
return root, nil
}
func parseBag(text []byte) (Node, []byte, error) {
root := NewBag("")
c := 0
bagOpen := false
for c < len(text) {
if text[c] != '{' {
c++
} else {
bagOpen = true
break
}
}
if !bagOpen && c == len(text) {
return nil, text, fmt.Errorf("unopened bag missing { after #")
}
root.SetTag(string(text[1:c]))
text = text[c+1:]
var k Node
var err error
for len(text) > 0 {
switch {
case text[0] == '#':
k, text, err = parseBag(text)
if err != nil {
return nil, text, err
}
root.AddKids(k)
case text[0] == '`':
k, text, err = parseCode(text)
if err != nil {
return nil, text, err
}
root.AddKids(k)
case text[0] == '}':
return root, text[1:], nil
default:
k, text, err = parseBlob(text)
if err != nil {
return nil, text, err
}
root.AddKids(k)
}
}
return nil, text, fmt.Errorf("bag not closed")
}
func parseCode(text []byte) (Node, []byte, error) {
root := NewCode("", "")
c := 0
for c < len(text) {
if text[c] == '`' {
c++
} else {
break
}
}
root.SetTag(string(text[0:c]))
j := c
for j < len(text)-c {
if string(text[j:j+c]) != root.tag {
j++
} else {
root.SetText(string(text[c:j]))
return root, text[j+c:], nil
}
}
return nil, text, fmt.Errorf("code not closed")
}
func parseBlob(text []byte) (Node, []byte, error) {
root := NewBlob("")
j := 0
loop:
for j < len(text) {
switch text[j] {
case '`', '#', '}':
break loop
case '{':
return nil, text, fmt.Errorf("unexpected {: %s", neighbourhood(text, j, 100))
default:
j++
}
}
root.SetText(string(text[:j]))
return root, text[j:], nil
}
func neighbourhood(text []byte, loc, size int) []byte {
l := loc - size
if l < 0 {
l = 0
}
r := loc + size
if r > len(text) {
r = len(text)
}
return text[l:r]
}