1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 type parseTest
struct {
19 var parseTests
= []parseTest
{
22 {`a.`, `cat{lit{a}dot{}}`},
23 {`a.b`, `cat{lit{a}dot{}lit{b}}`},
25 {`a.b.c`, `cat{lit{a}dot{}lit{b}dot{}lit{c}}`},
27 {`a|^`, `alt{lit{a}bol{}}`},
28 {`a|b`, `cc{0x61-0x62}`},
29 {`(a)`, `cap{lit{a}}`},
30 {`(a)|b`, `alt{cap{lit{a}}lit{b}}`},
31 {`a*`, `star{lit{a}}`},
32 {`a+`, `plus{lit{a}}`},
33 {`a?`, `que{lit{a}}`},
34 {`a{2}`, `rep{2,2 lit{a}}`},
35 {`a{2,3}`, `rep{2,3 lit{a}}`},
36 {`a{2,}`, `rep{2,-1 lit{a}}`},
37 {`a*?`, `nstar{lit{a}}`},
38 {`a+?`, `nplus{lit{a}}`},
39 {`a??`, `nque{lit{a}}`},
40 {`a{2}?`, `nrep{2,2 lit{a}}`},
41 {`a{2,3}?`, `nrep{2,3 lit{a}}`},
42 {`a{2,}?`, `nrep{2,-1 lit{a}}`},
43 // Malformed { } are treated as literals.
44 {`x{1001`, `str{x{1001}`},
45 {`x{9876543210`, `str{x{9876543210}`},
46 {`x{9876543210,`, `str{x{9876543210,}`},
47 {`x{2,1`, `str{x{2,1}`},
48 {`x{1,9876543210`, `str{x{1,9876543210}`},
50 {`|`, `emp{}`}, // alt{emp{}emp{}} but got factored
51 {`|x|`, `alt{emp{}lit{x}emp{}}`},
67 {`[ace]`, `cc{0x61 0x63 0x65}`},
68 {`[abc]`, `cc{0x61-0x63}`},
69 {`[a-z]`, `cc{0x61-0x7a}`},
75 {`abc|def`, `alt{str{abc}str{def}}`},
76 {`abc|def|ghi`, `alt{str{abc}str{def}str{ghi}}`},
78 // Posix and Perl extensions
79 {`[[:lower:]]`, `cc{0x61-0x7a}`},
80 {`[a-z]`, `cc{0x61-0x7a}`},
81 {`[^[:lower:]]`, `cc{0x0-0x60 0x7b-0x10ffff}`},
82 {`[[:^lower:]]`, `cc{0x0-0x60 0x7b-0x10ffff}`},
83 {`(?i)[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
84 {`(?i)[a-z]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
85 {`(?i)[^[:lower:]]`, `cc{0x0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`},
86 {`(?i)[[:^lower:]]`, `cc{0x0-0x40 0x5b-0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`},
87 {`\d`, `cc{0x30-0x39}`},
88 {`\D`, `cc{0x0-0x2f 0x3a-0x10ffff}`},
89 {`\s`, `cc{0x9-0xa 0xc-0xd 0x20}`},
90 {`\S`, `cc{0x0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}`},
91 {`\w`, `cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a}`},
92 {`\W`, `cc{0x0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x10ffff}`},
93 {`(?i)\w`, `cc{0x30-0x39 0x41-0x5a 0x5f 0x61-0x7a 0x17f 0x212a}`},
94 {`(?i)\W`, `cc{0x0-0x2f 0x3a-0x40 0x5b-0x5e 0x60 0x7b-0x17e 0x180-0x2129 0x212b-0x10ffff}`},
95 {`[^\\]`, `cc{0x0-0x5b 0x5d-0x10ffff}`},
96 // { `\C`, `byte{}` }, // probably never
98 // Unicode, negatives, and a double negative.
99 {`\p{Braille}`, `cc{0x2800-0x28ff}`},
100 {`\P{Braille}`, `cc{0x0-0x27ff 0x2900-0x10ffff}`},
101 {`\p{^Braille}`, `cc{0x0-0x27ff 0x2900-0x10ffff}`},
102 {`\P{^Braille}`, `cc{0x2800-0x28ff}`},
103 {`\pZ`, `cc{0x20 0xa0 0x1680 0x2000-0x200a 0x2028-0x2029 0x202f 0x205f 0x3000}`},
104 {`[\p{Braille}]`, `cc{0x2800-0x28ff}`},
105 {`[\P{Braille}]`, `cc{0x0-0x27ff 0x2900-0x10ffff}`},
106 {`[\p{^Braille}]`, `cc{0x0-0x27ff 0x2900-0x10ffff}`},
107 {`[\P{^Braille}]`, `cc{0x2800-0x28ff}`},
108 {`[\pZ]`, `cc{0x20 0xa0 0x1680 0x2000-0x200a 0x2028-0x2029 0x202f 0x205f 0x3000}`},
109 {`\p{Lu}`, mkCharClass(unicode
.IsUpper
)},
110 {`[\p{Lu}]`, mkCharClass(unicode
.IsUpper
)},
111 {`(?i)[\p{Lu}]`, mkCharClass(isUpperFold
)},
112 {`\p{Any}`, `dot{}`},
113 {`\p{^Any}`, `cc{}`},
116 {`[\012-\234]\141`, `cat{cc{0xa-0x9c}lit{a}}`},
117 {`[\x{41}-\x7a]\x61`, `cat{cc{0x41-0x7a}lit{a}}`},
119 // More interesting regular expressions.
120 {`a{,2}`, `str{a{,2}}`},
121 {`\.\^\$\\`, `str{.^$\}`},
122 {`[a-zABC]`, `cc{0x41-0x43 0x61-0x7a}`},
123 {`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`},
124 {`[α-ε☺]`, `cc{0x3b1-0x3b5 0x263a}`}, // utf-8
125 {`a*{`, `cat{star{lit{a}}lit{{}}`},
128 {`(?:ab)*`, `star{str{ab}}`},
129 {`(ab)*`, `star{cap{str{ab}}}`},
130 {`ab|cd`, `alt{str{ab}str{cd}}`},
131 {`a(b|c)d`, `cat{lit{a}cap{cc{0x62-0x63}}lit{d}}`},
135 {`(?:ab)(?:cd)`, `str{abcd}`},
136 {`(?:a+b+)(?:c+d+)`, `cat{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`},
137 {`(?:a+|b+)|(?:c+|d+)`, `alt{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`},
138 {`(?:a|b)|(?:c|d)`, `cc{0x61-0x64}`},
141 {`(?:[abc]|A|Z|hello|world)`, `alt{cc{0x41 0x5a 0x61-0x63}str{hello}str{world}}`},
142 {`(?:[abc]|A|Z)`, `cc{0x41 0x5a 0x61-0x63}`},
144 // Test Perl quoted literals
145 {`\Q+|*?{[\E`, `str{+|*?{[}`},
146 {`\Q+\E+`, `plus{lit{+}}`},
147 {`\Qab\E+`, `cat{lit{a}plus{lit{b}}}`},
149 {`\Q\\\E`, `str{\\}`},
151 // Test Perl \A and \z
157 {`(?m)\z`, `eot{\z}`},
158 {`(?-m)\A`, `bot{}`},
159 {`(?-m)\z`, `eot{\z}`},
161 // Test named captures
162 {`(?P<name>a)`, `cap{name:lit{a}}`},
164 // Case-folded literals
165 {`[Aa]`, `litfold{A}`},
166 {`[\x{100}\x{101}]`, `litfold{Ā}`},
167 {`[Δδ]`, `litfold{Δ}`},
170 {`abcde`, `str{abcde}`},
171 {`[Aa][Bb]cd`, `cat{strfold{AB}str{cd}}`},
174 {`abc|abd|aef|bcx|bcy`, `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}cat{str{bc}cc{0x78-0x79}}}`},
175 {`ax+y|ax+z|ay+w`, `cat{lit{a}alt{cat{plus{lit{x}}lit{y}}cat{plus{lit{x}}lit{z}}cat{plus{lit{y}}lit{w}}}}`},
179 {`(?:x|(?:xa))`, `cat{lit{x}alt{emp{}lit{a}}}`},
180 {`(?:.|(?:.a))`, `cat{dot{}alt{emp{}lit{a}}}`},
181 {`(?:A(?:A|a))`, `cat{lit{A}litfold{A}}`},
182 {`(?:A|a)`, `litfold{A}`},
183 {`A|(?:A|a)`, `litfold{A}`},
186 {`(?:(?:^).)`, `cat{bol{}dot{}}`},
187 {`(?-s)(?:(?:^).)`, `cat{bol{}dnl{}}`},
190 {`abc|abd`, `cat{str{ab}cc{0x63-0x64}}`},
191 {`a(?:b)c|abd`, `cat{str{ab}cc{0x63-0x64}}`},
192 {`abc|abd|aef|bcx|bcy`,
193 `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}` +
194 `cat{str{bc}cc{0x78-0x79}}}`},
195 {`abc|x|abd`, `alt{str{abc}lit{x}str{abd}}`},
196 {`(?i)abc|ABD`, `cat{strfold{AB}cc{0x43-0x44 0x63-0x64}}`},
197 {`[ab]c|[ab]d`, `cat{cc{0x61-0x62}cc{0x63-0x64}}`},
198 {`.c|.d`, `cat{dot{}cc{0x63-0x64}}`},
200 `cat{rep{2,2 lit{x}}alt{emp{}cc{0x30-0x39}}}`},
202 `cat{rep{2,2 lit{x}}alt{lit{y}cat{cc{0x30-0x39}lit{y}}}}`},
204 `cat{lit{a}alt{cat{nstar{dot{}}lit{c}}cat{nstar{dot{}}lit{b}}}}`},
206 // Valid repetitions.
207 {`((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))`, ``},
208 {`((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})`, ``},
211 const testFlags
= MatchNL | PerlX | UnicodeGroups
213 func TestParseSimple(t
*testing
.T
) {
214 testParseDump(t
, parseTests
, testFlags
)
217 var foldcaseTests
= []parseTest
{
218 {`AbCdE`, `strfold{ABCDE}`},
219 {`[Aa]`, `litfold{A}`},
222 // 0x17F is an old English long s (looks like an f) and folds to s.
223 // 0x212A is the Kelvin symbol and folds to k.
224 {`A[F-g]`, `cat{litfold{A}cc{0x41-0x7a 0x17f 0x212a}}`}, // [Aa][A-z...]
225 {`[[:upper:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
226 {`[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
229 func TestParseFoldCase(t
*testing
.T
) {
230 testParseDump(t
, foldcaseTests
, FoldCase
)
233 var literalTests
= []parseTest
{
234 {"(|)^$.[*+?]{5,10},\\", "str{(|)^$.[*+?]{5,10},\\}"},
237 func TestParseLiteral(t
*testing
.T
) {
238 testParseDump(t
, literalTests
, Literal
)
241 var matchnlTests
= []parseTest
{
244 {`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`},
245 {`[a\n]`, `cc{0xa 0x61}`},
248 func TestParseMatchNL(t
*testing
.T
) {
249 testParseDump(t
, matchnlTests
, MatchNL
)
252 var nomatchnlTests
= []parseTest
{
255 {`[^a]`, `cc{0x0-0x9 0xb-0x60 0x62-0x10ffff}`},
256 {`[a\n]`, `cc{0xa 0x61}`},
259 func TestParseNoMatchNL(t
*testing
.T
) {
260 testParseDump(t
, nomatchnlTests
, 0)
263 // Test Parse -> Dump.
264 func testParseDump(t
*testing
.T
, tests
[]parseTest
, flags Flags
) {
265 for _
, tt
:= range tests
{
266 re
, err
:= Parse(tt
.Regexp
, flags
)
268 t
.Errorf("Parse(%#q): %v", tt
.Regexp
, err
)
272 // It parsed. That's all we care about.
277 t
.Errorf("Parse(%#q).Dump() = %#q want %#q", tt
.Regexp
, d
, tt
.Dump
)
282 // dump prints a string representation of the regexp showing
283 // the structure explicitly.
284 func dump(re
*Regexp
) string {
285 var b strings
.Builder
290 var opNames
= []string{
295 OpAnyCharNotNL
: "dnl",
301 OpWordBoundary
: "wb",
302 OpNoWordBoundary
: "nwb",
312 // dumpRegexp writes an encoding of the syntax tree for the regexp re to b.
313 // It is used during testing to distinguish between parses that might print
314 // the same using re's String method.
315 func dumpRegexp(b
*strings
.Builder
, re
*Regexp
) {
316 if int(re
.Op
) >= len(opNames
) || opNames
[re
.Op
] == "" {
317 fmt
.Fprintf(b
, "op%d", re
.Op
)
321 b
.WriteString(opNames
[re
.Op
])
322 case OpStar
, OpPlus
, OpQuest
, OpRepeat
:
323 if re
.Flags
&NonGreedy
!= 0 {
326 b
.WriteString(opNames
[re
.Op
])
328 if len(re
.Rune
) > 1 {
333 if re
.Flags
&FoldCase
!= 0 {
334 for _
, r
:= range re
.Rune
{
335 if unicode
.SimpleFold(r
) != r
{
336 b
.WriteString("fold")
346 if re
.Flags
&WasDollar
== 0 {
350 for _
, r
:= range re
.Rune
{
353 case OpConcat
, OpAlternate
:
354 for _
, sub
:= range re
.Sub
{
357 case OpStar
, OpPlus
, OpQuest
:
358 dumpRegexp(b
, re
.Sub
[0])
360 fmt
.Fprintf(b
, "%d,%d ", re
.Min
, re
.Max
)
361 dumpRegexp(b
, re
.Sub
[0])
364 b
.WriteString(re
.Name
)
367 dumpRegexp(b
, re
.Sub
[0])
370 for i
:= 0; i
< len(re
.Rune
); i
+= 2 {
373 lo
, hi
:= re
.Rune
[i
], re
.Rune
[i
+1]
375 fmt
.Fprintf(b
, "%#x", lo
)
377 fmt
.Fprintf(b
, "%#x-%#x", lo
, hi
)
384 func mkCharClass(f
func(rune
) bool) string {
385 re
:= &Regexp
{Op
: OpCharClass
}
387 for i
:= rune(0); i
<= unicode
.MaxRune
; i
++ {
394 re
.Rune
= append(re
.Rune
, lo
, i
-1)
400 re
.Rune
= append(re
.Rune
, lo
, unicode
.MaxRune
)
405 func isUpperFold(r rune
) bool {
406 if unicode
.IsUpper(r
) {
409 c
:= unicode
.SimpleFold(r
)
411 if unicode
.IsUpper(c
) {
414 c
= unicode
.SimpleFold(c
)
419 func TestFoldConstants(t
*testing
.T
) {
421 for i
:= rune(0); i
<= unicode
.MaxRune
; i
++ {
422 if unicode
.SimpleFold(i
) == i
{
425 if last
== -1 && minFold
!= i
{
426 t
.Errorf("minFold=%#U should be %#U", minFold
, i
)
431 t
.Errorf("maxFold=%#U should be %#U", maxFold
, last
)
435 func TestAppendRangeCollapse(t
*testing
.T
) {
436 // AppendRange should collapse each of the new ranges
437 // into the earlier ones (it looks back two ranges), so that
438 // the slice never grows very large.
439 // Note that we are not calling cleanClass.
441 for i
:= rune('A'); i
<= 'Z'; i
++ {
442 r
= appendRange(r
, i
, i
)
443 r
= appendRange(r
, i
+'a'-'A', i
+'a'-'A')
445 if string(r
) != "AZaz" {
446 t
.Errorf("appendRange interlaced A-Z a-z = %s, want AZaz", string(r
))
450 var invalidRegexps
= []string{
470 "\xff", // Invalid UTF-8
483 "((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})",
487 var onlyPerl
= []string{
499 var onlyPOSIX
= []string{
508 func TestParseInvalidRegexps(t
*testing
.T
) {
509 for _
, regexp
:= range invalidRegexps
{
510 if re
, err
:= Parse(regexp
, Perl
); err
== nil {
511 t
.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp
, dump(re
))
513 if re
, err
:= Parse(regexp
, POSIX
); err
== nil {
514 t
.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp
, dump(re
))
517 for _
, regexp
:= range onlyPerl
{
518 if _
, err
:= Parse(regexp
, Perl
); err
!= nil {
519 t
.Errorf("Parse(%#q, Perl): %v", regexp
, err
)
521 if re
, err
:= Parse(regexp
, POSIX
); err
== nil {
522 t
.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp
, dump(re
))
525 for _
, regexp
:= range onlyPOSIX
{
526 if re
, err
:= Parse(regexp
, Perl
); err
== nil {
527 t
.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp
, dump(re
))
529 if _
, err
:= Parse(regexp
, POSIX
); err
!= nil {
530 t
.Errorf("Parse(%#q, POSIX): %v", regexp
, err
)
535 func TestToStringEquivalentParse(t
*testing
.T
) {
536 for _
, tt
:= range parseTests
{
537 re
, err
:= Parse(tt
.Regexp
, testFlags
)
539 t
.Errorf("Parse(%#q): %v", tt
.Regexp
, err
)
543 // It parsed. That's all we care about.
548 t
.Errorf("Parse(%#q).Dump() = %#q want %#q", tt
.Regexp
, d
, tt
.Dump
)
554 // If ToString didn't return the original regexp,
555 // it must have found one with fewer parens.
556 // Unfortunately we can't check the length here, because
557 // ToString produces "\\{" for a literal brace,
558 // but "{" is a shorter equivalent in some contexts.
559 nre
, err
:= Parse(s
, testFlags
)
561 t
.Errorf("Parse(%#q.String() = %#q): %v", tt
.Regexp
, s
, err
)
566 t
.Errorf("Parse(%#q) -> %#q; %#q vs %#q", tt
.Regexp
, s
, d
, nd
)
571 t
.Errorf("Parse(%#q) -> %#q -> %#q", tt
.Regexp
, s
, ns
)