/
parser.go
205 lines (163 loc) · 4.37 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
// Package jstn implements a reference parser, validator, and generator for JSON Type Notation.
package jstn
import (
"fmt"
"log"
"strings"
)
// Parse parses a JSTN text into a native representation.
func Parse(schema string) (Type, error) {
r := strings.NewReader(schema)
p := &parser{s: newScanner(r)}
return p.Parse()
}
// MustParse is equivalent to Parse, except that it panics if the text cannot be parsed.
func MustParse(schema string) Type {
t, err := Parse(schema)
if err != nil {
panic(err)
}
return t
}
type parser struct {
s *scanner
buf struct {
tok token
lit string
n int
}
}
func (p *parser) scan() (tok token, lit string) {
if p.buf.n != 0 {
p.buf.n = 0
return p.buf.tok, p.buf.lit
}
p.buf.tok, p.buf.lit = p.s.Scan()
tok, lit = p.buf.tok, p.buf.lit
return
}
func (p *parser) unscan() { p.buf.n = 1 }
func (p *parser) scanIgnoreWhitespace(ignoreNewlines bool) (tok token, lit string) {
tok, lit = p.scan()
for tok == WHITESPACE || (ignoreNewlines && tok == NEWLINE) {
tok, lit = p.scan()
}
return
}
func (p *parser) Parse() (Type, error) {
return p.parseType()
}
func (p *parser) parseType() (Type, error) {
t, err := p.parseTypeDecl()
if err != nil {
return t, err
}
// if the next token is '?' mark it as optional
tok, _ := p.scan()
if tok == QUESTION {
t.Optional = true
} else {
p.unscan()
}
return t, nil
}
func (p *parser) parseTypeDecl() (Type, error) {
tok, _ := p.scanIgnoreWhitespace(true)
switch tok {
case STRING:
return Type{Kind: String}, nil
case NUMBER:
return Type{Kind: Number}, nil
case BOOLEAN:
return Type{Kind: Boolean}, nil
case NULL:
return Type{Kind: Null}, nil
case SQUAREOPEN:
p.unscan()
return p.parseArray()
case CURLYOPEN:
p.unscan()
return p.parseObject()
default:
return Type{}, fmt.Errorf("unexpected token %s", tok.String())
}
}
func (p *parser) parseArray() (Type, error) {
// parse the opening brace
tok, _ := p.scanIgnoreWhitespace(true)
if tok != SQUAREOPEN {
return Type{}, fmt.Errorf("unexpected token %s", tok.String())
}
// peek at the next character
tok, _ = p.scanIgnoreWhitespace(true)
p.unscan()
var childType *Type
if tok != SQUARECLOSE {
// parse the internal type
t, err := p.parseType()
if err != nil {
return Type{}, err
}
childType = &t
}
// parse the closing brace
tok, _ = p.scanIgnoreWhitespace(true)
if tok != SQUARECLOSE {
return Type{}, fmt.Errorf("unexpected token %s", tok.String())
}
return Type{Kind: Array, Items: childType}, nil
}
func (p *parser) parseObject() (Type, error) {
var tok token
var lit string
// parse the opening brace
tok, _ = p.scanIgnoreWhitespace(true)
if tok != CURLYOPEN {
return Type{}, fmt.Errorf("unexpected token %s", tok.String())
}
props := make(map[string]*Type)
for {
tok, lit = p.scanIgnoreWhitespace(true)
// check for the curly brace
if tok == CURLYCLOSE {
p.unscan()
break
}
// parse the property name
if tok != IDENT {
log.Println("failing here")
return Type{}, fmt.Errorf("unexpected token %s for %s", tok.String(), IDENT.String())
}
// parse the colon
if tok, _ := p.scanIgnoreWhitespace(true); tok != COLON {
return Type{}, fmt.Errorf("unexpected token %s for %s", tok.String(), COLON.String())
}
// parse the object type
t, err := p.parseType()
if err != nil {
return Type{}, err
}
props[lit] = &t // save this property type
// Every property pair must finish with a delimiter token, which can be
// either a CURLYCLOSE (indicating the end of the object), a SEMICOLON,
// or a NEWLINE.
if tok, lit = p.scanIgnoreWhitespace(false); tok == CURLYCLOSE {
// The SEMICOLON acts as a delimiter because it marks
// the end of the entire object. There won't be any
// more properties. Unscan it and let the next iteration
// of the object property loop handle it.
p.unscan()
} else if tok != SEMICOLON && tok != NEWLINE {
// If we didn't find a SEMICOLON, the only other valid
// token is a NEWLINE. But we didn't find that either, so
// we've got an error here.
return Type{}, fmt.Errorf("unexpected token %s (%q) for [%s or %s]", tok.String(), lit, SEMICOLON.String(), NEWLINE.String())
}
}
// parse the closing brace
tok, _ = p.scanIgnoreWhitespace(true)
if tok != CURLYCLOSE {
return Type{}, fmt.Errorf("unexpected token %s", tok.String())
}
return Type{Kind: Object, Properties: props}, nil
}