/
extract.go
93 lines (81 loc) · 2.28 KB
/
extract.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
package xre
import (
"fmt"
"regexp"
)
func scanX(s string) (Command, string, error) {
if len(s) == 0 {
return nil, s, fmt.Errorf("empty x command")
}
switch c := s[0]; c {
case '[', '{', '(', '<':
s = s[1:]
return ProtoCommand{extractBalanced{c, balancedOpens[c]}}, s, nil
case '/':
pat, s, err := scanPat(c, s[1:])
if err != nil {
return nil, s, err
}
switch n := pat.NumSubexp(); n {
case 0:
return ProtoCommand{extractRe{pat}}, s, nil
case 1:
return ProtoCommand{extractReSub{pat}}, s, nil
default:
return ProtoCommand{extractReSubs{pat}}, s, nil
}
default:
return nil, s, fmt.Errorf("unrecognized x command")
}
}
type extractRe struct{ pat *regexp.Regexp }
type extractReSub extractRe
type extractReSubs extractRe
func (er extractRe) match(mp *matchProcessor, buf []byte) error {
if loc := er.pat.FindIndex(buf); loc != nil {
return mp.pushLoc(loc[0], loc[1], loc[1])
}
return nil
}
func (ers extractReSub) match(mp *matchProcessor, buf []byte) error {
if locs := ers.pat.FindSubmatchIndex(buf); locs != nil {
return mp.pushLoc(locs[2], locs[3], locs[1])
}
return nil
}
func (erss extractReSubs) match(mp *matchProcessor, buf []byte) error {
if locs := erss.pat.FindSubmatchIndex(buf); locs != nil {
off := 0
for li := 2; li < len(locs); {
start := locs[li] - off
li++
end := locs[li] - off
li++
next := locs[1] - off
if li < len(locs) {
next = locs[li] - off
}
if err := mp.pushLoc(start, end, next); err != nil {
return err
}
off = next
}
if err := mp.flush(); err != nil {
return err
}
}
return nil
}
func (er extractRe) Create(next Processor) Processor {
return &matchProcessor{matcher: er, next: next}
}
func (ers extractReSub) Create(next Processor) Processor {
return &matchProcessor{matcher: ers, next: next}
}
func (erss extractReSubs) Create(next Processor) Processor {
return &matchProcessor{matcher: erss, next: next}
}
func (eb extractBalanced) String() string { return fmt.Sprintf("x%s", string(eb.open)) }
func (er extractRe) String() string { return fmt.Sprintf("x%v", regexpString(er.pat)) }
func (ers extractReSub) String() string { return fmt.Sprintf("x%v", regexpString(ers.pat)) }
func (erss extractReSubs) String() string { return fmt.Sprintf("x%v", regexpString(erss.pat)) }