-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
714 lines (655 loc) · 17.3 KB
/
Copy pathparse.go
File metadata and controls
714 lines (655 loc) · 17.3 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package klon
import (
"strconv"
"strings"
"github.com/ProCode-Software/klar/internal/lexer"
"github.com/ProCode-Software/klar/internal/ranges"
"github.com/ProCode-Software/klar/pkg/klon/ast"
"github.com/ProCode-Software/klar/pkg/klon/klonerrs"
"github.com/ProCode-Software/klar/pkg/klon/klonflags"
)
const MaxDepth = 10000
// parseDocument parses a full Klon document.
func (rd *reader) parseDocument() (*ast.Document, []error) {
res := rd.parseValue()
// Check for EOF
rd.skipLines()
if tok := rd.currTok(); tok.Kind != EOF {
rd.tokenError(klonerrs.ErrExpectedEOF, tok, "Expected end of file")
}
return &ast.Document{
BaseNode: ast.BaseNode{ranges.Range{res.Pos().Start, rd.offset}},
Body: res,
Variables: rd.vars,
Comments: rd.comments,
}, rd.errs
}
// parseValue parses one or more values on a single line. parseValue may also
// parse an object or list if a dash is encountered on the next line.
func (rd *reader) parseValue() ast.Value {
tok := rd.currTok()
// Skip leading newlines
if tok.Kind == Newline {
old := rd.removeParseFlags(objectValue)
rd.skipLines()
tok = rd.currTok()
// `value:\n - x` and `\n x` technically start with the same depth
isTopLevel := rd.depth == 0 && old&objectValue == 0
rd.resetParseFlags(old)
// Object as a value
if tok.Kind == Dash {
if !isTopLevel {
rd.depthUp()
defer rd.depthDown()
}
return rd.parseObject()
}
}
// A. Top-level object
if rd.depth == 0 && tok.Kind != LeftBracket && tok.Kind != LeftCurly {
if (rd.parseFlags & key) == 0 {
if peek := rd.peekTok(); peek.Kind == Colon || peek.Kind == Dot {
return rd.parseObject()
}
} else if tok.Kind == Dash {
return rd.parseObject()
}
}
// B. EOF
if tok.Kind == EOF {
return &ast.None{BaseNode: ast.BaseNode{Range: tok.Range()}}
}
// C. Read 1+ items in a group
var items []ast.Value
startLine := tok.Pos.Line
loop:
for rd.hasTokens() && rd.currTok().Pos.Line == startLine {
var res ast.Value
switch tok = rd.currTok(); tok.Kind {
case EOF, Newline:
break loop
case Comma:
if len(items) == 0 {
rd.tokenError(klonerrs.ErrExpectedValue, tok, "Expected a value before comma")
rd.advanceTok()
}
break loop
case RightBracket, RightCurly:
if len(items) == 0 {
rd.tokenError(klonerrs.ErrUnmatchedBracket, tok, "'%s' should be escaped", tok.Src)
rd.advanceTok()
}
break loop
case Number:
res = rd.parseNumber(tok)
case String:
res = rd.parseString(tok)
case None:
res = rd.parseNone(tok)
case Boolean:
res = rd.parseBoolean(tok)
case AtRef:
res = rd.parseClass(tok)
case Variable:
res = rd.parseVariable(tok)
case Arrow:
res = rd.parseRest(tok)
case LeftBracket:
res = rd.parseInlineList(tok)
case LeftCurly:
res = rd.parseBracedObject(tok)
case Dash:
// Invalid dash
if rd.depth == 0 {
rd.tokenError(
klonerrs.ErrDashAtTopLevel, tok,
"Top-level objects and lists can't start with a dash",
)
// Still parse the object
rd.depthUp()
res = rd.parseObject()
rd.depthDown()
res = &ast.Bad{BaseNode: ast.BaseNode{res.Pos()}, Value: res}
} else {
rd.tokenError(
klonerrs.ErrDashWithoutNewline, tok,
"An object or list must begin on a new line",
)
rd.advanceTok()
continue
}
case Colon, Dot:
if (rd.parseFlags & key) != 0 {
break loop
}
fallthrough
default:
rd.tokenError(klonerrs.ErrUnexpectedToken, tok, "Unexpected '%s'", tok.Src)
rd.advanceTok()
continue
}
items = append(items, res)
}
switch len(items) {
case 0:
return &ast.None{BaseNode: ast.BaseNode{Range: tok.Range()}}
case 1:
return items[0]
default:
// More than 1 item should be in a StringGroup
return &ast.StringGroup{
BaseNode: ast.BaseNode{Range: sliceRange(items)},
Values: items,
}
}
}
// parseNumber parses a numeric literal.
func (rd *reader) parseNumber(num Token) *ast.Number {
float, err := strconv.ParseFloat(num.Src, 64)
if err != nil {
panic(err) // Shouldn't happen
}
rd.advanceTok()
return &ast.Number{
BaseNode: ast.BaseNode{Range: num.Range()},
Source: num.Src,
Value: float,
}
}
// parseRest parses a spread operator reference (<- $var).
func (rd *reader) parseRest(arrow Token) *ast.ArrowRef {
rd.advanceTok() // <-
if k := rd.currTok().Kind; k != Variable {
rd.tokenError(klonerrs.ErrExpectedVarInArrow, arrow, "Expected a variable after '<-'")
return &ast.ArrowRef{}
}
varRef := rd.parseVariable(rd.currTok())
return &ast.ArrowRef{
BaseNode: ast.BaseNode{ranges.Range{arrow.Pos, varRef.Pos().End}},
Var: varRef,
}
}
// parseVariable parses a variable reference ($name or ${name}).
func (rd *reader) parseVariable(vr Token) *ast.VarRef {
rd.advanceTok()
var (
name string
err = vr.Attrs["err"].(klonerrs.Code)
braces = vr.Attrs["brace"].(bool)
)
if braces {
if err == klonerrs.ErrUnterminatedVar {
name = vr.Src[2:]
rd.tokenError(err, vr, "Expected closing '}' in variable reference")
} else {
name = vr.Src[2 : len(vr.Src)-1]
}
} else {
name = vr.Src[1:]
}
if err == klonerrs.ErrInvalidIdentifier {
rd.tokenError(err, vr, "A variable name can't start with a digit")
}
return &ast.VarRef{
BaseNode: ast.BaseNode{Range: vr.Range()},
Name: name,
Braces: braces,
}
}
// parseInlineList parses an inline list literal [...].
func (rd *reader) parseInlineList(lb Token) *ast.List {
oldFlags := rd.addParseFlags(noComma | allowDot)
defer rd.resetParseFlags(oldFlags)
rd.depthUp()
defer rd.depthDown()
rd.advanceTok() // [
var items []ast.Value
for rd.hasTokens() && rd.currTok().Kind != RightBracket {
items = append(items, rd.parseValue())
rd.skipLines()
if rd.currTok().Kind != RightBracket {
rd.expect(
Comma, klonerrs.ErrExpectedToken,
"Expected ',' between list items or ']' to end the list",
)
}
}
rd.skipLines()
rb := rd.expect(RightBracket, klonerrs.ErrUnterminatedList, "Expected ']' to end list")
return &ast.List{
BaseNode: ast.BaseNode{ranges.Range{lb.Pos, rb.Pos}},
Inline: true,
Items: items,
}
}
// parseString parses a quoted string literal.
func (rd *reader) parseString(str Token) *ast.String {
rd.advanceTok()
var (
wrap bool
src string
quote rune
)
if str.Attrs == nil || str.Attrs["quote"].(rune) == 0 {
src = str.Src
} else {
wrap = str.Attrs["wrap"].(bool)
quote = str.Attrs["quote"].(rune)
src = str.Src[1:]
if wrap {
src = str.Src[2:]
}
if str.Attrs["unterm"].(bool) {
rd.tokenError(klonerrs.ErrUnterminatedString, str, "This string was left open")
} else {
src = src[:len(src)-1]
}
}
// Read segments
// TODO: move segment parsing to lexer
var b strings.Builder
b.Grow(len(src))
var segments []string
for i := 0; i < len(src); i++ {
c := src[i]
switch c {
case '\\':
if i+1 >= len(src) {
continue // End of file; error already reported for unterminated
}
c := src[i+1]
char, ok := getEscape(c)
if ok {
b.WriteByte(char)
i++ // Skip escape letter
continue
}
// Unknown escape; keep in string
rd.tokenError(klonerrs.ErrUnknownEscape, str, `Unknown escape sequence '\%c'`, c)
b.WriteByte('\\')
b.WriteByte(c)
case '$':
if i+1 >= len(src) {
// '$' is last character; leave
b.WriteByte(c)
continue
}
// Read a variable name
start := i + 1
if src[start] == '{' {
start++
}
var end int
for i, r := range src[start:] {
if r == '}' || !isValidIdentChar(r) {
end = i
break
}
}
// Write normalized variable name as segment (no braces)
varName := src[start : start+end]
if len(varName) == 0 {
b.WriteByte('$')
continue
}
// End current segment
segments = append(segments, b.String())
b.Reset()
// Add variable as own segment
b.WriteByte('$')
b.WriteString(varName)
segments = append(segments, b.String())
b.Reset()
default:
b.WriteByte(c)
}
}
return &ast.String{
BaseNode: ast.BaseNode{Range: str.Range()},
Raw: src,
Value: segments,
Wrap: wrap,
Quote: quote,
}
}
// getEscape returns the unescaped byte for a given escape character.
func getEscape(c byte) (byte, bool) {
escapes := map[byte]byte{
'f': '\f', 'n': '\n', 'r': '\r', 't': '\t', 'v': '\v', 'e': '\x1b',
'\\': '\\', '"': '"', '\'': '\'', '$': '$',
}
esc, ok := escapes[c]
return esc, ok
}
// parseBoolean parses a boolean literal.
func (rd *reader) parseBoolean(b Token) *ast.Boolean {
rd.advanceTok()
return &ast.Boolean{
BaseNode: ast.BaseNode{Range: b.Range()},
Value: b.Attrs["value"].(bool),
}
}
// parseNone parses a 'none' literal.
func (rd *reader) parseNone(none Token) *ast.None {
rd.advanceTok()
return &ast.None{BaseNode: ast.BaseNode{Range: none.Range()}, Literal: true}
}
// parseClass parses a class name (@identifier).
func (rd *reader) parseClass(cls Token) *ast.Class {
rd.advanceTok() // @
if cls.Attrs != nil && cls.Attrs["invalid"] == true {
rd.tokenError(klonerrs.ErrInvalidIdentifier, cls, "A class name can't start with a digit")
}
return &ast.Class{
BaseNode: ast.BaseNode{cls.Range()},
Name: cls.Src[1:],
}
}
// parseList parses a line-separated list of values. The first value was
// already parsed by [reader.parseEntry].
func (rd *reader) parseList(first ast.Value) *ast.List {
items := []ast.Value{first}
old := rd.addParseFlags(allowDot)
defer rd.resetParseFlags(old)
// objectValue was already removed by parseObject. It will be restored by its defer call.
for rd.hasTokens() {
if !rd.checkDashes(rd.parseDashes()) {
break
}
// TODO: will an item that starts with a newline and indents be parsed?
old := rd.removeParseFlags(objectValue)
items = append(items, rd.parseValue())
rd.resetParseFlags(old)
}
return &ast.List{
BaseNode: ast.BaseNode{Range: sliceRange(items)},
Items: items,
}
}
// parseObject parses an unbraced object or dashed list.
// It determines the type of the collection (List or Object) based on the first entry.
func (rd *reader) parseObject() ast.Value {
old := rd.removeParseFlags(objectValue)
defer rd.addParseFlags(old)
var (
fields []*ast.Field
start = rd.currTok().Pos
isObject bool
)
for rd.hasTokens() {
if rd.skipLines(); rd.currTok().Kind == EOF {
break
}
item, dashes := rd.parseEntry(isObject)
if dashes < rd.depth {
break
// TODO: return
}
var field *ast.Field
var needsNl bool
switch item := item.(type) {
case nil:
goto next // Variable declaration
case *ast.Field:
field = item
// If the value was an object, the newline was consumed before dedenting,
// so we don't need to check for a newline.
needsNl = needsNewlineAfter(item.Value)
case *ast.ArrowRef:
field = &ast.Field{Arrow: item}
default:
return rd.parseList(item) // It's a list
}
isObject = true
fields = append(fields, field)
next:
if rd.currTok().Kind != EOF && needsNl {
if rd.currTok().Kind == RightCurly {
break // This is an object inside a braced object value
}
rd.expect(Newline, klonerrs.ErrExpectedToken, "Expected a newline between fields")
}
}
if len(fields) == 0 {
// It didn't successfully read a field, due to variable declaration
return &ast.None{}
}
end := fields[len(fields)-1].Range.End
return &ast.Object{
BaseNode: ast.BaseNode{ranges.Range{start, end}},
Fields: fields,
Inline: false,
}
}
func needsNewlineAfter(item ast.Value) bool {
switch item := item.(type) {
case *ast.Object:
return item.Inline
case *ast.List:
return item.Inline
}
return true
}
// parseBracedObject parses a braced object { ... }.
func (rd *reader) parseBracedObject(lc Token) ast.Value {
rd.depthUp()
defer rd.depthDown()
old := rd.addParseFlags(noComma)
defer rd.resetParseFlags(old)
old2 := rd.removeParseFlags(objectValue)
defer rd.addParseFlags(old2)
// Dash depth resets in braced objects
oldDepth := rd.depth
rd.depth = 0
defer func() { rd.depth = oldDepth }()
rd.advanceTok() // {
var fields []*ast.Field
for rd.hasTokens() && rd.currTok().Kind != RightCurly {
field, dashes := rd.parseEntry(true)
if dashes < rd.depth {
break
}
fields = append(fields, field.(*ast.Field))
if curr := rd.currTok(); curr.Kind != RightCurly {
if curr.Kind != Comma && curr.Kind != Newline {
rd.tokenError(
klonerrs.ErrExpectedToken, curr,
"Expected ',' or newline to separate inline object fields",
)
continue
}
rd.advanceTok()
}
}
rc := rd.expect(
RightCurly, klonerrs.ErrUnterminatedObject,
"Expected '}' to close inline object",
)
return &ast.Object{
BaseNode: ast.BaseNode{ranges.Range{lc.Pos, rc.End()}},
Fields: fields,
Inline: true,
}
}
// parseEntry parses a single entry within an object or list block.
// The entry can be keyed or unkeyed, or a rest. Variable declarations
// are handled and return a nil entry when encountered. If the entry is a
// rest, an [*ast.ArrowRef] is returned. If isObject == true, an error is
// reported if the entry doesn't have a key.
func (rd *reader) parseEntry(forceObject bool) (entry ast.Value, dashes int) {
var (
varName *ast.VarRef
singleKey ast.Value
path *[]ast.Value
keyStart lexer.Position
value ast.Value
)
// Check dashes
dashes = rd.parseDashes()
if !rd.checkDashes(dashes) {
return nil, dashes
}
switch tok := rd.currTok(); tok.Kind {
case Arrow:
// Rest
return rd.parseRest(tok), dashes
case Variable:
// Variable declaration
varName = rd.parseVariable(tok)
singleKey = varName
default:
// Normal key or list item
singleKey, path, keyStart = rd.parseKey(forceObject)
}
// Key-value field
if rd.currTok().Kind == Colon {
rd.advanceTok() // :
old := rd.addParseFlags(allowDot | objectValue)
value = rd.parseValue()
rd.resetParseFlags(old)
// If the variable is a field, then it's a declaration
if varName != nil {
rd.declareVariable(varName, value)
return nil, dashes
}
return &ast.Field{
BaseNode: ast.BaseNode{Range: ranges.Range{Start: keyStart, End: value.Pos().End}},
Key: singleKey,
KeyPath: path,
Value: value,
}, dashes
}
// Unkeyed list item
// =====
// If we read a key-path, it has to be converted to a single value
if path != nil {
singleKey = rd.convertKeyPath(path)
}
// If forceObject == true, report an error because this should be a key-value pair
if forceObject {
rd.rangeError(
klonerrs.ErrExpectedKeyValue, singleKey.Pos(),
"Expected a key-value pair in this object",
)
singleKey = &ast.Field{Key: &ast.Bad{Value: singleKey}}
}
return singleKey, dashes
}
func (rd *reader) declareVariable(name *ast.VarRef, value ast.Value) {
switch {
case rd.flags.Has(klonflags.NoVariables):
rd.rangeError(
klonerrs.ErrVarsDisabled, name.Range,
"Variables aren't allowed to be declared in this file",
)
case rd.depth != 0:
rd.rangeError(
klonerrs.ErrVarNotTopLevel, name.Range,
"Variables must be declared at the top level",
)
case name.Braces:
rd.rangeError(
klonerrs.ErrInvalidVarDecl, name.Range,
"Variable declarations can't use braces",
)
case rd.vars != nil && rd.vars[name.Name] != nil:
existing := rd.vars[name.Name]
rd.rangeError(
klonerrs.ErrVarAlreadyDeclared, name.Range,
"Variable '%s' was already declared at %s", name.Name, existing.Pos(),
)
default:
if rd.vars == nil {
rd.vars = make(map[string]ast.Value)
}
rd.vars[name.Name] = value
}
}
// convertKeyPath converts a dot-path to a StringGroup value.
func (rd *reader) convertKeyPath(path *[]ast.Value) ast.Value {
return &ast.StringGroup{
BaseNode: ast.BaseNode{sliceRange(*path)},
Values: *path,
}
}
// parseKey parses a key for a field. The key can be either a single value,
// or a dot-path.
func (rd *reader) parseKey(forceObject bool) (singleKey ast.Value, dotPath *[]ast.Value,
start lexer.Position,
) {
validate := func(v ast.Value) bool {
switch v.(type) {
case *ast.String, *ast.Number, *ast.Bad, *ast.Boolean:
return true
default:
if !forceObject {
return false
}
rd.rangeError(
klonerrs.ErrInvalidKey, v.Pos(),
"A field key must be a string, number, or boolean",
)
return false
}
}
old := rd.addParseFlags(key)
defer rd.resetParseFlags(old)
// Single key
singleKey = rd.parseValue()
start = singleKey.Pos().Start
if !validate(singleKey) {
// An invalid key may be a valid list item
if !forceObject {
return singleKey, nil, start
}
singleKey = &ast.Bad{Value: singleKey}
}
if rd.currTok().Kind != Dot {
return singleKey, nil, start
}
// Dot-separated key path
dotPath = &[]ast.Value{singleKey}
for rd.currTok().Kind == Dot {
rd.advanceTok() // .
singleKey = rd.parseValue()
if !validate(singleKey) {
singleKey = &ast.Bad{Value: singleKey}
}
*dotPath = append(*dotPath, singleKey)
}
return nil, dotPath, start
}
// parseDashes parses consecutive dashes on a line, returning the count.
func (rd *reader) parseDashes() (n int) {
rd.skipLines()
if rd.lastDashes != -1 {
return rd.lastDashes
}
for rd.hasTokens() && rd.currTok().Kind == Dash {
n++
rd.advanceTok()
}
rd.lastDashes = n
return n
}
func (rd *reader) checkDashes(n int) bool {
if n < rd.depth {
return false // Dedent
}
if n > rd.depth+1 {
// Too many dashes
if rd.depth == 0 {
rd.tokenError(
klonerrs.ErrDashSkip, rd.currTok(),
"The top level object shouldn't include a dash",
)
} else {
rd.tokenError(
klonerrs.ErrDashSkip, rd.currTok(),
"Too many dashes: expected up to %d, there are %d", rd.depth+1, n,
)
}
return true // For recovery
}
return true
}