-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.go
161 lines (125 loc) · 2.75 KB
/
regex.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
// Copyright © 2018 Timothy E. Peoples <eng@toolman.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package scanner
import (
"fmt"
"regexp"
"strconv"
)
func init() { tokenStrings[Regex] = "Regex" }
func (s *Scanner) Regex() *regexp.Regexp {
return s.regex
}
var qrPairs = map[rune]rune{
'(': ')',
'<': '>',
'[': ']',
'{': '}',
}
func init() {
for _, r := range "!\"#$%&'*+,-./:;=?@^_`|~" {
qrPairs[r] = r
}
}
func (s *Scanner) canScanRegex(tok rune) (bool, rune) {
if !(s.can(ScanRegexen) || s.reAfter.can()) {
return false, 0
}
if tok == '/' {
return true, '/'
}
if tok != 'r' {
return false, 0
}
if end, ok := qrPairs[s.Peek()]; ok {
s.Next()
return true, end
}
return false, 0
}
func (s *Scanner) scanRegex(end rune) rune {
sp := s.gs.Position
res, nt, err := s.scanTo(end)
if err != nil {
s.error(err.Error())
return nt
}
if flags := s.scanFlags(); flags != "" {
res = fmt.Sprintf("(?%s:%s)", flags, res)
}
s.gs.Position = sp
stringDump("regex", res)
if s.regex, err = regexp.Compile(res); err != nil {
s.error(fmt.Sprintf("%v: `%s`", err.Error(), res))
}
s.text = res
return Regex
}
var stringDump = func(string, string) {}
func (s *Scanner) scanFlags() string {
var flags string
for done, nt := false, s.Peek(); !done; nt = s.Peek() {
switch nt {
case 'i', 'm', 's', 'U':
flags += string(s.Next())
default:
done = true
}
}
return flags
}
func (s *Scanner) scanTo(end rune) (string, rune, error) {
var (
out string
pt, nt rune
)
for {
nt = s.Peek()
if nt == EOF {
return out, nt, fmt.Errorf("regex not terminated: `%s`", out)
}
if nt == end {
if pt != '\\' {
s.Next()
break
}
out = out[:len(out)-1]
}
tok := s.Next()
out += string(tok)
if nt == '\\' && pt == '\\' {
tok = 0
}
pt = tok
}
out, err := unquote(out)
return out, nt, err
}
func unquote(str string) (string, error) {
var out string
for str != "" {
v, _, t, err := strconv.UnquoteChar(str, 0)
if err != nil {
if err == strconv.ErrSyntax && str[0] == '\\' {
str = "\\" + str
continue
}
return out, err
}
out += string(v)
str = t
}
return out, nil
}