-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
103 lines (90 loc) · 2.35 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
package pars
import (
"fmt"
"github.com/go-ascii/ascii"
)
// Parser is the function signature of a parser.
type Parser func(state *State, result *Result) error
// Map is the function signature for a result mapper.
type Map func(result *Result) error
// Map applies the callback if the parser matches.
func (p Parser) Map(f Map) Parser {
return func(state *State, result *Result) error {
state.Push()
if err := p(state, result); err != nil {
state.Pop()
return err
}
state.Drop()
return f(result)
}
}
// Child will map to the i'th child of the result.
func (p Parser) Child(i int) Parser { return p.Map(Child(i)) }
// Children will keep the children associated to the given indices.
func (p Parser) Children(indices ...int) Parser {
return p.Map(Children(indices...))
}
// ToString will convert the Token field to a string Value.
func (p Parser) ToString() Parser { return p.Map(ToString) }
// Bind will bind the given value as the parser result value.
func (p Parser) Bind(v interface{}) Parser {
return func(state *State, result *Result) error {
if err := p(state, result); err != nil {
return err
}
result.SetValue(v)
return nil
}
}
// Error will modify the Parser to return the given error if the Parser returns
// an error.
func (p Parser) Error(alt error) Parser {
return func(state *State, result *Result) error {
if err := p(state, result); err != nil {
return BoundError{alt, state.Position()}
}
return nil
}
}
// Parse the given state using the parser and return the Result.
func (p Parser) Parse(s *State) (Result, error) {
r := Result{}
err := p(s, &r)
return r, err
}
// AsParser attempts to create a Parser for a given argument.
func AsParser(q interface{}) Parser {
switch p := q.(type) {
case Parser:
return p
case func(*State, *Result) error:
return p
case *Parser:
return func(state *State, result *Result) error {
return (*p)(state, result)
}
case byte:
return Byte(p)
case []byte:
return Bytes(p)
case rune:
return Rune(p)
case []rune:
return Runes(p)
case string:
return String(p)
case ascii.Filter:
return Filter(p)
default:
panic(fmt.Errorf("cannot convert type `%T` to a parser", p))
}
}
// AsParsers applies the AsParser function to each argument.
func AsParsers(qs ...interface{}) []Parser {
ps := make([]Parser, len(qs))
for i, q := range qs {
ps[i] = AsParser(q)
}
return ps
}