-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparser.go
230 lines (198 loc) · 5.49 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
package vdf
import (
"bytes"
"fmt"
"io"
)
// Parser represents a parser.
type Parser struct {
s *Scanner
buf struct {
tok Token // last read token
lit string // last read literal
n int // buffer size (max=1)
}
}
// NewParser returns a new instance of Parser.
func NewParser(r io.Reader) *Parser {
return &Parser{s: NewScanner(r)}
}
// scan returns the next token from the underlying scanner.
// If a token has been unscanned then read that instead.
func (p *Parser) scan(respectWhitespace bool) (Token, string) {
// If we have a token on the buffer, then return it.
if p.buf.n != 0 {
p.buf.n = 0
return p.buf.tok, p.buf.lit
}
// Otherwise read the next token from the scanner.
tok, lit := p.s.Scan(respectWhitespace)
// Save it to the buffer in case we unscan later.
p.buf.tok, p.buf.lit = tok, lit
return tok, lit
}
// unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.buf.n = 1 }
// scanIgnoreWSAndComments ignores whitespace, end-of-line and comment tokens
// during scanning. It returns the next token + string that is not WS, EOL or CommentDoubleSlash
func (p *Parser) scanIgnoreWSAndComments() (Token, string) {
tok, lit := p.scan(false)
// If we have a whitespace, just continue scanning until the next token appears
for tok == WS || tok == EOL {
tok, lit = p.scan(false)
}
// If we have a comment, we need to drop the complete line, because
// the text would be detect as an ident. But the text after a "//"
// is part of the comment. So we ignore the complete line.
if tok == CommentDoubleSlash {
// Scan until the next line ending
for {
tok, _ = p.scan(true)
if tok == EOL {
break
}
}
return p.scanIgnoreWSAndComments()
}
return tok, lit
}
// Parse is the main entry point of the vdf parser.
// If parsed the complete VDF content and returns
// a map as a key / value pair.
// The value is a string (normal value) or a map[string]interface{}
// again if there is a nested structure.
func (p *Parser) Parse() (map[string]interface{}, error) {
m := make(map[string]interface{})
key := ""
// The first part is a simple ident (as a main map key)
tok, lit, err := p.scanMapKey()
if err != nil {
return nil, err
}
if tok != Ident {
return nil, fmt.Errorf("found %q, expected an ident as a first part", lit)
}
key = lit
tok, lit = p.scanIgnoreWSAndComments()
if tok != CurlyBraceOpen {
return nil, fmt.Errorf("found %q, expected a curly brace as second part (to open up the first level)", lit)
}
p.unscan()
m[key], err = p.parseMap()
if err != nil {
return nil, err
}
return m, nil
}
func (p *Parser) scanMapKey() (Token, string, error) {
tok, lit := p.scanIgnoreWSAndComments()
// Get the key
if tok == QuotationMark {
return p.scanIdentSurroundedQuotationMark()
} else if tok == Ident {
return tok, lit, nil
}
return Illegal, lit, nil
}
func (p *Parser) parseMap() (map[string]interface{}, error) {
m := make(map[string]interface{})
key := ""
// The first part should be a open curly brace
tok, _ := p.scanIgnoreWSAndComments()
if tok != CurlyBraceOpen {
return m, nil
}
var err error
for {
// At first: A key
tok, lit := p.scanIgnoreWSAndComments()
switch tok {
case QuotationMark:
_, key, err = p.scanIdentSurroundedQuotationMark()
if err != nil {
return nil, err
}
case Ident:
key = lit
// The default statement would also cover tokens like:
// - CommentDoubleSlash
// |-> Should in theory never happen, because comments
// are handled by scanIgnoreWSAndComments() already
// - CurlyBraceClose
default:
return m, nil
}
// After this: A value or a map again
tok, lit = p.scanIgnoreWSAndComments()
switch tok {
case QuotationMark:
_, m[key], err = p.scanIdentSurroundedQuotationMark()
if err != nil {
return nil, err
}
case Ident:
m[key] = lit
case CurlyBraceOpen:
p.unscan()
m1, err := p.parseMap()
if err != nil {
return nil, err
}
mergeMap(m, m1, key)
// The default statement would also cover tokens like:
// - CurlyBraceClose
default:
return m, nil
}
}
}
func (p *Parser) scanIdentSurroundedQuotationMark() (Token, string, error) {
var buf bytes.Buffer
escaped := false
terminated := false
for {
tok, lit := p.scan(true)
if tok == EOF && !terminated {
return tok, "", ErrNotValidFormat
}
if tok == QuotationMark && !escaped {
terminated = true
// We don`t unscan here, because
// we don`t need this quotation mark anymore.
break
}
// If the current character is escaped and it is NOT an escape sequence
// set character handling back to normal.
if escaped && tok != EscapeSequence {
escaped = false
}
// If we have an escape sequence and the current state is not escaped
// mark the next character as escaped.
if tok == EscapeSequence && !escaped {
escaped = true
continue
}
buf.WriteString(lit)
// If the current character is escaped and it is a backslash
// reset the character handing.
// This is only triggered if you want to add a \ into a key or a value.
// Then you have to add "\\".
if escaped && tok == EscapeSequence {
escaped = false
}
}
return Ident, buf.String(), nil
}
// VDF files can contain duplicates of keys, when this occurs we need to merge the existing map and the returned map
func mergeMap(m, r map[string]interface{}, key string) map[string]interface{} {
if _, ok := m[key]; !ok {
m[key] = r
} else {
for k, v := range r {
if t, ok := m[key].(map[string]interface{}); ok {
t[k] = v
}
}
}
return m
}