-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouping.go
184 lines (160 loc) · 3.99 KB
/
grouping.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
package peg
import "fmt"
type (
patternGrouping struct {
pat Pattern
grpname string
}
patternTrigger struct {
pat Pattern
label string
trigger func(string, Position) error
}
patternInjector struct {
pat Pattern
label string
inject func(string) (n int, ok bool)
}
)
// G saves the the matched text to an anonymous group.
func G(pat Pattern) Pattern {
return &patternGrouping{pat: pat, grpname: ""}
}
// NG saves the the matched text to a group named grpname.
func NG(grpname string, pat Pattern) Pattern {
return &patternGrouping{pat: pat, grpname: grpname}
}
// Trigger invokes user defined hook with the text matched.
//
// Note that, the hook could be triggered when the inner pattern was matched
// while the parent pattern is later proved to be dismatched.
func Trigger(hook func(string, Position) error, pat Pattern) Pattern {
return &patternTrigger{
pat: pat,
label: fmt.Sprintf("trigger_%p", hook),
trigger: hook}
}
// Inject attaches an injector to given pattern which checks the matched text
// after matched, then determines whether anything should be matched and
// how many bytes to consume.
//
// Note that, minimum of the original consumed bytes and the injector's
// returned bytes number would be taken.
func Inject(fn func(string) (int, bool), pat Pattern) Pattern {
if fn == nil {
return pat
}
return &patternInjector{
pat: pat,
label: fmt.Sprintf("inject_%p", fn),
inject: fn,
}
}
// Check attaches a validator to given pattern which checks the matched text
// after matched, then determines whether anything should be matched.
//
// Note that, Check(fn, pat) is not a predicator, it may consume text.
func Check(fn func(string) bool, pat Pattern) Pattern {
if fn == nil {
return pat
}
return &patternInjector{
pat: pat,
label: fmt.Sprintf("check_%p", fn),
inject: newCheckInjector(fn),
}
}
func newCheckInjector(fn func(string) bool) func(string) (int, bool) {
return func(s string) (n int, ok bool) {
if fn(s) {
return len(s), true
}
return 0, false
}
}
// Trunc detects the matched text's length to keep the matched rune count
// is no more than maxrune.
func Trunc(maxrune int, pat Pattern) Pattern {
return &patternInjector{
pat: pat,
label: fmt.Sprintf("trunc_%d", maxrune),
inject: newTruncateInjector(maxrune),
}
}
func newTruncateInjector(maxrune int) func(string) (int, bool) {
return func(s string) (int, bool) {
if maxrune < 0 {
return 0, false
} else if maxrune == 0 {
return 0, true
}
// rune_count(s) < len(s) < maxrune
if len(s) < maxrune {
return len(s), true
}
n := 0
for i := range s {
if n >= maxrune {
return i, true
}
n++
}
return len(s), true
}
}
// Captures text to a group.
func (pat *patternGrouping) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(false)
}
ctx.consume(ret.n)
ctx.group(pat.grpname)
return ctx.commit()
}
// Captures text to trigger a hook.
func (pat *patternTrigger) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(false)
}
head := ctx.tell()
ctx.consume(ret.n)
err := pat.trigger(ctx.span(), head)
if err != nil {
return err
}
return ctx.commit()
}
// Further validate matched text, determines how many bytes to consume.
func (pat *patternInjector) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if ret.ok {
if n, ok := pat.inject(ctx.next(ret.n)); ok {
ctx.consume(n)
return ctx.commit()
}
}
return ctx.predicates(false)
}
func (pat *patternGrouping) String() string {
if pat.grpname == "" {
return fmt.Sprintf("{%s}", pat.pat)
}
return fmt.Sprintf("%%%s%%{%s}", pat.grpname, pat.pat)
}
func (pat *patternTrigger) String() string {
return fmt.Sprintf("%s(%s)", pat.label, pat.pat)
}
func (pat *patternInjector) String() string {
return fmt.Sprintf("%s(%s)", pat.label, pat.pat)
}