-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlex.go
94 lines (82 loc) · 1.88 KB
/
lex.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
// Written by https://xojoc.pw. GPLv3 or later.
package useragent
import (
"regexp"
"strings"
"unicode/utf8"
)
type lex struct {
s string
p int
}
func newLex(s string) *lex {
return &lex{s, 0}
}
// Returns true iff current position matches input string
func (l *lex) matchNoConsume(m string) bool {
return strings.HasPrefix(l.s[l.p:], m)
}
// If current position matches input string, consumes it and returns true (else false)
func (l *lex) match(m string) bool {
if !l.matchNoConsume(m) {
return false
}
l.p += len(m)
return true
}
// Consumes first provided string that matches the current position;
// returns true on finding any match, false otherwise
func (l *lex) matchFirst(args ...string) bool {
for _, m := range args {
if l.match(m) {
return true
}
}
return false
}
func (l *lex) span(m string) (string, bool) {
i := strings.Index(l.s[l.p:], m)
if i < 0 {
return "", false
}
s := l.s[l.p : l.p+i]
l.p += i + len(m)
return s, true
}
func (l *lex) spanAny(chars string) (string, bool) {
// should this whole function loop-consume until char doesn't match?
i := strings.IndexAny(l.s[l.p:], chars)
if i < 0 {
return "", false
}
s := l.s[l.p : l.p+i]
_, matchWidth := utf8.DecodeRuneInString(l.s[l.p+i:])
l.p += i + matchWidth
return s, true
}
func (l *lex) spanBefore(m, stopAt string) (string, bool) {
i := strings.Index(l.s[l.p:], m)
if i < 0 {
return "", false
}
j := strings.Index(l.s[l.p:], stopAt)
if j >= 0 && j < i {
return "", false
}
s := l.s[l.p : l.p+i]
l.p += i + len(m)
return s, true
}
// assumes the first group is the bit we want
func (l *lex) spanRegexp(re *regexp.Regexp) (before string, match string, success bool) {
loc := re.FindStringSubmatchIndex(l.s[l.p:])
if loc == nil {
return "", "", false
}
success = true
start, end := loc[2], loc[3]
before = l.s[l.p : l.p+start]
match = l.s[l.p:][start:end]
l.p += end
return
}