-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.go
374 lines (316 loc) · 6.85 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2013 Google, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yaml
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
)
// Parse returns a root-level Node parsed from the lines read from r. In
// general, this will be done for you by one of the File constructors.
func Parse(r io.Reader) (node Node, err error) {
lb := &lineBuffer{
Reader: bufio.NewReader(r),
}
defer func() {
if r := recover(); r != nil {
switch r := r.(type) {
case error:
err = r
case string:
err = errors.New(r)
default:
err = fmt.Errorf("%v", r)
}
}
}()
node = parseNode(lb, 0, nil)
return
}
// Supporting types and constants
const (
typUnknown = iota
typSequence
typMapping
typScalar
)
var typNames = []string{
"Unknown", "Sequence", "Mapping", "Scalar",
}
type lineReader interface {
Next(minIndent int) *indentedLine
}
type indentedLine struct {
lineno int
indent int
line []byte
}
func (line *indentedLine) String() string {
return fmt.Sprintf("%2d: %s%s", line.indent,
strings.Repeat(" ", 0*line.indent), string(line.line))
}
func parseNode(r lineReader, ind int, initial Node) (node Node) {
first := true
node = initial
// read lines
for {
line := r.Next(ind)
if line == nil {
break
}
if len(line.line) == 0 {
continue
}
if first {
ind = line.indent
first = false
}
types := []int{}
pieces := []string{}
var inlineValue func([]byte)
inlineValue = func(partial []byte) {
// TODO(kevlar): This can be a for loop now
vtyp, brk := getType(partial)
begin, end := partial[:brk], partial[brk:]
if vtyp == typMapping {
end = end[1:]
}
end = bytes.TrimLeft(end, " ")
switch vtyp {
case typScalar:
types = append(types, typScalar)
pieces = append(pieces, string(end))
return
case typMapping:
types = append(types, typMapping)
pieces = append(pieces, strings.TrimSpace(string(begin)))
trimmed := bytes.TrimSpace(end)
if len(trimmed) == 1 && trimmed[0] == '|' {
text := ""
for {
l := r.Next(1)
if l == nil {
break
}
s := string(l.line)
s = strings.TrimSpace(s)
if len(s) == 0 {
break
}
text = text + "\n" + s
}
types = append(types, typScalar)
pieces = append(pieces, string(text))
return
}
inlineValue(end)
case typSequence:
types = append(types, typSequence)
pieces = append(pieces, "-")
inlineValue(end)
}
}
inlineValue(line.line)
var prev Node
// Nest inlines
for len(types) > 0 {
last := len(types) - 1
typ, piece := types[last], pieces[last]
var current Node
if last == 0 {
current = node
}
//child := parseNode(r, line.indent+1, typUnknown) // TODO allow scalar only
// Add to current node
switch typ {
case typScalar: // last will be == nil
if _, ok := current.(Scalar); current != nil && !ok {
panic("cannot append scalar to non-scalar node")
}
if current != nil {
current = Scalar(piece) + " " + current.(Scalar)
break
}
current = Scalar(piece)
case typMapping:
var mapNode Map
var ok bool
var child Node
// Get the current map, if there is one
if mapNode, ok = current.(Map); current != nil && !ok {
_ = current.(Map) // panic
} else if current == nil {
mapNode = make(Map)
}
if _, inlineMap := prev.(Scalar); inlineMap && last > 0 {
current = Map{
piece: prev,
}
break
}
child = parseNode(r, line.indent+1, prev)
mapNode[piece] = child
current = mapNode
case typSequence:
var listNode List
var ok bool
var child Node
// Get the current list, if there is one
if listNode, ok = current.(List); current != nil && !ok {
_ = current.(List) // panic
} else if current == nil {
listNode = make(List, 0)
}
if _, inlineList := prev.(Scalar); inlineList && last > 0 {
current = List{
prev,
}
break
}
child = parseNode(r, line.indent+1, prev)
listNode = append(listNode, child)
current = listNode
}
if last < 0 {
last = 0
}
types = types[:last]
pieces = pieces[:last]
prev = current
}
node = prev
}
return
}
func getType(line []byte) (typ, split int) {
if len(line) == 0 {
return
}
if line[0] == '-' {
typ = typSequence
split = 1
return
}
typ = typScalar
if line[0] == ' ' || line[0] == '"' {
return
}
// the first character is real
// need to iterate past the first word
// things like "foo:" and "foo :" are mappings
// everything else is a scalar
idx := bytes.IndexAny(line, " \":")
if idx < 0 {
return
}
if line[idx] == '"' {
return
}
if line[idx] == ':' {
typ = typMapping
split = idx
} else if line[idx] == ' ' {
// we have a space
// need to see if its all spaces until a :
for i := idx; i < len(line); i++ {
switch ch := line[i]; ch {
case ' ':
continue
case ':':
// only split on colons followed by a space
if i+1 < len(line) && line[i+1] != ' ' {
continue
}
typ = typMapping
split = i
break
default:
break
}
}
}
if typ == typMapping && split+1 < len(line) && line[split+1] != ' ' {
typ = typScalar
split = 0
}
return
}
// lineReader implementations
type lineBuffer struct {
*bufio.Reader
readLines int
pending *indentedLine
}
func (lb *lineBuffer) Next(min int) (next *indentedLine) {
if lb.pending == nil {
var (
read []byte
more bool
err error
)
l := new(indentedLine)
l.lineno = lb.readLines
more = true
for more {
read, more, err = lb.ReadLine()
if err != nil {
if err == io.EOF {
return nil
}
panic(err)
}
l.line = append(l.line, read...)
}
lb.readLines++
for _, ch := range l.line {
switch ch {
case ' ':
l.indent += 1
continue
default:
}
break
}
l.line = l.line[l.indent:]
// Ignore blank lines and comments.
if len(l.line) == 0 || l.line[0] == '#' {
return lb.Next(min)
}
lb.pending = l
}
next = lb.pending
if next.indent < min {
return nil
}
lb.pending = nil
return
}
type lineSlice []*indentedLine
func (ls *lineSlice) Next(min int) (next *indentedLine) {
if len(*ls) == 0 {
return nil
}
next = (*ls)[0]
if next.indent < min {
return nil
}
*ls = (*ls)[1:]
return
}
func (ls *lineSlice) Push(line *indentedLine) {
*ls = append(*ls, line)
}