-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
177 lines (139 loc) · 3.9 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
/*
* Copyright (c) 2022, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package schema
import (
"errors"
"fmt"
"github.com/dburkart/fossil/pkg/common/parse"
"strconv"
"strings"
)
func Parse(s string) (Object, error) {
p := Parser{Scanner{Input: s}}
return p.Parse()
}
type Parser struct {
Scanner Scanner
}
func (p *Parser) Parse() (schema Object, err error) {
defer func() {
if e := recover(); e != nil {
syntaxError, ok := e.(parse.SyntaxError)
if !ok {
panic(e)
}
err = errors.New(syntaxError.FormatError(p.Scanner.Input))
}
}()
p.Scanner.Input = strings.Trim(p.Scanner.Input, " \t\n")
schema = p.schema()
if schema == nil {
syntaxError := parse.NewSyntaxError(parse.Token{
Type: TOK_INVALID,
Location: parse.Location{Start: p.Scanner.Pos, End: len(p.Scanner.Input) - 1},
}, "Error: Unrecognized schema")
err = errors.New(syntaxError.FormatError(p.Scanner.Input))
} else if p.Scanner.Pos != len(p.Scanner.Input) {
syntaxError := parse.NewSyntaxError(parse.Token{
Type: TOK_INVALID,
Location: parse.Location{Start: p.Scanner.Pos, End: len(p.Scanner.Input) - 1},
}, "Error: Schema not valid, starting here")
err = errors.New(syntaxError.FormatError(p.Scanner.Input))
}
return
}
func (p *Parser) schema() Object {
var schema Object
if schema = p.dType(); schema != nil {
return schema
}
if schema = p.array(); schema != nil {
return schema
}
if schema = p.composite(); schema != nil {
return schema
}
return nil
}
func (p *Parser) dType() Object {
var dType Type
tok := p.Scanner.Emit()
if tok.Type != TOK_TYPE {
p.Scanner.Rewind()
return nil
}
dType.Name = tok.Lexeme
return &dType
}
func (p *Parser) array() Object {
var array Array
var err error
tok := p.Scanner.Emit()
if tok.Type != TOK_BRACKET_O {
p.Scanner.Rewind()
return nil
}
tok = p.Scanner.Emit()
if tok.Type != TOK_NUMBER {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected a number indicating array size", tok.Lexeme)))
}
array.Length, err = strconv.Atoi(tok.Lexeme)
if err != nil {
panic(err)
}
tok = p.Scanner.Emit()
if tok.Type != TOK_BRACKET_X {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected a ']'", tok.Lexeme)))
}
var dType *Type
dType = p.dType().(*Type)
if dType == nil {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected a valid type", tok.Lexeme)))
}
if dType.Name == "string" || dType.Name == "binary" {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: variable-length type '%s' not valid in array", dType.Name)))
}
array.Type = *dType
return &array
}
func (p *Parser) composite() Object {
var composite Composite
tok := p.Scanner.Emit()
if tok.Type != TOK_CURLY_O {
p.Scanner.Rewind()
return nil
}
tok = p.Scanner.Emit()
// Iterate over the keys and values of our map
for tok.Type != TOK_CURLY_X {
if tok.Type != TOK_KEY {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected a map key (\"...\")", tok.Lexeme)))
}
composite.Keys = append(composite.Keys, tok.Lexeme)
tok = p.Scanner.Emit()
if tok.Type != TOK_COLON {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected ':'", tok.Lexeme)))
}
// Now we must find a valid type
val := p.dType()
if val == nil {
// It could be an array
val = p.array()
if val == nil {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: expected a valid type or array as value for key '%s'", tok.Lexeme)))
}
}
composite.Values = append(composite.Values, val)
// Finally, every line must have a comma
tok = p.Scanner.Emit()
if tok.Type != TOK_COMMA {
panic(parse.NewSyntaxError(tok, fmt.Sprintf("Error: unexpected token '%s', expected ','", tok.Lexeme)))
}
// Pull off token for the next iteration
tok = p.Scanner.Emit()
}
return &composite
}