Skip to content
Snippets Groups Projects
parse.go 54.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2011 The Go Authors. All rights reserved.
    
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    package syntax
    
    import (
    	"sort"
    
    )
    
    // An Error describes a failure to parse a regular expression
    // and gives the offending expression.
    type Error struct {
    	Code ErrorCode
    	Expr string
    }
    
    
    func (e *Error) Error() string {
    
    	return "error parsing regexp: " + e.Code.String() + ": `" + e.Expr + "`"
    }
    
    // An ErrorCode describes a failure to parse a regular expression.
    type ErrorCode string
    
    const (
    	// Unexpected error
    	ErrInternalError ErrorCode = "regexp/syntax: internal error"
    
    	// Parse errors
    	ErrInvalidCharClass      ErrorCode = "invalid character class"
    	ErrInvalidCharRange      ErrorCode = "invalid character class range"
    	ErrInvalidEscape         ErrorCode = "invalid escape sequence"
    	ErrInvalidNamedCapture   ErrorCode = "invalid named capture"
    	ErrInvalidPerlOp         ErrorCode = "invalid or unsupported Perl syntax"
    	ErrInvalidRepeatOp       ErrorCode = "invalid nested repetition operator"
    	ErrInvalidRepeatSize     ErrorCode = "invalid repeat count"
    	ErrInvalidUTF8           ErrorCode = "invalid UTF-8"
    	ErrMissingBracket        ErrorCode = "missing closing ]"
    	ErrMissingParen          ErrorCode = "missing closing )"
    	ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator"
    	ErrTrailingBackslash     ErrorCode = "trailing backslash at end of expression"
    
    	ErrUnexpectedParen       ErrorCode = "unexpected )"
    
    	ErrNestingDepth          ErrorCode = "expression nests too deeply"
    
    cuiweixie's avatar
    cuiweixie committed
    	ErrLarge                 ErrorCode = "expression too large"
    
    )
    
    func (e ErrorCode) String() string {
    	return string(e)
    }
    
    // Flags control the behavior of the parser and record information about regexp context.
    type Flags uint16
    
    const (
    	FoldCase      Flags = 1 << iota // case-insensitive match
    	Literal                         // treat pattern as literal string
    	ClassNL                         // allow character classes like [^a-z] and [[:space:]] to match newline
    	DotNL                           // allow . to match newline
    	OneLine                         // treat ^ and $ as only matching at beginning and end of text
    	NonGreedy                       // make repetition operators default to non-greedy
    	PerlX                           // allow Perl extensions
    	UnicodeGroups                   // allow \p{Han}, \P{Han} for Unicode group and negation
    	WasDollar                       // regexp OpEndText was $, not \z
    	Simple                          // regexp contains no counted repetition
    
    	MatchNL = ClassNL | DotNL
    
    	Perl        = ClassNL | OneLine | PerlX | UnicodeGroups // as close to Perl as possible
    	POSIX Flags = 0                                         // POSIX syntax
    )
    
    // Pseudo-ops for parsing stack.
    const (
    	opLeftParen = opPseudo + iota
    	opVerticalBar
    )
    
    
    // maxHeight is the maximum height of a regexp parse tree.
    // It is somewhat arbitrarily chosen, but the idea is to be large enough
    // that no one will actually hit in real use but at the same time small enough
    // that recursion on the Regexp tree will not hit the 1GB Go stack limit.
    // The maximum amount of stack for a single recursive frame is probably
    // closer to 1kB, so this could potentially be raised, but it seems unlikely
    // that people have regexps nested even this deeply.
    // We ran a test on Google's C++ code base and turned up only
    // a single use case with depth > 100; it had depth 128.
    // Using depth 1000 should be plenty of margin.
    // As an optimization, we don't even bother calculating heights
    // until we've allocated at least maxHeight Regexp structures.
    const maxHeight = 1000
    
    
    // maxSize is the maximum size of a compiled regexp in Insts.
    // It too is somewhat arbitrarily chosen, but the idea is to be large enough
    // to allow significant regexps while at the same time small enough that
    // the compiled form will not take up too much memory.
    // 128 MB is enough for a 3.3 million Inst structures, which roughly
    // corresponds to a 3.3 MB regexp.
    const (
    	maxSize  = 128 << 20 / instSize
    	instSize = 5 * 8 // byte, 2 uint32, slice is 5 64-bit words
    )
    
    // maxRunes is the maximum number of runes allowed in a regexp tree
    // counting the runes in all the nodes.
    // Ignoring character classes p.numRunes is always less than the length of the regexp.
    // Character classes can make it much larger: each \pL adds 1292 runes.
    // 128 MB is enough for 32M runes, which is over 26k \pL instances.
    // Note that repetitions do not make copies of the rune slices,
    // so \pL{1000} is only one rune slice, not 1000.
    // We could keep a cache of character classes we've seen,
    // so that all the \pL we see use the same rune list,
    // but that doesn't remove the problem entirely:
    // consider something like [\pL01234][\pL01235][\pL01236]...[\pL^&*()].
    // And because the Rune slice is exposed directly in the Regexp,
    // there is not an opportunity to change the representation to allow
    // partial sharing between different character classes.
    // So the limit is the best we can do.
    const (
    	maxRunes = 128 << 20 / runeSize
    	runeSize = 4 // rune is int32
    )
    
    
    type parser struct {
    	flags       Flags     // parse mode flags
    	stack       []*Regexp // stack of parsed expressions
    
    	free        *Regexp
    	numCap      int // number of capturing groups seen
    
    	wholeRegexp string
    
    	tmpClass    []rune            // temporary char class work space
    	numRegexp   int               // number of regexps allocated
    	numRunes    int               // number of runes in char classes
    	repeats     int64             // product of all repetitions seen
    	height      map[*Regexp]int   // regexp height, for height limit check
    	size        map[*Regexp]int64 // regexp compiled size, for size limit check
    
    func (p *parser) newRegexp(op Op) *Regexp {
    	re := p.free
    	if re != nil {
    		p.free = re.Sub0[0]
    		*re = Regexp{}
    	} else {
    		re = new(Regexp)
    
    	}
    	re.Op = op
    	return re
    }
    
    func (p *parser) reuse(re *Regexp) {
    
    	if p.height != nil {
    		delete(p.height, re)
    	}
    
    	re.Sub0[0] = p.free
    	p.free = re
    }
    
    
    func (p *parser) checkLimits(re *Regexp) {
    	if p.numRunes > maxRunes {
    
    cuiweixie's avatar
    cuiweixie committed
    		panic(ErrLarge)
    
    	}
    	p.checkSize(re)
    	p.checkHeight(re)
    }
    
    func (p *parser) checkSize(re *Regexp) {
    	if p.size == nil {
    		// We haven't started tracking size yet.
    		// Do a relatively cheap check to see if we need to start.
    		// Maintain the product of all the repeats we've seen
    		// and don't track if the total number of regexp nodes
    		// we've seen times the repeat product is in budget.
    		if p.repeats == 0 {
    			p.repeats = 1
    		}
    		if re.Op == OpRepeat {
    			n := re.Max
    			if n == -1 {
    				n = re.Min
    			}
    			if n <= 0 {
    				n = 1
    			}
    			if int64(n) > maxSize/p.repeats {
    				p.repeats = maxSize
    			} else {
    				p.repeats *= int64(n)
    			}
    		}
    		if int64(p.numRegexp) < maxSize/p.repeats {
    			return
    		}
    
    		// We need to start tracking size.
    		// Make the map and belatedly populate it
    		// with info about everything we've constructed so far.
    		p.size = make(map[*Regexp]int64)
    		for _, re := range p.stack {
    			p.checkSize(re)
    		}
    	}
    
    	if p.calcSize(re, true) > maxSize {
    
    cuiweixie's avatar
    cuiweixie committed
    		panic(ErrLarge)
    
    	}
    }
    
    func (p *parser) calcSize(re *Regexp, force bool) int64 {
    	if !force {
    		if size, ok := p.size[re]; ok {
    			return size
    		}
    	}
    
    	var size int64
    	switch re.Op {
    	case OpLiteral:
    		size = int64(len(re.Rune))
    	case OpCapture, OpStar:
    		// star can be 1+ or 2+; assume 2 pessimistically
    		size = 2 + p.calcSize(re.Sub[0], false)
    	case OpPlus, OpQuest:
    		size = 1 + p.calcSize(re.Sub[0], false)
    	case OpConcat:
    		for _, sub := range re.Sub {
    			size += p.calcSize(sub, false)
    		}
    	case OpAlternate:
    		for _, sub := range re.Sub {
    			size += p.calcSize(sub, false)
    		}
    		if len(re.Sub) > 1 {
    			size += int64(len(re.Sub)) - 1
    		}
    	case OpRepeat:
    		sub := p.calcSize(re.Sub[0], false)
    		if re.Max == -1 {
    			if re.Min == 0 {
    				size = 2 + sub // x*
    			} else {
    				size = 1 + int64(re.Min)*sub // xxx+
    			}
    			break
    		}
    		// x{2,5} = xx(x(x(x)?)?)?
    		size = int64(re.Max)*sub + int64(re.Max-re.Min)
    	}
    
    
    	size = max(1, size)
    
    	p.size[re] = size
    	return size
    }
    
    
    func (p *parser) checkHeight(re *Regexp) {
    	if p.numRegexp < maxHeight {
    		return
    	}
    	if p.height == nil {
    		p.height = make(map[*Regexp]int)
    		for _, re := range p.stack {
    			p.checkHeight(re)
    		}
    	}
    	if p.calcHeight(re, true) > maxHeight {
    
    	}
    }
    
    func (p *parser) calcHeight(re *Regexp, force bool) int {
    	if !force {
    		if h, ok := p.height[re]; ok {
    			return h
    		}
    	}
    	h := 1
    	for _, sub := range re.Sub {
    		hsub := p.calcHeight(sub, false)
    		if h < 1+hsub {
    			h = 1 + hsub
    		}
    	}
    	p.height[re] = h
    	return h
    }
    
    
    // Parse stack manipulation.
    
    // push pushes the regexp re onto the parse stack and returns the regexp.
    func (p *parser) push(re *Regexp) *Regexp {
    
    	p.numRunes += len(re.Rune)
    
    	if re.Op == OpCharClass && len(re.Rune) == 2 && re.Rune[0] == re.Rune[1] {
    		// Single rune.
    		if p.maybeConcat(re.Rune[0], p.flags&^FoldCase) {
    			return nil
    		}
    		re.Op = OpLiteral
    		re.Rune = re.Rune[:1]
    		re.Flags = p.flags &^ FoldCase
    	} else if re.Op == OpCharClass && len(re.Rune) == 4 &&
    		re.Rune[0] == re.Rune[1] && re.Rune[2] == re.Rune[3] &&
    		unicode.SimpleFold(re.Rune[0]) == re.Rune[2] &&
    		unicode.SimpleFold(re.Rune[2]) == re.Rune[0] ||
    		re.Op == OpCharClass && len(re.Rune) == 2 &&
    			re.Rune[0]+1 == re.Rune[1] &&
    			unicode.SimpleFold(re.Rune[0]) == re.Rune[1] &&
    			unicode.SimpleFold(re.Rune[1]) == re.Rune[0] {
    		// Case-insensitive rune like [Aa] or [Δδ].
    		if p.maybeConcat(re.Rune[0], p.flags|FoldCase) {
    			return nil
    		}
    
    		// Rewrite as (case-insensitive) literal.
    		re.Op = OpLiteral
    		re.Rune = re.Rune[:1]
    		re.Flags = p.flags | FoldCase
    	} else {
    		// Incremental concatenation.
    		p.maybeConcat(-1, 0)
    	}
    
    
    	p.stack = append(p.stack, re)
    
    	p.checkLimits(re)
    
    // maybeConcat implements incremental concatenation
    
    // of literal runes into string nodes. The parser calls this
    
    // before each push, so only the top fragment of the stack
    
    // might need processing. Since this is called before a push,
    
    // the topmost literal is no longer subject to operators like *
    // (Otherwise ab* would turn into (ab)*.)
    // If r >= 0 and there's a node left over, maybeConcat uses it
    // to push r with the given flags.
    // maybeConcat reports whether r was pushed.
    
    Russ Cox's avatar
    Russ Cox committed
    func (p *parser) maybeConcat(r rune, flags Flags) bool {
    
    	n := len(p.stack)
    	if n < 2 {
    		return false
    
    
    	re1 := p.stack[n-1]
    	re2 := p.stack[n-2]
    	if re1.Op != OpLiteral || re2.Op != OpLiteral || re1.Flags&FoldCase != re2.Flags&FoldCase {
    		return false
    	}
    
    	// Push re1 into re2.
    	re2.Rune = append(re2.Rune, re1.Rune...)
    
    	// Reuse re1 if possible.
    	if r >= 0 {
    		re1.Rune = re1.Rune0[:1]
    		re1.Rune[0] = r
    		re1.Flags = flags
    		return true
    	}
    
    	p.stack = p.stack[:n-1]
    	p.reuse(re1)
    	return false // did not push r
    }
    
    
    // literal pushes a literal regexp for the rune r on the stack.
    func (p *parser) literal(r rune) {
    
    	re := p.newRegexp(OpLiteral)
    
    	re.Flags = p.flags
    	if p.flags&FoldCase != 0 {
    
    	re.Rune0[0] = r
    	re.Rune = re.Rune0[:1]
    
    // minFoldRune returns the minimum rune fold-equivalent to r.
    
    Russ Cox's avatar
    Russ Cox committed
    func minFoldRune(r rune) rune {
    
    	if r < minFold || r > maxFold {
    		return r
    	}
    
    	r0 := r
    	for r = unicode.SimpleFold(r); r != r0; r = unicode.SimpleFold(r) {
    
    		m = min(m, r)
    
    	return m
    
    // op pushes a regexp with the given op onto the stack
    // and returns that regexp.
    func (p *parser) op(op Op) *Regexp {
    
    	re := p.newRegexp(op)
    	re.Flags = p.flags
    	return p.push(re)
    
    // repeat replaces the top stack element with itself repeated according to op, min, max.
    // before is the regexp suffix starting at the repetition operator.
    // after is the regexp suffix following after the repetition operator.
    // repeat returns an updated 'after' and an error, if any.
    
    func (p *parser) repeat(op Op, min, max int, before, after, lastRepeat string) (string, error) {
    
    Russ Cox's avatar
    Russ Cox committed
    	flags := p.flags
    	if p.flags&PerlX != 0 {
    
    		if len(after) > 0 && after[0] == '?' {
    			after = after[1:]
    
    Russ Cox's avatar
    Russ Cox committed
    			flags ^= NonGreedy
    		}
    		if lastRepeat != "" {
    			// In Perl it is not allowed to stack repetition operators:
    			// a** is a syntax error, not a doubled star, and a++ means
    			// something else entirely, which we don't support!
    
    			return "", &Error{ErrInvalidRepeatOp, lastRepeat[:len(lastRepeat)-len(after)]}
    
    	n := len(p.stack)
    	if n == 0 {
    
    		return "", &Error{ErrMissingRepeatArgument, before[:len(before)-len(after)]}
    
    	}
    	sub := p.stack[n-1]
    
    	if sub.Op >= opPseudo {
    
    		return "", &Error{ErrMissingRepeatArgument, before[:len(before)-len(after)]}
    
    	re := p.newRegexp(op)
    	re.Min = min
    	re.Max = max
    	re.Flags = flags
    
    	re.Sub = re.Sub0[:1]
    	re.Sub[0] = sub
    	p.stack[n-1] = re
    
    	p.checkLimits(re)
    
    
    	if op == OpRepeat && (min >= 2 || max >= 2) && !repeatIsValid(re, 1000) {
    		return "", &Error{ErrInvalidRepeatSize, before[:len(before)-len(after)]}
    	}
    
    
    	return after, nil
    
    // repeatIsValid reports whether the repetition re is valid.
    // Valid means that the combination of the top-level repetition
    // and any inner repetitions does not exceed n copies of the
    // innermost thing.
    // This function rewalks the regexp tree and is called for every repetition,
    // so we have to worry about inducing quadratic behavior in the parser.
    // We avoid this by only calling repeatIsValid when min or max >= 2.
    // In that case the depth of any >= 2 nesting can only get to 9 without
    // triggering a parse error, so each subtree can only be rewalked 9 times.
    func repeatIsValid(re *Regexp, n int) bool {
    	if re.Op == OpRepeat {
    		m := re.Max
    
    	}
    	for _, sub := range re.Sub {
    		if !repeatIsValid(sub, n) {
    			return false
    		}
    	}
    	return true
    }
    
    
    // concat replaces the top of the stack (above the topmost '|' or '(') with its concatenation.
    func (p *parser) concat() *Regexp {
    
    	p.maybeConcat(-1, 0)
    
    
    	// Scan down to find pseudo-operator | or (.
    	i := len(p.stack)
    	for i > 0 && p.stack[i-1].Op < opPseudo {
    		i--
    	}
    
    	subs := p.stack[i:]
    
    	p.stack = p.stack[:i]
    
    
    	// Empty concatenation is special case.
    	if len(subs) == 0 {
    		return p.push(p.newRegexp(OpEmptyMatch))
    
    	return p.push(p.collapse(subs, OpConcat))
    
    }
    
    // alternate replaces the top of the stack (above the topmost '(') with its alternation.
    func (p *parser) alternate() *Regexp {
    	// Scan down to find pseudo-operator (.
    	// There are no | above (.
    	i := len(p.stack)
    	for i > 0 && p.stack[i-1].Op < opPseudo {
    		i--
    	}
    
    	subs := p.stack[i:]
    
    	p.stack = p.stack[:i]
    
    
    	// Make sure top class is clean.
    	// All the others already are (see swapVerticalBar).
    	if len(subs) > 0 {
    		cleanAlt(subs[len(subs)-1])
    	}
    
    	// Empty alternate is special case
    	// (shouldn't happen but easy to handle).
    	if len(subs) == 0 {
    		return p.push(p.newRegexp(OpNoMatch))
    	}
    
    
    	return p.push(p.collapse(subs, OpAlternate))
    
    }
    
    // cleanAlt cleans re for eventual inclusion in an alternation.
    func cleanAlt(re *Regexp) {
    	switch re.Op {
    	case OpCharClass:
    		re.Rune = cleanClass(&re.Rune)
    		if len(re.Rune) == 2 && re.Rune[0] == 0 && re.Rune[1] == unicode.MaxRune {
    			re.Rune = nil
    			re.Op = OpAnyChar
    			return
    		}
    		if len(re.Rune) == 4 && re.Rune[0] == 0 && re.Rune[1] == '\n'-1 && re.Rune[2] == '\n'+1 && re.Rune[3] == unicode.MaxRune {
    			re.Rune = nil
    			re.Op = OpAnyCharNotNL
    			return
    		}
    		if cap(re.Rune)-len(re.Rune) > 100 {
    			// re.Rune will not grow any more.
    			// Make a copy or inline to reclaim storage.
    			re.Rune = append(re.Rune0[:0], re.Rune...)
    		}
    	}
    }
    
    
    // collapse returns the result of applying op to sub.
    // If sub contains op nodes, they all get hoisted up
    // so that there is never a concat of a concat or an
    // alternate of an alternate.
    
    func (p *parser) collapse(subs []*Regexp, op Op) *Regexp {
    	if len(subs) == 1 {
    
    		return subs[0]
    
    	}
    	re := p.newRegexp(op)
    	re.Sub = re.Sub0[:0]
    	for _, sub := range subs {
    		if sub.Op == op {
    			re.Sub = append(re.Sub, sub.Sub...)
    			p.reuse(sub)
    		} else {
    			re.Sub = append(re.Sub, sub)
    		}
    
    	if op == OpAlternate {
    
    		re.Sub = p.factor(re.Sub)
    
    		if len(re.Sub) == 1 {
    			old := re
    			re = re.Sub[0]
    			p.reuse(old)
    		}
    	}
    	return re
    }
    
    // factor factors common prefixes from the alternation list sub.
    // It returns a replacement list that reuses the same storage and
    // frees (passes to p.reuse) any removed *Regexps.
    //
    // For example,
    
    Russ Cox's avatar
    Russ Cox committed
    //
    //	ABC|ABD|AEF|BCX|BCY
    //
    
    // simplifies by literal prefix extraction to
    
    Russ Cox's avatar
    Russ Cox committed
    //
    //	A(B(C|D)|EF)|BC(X|Y)
    //
    
    // which simplifies by character class introduction to
    
    Russ Cox's avatar
    Russ Cox committed
    //
    //	A(B[CD]|EF)|BC[XY]
    
    func (p *parser) factor(sub []*Regexp) []*Regexp {
    
    	if len(sub) < 2 {
    		return sub
    	}
    
    	// Round 1: Factor out common literal prefixes.
    
    Russ Cox's avatar
    Russ Cox committed
    	var str []rune
    
    	var strflags Flags
    	start := 0
    	out := sub[:0]
    	for i := 0; i <= len(sub); i++ {
    		// Invariant: the Regexps that were in sub[0:start] have been
    		// used or marked for reuse, and the slice space has been reused
    		// for out (len(out) <= start).
    		//
    		// Invariant: sub[start:i] consists of regexps that all begin
    		// with str as modified by strflags.
    
    Russ Cox's avatar
    Russ Cox committed
    		var istr []rune
    
    		var iflags Flags
    		if i < len(sub) {
    			istr, iflags = p.leadingString(sub[i])
    			if iflags == strflags {
    				same := 0
    				for same < len(str) && same < len(istr) && str[same] == istr[same] {
    					same++
    				}
    				if same > 0 {
    					// Matches at least one rune in current range.
    					// Keep going around.
    					str = str[:same]
    					continue
    				}
    			}
    		}
    
    		// Found end of a run with common leading literal string:
    
    		// sub[start:i] all begin with str[:len(str)], but sub[i]
    
    		// does not even begin with str[0].
    		//
    		// Factor out common string and append factored expression to out.
    		if i == start {
    			// Nothing to do - run of length 0.
    		} else if i == start+1 {
    			// Just one: don't bother factoring.
    			out = append(out, sub[start])
    		} else {
    			// Construct factored form: prefix(suffix1|suffix2|...)
    			prefix := p.newRegexp(OpLiteral)
    			prefix.Flags = strflags
    			prefix.Rune = append(prefix.Rune[:0], str...)
    
    			for j := start; j < i; j++ {
    				sub[j] = p.removeLeadingString(sub[j], len(str))
    
    				p.checkLimits(sub[j])
    
    			}
    			suffix := p.collapse(sub[start:i], OpAlternate) // recurse
    
    			re := p.newRegexp(OpConcat)
    			re.Sub = append(re.Sub[:0], prefix, suffix)
    			out = append(out, re)
    		}
    
    		// Prepare for next iteration.
    		start = i
    		str = istr
    		strflags = iflags
    	}
    	sub = out
    
    
    	// Round 2: Factor out common simple prefixes,
    	// just the first piece of each concatenation.
    	// This will be good enough a lot of the time.
    	//
    	// Complex subexpressions (e.g. involving quantifiers)
    	// are not safe to factor because that collapses their
    	// distinct paths through the automaton, which affects
    	// correctness in some cases.
    
    	start = 0
    	out = sub[:0]
    	var first *Regexp
    	for i := 0; i <= len(sub); i++ {
    		// Invariant: the Regexps that were in sub[0:start] have been
    		// used or marked for reuse, and the slice space has been reused
    		// for out (len(out) <= start).
    		//
    
    		// Invariant: sub[start:i] consists of regexps that all begin with ifirst.
    
    		var ifirst *Regexp
    		if i < len(sub) {
    			ifirst = p.leadingRegexp(sub[i])
    
    			if first != nil && first.Equal(ifirst) &&
    				// first must be a character class OR a fixed repeat of a character class.
    				(isCharClass(first) || (first.Op == OpRepeat && first.Min == first.Max && isCharClass(first.Sub[0]))) {
    
    				continue
    			}
    		}
    
    		// Found end of a run with common leading regexp:
    		// sub[start:i] all begin with first but sub[i] does not.
    		//
    		// Factor out common regexp and append factored expression to out.
    		if i == start {
    			// Nothing to do - run of length 0.
    		} else if i == start+1 {
    			// Just one: don't bother factoring.
    			out = append(out, sub[start])
    		} else {
    			// Construct factored form: prefix(suffix1|suffix2|...)
    			prefix := first
    			for j := start; j < i; j++ {
    
    				reuse := j != start // prefix came from sub[start]
    
    				sub[j] = p.removeLeadingRegexp(sub[j], reuse)
    
    				p.checkLimits(sub[j])
    
    			}
    			suffix := p.collapse(sub[start:i], OpAlternate) // recurse
    
    			re := p.newRegexp(OpConcat)
    			re.Sub = append(re.Sub[:0], prefix, suffix)
    			out = append(out, re)
    		}
    
    		// Prepare for next iteration.
    		start = i
    		first = ifirst
    	}
    	sub = out
    
    	// Round 3: Collapse runs of single literals into character classes.
    	start = 0
    	out = sub[:0]
    	for i := 0; i <= len(sub); i++ {
    		// Invariant: the Regexps that were in sub[0:start] have been
    		// used or marked for reuse, and the slice space has been reused
    		// for out (len(out) <= start).
    		//
    		// Invariant: sub[start:i] consists of regexps that are either
    		// literal runes or character classes.
    		if i < len(sub) && isCharClass(sub[i]) {
    			continue
    		}
    
    		// sub[i] is not a char or char class;
    		// emit char class for sub[start:i]...
    		if i == start {
    			// Nothing to do - run of length 0.
    		} else if i == start+1 {
    			out = append(out, sub[start])
    		} else {
    			// Make new char class.
    			// Start with most complex regexp in sub[start].
    			max := start
    			for j := start + 1; j < i; j++ {
    				if sub[max].Op < sub[j].Op || sub[max].Op == sub[j].Op && len(sub[max].Rune) < len(sub[j].Rune) {
    					max = j
    				}
    			}
    			sub[start], sub[max] = sub[max], sub[start]
    
    			for j := start + 1; j < i; j++ {
    				mergeCharClass(sub[start], sub[j])
    				p.reuse(sub[j])
    			}
    			cleanAlt(sub[start])
    			out = append(out, sub[start])
    		}
    
    		// ... and then emit sub[i].
    		if i < len(sub) {
    			out = append(out, sub[i])
    		}
    		start = i + 1
    	}
    	sub = out
    
    	// Round 4: Collapse runs of empty matches into a single empty match.
    	start = 0
    	out = sub[:0]
    	for i := range sub {
    		if i+1 < len(sub) && sub[i].Op == OpEmptyMatch && sub[i+1].Op == OpEmptyMatch {
    			continue
    		}
    		out = append(out, sub[i])
    	}
    	sub = out
    
    	return sub
    }
    
    // leadingString returns the leading literal string that re begins with.
    // The string refers to storage in re or its children.
    
    Russ Cox's avatar
    Russ Cox committed
    func (p *parser) leadingString(re *Regexp) ([]rune, Flags) {
    
    	if re.Op == OpConcat && len(re.Sub) > 0 {
    		re = re.Sub[0]
    	}
    	if re.Op != OpLiteral {
    		return nil, 0
    	}
    	return re.Rune, re.Flags & FoldCase
    }
    
    // removeLeadingString removes the first n leading runes
    
    // from the beginning of re. It returns the replacement for re.
    
    func (p *parser) removeLeadingString(re *Regexp, n int) *Regexp {
    	if re.Op == OpConcat && len(re.Sub) > 0 {
    		// Removing a leading string in a concatenation
    		// might simplify the concatenation.
    		sub := re.Sub[0]
    		sub = p.removeLeadingString(sub, n)
    		re.Sub[0] = sub
    		if sub.Op == OpEmptyMatch {
    			p.reuse(sub)
    			switch len(re.Sub) {
    			case 0, 1:
    				// Impossible but handle.
    				re.Op = OpEmptyMatch
    				re.Sub = nil
    			case 2:
    				old := re
    				re = re.Sub[1]
    				p.reuse(old)
    			default:
    				copy(re.Sub, re.Sub[1:])
    				re.Sub = re.Sub[:len(re.Sub)-1]
    			}
    		}
    		return re
    	}
    
    	if re.Op == OpLiteral {
    		re.Rune = re.Rune[:copy(re.Rune, re.Rune[n:])]
    		if len(re.Rune) == 0 {
    			re.Op = OpEmptyMatch
    		}
    	}
    	return re
    }
    
    // leadingRegexp returns the leading regexp that re begins with.
    // The regexp refers to storage in re or its children.
    func (p *parser) leadingRegexp(re *Regexp) *Regexp {
    	if re.Op == OpEmptyMatch {
    		return nil
    	}
    	if re.Op == OpConcat && len(re.Sub) > 0 {
    		sub := re.Sub[0]
    		if sub.Op == OpEmptyMatch {
    			return nil
    		}
    		return sub
    	}
    	return re
    }
    
    // removeLeadingRegexp removes the leading regexp in re.
    // It returns the replacement for re.
    // If reuse is true, it passes the removed regexp (if no longer needed) to p.reuse.
    func (p *parser) removeLeadingRegexp(re *Regexp, reuse bool) *Regexp {
    	if re.Op == OpConcat && len(re.Sub) > 0 {
    		if reuse {
    			p.reuse(re.Sub[0])
    		}
    		re.Sub = re.Sub[:copy(re.Sub, re.Sub[1:])]
    		switch len(re.Sub) {
    		case 0:
    			re.Op = OpEmptyMatch
    			re.Sub = nil
    		case 1:
    			old := re
    			re = re.Sub[0]
    			p.reuse(old)
    		}
    		return re
    	}
    
    	if reuse {
    		p.reuse(re)
    	}
    	return p.newRegexp(OpEmptyMatch)
    
    func literalRegexp(s string, flags Flags) *Regexp {
    
    	re := &Regexp{Op: OpLiteral}
    	re.Flags = flags
    
    	re.Rune = re.Rune0[:0] // use local storage for small strings
    	for _, c := range s {
    		if len(re.Rune) >= cap(re.Rune) {
    			// string is too long to fit in Rune0.  let Go handle it
    
    Russ Cox's avatar
    Russ Cox committed
    			re.Rune = []rune(s)
    
    			break
    		}
    		re.Rune = append(re.Rune, c)
    	}
    	return re
    }
    
    
    // Parse parses a regular expression string s, controlled by the specified
    // Flags, and returns a regular expression parse tree. The syntax is
    
    // described in the top-level comment.
    
    func Parse(s string, flags Flags) (*Regexp, error) {
    
    	return parse(s, flags)
    }
    
    func parse(s string, flags Flags) (_ *Regexp, err error) {
    	defer func() {
    		switch r := recover(); r {
    		default:
    			panic(r)
    		case nil:
    			// ok
    
    cuiweixie's avatar
    cuiweixie committed
    		case ErrLarge: // too big
    			err = &Error{Code: ErrLarge, Expr: s}
    
    		case ErrNestingDepth:
    			err = &Error{Code: ErrNestingDepth, Expr: s}
    
    	if flags&Literal != 0 {
    		// Trivial parser for literal string.
    		if err := checkUTF8(s); err != nil {
    			return nil, err
    		}
    
    		return literalRegexp(s, flags), nil
    
    	}
    
    	// Otherwise, must do real work.
    	var (
    
    Russ Cox's avatar
    Russ Cox committed
    		c          rune
    
    		op         Op
    		lastRepeat string
    
    	)
    	p.flags = flags
    	p.wholeRegexp = s
    	t := s
    	for t != "" {
    
    		repeat := ""
    	BigSwitch:
    
    		switch t[0] {
    		default:
    			if c, t, err = nextRune(t); err != nil {
    				return nil, err
    			}
    			p.literal(c)
    
    		case '(':
    
    			if p.flags&PerlX != 0 && len(t) >= 2 && t[1] == '?' {
    				// Flag changes and non-capturing groups.
    				if t, err = p.parsePerlFlags(t); err != nil {
    					return nil, err
    				}
    
    				break
    			}
    			p.numCap++
    			p.op(opLeftParen).Cap = p.numCap
    			t = t[1:]
    		case '|':
    
    			t = t[1:]
    		case ')':
    			if err = p.parseRightParen(); err != nil {
    				return nil, err
    			}
    			t = t[1:]
    		case '^':
    			if p.flags&OneLine != 0 {
    				p.op(OpBeginText)
    			} else {
    				p.op(OpBeginLine)
    			}
    			t = t[1:]
    		case '$':
    			if p.flags&OneLine != 0 {
    				p.op(OpEndText).Flags |= WasDollar
    			} else {
    				p.op(OpEndLine)
    			}
    			t = t[1:]
    		case '.':
    			if p.flags&DotNL != 0 {
    				p.op(OpAnyChar)
    			} else {
    				p.op(OpAnyCharNotNL)
    			}
    			t = t[1:]
    		case '[':
    			if t, err = p.parseClass(t); err != nil {
    				return nil, err
    			}
    		case '*', '+', '?':
    
    			switch t[0] {
    			case '*':
    				op = OpStar
    			case '+':
    				op = OpPlus
    			case '?':
    				op = OpQuest
    			}
    
    			if after, err = p.repeat(op, 0, 0, before, after, lastRepeat); err != nil {
    
    Russ Cox's avatar
    Russ Cox committed
    				return nil, err
    			}
    
    			repeat = before
    			t = after
    
    			before := t
    			min, max, after, ok := p.parseRepeat(t)
    
    			if !ok {
    				// If the repeat cannot be parsed, { is a literal.
    				p.literal('{')
    				t = t[1:]