-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
195 lines (181 loc) · 4.28 KB
/
Copy pathparser.go
File metadata and controls
195 lines (181 loc) · 4.28 KB
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
package flatjson
import (
"encoding/json"
"fmt"
"io"
"strconv"
)
type unexpectedError struct {
tok token
lit string
expected []token
}
func newErrUnexpected(tok token, lit string, expected ...token) error {
return &unexpectedError{
tok: tok,
lit: lit,
expected: expected,
}
}
func (e unexpectedError) Error() string {
return fmt.Sprintf("expected %v, found %s", e.expected, e.tok.formatLiteral(e.lit))
}
type parser struct {
s *scanner
buf struct {
tok token
lit string
n int
}
}
type assignment struct {
identifier string
properties []interface{}
value interface{}
}
func newParser(r io.Reader) *parser {
return &parser{
s: newScanner(r),
}
}
func (p *parser) parseAssignment() (*assignment, error) {
tok, lit := p.scanIgnoreWhitespaceAndComments()
if tok != tokenIdentifier {
p.unscan()
return nil, newErrUnexpected(tok, lit, tokenIdentifier)
}
identifier := lit
var properties []interface{}
FOR:
for {
tok, lit := p.scanIgnoreWhitespaceAndComments()
switch {
case tok == token('='):
p.unscan()
break FOR
case tok == token('.') || tok == token('['):
p.unscan()
property, err := p.parsePropertyAccess()
if err != nil {
return nil, err
}
properties = append(properties, property)
default:
return nil, newErrUnexpected(tok, lit, token('='), token('.'), token('['))
}
}
tok, lit = p.scanIgnoreWhitespaceAndComments()
if tok != token('=') {
return nil, newErrUnexpected(tok, lit, token('='))
}
value, err := p.parseValue()
if err != nil {
return nil, err
}
tok, lit = p.scanIgnoreWhitespaceAndComments()
if tok != token(';') {
return nil, newErrUnexpected(tok, lit, token(';'))
}
return &assignment{
identifier: identifier,
properties: properties,
value: value,
}, nil
}
func (p *parser) parseAssignments() ([]*assignment, error) {
var assignments []*assignment
FOR:
for {
tok, lit := p.scanIgnoreWhitespaceAndComments()
switch tok {
case tokenEOF:
break FOR
case tokenIdentifier:
p.unscan()
assignment, err := p.parseAssignment()
if err != nil {
return nil, err
}
assignments = append(assignments, assignment)
default:
return nil, newErrUnexpected(tok, lit, tokenEOF, tokenIdentifier)
}
}
return assignments, nil
}
func (p *parser) parsePropertyAccess() (interface{}, error) {
tok, lit := p.scanIgnoreWhitespaceAndComments()
switch tok {
case token('.'):
switch tok, lit := p.scanIgnoreWhitespaceAndComments(); tok {
case tokenIdentifier:
return lit, nil
default:
return nil, newErrUnexpected(tok, lit, tokenIdentifier)
}
case token('['):
var property interface{}
switch tok, lit := p.scanIgnoreWhitespaceAndComments(); tok {
case tokenNumber:
property64, _ := strconv.ParseUint(lit, 10, 64)
property = int(property64)
case tokenString:
property = lit
default:
return nil, newErrUnexpected(tok, lit, tokenNumber, tokenString)
}
if tok, lit := p.scanIgnoreWhitespaceAndComments(); tok != token(']') {
return nil, newErrUnexpected(tok, lit, token(']'))
}
return property, nil
default:
return nil, newErrUnexpected(tok, lit, token('.'), token('['))
}
}
func (p *parser) parseValue() (interface{}, error) {
tok, lit := p.scanIgnoreWhitespaceAndComments()
switch tok {
case tokenNumber:
return json.Number(lit), nil
case tokenString:
return lit, nil
case tokenFalse:
return false, nil
case tokenTrue:
return true, nil
case tokenNull:
return nil, nil
case '[':
tok, lit := p.scanIgnoreWhitespaceAndComments()
if tok != token(']') {
return nil, newErrUnexpected(tok, lit, token(']'))
}
return []interface{}{}, nil
case '{':
tok, lit := p.scanIgnoreWhitespaceAndComments()
if tok != token('}') {
return nil, newErrUnexpected(tok, lit, token('}'))
}
return make(map[string]interface{}), nil
default:
return nil, newErrUnexpected(tok, lit, tokenNumber, tokenString, tokenFalse, tokenTrue, tokenNull, token('['), token('{'))
}
}
func (p *parser) scan() (token, string) {
if p.buf.n > 0 {
p.buf.n = 0
} else {
p.buf.tok, p.buf.lit = p.s.scan()
}
return p.buf.tok, p.buf.lit
}
func (p *parser) scanIgnoreWhitespaceAndComments() (token, string) {
for {
if tok, lit := p.scan(); tok != tokenWhitespace && tok != tokenComment {
return tok, lit
}
}
}
func (p *parser) unscan() {
p.buf.n = 1
}