/
combinators.go
192 lines (163 loc) · 4.19 KB
/
combinators.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
package pcomb
import (
"fmt"
"utf8"
)
type Output struct {
Success bool
Errors []Error
Value interface{}
State
}
func success(value interface{}, state State) Output {
return Output{true, NoErrors(), value, state}
}
func failure(message string, state State) Output {
return Output{false, []Error{NewError(state.Position, message)}, nil, state}
}
func multipleFailure(message string, state State, outputs ...Output) Output {
errors := []Error{}
for _, o := range outputs {
for _, e := range o.Errors {
errors = append(errors, e)
}
}
error := NewError(state.Position, message)
error.Errors = errors
return Output{false, []Error{error}, nil, state}
}
type Parser func(state State) Output
func (p Parser) parse(text string) Output {
return p(newState(text))
}
var Fail Parser = func(state State) Output {
return Output{false, NoErrors(), nil, state}
}
func Return(value interface{}) Parser {
return func(state State) Output {
return Output{true, NoErrors(), value, state}
}
}
func Satisfy(predicate func(int) bool) Parser {
return func(state State) Output {
rune, next, ok := state.Next()
if !ok {
return failure("End of data", state)
}
if predicate(rune) {
return success(rune, next)
}
return failure(fmt.Sprintf("Character (%c) does not match predicate", rune), next)
}
}
func Item() Parser {
return Satisfy(func(rune int) bool { return true })
}
func Or(a, b Parser) Parser {
return func(state State) Output {
outA := a(state)
if outA.Success || !state.Equals(outA.State) {
return outA
}
outB := b(state)
if outB.Success || !state.Equals(outB.State) {
return outB
}
return multipleFailure("Both sides of Or expression failed", state, outA, outB)
}
}
func Try(p Parser) Parser {
return func(state State) Output {
output := p(state)
if !state.Equals(output.State) && !output.Success {
return Output{false, output.Errors, output.Value, state}
}
return output
}
}
const labelFormat = "Attempted to consume %s and failed"
func Tag(p Parser, label string) Parser {
return func(state State) Output {
out := p(state)
if !state.Equals(out.State) {
return out
}
return failure(fmt.Sprintf(labelFormat, label), state)
}
}
func Sequence(p Parser, f func(value interface{}) Parser) Parser {
return func(state State) Output {
out := p(state)
if out.Success {
return f(out.Value)(out.State)
}
return out
}
}
func Sequence_(a, b Parser) Parser {
return Sequence(a, func(ignore interface{}) Parser {
return b
})
}
func literalRune(rune int) Parser {
return Satisfy(func(r int) bool { return rune == r })
}
func Literal(s string, result interface{}) Parser {
match := utf8.NewString(s)
if len(s) == 0 {
return Return(result)
}
return Sequence_(literalRune(match.At(0)), Literal(match.Slice(1, match.RuneCount()), result))
}
func cons(x interface{}, xs []interface{}) []interface{} {
if x != nil {
return append([]interface{}{x}, xs...)
}
return xs
}
func emptySlice() []interface{} {
return make([]interface{}, 0)
}
func ZeroOrMore(p Parser) Parser {
return Or(OneOrMore(p), Return(emptySlice()))
}
func OneOrMore(p Parser) Parser {
op := func(x interface{}) Parser {
consOp := func(xs interface{}) Parser {
slice, _ := xs.([]interface{})
return Return(cons(x, slice))
}
return Sequence(ZeroOrMore(p), consOp)
}
return Sequence(p, op)
}
func chain(x interface{}, p, op Parser) Parser {
f_func := func(fval interface{}) Parser {
f, _ := fval.(func(a, b interface{}) interface{})
y_func := func(y interface{}) Parser {
return chain(f(x, y), p, op)
}
return Sequence(p, y_func)
}
return Or(Sequence(op, f_func), Return(x))
}
func ChainLeft1(p Parser, op Parser) Parser {
remainder := func(x interface{}) Parser {
return chain(x, p, op)
}
return Sequence(p, remainder)
}
func SeperatedBy(p, sep Parser) Parser {
return Or(SeperatedBy1(p, sep), Return(emptySlice()))
}
func SeperatedBy1(p, sep Parser) Parser {
return Sequence(p, func(x interface{}) Parser {
return Sequence(seperated(p, sep), func(xs interface{}) Parser {
slice, _ := xs.([]interface{})
return Return(cons(x, slice))
})
})
}
func seperated(p, sep Parser) Parser {
return Or(Sequence_(sep, SeperatedBy1(p, sep)), Return(emptySlice()))
}