forked from brettlangdon/forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
355 lines (318 loc) · 8.09 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
package forge
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"github.com/go-aah/forge/token"
)
func isSemicolonOrNewline(id token.TokenID) bool {
return id == token.SEMICOLON || id == token.NEWLINE
}
// Parser is a struct to hold data necessary for parsing a config from a scanner
type Parser struct {
files []string
settings *Section
scanner *Scanner
curTok token.Token
curSection *Section
previous []*Section
}
// NewParser will create and initialize a new Parser from a provided io.Reader
func NewParser(reader io.Reader) *Parser {
settings := NewSection()
return &Parser{
files: make([]string, 0),
scanner: NewScanner(reader),
settings: settings,
curSection: settings,
previous: make([]*Section, 0),
}
}
// NewFileParser will create and initialize a new Parser from a provided from a filename string
func NewFileParser(filename string) (*Parser, error) {
reader, err := open(filename)
if err != nil {
return nil, err
}
parser := NewParser(reader)
parser.addFile(filename)
return parser, nil
}
func (parser *Parser) addFile(filename string) {
parser.files = append(parser.files, filename)
}
func (parser *Parser) hasParsed(search string) bool {
for _, filename := range parser.files {
if filename == search {
return true
}
}
return false
}
func (parser *Parser) syntaxError(msg string) error {
msg = fmt.Sprintf(
"syntax error line <%d> column <%d>: %s",
parser.curTok.Line,
parser.curTok.Column,
msg,
)
return errors.New(msg)
}
func (parser *Parser) readToken() token.Token {
parser.curTok = parser.scanner.NextToken()
return parser.curTok
}
func (parser *Parser) skipNewlines() {
for parser.curTok.ID == token.NEWLINE {
parser.readToken()
}
}
func (parser *Parser) parseList() ([]Value, error) {
var values []Value
for {
parser.skipNewlines()
value, err := parser.parseSettingValue()
if err != nil {
return nil, err
}
values = append(values, value)
if parser.curTok.ID == token.COMMA {
parser.readToken()
}
parser.skipNewlines()
if parser.curTok.ID == token.RBRACKET {
parser.readToken()
break
}
}
return values, nil
}
func (parser *Parser) parseReference(startingSection *Section, period bool) (Value, error) {
name := ""
if !period {
name = parser.curTok.Literal
}
for {
parser.readToken()
if parser.curTok.ID == token.PERIOD && !period {
period = true
} else if period && parser.curTok.ID == token.IDENTIFIER {
if len(name) > 0 {
name += "."
}
name += parser.curTok.Literal
period = false
} else if isSemicolonOrNewline(parser.curTok.ID) {
break
} else {
msg := fmt.Sprintf("expected 'SEMICOLON' or 'NEWLINE' instead found '%s'", parser.curTok.Literal)
return nil, parser.syntaxError(msg)
}
}
if len(name) == 0 {
return nil, parser.syntaxError(
fmt.Sprintf("expected IDENTIFIER instead found %s", parser.curTok.Literal),
)
}
if period {
return nil, parser.syntaxError(fmt.Sprintf("expected IDENTIFIER after PERIOD"))
}
return NewReference(name, startingSection), nil
}
func (parser *Parser) parseSettingValue() (Value, error) {
var value Value
readNext := true
switch parser.curTok.ID {
case token.STRING:
value = NewString(parser.curTok.Literal)
case token.BOOLEAN:
boolVal, err := strconv.ParseBool(parser.curTok.Literal)
if err != nil {
return value, nil
}
value = NewBoolean(boolVal)
case token.NULL:
value = NewNull()
case token.INTEGER:
intVal, err := strconv.ParseInt(parser.curTok.Literal, 10, 64)
if err != nil {
return value, err
}
value = NewInteger(intVal)
case token.FLOAT:
floatVal, err := strconv.ParseFloat(parser.curTok.Literal, 64)
if err != nil {
return value, err
}
value = NewFloat(floatVal)
case token.PERIOD:
reference, err := parser.parseReference(parser.curSection, true)
if err != nil {
return value, err
}
value = reference
readNext = false
case token.IDENTIFIER:
reference, err := parser.parseReference(parser.settings, false)
if err != nil {
return value, err
}
value = reference
readNext = false
case token.ENVIRONMENT:
var envVal = os.Getenv(parser.curTok.Literal)
value = NewString(envVal)
case token.LBRACKET:
parser.readToken()
listVal, err := parser.parseList()
if err != nil {
return value, err
}
value = NewList()
_ = value.UpdateValue(listVal)
readNext = false
default:
return value, parser.syntaxError(
fmt.Sprintf("expected STRING, INTEGER, FLOAT, BOOLEAN or IDENTIFIER, instead found %s", parser.curTok.ID),
)
}
if readNext {
parser.readToken()
}
return value, nil
}
func (parser *Parser) parseSetting(name string) error {
parser.readToken()
value, err := parser.parseSettingValue()
if err != nil {
return err
}
if !isSemicolonOrNewline(parser.curTok.ID) {
msg := fmt.Sprintf("expected 'SEMICOLON' or 'NEWLINE' instead found '%s'", parser.curTok.Literal)
return parser.syntaxError(msg)
}
parser.readToken()
parser.curSection.Set(name, value)
return nil
}
func (parser *Parser) parseInclude() error {
if parser.curTok.ID != token.STRING {
msg := fmt.Sprintf("expected STRING instead found '%s'", parser.curTok.ID)
return parser.syntaxError(msg)
}
pattern := parser.curTok.Literal
parser.readToken()
if !isSemicolonOrNewline(parser.curTok.ID) {
msg := fmt.Sprintf("expected 'SEMICOLON' or 'NEWLINE' instead found '%s'", parser.curTok.Literal)
return parser.syntaxError(msg)
}
// if it is not absolute path, resolve to relative from parent config directory
if !filepath.IsAbs(pattern) && len(parser.files) > 0 {
pattern = filepath.Join(filepath.Dir(parser.files[0]), filepath.Clean(pattern))
}
filenames, err := glob(pattern)
if err != nil {
return err
}
oldScanner := parser.scanner
for _, filename := range filenames {
// We have already visited this file, don't include again
// DEV: This can cause recursive includes if this isn't here :o
if parser.hasParsed(filename) {
continue
}
reader, err := open(filename)
if err != nil {
return err
}
parser.curSection.AddInclude(filename)
parser.scanner = NewScanner(reader)
err = parser.parse()
if err != nil {
return err
}
// Make sure to add the filename to the internal list to ensure we don't
// accidentally recursively include config files
parser.addFile(filename)
}
parser.scanner = oldScanner
parser.readToken()
return nil
}
func (parser *Parser) parseSection(name string) error {
section := parser.curSection.AddSection(name)
parser.previous = append(parser.previous, parser.curSection)
parser.curSection = section
return nil
}
func (parser *Parser) endSection() error {
if len(parser.previous) == 0 {
return parser.syntaxError("unexpected section end '}'")
}
pLen := len(parser.previous)
previous := parser.previous[pLen-1]
parser.previous = parser.previous[0 : pLen-1]
parser.curSection = previous
return nil
}
func (parser *Parser) parse() error {
parser.readToken()
for {
if parser.curTok.ID == token.EOF {
break
}
tok := parser.curTok
parser.readToken()
switch tok.ID {
case token.COMMENT:
parser.curSection.AddComment(tok.Literal)
case token.INCLUDE:
err := parser.parseInclude()
if err != nil {
return err
}
case token.IDENTIFIER:
if parser.curTok.ID == token.LBRACE {
err := parser.parseSection(tok.Literal)
if err != nil {
return err
}
parser.readToken()
} else if parser.curTok.ID == token.EQUAL {
err := parser.parseSetting(tok.Literal)
if err != nil {
return err
}
}
case token.RBRACE:
err := parser.endSection()
if err != nil {
return err
}
case token.NEWLINE:
// Ignore extra newlines
continue
default:
return parser.syntaxError(fmt.Sprintf("unexpected token %s", tok))
}
}
return nil
}
// GetSettings will fetch the parsed settings from this Parser
func (parser *Parser) GetSettings() *Section {
return parser.settings
}
// Parse will tell the Parser to parse all settings from the config
func (parser *Parser) Parse() error {
err := parser.parse()
if err != nil {
return err
}
if len(parser.previous) > 0 {
return parser.syntaxError("expected end of section, instead found EOF")
}
return nil
}