-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicating.go
326 lines (276 loc) · 7.26 KB
/
predicating.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
package peg
import (
"fmt"
"strings"
)
var (
// True always matches, but consumes no text.
True Pattern = &patternBoolean{true}
// False always dismatch.
False Pattern = &patternBoolean{false}
// SOL predicates start of line.
SOL Pattern = &patternLineAnchorPredicate{true}
// EOL predicates end of line.
EOL Pattern = &patternLineAnchorPredicate{false}
// EOF predicates end of file.
EOF Pattern = patternEOFPredicate{}
)
// Underlying types implemented Pattern interface.
type (
patternBoolean struct {
ok bool
}
patternAbort struct {
msg string
}
patternLineAnchorPredicate struct {
linestart bool
}
patternEOFPredicate struct{}
patternPredicate struct {
not bool
pat Pattern
}
patternAndPredicate struct {
pats []Pattern
}
patternOrPredicate struct {
pats []Pattern
}
patternIf struct {
cond Pattern
yes Pattern
no Pattern
}
patternSwitch struct {
cases []struct {
cond Pattern
then Pattern
}
otherwise Pattern
}
)
// Test predicates if pattern is matched, consuming no text.
//
// Note that, if predicates true, groups/parse captures won't be discarded.
func Test(pat Pattern) Pattern {
return &patternPredicate{not: false, pat: pat}
}
// Not predicates if pattern is dismatched, consuming no text.
//
// Note that, if predicates true, groups/parse captures won't be discarded.
func Not(pat Pattern) Pattern {
return &patternPredicate{not: true, pat: pat}
}
// And searches the patterns in order to predicate if all the pattern
// is matched at current position. It consumes no text.
//
// Note that, if predicates true, groups/parse captures won't be discarded.
func And(pats ...Pattern) Pattern {
if len(pats) == 0 {
return True
}
return &patternAndPredicate{pats}
}
// Or searches the patterns in order to predicate if any the pattern
// is matched at current position. It consumes no text.
//
// Note that, if predicates true, groups/parse captures won't be discarded.
func Or(pats ...Pattern) Pattern {
if len(pats) == 0 {
return False
}
return &patternOrPredicate{pats}
}
// Abort quit the normal matching process, reporting an error message.
func Abort(msg string) Pattern {
return &patternAbort{msg}
}
// When tests the given condition but consumes no text, then determines to
// execute then-branch or just predicates false.
//
// Note that, if cond predicates true, groups/parse captures won't be
// discarded.
func When(cond, then Pattern) Pattern {
return &patternIf{cond: cond, yes: then, no: False}
}
// If tests the given condition but consumes no text, then determines to
// execute yes-branch or no-branch.
//
// Note that, if cond predicates true, groups/parse captures won't be
// discarded.
func If(cond, yes, no Pattern) Pattern {
return &patternIf{cond: cond, yes: yes, no: no}
}
// Switch tests cond-then pairs in order, executes then-branch if cond is true.
// If there is no true cond, executes the optional otherwise-branch
// (the default pattern is True).
//
// Note that, if cond predicates true, groups/parse captures won't be
// discarded.
func Switch(cond, then Pattern, rest ...Pattern) Pattern {
pat := &patternSwitch{}
pat.cases = append(pat.cases, struct {
cond Pattern
then Pattern
}{cond, then})
if len(rest)%2 != 0 {
pat.otherwise = rest[len(rest)-1]
rest = rest[:len(rest)-1]
} else {
pat.otherwise = True
}
for len(rest) > 0 {
cond, then = rest[0], rest[1]
rest = rest[2:]
pat.cases = append(pat.cases, struct {
cond Pattern
then Pattern
}{cond, then})
}
return pat
}
// Matches empty string if true, dismatches if false.
func (pat *patternBoolean) match(ctx *context) error {
return ctx.predicates(pat.ok)
}
// Predicates SOL/EOL.
func (pat *patternLineAnchorPredicate) match(ctx *context) error {
p, n := ctx.previous(1), ctx.next(1)
if pat.linestart {
// SOL matches start of file or just after a "\n"|"\r"|"\r\n".
if p == "" {
return ctx.predicates(true)
}
return ctx.predicates(
p == "\n" || (p == "\r" && n != "\n"))
}
// EOL matches end of file or just before a "\n"|"\r"|"\r\n".
if n == "" {
return ctx.predicates(true)
}
return ctx.predicates(n == "\r" || (n == "\n" && p != "\r"))
}
// Predicates EOF.
func (patternEOFPredicate) match(ctx *context) error {
return ctx.predicates(ctx.next(1) == "")
}
// Predicates if sub-pattern matches.
func (pat *patternPredicate) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if pat.not {
ret.ok = !ret.ok
}
return ctx.predicates(ret.ok)
}
// Predicates if all the sub-patterns match.
func (pat *patternAndPredicate) match(ctx *context) error {
for ctx.locals.i < len(pat.pats) {
if !ctx.justReturned() {
return ctx.call(pat.pats[ctx.locals.i])
}
if !ctx.ret.ok {
return ctx.predicates(false)
}
ctx.locals.i++
}
return ctx.predicates(true)
}
// Predicates if any sub-pattern matches.
func (pat *patternOrPredicate) match(ctx *context) error {
for ctx.locals.i < len(pat.pats) {
if !ctx.justReturned() {
return ctx.call(pat.pats[ctx.locals.i])
}
if ctx.ret.ok {
return ctx.predicates(true)
}
ctx.locals.i++
}
return ctx.predicates(false)
}
// Abort directly.
func (pat *patternAbort) match(ctx *context) error {
pos := ctx.tell()
return errorf("abort:%s: %s", pos.String(), pat.msg)
}
// Branch `if'.
func (pat *patternIf) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.cond)
}
if ctx.ret.ok {
return ctx.execute(pat.yes)
}
return ctx.execute(pat.no)
}
// Branch `switch'.
func (pat *patternSwitch) match(ctx *context) error {
for ctx.locals.i < len(pat.cases) {
if !ctx.justReturned() {
return ctx.call(pat.cases[ctx.locals.i].cond)
}
if ctx.ret.ok {
return ctx.execute(pat.cases[ctx.locals.i].then)
}
ctx.locals.i++
}
return ctx.execute(pat.otherwise)
}
func (pat *patternBoolean) String() string {
if pat.ok {
return "true"
}
return "false"
}
func (pat *patternLineAnchorPredicate) String() string {
if pat.linestart {
return "sol?"
}
return "eol?"
}
func (patternEOFPredicate) String() string {
return "eof?"
}
func (pat *patternPredicate) String() string {
if pat.not {
return fmt.Sprintf("!%s", pat.pat)
}
return fmt.Sprintf("?%s", pat.pat)
}
func (pat *patternAndPredicate) String() string {
strs := make([]string, len(pat.pats))
for i, pat := range pat.pats {
strs[i] = fmt.Sprint(pat)
}
return fmt.Sprintf("(%s)", strings.Join(strs, " && "))
}
func (pat *patternOrPredicate) String() string {
strs := make([]string, len(pat.pats))
for i, pat := range pat.pats {
strs[i] = fmt.Sprint(pat)
}
return fmt.Sprintf("(%s)", strings.Join(strs, " || "))
}
func (pat *patternAbort) String() string {
return fmt.Sprintf("abort(%q)", pat.msg)
}
func (pat *patternIf) String() string {
if b, ok := pat.no.(*patternBoolean); ok && b.ok == false {
return fmt.Sprintf("switch(%s: %s)", pat.cond, pat.yes)
}
return fmt.Sprintf("switch(%s: %s; %s)", pat.cond, pat.yes, pat.no)
}
func (pat *patternSwitch) String() string {
strs := make([]string, len(pat.cases))
for i := range pat.cases {
strs[i] = fmt.Sprintf("%s: %s", pat.cases[i].cond, pat.cases[i].then)
}
if b, ok := pat.otherwise.(*patternBoolean); ok && b.ok == false {
return fmt.Sprintf("switch(%s)", strings.Join(strs, "; "))
}
return fmt.Sprintf("switch(%s; %s)", strings.Join(strs, "; "), pat.otherwise)
}