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
|
package parser
import (
"regexp"
"testing"
)
func TestRegExp(t *testing.T) {
tt(t, func() {
{
// err
test := func(input string, expect interface{}) {
_, err := TransformRegExp(input)
is(err, expect)
}
test("[", "Unterminated character class")
test("(", "Unterminated group")
test("(?=)", "re2: Invalid (?=) <lookahead>")
test("(?=)", "re2: Invalid (?=) <lookahead>")
test("(?!)", "re2: Invalid (?!) <lookahead>")
// An error anyway
test("(?=", "re2: Invalid (?=) <lookahead>")
test("\\1", "re2: Invalid \\1 <backreference>")
test("\\90", "re2: Invalid \\90 <backreference>")
test("\\9123456789", "re2: Invalid \\9123456789 <backreference>")
test("\\(?=)", "Unmatched ')'")
test(")", "Unmatched ')'")
}
{
// err
test := func(input, expect string, expectErr interface{}) {
output, err := TransformRegExp(input)
is(output, expect)
is(err, expectErr)
}
test("(?!)", "(?!)", "re2: Invalid (?!) <lookahead>")
test(")", "", "Unmatched ')'")
test("(?!))", "", "re2: Invalid (?!) <lookahead>")
test("\\0", "\\0", nil)
test("\\1", "\\1", "re2: Invalid \\1 <backreference>")
test("\\9123456789", "\\9123456789", "re2: Invalid \\9123456789 <backreference>")
}
{
// err
test := func(input string, expect string) {
result, err := TransformRegExp(input)
is(err, nil)
if is(result, expect) {
_, err := regexp.Compile(result)
if !is(err, nil) {
t.Log(result)
}
}
}
test("", "")
test("abc", "abc")
test(`\abc`, `abc`)
test(`\a\b\c`, `a\bc`)
test(`\x`, `x`)
test(`\c`, `c`)
test(`\cA`, `\x01`)
test(`\cz`, `\x1a`)
test(`\ca`, `\x01`)
test(`\cj`, `\x0a`)
test(`\ck`, `\x0b`)
test(`\+`, `\+`)
test(`[\b]`, `[\x08]`)
test(`\u0z01\x\undefined`, `u0z01xundefined`)
test(`\\|'|\r|\n|\t|\u2028|\u2029`, `\\|'|\r|\n|\t|\x{2028}|\x{2029}`)
test("]", "]")
test("}", "}")
test("%", "%")
test("(%)", "(%)")
test("(?:[%\\s])", "(?:[%\\s])")
test("[[]", "[[]")
test("\\101", "\\x41")
test("\\51", "\\x29")
test("\\051", "\\x29")
test("\\175", "\\x7d")
test("\\04", "\\x04")
test(`<%([\s\S]+?)%>`, `<%([\s\S]+?)%>`)
test(`(.)^`, "(.)^")
test(`<%-([\s\S]+?)%>|<%=([\s\S]+?)%>|<%([\s\S]+?)%>|$`, `<%-([\s\S]+?)%>|<%=([\s\S]+?)%>|<%([\s\S]+?)%>|$`)
test(`\$`, `\$`)
test(`[G-b]`, `[G-b]`)
test(`[G-b\0]`, `[G-b\0]`)
}
})
}
func TestTransformRegExp(t *testing.T) {
tt(t, func() {
pattern, err := TransformRegExp(`\s+abc\s+`)
is(err, nil)
is(pattern, `\s+abc\s+`)
is(regexp.MustCompile(pattern).MatchString("\t abc def"), true)
})
}
|