-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
342 lines (303 loc) · 7.41 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
package parser
import (
"github.com/di-wu/parser/op"
"unicode/utf8"
)
// EOD indicates the End Of (the) Data.
const EOD = 1<<31 - 1
// Parser represents a general purpose parser.
type Parser struct {
buffer []byte
cursor *Cursor
decode func([]byte) (rune, int)
converter func(interface{}) interface{}
operator func(interface{}) (*Cursor, error)
}
// New creates a new Parser.
func New(input []byte) (*Parser, error) {
p := Parser{
buffer: input,
decode: utf8.DecodeRune,
}
current, size := p.decode(p.buffer)
if size == 0 {
// Nothing got decoded.
return nil, &InitError{
Message: "failed to scan the first rune",
}
}
p.cursor = &Cursor{
Rune: current,
size: size,
}
return &p, nil
}
// DecodeRune allows you to redefine the way runes are decoded form the byte
// stream. By default utf8.DecodeRune is used.
func (p *Parser) DecodeRune(d func(p []byte) (rune, int)) {
p.decode = d
}
// SetConverter allows you to add additional (prioritized) converters to the
// parser. e.g. convert aliases to other types or overwrite defaults.
func (p *Parser) SetConverter(c func(i interface{}) interface{}) {
p.converter = c
}
// SetOperator allows you to support additional (prioritized) operators.
// Should return an UnsupportedType error if the given value is not supported.
func (p *Parser) SetOperator(o func(i interface{}) (*Cursor, error)) {
p.operator = o
}
// Next advances the parser by one rune.
func (p *Parser) Next() *Parser {
if p.Done() {
return p
}
// Move position to the next rune.
// |__|_| < next rune is position + size
// ^
// rune of size 2, position 0
p.cursor.position += p.cursor.size
current, size := p.decode(p.buffer[p.cursor.position:])
if size == 0 {
// Nothing got decoded.
current = EOD
}
// Previous rune was an end of line, we are on a new line now.
if p.cursor.Rune == '\n' || (p.cursor.Rune == '\r' && current != '\n') {
p.cursor.row += 1
p.cursor.column = 0
} else {
p.cursor.column += p.cursor.size
}
p.cursor.Rune = current
p.cursor.size = size
return p
}
// Current returns the value to which the cursor is pointing at.
func (p *Parser) Current() rune {
return p.cursor.Rune
}
// Done checks whether the parser is done parsing.
func (p *Parser) Done() bool {
return p.cursor.Rune == EOD
}
// Mark returns a copy of the current cursor.
func (p *Parser) Mark() *Cursor {
mark := *p.cursor
return &mark
}
// LookBack returns the previous cursor without decreasing the parser.
func (p *Parser) LookBack() *Cursor {
if p.cursor.position == 0 || p.Done() {
// Not possible to go back
return p.Mark()
}
// We don't know the size of the previous rune... 1 or more?
previous, size := p.decode(p.buffer[p.cursor.position-1:])
for i := 2; previous == utf8.RuneError; i++ {
previous, size = p.decode(p.buffer[p.cursor.position-i:])
}
var (
row = p.cursor.row
column = p.cursor.column
)
if p.cursor.Rune == '\n' || (p.cursor.Rune == '\r' && p.Peek().Rune != '\n') {
row -= 1
column = 0
} else {
column -= size
}
return &Cursor{
Rune: previous,
size: size,
position: p.cursor.position - size,
row: row,
column: column,
}
}
// Peek returns the next cursor without advancing the parser.
func (p *Parser) Peek() *Cursor {
start := p.Mark()
defer p.Jump(start)
return p.Next().Mark()
}
// Jump goes to the position of the given mark.
func (p *Parser) Jump(mark *Cursor) *Parser {
cursor := *mark
p.cursor = &cursor
return p
}
// Slice returns the value in between the two given cursors [start:end]. The end
// value is inclusive!
func (p *Parser) Slice(start *Cursor, end *Cursor) string {
if start.Rune == EOD {
return ""
}
if end == nil { // Just to be sure...
end = start
}
return string(p.buffer[start.position : end.position+end.size])
}
// Expect checks whether the buffer contains the given value. It consumes their
// corresponding runes and returns a mark to the last rune of the consumed
// value. It returns an error if can not find a match with the given value.
//
// It currently supports:
// - rune & string
// - func(p *Parser) (*Cursor, bool)
// (== AnonymousClass)
// - []interface{}
// (== op.And)
// - operators: op.Not, op.And, op.Or & op.XOr
func (p *Parser) Expect(i interface{}) (*Cursor, error) {
state := state{p: p}
i = ConvertAliases(i)
if p.converter != nil {
// Can undo previous conversions!
i = p.converter(i)
}
if p.operator != nil {
// Takes priority over default values. If an unsupported error is
// returned we can check if one of the predefined types match.
mark, err := p.operator(i)
if _, ok := err.(*UnsupportedType); !ok {
return mark, err
}
}
switch start := p.Mark(); v := i.(type) {
case rune:
if p.cursor.Rune != v {
return nil, p.ExpectedParseError(v, start, start)
}
state.Ok(p.Mark())
case string:
if v == "" {
return nil, &ExpectError{
Message: "can not parse empty string",
}
}
for _, r := range []rune(v) {
if p.cursor.Rune != r {
return nil, p.ExpectedParseError(v, start, p.Mark())
}
state.Ok(p.Mark())
}
case AnonymousClass:
last, passed := v(p)
if !passed {
if last == nil {
last = start
}
return nil, p.ExpectedParseError(v, start, p.Jump(last).Peek())
}
state.Ok(last)
case op.Not:
defer p.Jump(start)
if last, err := p.Expect(v.Value); err == nil {
return nil, p.ExpectedParseError(v, start, last)
}
case op.Ensure:
if last, err := p.Expect(v.Value); err != nil {
return last, err
}
p.Jump(start)
case op.And:
var last *Cursor
for _, i := range v {
mark, err := p.Expect(i)
if err != nil {
if last == nil {
last = start
}
return nil, p.ExpectedParseError(v, start, p.Jump(last).Peek())
}
last = mark
}
state.Ok(last)
case op.Or:
var last *Cursor
for _, i := range v {
mark, err := p.Expect(i)
if err == nil {
last = mark
break
}
}
if last == nil {
return nil, p.ExpectedParseError(v, start, start)
}
state.Ok(last)
case op.XOr:
var last *Cursor
for _, i := range v {
mark, err := p.Expect(i)
if err == nil {
if last != nil {
p.Jump(start)
return nil, p.ExpectedParseError(v, start, mark)
}
last = mark
p.Jump(start) // Go back to the start.
}
}
if last == nil {
return nil, p.ExpectedParseError(v, start, last)
}
state.Ok(last)
case op.Range:
var (
count int
last *Cursor
)
for {
mark, err := p.Expect(v.Value)
if err != nil {
break
}
last = mark
count++
if v.Max != -1 && count == v.Max {
// Break if you have parsed the maximum amount of values.
// This way count will never be larger than v.Max.
break
}
}
if count < v.Min {
return nil, p.ExpectedParseError(v, start, last)
}
state.Ok(last)
default:
return nil, &UnsupportedType{
Value: i,
}
}
return state.End(), nil
}
// Check works the same as Parser.Expect, but instead it returns a bool instead
// of an error.
func (p *Parser) Check(i interface{}) (*Cursor, bool) {
mark, err := p.Expect(i)
if err != nil {
return mark, false
}
return mark, true
}
// ConvertAliases converts various default primitive types to aliases for type
// matching.
//
// - (int, rune)
// - ([]interface{}, op.And)
func ConvertAliases(i interface{}) interface{} {
switch v := i.(type) {
case int:
return rune(v)
case func(p *Parser) (*Cursor, bool):
return AnonymousClass(v)
case Class:
return AnonymousClass(v.Check)
case []interface{}:
return op.And(v)
default:
return i
}
}