Source file src/regexp/syntax/parse_test.go

     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.
     4  
     5  package syntax
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  	"unicode"
    12  )
    13  
    14  type parseTest struct {
    15  	Regexp string
    16  	Dump   string
    17  }
    18  
    19  var parseTests = []parseTest{
    20  	// Base cases
    21  	{`a`, `lit{a}`},
    22  	{`a.`, `cat{lit{a}dot{}}`},
    23  	{`a.b`, `cat{lit{a}dot{}lit{b}}`},
    24  	{`ab`, `str{ab}`},
    25  	{`a.b.c`, `cat{lit{a}dot{}lit{b}dot{}lit{c}}`},
    26  	{`abc`, `str{abc}`},
    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}`},
    49  	{``, `emp{}`},
    50  	{`|`, `emp{}`}, // alt{emp{}emp{}} but got factored
    51  	{`|x|`, `alt{emp{}lit{x}emp{}}`},
    52  	{`.`, `dot{}`},
    53  	{`^`, `bol{}`},
    54  	{`$`, `eol{}`},
    55  	{`\|`, `lit{|}`},
    56  	{`\(`, `lit{(}`},
    57  	{`\)`, `lit{)}`},
    58  	{`\*`, `lit{*}`},
    59  	{`\+`, `lit{+}`},
    60  	{`\?`, `lit{?}`},
    61  	{`{`, `lit{{}`},
    62  	{`}`, `lit{}}`},
    63  	{`\.`, `lit{.}`},
    64  	{`\^`, `lit{^}`},
    65  	{`\$`, `lit{$}`},
    66  	{`\\`, `lit{\}`},
    67  	{`[ace]`, `cc{0x61 0x63 0x65}`},
    68  	{`[abc]`, `cc{0x61-0x63}`},
    69  	{`[a-z]`, `cc{0x61-0x7a}`},
    70  	{`[a]`, `lit{a}`},
    71  	{`\-`, `lit{-}`},
    72  	{`-`, `lit{-}`},
    73  	{`\_`, `lit{_}`},
    74  	{`abc`, `str{abc}`},
    75  	{`abc|def`, `alt{str{abc}str{def}}`},
    76  	{`abc|def|ghi`, `alt{str{abc}str{def}str{ghi}}`},
    77  
    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
    97  
    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{Uppercase_Letter}`, mkCharClass(unicode.IsUpper)},
   111  	{`\p{upper case-let ter}`, mkCharClass(unicode.IsUpper)},
   112  	{`\p{__upper case-let ter}`, mkCharClass(unicode.IsUpper)},
   113  	{`[\p{Lu}]`, mkCharClass(unicode.IsUpper)},
   114  	{`(?i)[\p{Lu}]`, mkCharClass(isUpperFold)},
   115  	{`\p{Any}`, `dot{}`},
   116  	{`\p{^Any}`, `cc{}`},
   117  	{`(?i)\p{ascii}`, `cc{0x0-0x7f 0x17f 0x212a}`},
   118  	{`\p{Assigned}`, mkCharClass(func(r rune) bool { return !unicode.In(r, unicode.Cn) })},
   119  	{`\p{^Assigned}`, mkCharClass(func(r rune) bool { return unicode.In(r, unicode.Cn) })},
   120  
   121  	// Hex, octal.
   122  	{`[\012-\234]\141`, `cat{cc{0xa-0x9c}lit{a}}`},
   123  	{`[\x{41}-\x7a]\x61`, `cat{cc{0x41-0x7a}lit{a}}`},
   124  
   125  	// More interesting regular expressions.
   126  	{`a{,2}`, `str{a{,2}}`},
   127  	{`\.\^\$\\`, `str{.^$\}`},
   128  	{`[a-zABC]`, `cc{0x41-0x43 0x61-0x7a}`},
   129  	{`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`},
   130  	{`[α-ε☺]`, `cc{0x3b1-0x3b5 0x263a}`}, // utf-8
   131  	{`a*{`, `cat{star{lit{a}}lit{{}}`},
   132  
   133  	// Test precedences
   134  	{`(?:ab)*`, `star{str{ab}}`},
   135  	{`(ab)*`, `star{cap{str{ab}}}`},
   136  	{`ab|cd`, `alt{str{ab}str{cd}}`},
   137  	{`a(b|c)d`, `cat{lit{a}cap{cc{0x62-0x63}}lit{d}}`},
   138  
   139  	// Test flattening.
   140  	{`(?:a)`, `lit{a}`},
   141  	{`(?:ab)(?:cd)`, `str{abcd}`},
   142  	{`(?:a+b+)(?:c+d+)`, `cat{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`},
   143  	{`(?:a+|b+)|(?:c+|d+)`, `alt{plus{lit{a}}plus{lit{b}}plus{lit{c}}plus{lit{d}}}`},
   144  	{`(?:a|b)|(?:c|d)`, `cc{0x61-0x64}`},
   145  	{`a|.`, `dot{}`},
   146  	{`.|a`, `dot{}`},
   147  	{`(?:[abc]|A|Z|hello|world)`, `alt{cc{0x41 0x5a 0x61-0x63}str{hello}str{world}}`},
   148  	{`(?:[abc]|A|Z)`, `cc{0x41 0x5a 0x61-0x63}`},
   149  
   150  	// Test Perl quoted literals
   151  	{`\Q+|*?{[\E`, `str{+|*?{[}`},
   152  	{`\Q+\E+`, `plus{lit{+}}`},
   153  	{`\Qab\E+`, `cat{lit{a}plus{lit{b}}}`},
   154  	{`\Q\\E`, `lit{\}`},
   155  	{`\Q\\\E`, `str{\\}`},
   156  
   157  	// Test Perl \A and \z
   158  	{`(?m)^`, `bol{}`},
   159  	{`(?m)$`, `eol{}`},
   160  	{`(?-m)^`, `bot{}`},
   161  	{`(?-m)$`, `eot{}`},
   162  	{`(?m)\A`, `bot{}`},
   163  	{`(?m)\z`, `eot{\z}`},
   164  	{`(?-m)\A`, `bot{}`},
   165  	{`(?-m)\z`, `eot{\z}`},
   166  
   167  	// Test named captures
   168  	{`(?P<name>a)`, `cap{name:lit{a}}`},
   169  	{`(?<name>a)`, `cap{name:lit{a}}`},
   170  
   171  	// Case-folded literals
   172  	{`[Aa]`, `litfold{A}`},
   173  	{`[\x{100}\x{101}]`, `litfold{Ā}`},
   174  	{`[Δδ]`, `litfold{Δ}`},
   175  
   176  	// Strings
   177  	{`abcde`, `str{abcde}`},
   178  	{`[Aa][Bb]cd`, `cat{strfold{AB}str{cd}}`},
   179  
   180  	// Factoring.
   181  	{`abc|abd|aef|bcx|bcy`, `alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}cat{str{bc}cc{0x78-0x79}}}`},
   182  	{`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}}}}`},
   183  
   184  	// Bug fixes.
   185  	{`(?:.)`, `dot{}`},
   186  	{`(?:x|(?:xa))`, `cat{lit{x}alt{emp{}lit{a}}}`},
   187  	{`(?:.|(?:.a))`, `cat{dot{}alt{emp{}lit{a}}}`},
   188  	{`(?:A(?:A|a))`, `cat{lit{A}litfold{A}}`},
   189  	{`(?:A|a)`, `litfold{A}`},
   190  	{`A|(?:A|a)`, `litfold{A}`},
   191  	{`(?s).`, `dot{}`},
   192  	{`(?-s).`, `dnl{}`},
   193  	{`(?:(?:^).)`, `cat{bol{}dot{}}`},
   194  	{`(?-s)(?:(?:^).)`, `cat{bol{}dnl{}}`},
   195  	{`[\s\S]a`, `cat{cc{0x0-0x10ffff}lit{a}}`},
   196  
   197  	// RE2 prefix_tests
   198  	{`abc|abd`, `cat{str{ab}cc{0x63-0x64}}`},
   199  	{`a(?:b)c|abd`, `cat{str{ab}cc{0x63-0x64}}`},
   200  	{`abc|abd|aef|bcx|bcy`,
   201  		`alt{cat{lit{a}alt{cat{lit{b}cc{0x63-0x64}}str{ef}}}` +
   202  			`cat{str{bc}cc{0x78-0x79}}}`},
   203  	{`abc|x|abd`, `alt{str{abc}lit{x}str{abd}}`},
   204  	{`(?i)abc|ABD`, `cat{strfold{AB}cc{0x43-0x44 0x63-0x64}}`},
   205  	{`[ab]c|[ab]d`, `cat{cc{0x61-0x62}cc{0x63-0x64}}`},
   206  	{`.c|.d`, `cat{dot{}cc{0x63-0x64}}`},
   207  	{`x{2}|x{2}[0-9]`,
   208  		`cat{rep{2,2 lit{x}}alt{emp{}cc{0x30-0x39}}}`},
   209  	{`x{2}y|x{2}[0-9]y`,
   210  		`cat{rep{2,2 lit{x}}alt{lit{y}cat{cc{0x30-0x39}lit{y}}}}`},
   211  	{`a.*?c|a.*?b`,
   212  		`cat{lit{a}alt{cat{nstar{dot{}}lit{c}}cat{nstar{dot{}}lit{b}}}}`},
   213  
   214  	// Valid repetitions.
   215  	{`((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))`, ``},
   216  	{`((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})`, ``},
   217  
   218  	// Valid nesting.
   219  	{strings.Repeat("(", 999) + strings.Repeat(")", 999), ``},
   220  	{strings.Repeat("(?:", 999) + strings.Repeat(")*", 999), ``},
   221  	{"(" + strings.Repeat("|", 12345) + ")", ``}, // not nested at all
   222  }
   223  
   224  const testFlags = MatchNL | PerlX | UnicodeGroups
   225  
   226  func TestParseSimple(t *testing.T) {
   227  	testParseDump(t, parseTests, testFlags)
   228  }
   229  
   230  var foldcaseTests = []parseTest{
   231  	{`AbCdE`, `strfold{ABCDE}`},
   232  	{`[Aa]`, `litfold{A}`},
   233  	{`a`, `litfold{A}`},
   234  
   235  	// 0x17F is an old English long s (looks like an f) and folds to s.
   236  	// 0x212A is the Kelvin symbol and folds to k.
   237  	{`A[F-g]`, `cat{litfold{A}cc{0x41-0x7a 0x17f 0x212a}}`}, // [Aa][A-z...]
   238  	{`[[:upper:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
   239  	{`[[:lower:]]`, `cc{0x41-0x5a 0x61-0x7a 0x17f 0x212a}`},
   240  }
   241  
   242  func TestParseFoldCase(t *testing.T) {
   243  	testParseDump(t, foldcaseTests, FoldCase)
   244  }
   245  
   246  var literalTests = []parseTest{
   247  	{"(|)^$.[*+?]{5,10},\\", "str{(|)^$.[*+?]{5,10},\\}"},
   248  }
   249  
   250  func TestParseLiteral(t *testing.T) {
   251  	testParseDump(t, literalTests, Literal)
   252  }
   253  
   254  var matchnlTests = []parseTest{
   255  	{`.`, `dot{}`},
   256  	{"\n", "lit{\n}"},
   257  	{`[^a]`, `cc{0x0-0x60 0x62-0x10ffff}`},
   258  	{`[a\n]`, `cc{0xa 0x61}`},
   259  }
   260  
   261  func TestParseMatchNL(t *testing.T) {
   262  	testParseDump(t, matchnlTests, MatchNL)
   263  }
   264  
   265  var nomatchnlTests = []parseTest{
   266  	{`.`, `dnl{}`},
   267  	{"\n", "lit{\n}"},
   268  	{`[^a]`, `cc{0x0-0x9 0xb-0x60 0x62-0x10ffff}`},
   269  	{`[a\n]`, `cc{0xa 0x61}`},
   270  }
   271  
   272  func TestParseNoMatchNL(t *testing.T) {
   273  	testParseDump(t, nomatchnlTests, 0)
   274  }
   275  
   276  // Test Parse -> Dump.
   277  func testParseDump(t *testing.T, tests []parseTest, flags Flags) {
   278  	for _, tt := range tests {
   279  		re, err := Parse(tt.Regexp, flags)
   280  		if err != nil {
   281  			t.Errorf("Parse(%#q): %v", tt.Regexp, err)
   282  			continue
   283  		}
   284  		if tt.Dump == "" {
   285  			// It parsed. That's all we care about.
   286  			continue
   287  		}
   288  		d := dump(re)
   289  		if d != tt.Dump {
   290  			t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump)
   291  		}
   292  	}
   293  }
   294  
   295  // dump prints a string representation of the regexp showing
   296  // the structure explicitly.
   297  func dump(re *Regexp) string {
   298  	var b strings.Builder
   299  	dumpRegexp(&b, re)
   300  	return b.String()
   301  }
   302  
   303  var opNames = []string{
   304  	OpNoMatch:        "no",
   305  	OpEmptyMatch:     "emp",
   306  	OpLiteral:        "lit",
   307  	OpCharClass:      "cc",
   308  	OpAnyCharNotNL:   "dnl",
   309  	OpAnyChar:        "dot",
   310  	OpBeginLine:      "bol",
   311  	OpEndLine:        "eol",
   312  	OpBeginText:      "bot",
   313  	OpEndText:        "eot",
   314  	OpWordBoundary:   "wb",
   315  	OpNoWordBoundary: "nwb",
   316  	OpCapture:        "cap",
   317  	OpStar:           "star",
   318  	OpPlus:           "plus",
   319  	OpQuest:          "que",
   320  	OpRepeat:         "rep",
   321  	OpConcat:         "cat",
   322  	OpAlternate:      "alt",
   323  }
   324  
   325  // dumpRegexp writes an encoding of the syntax tree for the regexp re to b.
   326  // It is used during testing to distinguish between parses that might print
   327  // the same using re's String method.
   328  func dumpRegexp(b *strings.Builder, re *Regexp) {
   329  	if int(re.Op) >= len(opNames) || opNames[re.Op] == "" {
   330  		fmt.Fprintf(b, "op%d", re.Op)
   331  	} else {
   332  		switch re.Op {
   333  		default:
   334  			b.WriteString(opNames[re.Op])
   335  		case OpStar, OpPlus, OpQuest, OpRepeat:
   336  			if re.Flags&NonGreedy != 0 {
   337  				b.WriteByte('n')
   338  			}
   339  			b.WriteString(opNames[re.Op])
   340  		case OpLiteral:
   341  			if len(re.Rune) > 1 {
   342  				b.WriteString("str")
   343  			} else {
   344  				b.WriteString("lit")
   345  			}
   346  			if re.Flags&FoldCase != 0 {
   347  				for _, r := range re.Rune {
   348  					if unicode.SimpleFold(r) != r {
   349  						b.WriteString("fold")
   350  						break
   351  					}
   352  				}
   353  			}
   354  		}
   355  	}
   356  	b.WriteByte('{')
   357  	switch re.Op {
   358  	case OpEndText:
   359  		if re.Flags&WasDollar == 0 {
   360  			b.WriteString(`\z`)
   361  		}
   362  	case OpLiteral:
   363  		for _, r := range re.Rune {
   364  			b.WriteRune(r)
   365  		}
   366  	case OpConcat, OpAlternate:
   367  		for _, sub := range re.Sub {
   368  			dumpRegexp(b, sub)
   369  		}
   370  	case OpStar, OpPlus, OpQuest:
   371  		dumpRegexp(b, re.Sub[0])
   372  	case OpRepeat:
   373  		fmt.Fprintf(b, "%d,%d ", re.Min, re.Max)
   374  		dumpRegexp(b, re.Sub[0])
   375  	case OpCapture:
   376  		if re.Name != "" {
   377  			b.WriteString(re.Name)
   378  			b.WriteByte(':')
   379  		}
   380  		dumpRegexp(b, re.Sub[0])
   381  	case OpCharClass:
   382  		sep := ""
   383  		for i := 0; i < len(re.Rune); i += 2 {
   384  			b.WriteString(sep)
   385  			sep = " "
   386  			lo, hi := re.Rune[i], re.Rune[i+1]
   387  			if lo == hi {
   388  				fmt.Fprintf(b, "%#x", lo)
   389  			} else {
   390  				fmt.Fprintf(b, "%#x-%#x", lo, hi)
   391  			}
   392  		}
   393  	}
   394  	b.WriteByte('}')
   395  }
   396  
   397  func mkCharClass(f func(rune) bool) string {
   398  	re := &Regexp{Op: OpCharClass}
   399  	lo := rune(-1)
   400  	for i := rune(0); i <= unicode.MaxRune; i++ {
   401  		if f(i) {
   402  			if lo < 0 {
   403  				lo = i
   404  			}
   405  		} else {
   406  			if lo >= 0 {
   407  				re.Rune = append(re.Rune, lo, i-1)
   408  				lo = -1
   409  			}
   410  		}
   411  	}
   412  	if lo >= 0 {
   413  		re.Rune = append(re.Rune, lo, unicode.MaxRune)
   414  	}
   415  	return dump(re)
   416  }
   417  
   418  func isUpperFold(r rune) bool {
   419  	if unicode.IsUpper(r) {
   420  		return true
   421  	}
   422  	c := unicode.SimpleFold(r)
   423  	for c != r {
   424  		if unicode.IsUpper(c) {
   425  			return true
   426  		}
   427  		c = unicode.SimpleFold(c)
   428  	}
   429  	return false
   430  }
   431  
   432  func TestFoldConstants(t *testing.T) {
   433  	last := rune(-1)
   434  	for i := rune(0); i <= unicode.MaxRune; i++ {
   435  		if unicode.SimpleFold(i) == i {
   436  			continue
   437  		}
   438  		if last == -1 && minFold != i {
   439  			t.Errorf("minFold=%#U should be %#U", minFold, i)
   440  		}
   441  		last = i
   442  	}
   443  	if maxFold != last {
   444  		t.Errorf("maxFold=%#U should be %#U", maxFold, last)
   445  	}
   446  }
   447  
   448  func TestAppendRangeCollapse(t *testing.T) {
   449  	// AppendRange should collapse each of the new ranges
   450  	// into the earlier ones (it looks back two ranges), so that
   451  	// the slice never grows very large.
   452  	// Note that we are not calling cleanClass.
   453  	var r []rune
   454  	for i := rune('A'); i <= 'Z'; i++ {
   455  		r = appendRange(r, i, i)
   456  		r = appendRange(r, i+'a'-'A', i+'a'-'A')
   457  	}
   458  	if string(r) != "AZaz" {
   459  		t.Errorf("appendRange interlaced A-Z a-z = %s, want AZaz", string(r))
   460  	}
   461  }
   462  
   463  var invalidRegexps = []string{
   464  	`(`,
   465  	`)`,
   466  	`(a`,
   467  	`a)`,
   468  	`(a))`,
   469  	`(a|b|`,
   470  	`a|b|)`,
   471  	`(a|b|))`,
   472  	`(a|b`,
   473  	`a|b)`,
   474  	`(a|b))`,
   475  	`[a-z`,
   476  	`([a-z)`,
   477  	`[a-z)`,
   478  	`([a-z]))`,
   479  	`x{1001}`,
   480  	`x{9876543210}`,
   481  	`x{2,1}`,
   482  	`x{1,9876543210}`,
   483  	"\xff", // Invalid UTF-8
   484  	"[\xff]",
   485  	"[\\\xff]",
   486  	"\\\xff",
   487  	`(?P<name>a`,
   488  	`(?P<name>`,
   489  	`(?P<name`,
   490  	`(?P<x y>a)`,
   491  	`(?P<>a)`,
   492  	`(?<name>a`,
   493  	`(?<name>`,
   494  	`(?<name`,
   495  	`(?<x y>a)`,
   496  	`(?<>a)`,
   497  	`[a-Z]`,
   498  	`(?i)[a-Z]`,
   499  	`\Q\E*`,
   500  	`a{100000}`,  // too much repetition
   501  	`a{100000,}`, // too much repetition
   502  	"((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})",    // too much repetition
   503  	strings.Repeat("(", 1000) + strings.Repeat(")", 1000),    // too deep
   504  	strings.Repeat("(?:", 1000) + strings.Repeat(")*", 1000), // too deep
   505  	"(" + strings.Repeat("(xx?)", 1000) + "){1000}",          // too long
   506  	strings.Repeat("(xx?){1000}", 1000),                      // too long
   507  	strings.Repeat(`\pL`, 27000),                             // too many runes
   508  }
   509  
   510  var onlyPerl = []string{
   511  	`[a-b-c]`,
   512  	`\Qabc\E`,
   513  	`\Q*+?{[\E`,
   514  	`\Q\\E`,
   515  	`\Q\\\E`,
   516  	`\Q\\\\E`,
   517  	`\Q\\\\\E`,
   518  	`(?:a)`,
   519  	`(?P<name>a)`,
   520  }
   521  
   522  var onlyPOSIX = []string{
   523  	"a++",
   524  	"a**",
   525  	"a?*",
   526  	"a+*",
   527  	"a{1}*",
   528  	".{1}{2}.{3}",
   529  }
   530  
   531  func TestParseInvalidRegexps(t *testing.T) {
   532  	for _, regexp := range invalidRegexps {
   533  		if re, err := Parse(regexp, Perl); err == nil {
   534  			t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re))
   535  		}
   536  		if re, err := Parse(regexp, POSIX); err == nil {
   537  			t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re))
   538  		}
   539  	}
   540  	for _, regexp := range onlyPerl {
   541  		if _, err := Parse(regexp, Perl); err != nil {
   542  			t.Errorf("Parse(%#q, Perl): %v", regexp, err)
   543  		}
   544  		if re, err := Parse(regexp, POSIX); err == nil {
   545  			t.Errorf("Parse(%#q, POSIX) = %s, should have failed", regexp, dump(re))
   546  		}
   547  	}
   548  	for _, regexp := range onlyPOSIX {
   549  		if re, err := Parse(regexp, Perl); err == nil {
   550  			t.Errorf("Parse(%#q, Perl) = %s, should have failed", regexp, dump(re))
   551  		}
   552  		if _, err := Parse(regexp, POSIX); err != nil {
   553  			t.Errorf("Parse(%#q, POSIX): %v", regexp, err)
   554  		}
   555  	}
   556  }
   557  
   558  func TestToStringEquivalentParse(t *testing.T) {
   559  	for _, tt := range parseTests {
   560  		re, err := Parse(tt.Regexp, testFlags)
   561  		if err != nil {
   562  			t.Errorf("Parse(%#q): %v", tt.Regexp, err)
   563  			continue
   564  		}
   565  		if tt.Dump == "" {
   566  			// It parsed. That's all we care about.
   567  			continue
   568  		}
   569  		d := dump(re)
   570  		if d != tt.Dump {
   571  			t.Errorf("Parse(%#q).Dump() = %#q want %#q", tt.Regexp, d, tt.Dump)
   572  			continue
   573  		}
   574  
   575  		s := re.String()
   576  		if s != tt.Regexp {
   577  			// If ToString didn't return the original regexp,
   578  			// it must have found one with fewer parens.
   579  			// Unfortunately we can't check the length here, because
   580  			// ToString produces "\\{" for a literal brace,
   581  			// but "{" is a shorter equivalent in some contexts.
   582  			nre, err := Parse(s, testFlags)
   583  			if err != nil {
   584  				t.Errorf("Parse(%#q.String() = %#q): %v", tt.Regexp, s, err)
   585  				continue
   586  			}
   587  			nd := dump(nre)
   588  			if d != nd {
   589  				t.Errorf("Parse(%#q) -> %#q; %#q vs %#q", tt.Regexp, s, d, nd)
   590  			}
   591  
   592  			ns := nre.String()
   593  			if s != ns {
   594  				t.Errorf("Parse(%#q) -> %#q -> %#q", tt.Regexp, s, ns)
   595  			}
   596  		}
   597  	}
   598  }
   599  
   600  var stringTests = []struct {
   601  	re  string
   602  	out string
   603  }{
   604  	{`x(?i:ab*c|d?e)1`, `x(?i:AB*C|D?E)1`},
   605  	{`x(?i:ab*cd?e)1`, `x(?i:AB*CD?E)1`},
   606  	{`0(?i:ab*c|d?e)1`, `(?i:0(?:AB*C|D?E)1)`},
   607  	{`0(?i:ab*cd?e)1`, `(?i:0AB*CD?E1)`},
   608  	{`x(?i:ab*c|d?e)`, `x(?i:AB*C|D?E)`},
   609  	{`x(?i:ab*cd?e)`, `x(?i:AB*CD?E)`},
   610  	{`0(?i:ab*c|d?e)`, `(?i:0(?:AB*C|D?E))`},
   611  	{`0(?i:ab*cd?e)`, `(?i:0AB*CD?E)`},
   612  	{`(?i:ab*c|d?e)1`, `(?i:(?:AB*C|D?E)1)`},
   613  	{`(?i:ab*cd?e)1`, `(?i:AB*CD?E1)`},
   614  	{`(?i:ab)[123](?i:cd)`, `(?i:AB[1-3]CD)`},
   615  	{`(?i:ab*c|d?e)`, `(?i:AB*C|D?E)`},
   616  	{`[Aa][Bb]`, `(?i:AB)`},
   617  	{`[Aa][Bb]*[Cc]`, `(?i:AB*C)`},
   618  	{`A(?:[Bb][Cc]|[Dd])[Zz]`, `A(?i:(?:BC|D)Z)`},
   619  	{`[Aa](?:[Bb][Cc]|[Dd])Z`, `(?i:A(?:BC|D))Z`},
   620  }
   621  
   622  func TestString(t *testing.T) {
   623  	for _, tt := range stringTests {
   624  		re, err := Parse(tt.re, Perl)
   625  		if err != nil {
   626  			t.Errorf("Parse(%#q): %v", tt.re, err)
   627  			continue
   628  		}
   629  		out := re.String()
   630  		if out != tt.out {
   631  			t.Errorf("Parse(%#q).String() = %#q, want %#q", tt.re, out, tt.out)
   632  		}
   633  	}
   634  }
   635  

View as plain text