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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
// Me: Mom can we have textproto?
// Mom: no we have textproto at home
// textproto at home:
//
// The ccl language has similar semantics to JSON, the only exception being the
// lack of null.
//
// # Comments
//
// There are two types of comments, line comments and C-style comments. Line
// comments are written with # or //, and extend from there to the end of the
// line. C-style comments are written with /* and */, and like C they may not
// be nested.
//
// # Comments are important
// // in a configuration language
// /* what do I know */
//
// # Numbers
//
// Numbers are written in base 10 and can optionally have a fractional part or
// an exponent written with "e" or "E". As a special case, a number prefixed
// with "0x" or "0X" can be written in base 16.
//
// 100
// -30
// 0xabc
// -0xdef
// 13.5
// 1e100
//
// Leading zeros are not permitted in decimal numbers, due to potential
// confusion with octal (which is not supported).
//
// As a lexical matter, numbers must be separated from subsequent field names by
// intervening whitespace or comments:
//
// # invalid
// field1:10field2:20
// # ok
// field1:10 field2:20
//
// # Strings
//
// Strings are written with " or ' and any sequence of intermediate bytes (with
// the exception of escape sequences which are described below). Strings must be
// valid UTF-8 after escape sequences are expanded.
//
// 'asdf'
// "that's cool"
// "\tall\n\tyour\n\tfavorite\n\tescape\n\tsequences"
//
// Note that strings can contain newline without needing an escape sequence
//
// 'a multiline
// string'
//
// Backslash characters inside a string are interpreted as an escape sequence.
// Any escape sequence not described below is an error. The escape sequences
// are identical to C11, with the exception that \x always takes exactly 2
// hex characters.
//
// \' single quote 0x27
// \" double quote 0x22
// \? question mark 0x3f (why is this in C)
// \\ backslash 0x5c
// \a bell 0x07
// \b backspace 0x07
// \f form feed 0x0c
// \n newline 0x0a
// \r carriage return 0x0d
// \t tab 0x09
// \v vertical tab 0x0b
//
// \nnn 3-digit octal value nnn
// \xnn 2-digit hex value nn
// \unnnn unicode code point U+nnnn
// \Unnnnnnnn unicode code point U+nnnnnnnn (UTF8)
//
// As an extension to the C11 escapes, a backslash immediately before a newline
// character (0x0a) will remove the newline character from the resulting string
// (and for you Microsoft Windows users, backslash followed by \r\n is
// also removed)
//
// 'backslash also can \
// remove newlines'
// # equivalent to
// 'backslash also can remove newlines'
//
// If multiple string literals are written next to each other with only
// whitespace or comments in between, the result is to concatenate the strings
//
// 'multiple strings' " concatenated"
// # equivalent to
// 'multiple strings concatenated'
//
// # Bool
//
// Bool values can be true or false (classic), and are written using one of the
// below strings.
//
// true
// yes
// on
//
// false
// no
// off
//
// # Lists
//
// Lists are written with square brackets and elements are separated by comma.
//
// [1, 2, 3]
// [{nested: "messages"}, {are: "also"}, {allowed: yes}]
//
// Trailing comma is allowed
//
// [
// "suck",
// "it",
// "JSON",
// ]
//
// # Messages
//
// Messages are an unordered set of key-value pairs:
//
// {key1: "value1" key2: "value2"}
//
// Keys can be alphanumeric or use underscore; no other characters are
// permitted. Values can be any of the value types here described. Key-value
// pairs must be written with a : between the key and value, except when the
// value is syntactically a message (in that case the colon is optional)
//
// {
// key1: "value1"
// key2 {}
// }
//
// As a special case, when a key is written more than once in a message, it's
// treated the same as if the values had been written in a list. If some of the
// values are already lists, they are appended, preserving the order in which
// the values appear in the input file.
//
// {
// key: [1, 2]
// key: 3
// key: [4, 5, 6]
// }
// # equivalent to
// {
// key: [1, 2, 3, 4, 5, 6]
// }
//
// # Disclaimer
//
// This package is still experimental, expect breaking changes.
package ccl
import (
"bytes"
"encoding"
"encoding/base64"
"errors"
"fmt"
"iter"
"math"
"reflect"
"strconv"
"strings"
"unicode/utf8"
)
type syntaxError struct {
line, col int
reason string
}
func newSyntaxError(data []byte, idx int, reason string, args ...any) error {
line, col := 1, 1
for _, b := range data[:idx] {
if b == '\n' {
line++
col = 1
} else {
col++
}
}
return &syntaxError{line, col, fmt.Sprintf(reason, args...)}
}
func (e *syntaxError) Error() string {
return fmt.Sprintf("%d:%d syntax error: %s", e.line, e.col, e.reason)
}
type structField struct {
ty reflect.Type
name string
}
func fieldMap(out map[structField]int, types map[reflect.Type]bool, s reflect.Type) error {
if types[s] {
// Already processed
return nil
}
types[s] = true
for i := range s.NumField() {
field := s.Field(i)
if !field.IsExported() {
continue
}
fieldName := field.Name
if tag, ok := field.Tag.Lookup("ccl"); ok {
var opts string
fieldName, opts, _ = strings.Cut(tag, ",")
if fieldName == "-" {
continue
}
for opt := range strings.FieldsFuncSeq(opts, func(r rune) bool { return r == ',' }) {
return fmt.Errorf("unknown option %q", opt)
}
}
if _, ok := out[structField{s, fieldName}]; ok {
return fmt.Errorf("multiple fields with name %q", fieldName)
}
out[structField{s, fieldName}] = i
if field.Type.Kind() == reflect.Struct {
if err := fieldMap(out, types, field.Type); err != nil {
return err
}
} else if (field.Type.Kind() == reflect.Pointer || field.Type.Kind() == reflect.Slice) && field.Type.Elem().Kind() == reflect.Struct {
if err := fieldMap(out, types, field.Type.Elem()); err != nil {
return err
}
} else if field.Type.Kind() == reflect.Slice && field.Type.Elem().Kind() == reflect.Pointer && field.Type.Elem().Elem().Kind() == reflect.Struct {
if err := fieldMap(out, types, field.Type.Elem().Elem()); err != nil {
return err
}
}
}
return nil
}
type parser struct {
nextTok func() (token, error, bool)
tok []byte
err error
data []byte
i int
}
func (p *parser) error(reason string, args ...any) error {
return newSyntaxError(p.data, p.i, reason, args...)
}
var errEOF = errors.New("premature EOF")
func (p *parser) peek() ([]byte, error) {
if p.err != nil || p.tok != nil {
return p.tok, p.err
}
tok, err, ok := p.nextTok()
if !ok {
p.err = errEOF
return nil, p.err
}
if err != nil {
p.err = err
return nil, p.err
}
p.tok = tok.b
p.i = tok.i
return p.tok, nil
}
func (p *parser) next() ([]byte, error) {
tok, err := p.peek()
if err != nil {
return nil, err
}
p.tok = nil
return tok, nil
}
func checkNum(b []byte) bool {
if bytes.Equal(b, []byte("0")) {
return true
}
if len(b) == 0 || !(b[0] == '.' || '1' <= b[0] && b[0] <= '9') {
return false
}
haveDigits := false
for ; len(b) > 0 && '0' <= b[0] && b[0] <= '9'; b = b[1:] {
haveDigits = true
}
if len(b) > 0 && b[0] == '.' {
b = b[1:]
for ; len(b) > 0 && '0' <= b[0] && b[0] <= '9'; b = b[1:] {
haveDigits = true
}
}
if !haveDigits {
return false
}
if len(b) == 0 || !(b[0] == 'e' || b[0] == 'E') {
return true
}
b = b[1:]
if len(b) > 0 && b[0] == '-' || b[0] == '+' {
b = b[1:]
}
if len(b) == 0 {
return false
}
for ; len(b) > 0 && '0' <= b[0] && b[0] <= '9'; b = b[1:] {
}
return len(b) == 0
}
type integer struct {
n uint64
sgn int8
}
func (p *parser) parseNum(numBytes []byte) (any, error) {
n := numBytes
var sgn int8 = 1
switch numBytes[0] {
case '-':
sgn = -1
n = numBytes[1:]
case '+':
n = numBytes[1:]
}
if len(n) > 2 && n[0] == '0' && (n[1] == 'x' || n[1] == 'X') {
n, err := strconv.ParseUint(string(n[2:]), 16, 64)
if err != nil {
return nil, p.error("invalid hex number: %s", err)
}
return &integer{n, sgn}, nil
}
if !checkNum(n) {
return nil, p.error("invalid number")
}
if bytes.ContainsAny(n, ".eE") {
n, err := strconv.ParseFloat(string(numBytes), 64)
if err != nil {
return nil, p.error("invalid number (unreachable)")
}
return n, nil
}
un, err := strconv.ParseUint(string(n), 10, 64)
if err != nil {
return nil, p.error("invalid number (unreachable)")
}
return &integer{un, sgn}, nil
}
func (p *parser) unescape(rawStr []byte) ([]byte, error) {
var escaped []byte
for i := 0; i < len(rawStr); i++ {
if rawStr[i] != '\\' {
escaped = append(escaped, rawStr[i])
continue
}
i++
var b []byte
switch rawStr[i] {
case '\'':
b = []byte("'")
case '"':
b = []byte(`"`)
case '?':
b = []byte("?")
case '\\':
b = []byte(`\`)
case 'a':
b = []byte("\a")
case 'b':
b = []byte("\b")
case 'f':
b = []byte("\f")
case 'n':
b = []byte("\n")
case 'r':
b = []byte("\r")
case 't':
b = []byte("\t")
case 'v':
b = []byte("\v")
case '\n':
b = nil
case '\r':
i++
if i < len(rawStr) && rawStr[i] == '\n' {
b = nil
} else {
return nil, fmt.Errorf("invalid escape sequence %q", rawStr[i-2:min(i+1, len(rawStr))])
}
case 'x':
i++
if i+2 > len(rawStr) {
return nil, fmt.Errorf("invalid hex escape %q", rawStr[i-2:min(i+2, len(rawStr))])
}
n, err := strconv.ParseUint(string(rawStr[i:i+2]), 16, 8)
if err != nil {
return nil, fmt.Errorf("invalid hex escape %q: %s", rawStr[i-2:i+2], err)
}
i++
b = []byte{byte(n)}
case 'u', 'U':
nBytes := 4
if rawStr[i] == 'U' {
nBytes = 8
}
i++
if i+nBytes > len(rawStr) {
return nil, fmt.Errorf("invalid unicode escape %q", rawStr[i-2:min(i+nBytes, len(rawStr))])
}
n, err := strconv.ParseUint(string(rawStr[i:i+nBytes]), 16, 31)
if err != nil {
return nil, fmt.Errorf("invalid hex escape %q: %s", rawStr[i-2:i+2], err)
}
i += nBytes - 1
b = utf8.AppendRune(nil, rune(n))
default:
if i+3 > len(rawStr) {
return nil, fmt.Errorf("invalid string escape %q", rawStr[i-1:i+1])
}
n, err := strconv.ParseUint(string(rawStr[i:i+3]), 8, 8)
if err != nil {
return nil, fmt.Errorf("invalid octal escape %q: %s", rawStr[i:i+3], err)
}
i += 2
b = []byte{byte(n)}
}
escaped = append(escaped, b...)
}
if !utf8.Valid(escaped) {
return nil, p.error("syntax error: string %q is not UTF-8 encoded", escaped)
}
return escaped, nil
}
func (p *parser) parseString(tok []byte) (string, error) {
s := new(strings.Builder)
for {
ss, err := p.unescape(tok[1 : len(tok)-1])
if err != nil {
return "", err
}
s.Write(ss)
nextTok, err := p.peek()
if err != nil || nextTok[0] != '\'' && nextTok[0] != '"' {
return s.String(), nil
}
p.next()
tok = nextTok
}
}
func (p *parser) parseMessage() (map[string]any, error) {
m := make(map[string]any)
for {
tok, err := p.next()
if err != nil || tok[0] == '}' {
return m, err
}
if err := p.parseFieldVal(m, tok); err != nil {
return nil, err
}
}
}
func (p *parser) parseVal(tok []byte) (any, error) {
switch tok[0] {
case '{':
m, err := p.parseMessage()
if err != nil {
return nil, err
}
return m, nil
case '[':
return p.parseList()
case '\'', '"':
s, err := p.parseString(tok)
if err != nil {
return nil, err
}
return s, nil
default:
switch string(tok) {
case "true", "yes", "on":
return true, nil
case "false", "no", "off":
return false, nil
default:
n, err := p.parseNum(tok)
if err != nil {
return nil, err
}
return n, nil
}
}
}
func (p *parser) parseList() ([]any, error) {
var l []any
for i := 0; ; i++ {
tok, err := p.next()
if err != nil || tok[0] == ']' {
return l, err
}
if i > 0 {
if tok[0] != ',' {
return nil, p.error("expecting comma")
}
tok, err = p.next()
if err != nil || tok[0] == ']' { // allow trailing comma
return l, err
}
}
vs, err := p.parseVal(tok)
if err != nil {
return nil, err
}
l = append(l, vs)
}
}
func appendAny(prevVal any, newVal any) any {
if prevVal == nil {
return newVal
}
var l []any
if ll, ok := prevVal.([]any); ok {
l = ll
} else {
l = []any{prevVal}
}
if ll, ok := newVal.([]any); ok {
return append(l, ll...)
}
return append(l, newVal)
}
func (p *parser) parseFieldVal(m map[string]any, field []byte) error {
if b := field[0]; !(b == '_' || 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z') {
return p.error("expecting field")
}
tok, err := p.next()
if err != nil {
return err
}
switch tok[0] {
case '{':
vs, err := p.parseVal(tok)
if err != nil {
return err
}
m[string(field)] = appendAny(m[string(field)], vs)
case ':':
tok, err := p.next()
if err != nil {
return err
}
vs, err := p.parseVal(tok)
if err != nil {
return err
}
m[string(field)] = appendAny(m[string(field)], vs)
default:
return p.error("expecting colon")
}
return nil
}
func (p *parser) parse() (map[string]any, error) {
m := make(map[string]any)
for {
tok, err := p.next()
if err != nil {
if err == errEOF {
return m, nil
}
return nil, err
}
if err := p.parseFieldVal(m, tok); err != nil {
return nil, err
}
}
}
func setPtr(val reflect.Value) reflect.Value {
if val.Kind() != reflect.Pointer {
return val
}
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
return val.Elem()
}
func intLimits(kind reflect.Kind) (min, max uint64, ok bool) {
switch kind {
case reflect.Int:
return -math.MinInt, math.MaxInt, true
case reflect.Int8:
return -math.MinInt8, math.MaxInt8, true
case reflect.Int16:
return -math.MinInt16, math.MaxInt16, true
case reflect.Int32:
return -math.MinInt32, math.MaxInt32, true
case reflect.Int64:
return -math.MinInt64, math.MaxInt64, true
case reflect.Uint:
return 0, math.MaxUint, true
case reflect.Uint8:
return 0, math.MaxUint8, true
case reflect.Uint16:
return 0, math.MaxUint16, true
case reflect.Uint32:
return 0, math.MaxUint32, true
case reflect.Uint64:
return 0, math.MaxUint64, true
default:
return 0, 0, false
}
}
func unpackVal(fieldVal reflect.Value, fieldMap map[structField]int, val any, field string) error {
switch val := val.(type) {
case bool:
fieldVal := setPtr(fieldVal)
switch fieldVal.Kind() {
case reflect.Bool:
fieldVal.SetBool(val)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if val {
fieldVal.SetInt(1)
} else {
fieldVal.SetInt(0)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if val {
fieldVal.SetUint(1)
} else {
fieldVal.SetUint(0)
}
default:
return fmt.Errorf("field %q should have type bool", field)
}
case *integer:
fieldVal := setPtr(fieldVal)
switch fieldVal.Kind() {
case reflect.Float32, reflect.Float64:
fieldVal.SetFloat(float64(val.sgn) * float64(val.n))
return nil
}
min, max, ok := intLimits(fieldVal.Kind())
if !ok {
return fmt.Errorf("field %q should have type int", field)
}
if val.sgn < 0 && val.n > min || val.sgn > 0 && val.n > max {
return fmt.Errorf("number %d is out of range for %s", val, fieldVal.Kind())
}
if min == 0 { // unsigned
fieldVal.SetUint(val.n)
} else {
fieldVal.SetInt(int64(val.sgn) * int64(val.n))
}
case float64:
fieldVal := setPtr(fieldVal)
switch fieldVal.Kind() {
case reflect.Float32, reflect.Float64:
fieldVal.SetFloat(float64(val))
default:
return fmt.Errorf("field %q should have type float64 or float32", field)
}
case string:
if _, ok := fieldVal.Interface().(encoding.TextUnmarshaler); ok {
if fieldVal.Kind() == reflect.Pointer && fieldVal.IsNil() {
fieldVal.Set(reflect.New(fieldVal.Type().Elem()))
}
return fieldVal.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val))
}
if unmarshaler, ok := fieldVal.Addr().Interface().(encoding.TextUnmarshaler); ok {
return unmarshaler.UnmarshalText([]byte(val))
}
fieldVal := setPtr(fieldVal)
switch {
case fieldVal.Kind() == reflect.String:
fieldVal.SetString(val)
case fieldVal.Type() == reflect.TypeFor[[]byte]():
b, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return fmt.Errorf("field %q: bad base64", field)
}
fieldVal.Set(reflect.ValueOf(b))
default:
return fmt.Errorf("field %q should have type string (got %s)", field, fieldVal.Type())
}
case map[string]any:
fieldVal := setPtr(fieldVal)
if fieldVal.Kind() != reflect.Struct {
return fmt.Errorf("field %q should have type struct (got %s)", field, fieldVal.Type())
}
if err := unpackStruct(fieldVal, fieldMap, val); err != nil {
return err
}
case []any:
return fmt.Errorf("invalid repeated field")
default:
return fmt.Errorf("unexpected AST node (unreachable)")
}
return nil
}
func unpackStruct(out reflect.Value, fieldMap map[structField]int, msg map[string]any) error {
for field, val := range msg {
fieldIdx, ok := fieldMap[structField{out.Type(), field}]
if !ok {
return fmt.Errorf("no field named %q", field)
}
fieldVal := out.Field(fieldIdx)
if fieldVal.Kind() == reflect.Slice && fieldVal.Type() != reflect.TypeFor[[]byte]() {
var vals []any
if l, ok := val.([]any); ok {
vals = l
} else {
vals = []any{val}
}
l := reflect.MakeSlice(fieldVal.Type(), len(vals), len(vals))
for i, val := range vals {
if err := unpackVal(l.Index(i), fieldMap, val, field); err != nil {
return err
}
}
if fieldVal.IsNil() {
fieldVal.Set(reflect.MakeSlice(fieldVal.Type(), 0, 0))
}
fieldVal.Set(reflect.AppendSlice(fieldVal, l))
continue
}
if err := unpackVal(fieldVal, fieldMap, val, field); err != nil {
return err
}
}
return nil
}
// Unmarshal parses a ccl message and writes the result into v. v must be a
// non-nil pointer to a struct.
//
// Unmarshal accepts a top-level message, which is equivalent to the "message"
// type described above, but without the surrounding braces. For example:
//
// key1: "val1"
// key2: "val2"
//
// The exact semantics of which ccl types map to which Go types is a bit
// complicated and I don't feel like writing out all the rules, so suffice it
// to say that the usual stuff should work. As a special case, a []byte field
// expects a base64-encoded string.
//
// You can override a field's name using a struct tag "ccl", for example
//
// type message struct {
// MyField int `ccl:"my_field"`
// }
//
// This message could decode, for example `my_field:5`
//
// If a field has type T where T or *T implements [encoding.TextUnmarshaler],
// then a string value will be decoded by calling UnmarshalText. No other
// customization is supported, this isn't encoding/json.
func Unmarshal(data []byte, v any) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Pointer || val.IsNil() || val.Type().Elem().Kind() != reflect.Struct {
return fmt.Errorf("value must be a non-nil pointer to a struct")
}
fields := make(map[structField]int)
if err := fieldMap(fields, make(map[reflect.Type]bool), val.Type().Elem()); err != nil {
return err
}
nextToken, stop := iter.Pull2(tokens(data))
defer stop()
msg, err := (&parser{nextTok: nextToken, data: data}).parse()
if err != nil {
return err
}
return unpackStruct(val.Elem(), fields, msg)
}
|