Skip to content
Snippets Groups Projects
parse.go 35.1 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 (
    	"os"
    	"sort"
    
    	"unicode"
    	"utf8"
    )
    
    // 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) String() 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"
    )
    
    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
    )
    
    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    []int // temporary char class work space
    
    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) {
    	re.Sub0[0] = p.free
    	p.free = re
    }
    
    
    // Parse stack manipulation.
    
    // push pushes the regexp re onto the parse stack and returns the regexp.
    func (p *parser) push(re *Regexp) *Regexp {
    	// TODO: compute simple
    
    
    	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)
    	return 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.
    func (p *parser) maybeConcat(r int, 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
    }
    
    // newLiteral returns a new OpLiteral Regexp with the given flags
    func (p *parser) newLiteral(r int, flags Flags) *Regexp {
    	re := p.newRegexp(OpLiteral)
    	re.Flags = flags
    
    	re.Rune0[0] = r
    	re.Rune = re.Rune0[:1]
    	return re
    }
    
    // literal pushes a literal regexp for the rune r on the stack
    // and returns that regexp.
    
    func (p *parser) literal(r int) {
    	p.push(p.newLiteral(r, p.flags))
    
    }
    
    // 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.
    
    Russ Cox's avatar
    Russ Cox committed
    func (p *parser) repeat(op Op, min, max int, opstr, t, lastRepeat string) (string, os.Error) {
    	flags := p.flags
    	if p.flags&PerlX != 0 {
    		if len(t) > 0 && t[0] == '?' {
    			t = t[1:]
    			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(t)]}
    		}
    	}
    
    	n := len(p.stack)
    	if n == 0 {
    
    Russ Cox's avatar
    Russ Cox committed
    		return "", &Error{ErrMissingRepeatArgument, opstr}
    
    	}
    	sub := p.stack[n-1]
    
    	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
    
    Russ Cox's avatar
    Russ Cox committed
    	return t, nil
    
    }
    
    // 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.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.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 pushes the result of applying op to sub
    // onto the stack.  If sub contains op nodes, they all
    // get flattened into a single node.
    // sub points into p.stack so it cannot be kept.
    func (p *parser) collapse(subs []*Regexp, op Op) *Regexp {
    	if len(subs) == 1 {
    		return p.push(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)
    		}
    
    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
    			re.Rune = []int(s)
    			break
    		}
    		re.Rune = append(re.Rune, c)
    	}
    	return re
    }
    
    
    // Parsing.
    
    func Parse(s string, flags Flags) (*Regexp, os.Error) {
    	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 (
    
    		p          parser
    		err        os.Error
    		c          int
    		op         Op
    		lastRepeat string
    		min, max   int
    
    	)
    	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 '|':
    			if err = p.parseVerticalBar(); err != nil {
    				return nil, err
    			}
    			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
    			}
    
    Russ Cox's avatar
    Russ Cox committed
    			if t, err = p.repeat(op, min, max, t[:1], t[1:], lastRepeat); err != nil {
    				return nil, err
    			}
    
    Russ Cox's avatar
    Russ Cox committed
    			min, max, tt, ok := p.parseRepeat(t)
    
    			if !ok {
    				// If the repeat cannot be parsed, { is a literal.
    				p.literal('{')
    				t = t[1:]
    				break
    			}
    
    Russ Cox's avatar
    Russ Cox committed
    			if t, err = p.repeat(op, min, max, t[:len(t)-len(tt)], tt, lastRepeat); err != nil {
    
    			if p.flags&PerlX != 0 && len(t) >= 2 {
    				switch t[1] {
    				case 'A':
    					p.op(OpBeginText)
    					t = t[2:]
    					break BigSwitch
    				case 'b':
    					p.op(OpWordBoundary)
    					t = t[2:]
    					break BigSwitch
    				case 'B':
    					p.op(OpNoWordBoundary)
    					t = t[2:]
    					break BigSwitch
    				case 'C':
    					// any byte; not supported
    					return nil, &Error{ErrInvalidEscape, t[:2]}
    				case 'Q':
    					// \Q ... \E: the ... is always literals
    					var lit string
    					if i := strings.Index(t, `\E`); i < 0 {
    						lit = t[2:]
    						t = ""
    					} else {
    						lit = t[2:i]
    						t = t[i+2:]
    					}
    					p.push(literalRegexp(lit, p.flags))
    					break BigSwitch
    				case 'z':
    					p.op(OpEndText)
    					t = t[2:]
    					break BigSwitch
    				}
    			}
    
    
    			re := p.newRegexp(OpCharClass)
    			re.Flags = p.flags
    
    
    			// Look for Unicode character group like \p{Han}
    			if len(t) >= 2 && (t[1] == 'p' || t[1] == 'P') {
    				r, rest, err := p.parseUnicodeClass(t, re.Rune0[:0])
    				if err != nil {
    					return nil, err
    				}
    				if r != nil {
    					re.Rune = r
    					t = rest
    					p.push(re)
    					break BigSwitch
    				}
    			}
    
    			// Perl character class escape.
    			if r, rest := p.parsePerlClassEscape(t, re.Rune0[:0]); r != nil {
    				re.Rune = r
    				t = rest
    				p.push(re)
    				break BigSwitch
    			}
    
    
    			// Ordinary single-character escape.
    			if c, t, err = p.parseEscape(t); err != nil {
    				return nil, err
    			}
    			p.literal(c)
    
    		lastRepeat = repeat
    
    	}
    
    	p.concat()
    	if p.swapVerticalBar() {
    		// pop vertical bar
    		p.stack = p.stack[:len(p.stack)-1]
    	}
    	p.alternate()
    
    	n := len(p.stack)
    	if n != 1 {
    		return nil, &Error{ErrMissingParen, s}
    	}
    	return p.stack[0], nil
    }
    
    
    // parseRepeat parses {min} (max=min) or {min,} (max=-1) or {min,max}.
    // If s is not of that form, it returns ok == false.
    func (p *parser) parseRepeat(s string) (min, max int, rest string, ok bool) {
    	if s == "" || s[0] != '{' {
    		return
    	}
    	s = s[1:]
    	if min, s, ok = p.parseInt(s); !ok {
    		return
    	}
    	if s == "" {
    		return
    	}
    	if s[0] != ',' {
    		max = min
    	} else {
    		s = s[1:]
    		if s == "" {
    			return
    		}
    		if s[0] == '}' {
    			max = -1
    		} else if max, s, ok = p.parseInt(s); !ok {
    			return
    		}
    	}
    	if s == "" || s[0] != '}' {
    		return
    	}
    	rest = s[1:]
    	ok = true
    	return
    }
    
    // parsePerlFlags parses a Perl flag setting or non-capturing group or both,
    // like (?i) or (?: or (?i:.  It removes the prefix from s and updates the parse state.
    // The caller must have ensured that s begins with "(?".
    func (p *parser) parsePerlFlags(s string) (rest string, err os.Error) {
    	t := s
    
    	// Check for named captures, first introduced in Python's regexp library.
    	// As usual, there are three slightly different syntaxes:
    	//
    	//   (?P<name>expr)   the original, introduced by Python
    	//   (?<name>expr)    the .NET alteration, adopted by Perl 5.10
    	//   (?'name'expr)    another .NET alteration, adopted by Perl 5.10
    	//
    	// Perl 5.10 gave in and implemented the Python version too,
    	// but they claim that the last two are the preferred forms.
    	// PCRE and languages based on it (specifically, PHP and Ruby)
    	// support all three as well.  EcmaScript 4 uses only the Python form.
    	//
    	// In both the open source world (via Code Search) and the
    	// Google source tree, (?P<expr>name) is the dominant form,
    	// so that's the one we implement.  One is enough.
    	if len(t) > 4 && t[2] == 'P' && t[3] == '<' {
    		// Pull out name.
    		end := strings.IndexRune(t, '>')
    		if end < 0 {
    			if err = checkUTF8(t); err != nil {
    				return "", err
    			}
    			return "", &Error{ErrInvalidNamedCapture, s}
    		}
    
    		capture := t[:end+1] // "(?P<name>"
    		name := t[4:end]     // "name"
    		if err = checkUTF8(name); err != nil {
    			return "", err
    		}
    		if !isValidCaptureName(name) {
    			return "", &Error{ErrInvalidNamedCapture, capture}
    		}
    
    		// Like ordinary capture, but named.
    		p.numCap++
    		re := p.op(opLeftParen)
    		re.Cap = p.numCap
    		re.Name = name
    		return t[end+1:], nil
    	}
    
    	// Non-capturing group.  Might also twiddle Perl flags.
    	var c int
    	t = t[2:] // skip (?
    	flags := p.flags
    	sign := +1
    	sawFlag := false
    Loop:
    	for t != "" {
    		if c, t, err = nextRune(t); err != nil {
    			return "", err
    		}
    		switch c {
    		default:
    			break Loop
    
    		// Flags.
    		case 'i':
    			flags |= FoldCase
    			sawFlag = true
    		case 'm':
    			flags &^= OneLine
    			sawFlag = true
    		case 's':
    			flags |= DotNL
    			sawFlag = true
    		case 'U':
    			flags |= NonGreedy
    			sawFlag = true
    
    		// Switch to negation.
    		case '-':
    			if sign < 0 {
    				break Loop
    			}
    			sign = -1
    			// Invert flags so that | above turn into &^ and vice versa.
    			// We'll invert flags again before using it below.
    			flags = ^flags
    			sawFlag = false
    
    		// End of flags, starting group or not.
    		case ':', ')':
    			if sign < 0 {
    				if !sawFlag {
    					break Loop
    				}
    				flags = ^flags
    			}
    			if c == ':' {
    				// Open new group
    				p.op(opLeftParen)
    			}
    			p.flags = flags
    			return t, nil
    		}
    	}
    
    	return "", &Error{ErrInvalidPerlOp, s[:len(s)-len(t)]}
    }
    
    // isValidCaptureName reports whether name
    // is a valid capture name: [A-Za-z0-9_]+.
    // PCRE limits names to 32 bytes.
    // Python rejects names starting with digits.
    // We don't enforce either of those.
    func isValidCaptureName(name string) bool {
    	if name == "" {
    		return false
    	}
    	for _, c := range name {
    		if c != '_' && !isalnum(c) {
    			return false
    		}
    	}
    	return true
    }
    
    // parseInt parses a decimal integer.
    func (p *parser) parseInt(s string) (n int, rest string, ok bool) {
    	if s == "" || s[0] < '0' || '9' < s[0] {
    		return
    	}
    	// Disallow leading zeros.
    	if len(s) >= 2 && s[0] == '0' && '0' <= s[1] && s[1] <= '9' {
    		return
    	}
    	for s != "" && '0' <= s[0] && s[0] <= '9' {
    		// Avoid overflow.
    		if n >= 1e8 {
    			return
    		}
    		n = n*10 + int(s[0]) - '0'
    		s = s[1:]
    	}
    	rest = s
    	ok = true
    	return
    }
    
    
    // can this be represented as a character class?
    // single-rune literal string, char class, ., and .|\n.
    func isCharClass(re *Regexp) bool {
    	return re.Op == OpLiteral && len(re.Rune) == 1 ||
    		re.Op == OpCharClass ||
    		re.Op == OpAnyCharNotNL ||
    		re.Op == OpAnyChar
    }
    
    // does re match r?
    func matchRune(re *Regexp, r int) bool {
    	switch re.Op {
    	case OpLiteral:
    		return len(re.Rune) == 1 && re.Rune[0] == r
    	case OpCharClass:
    		for i := 0; i < len(re.Rune); i += 2 {
    			if re.Rune[i] <= r && r <= re.Rune[i+1] {
    				return true
    			}
    		}
    		return false
    	case OpAnyCharNotNL:
    		return r != '\n'
    	case OpAnyChar:
    		return true
    	}
    	return false
    }
    
    
    // parseVerticalBar handles a | in the input.
    func (p *parser) parseVerticalBar() os.Error {
    	p.concat()
    
    	// The concatenation we just parsed is on top of the stack.
    	// If it sits above an opVerticalBar, swap it below
    	// (things below an opVerticalBar become an alternation).
    	// Otherwise, push a new vertical bar.
    	if !p.swapVerticalBar() {
    		p.op(opVerticalBar)
    	}
    
    	return nil
    }
    
    // If the top of the stack is an element followed by an opVerticalBar
    // swapVerticalBar swaps the two and returns true.
    // Otherwise it returns false.
    func (p *parser) swapVerticalBar() bool {
    
    	// If above and below vertical bar are literal or char class,
    	// can merge into a single char class.
    	n := len(p.stack)
    	if n >= 3 && p.stack[n-2].Op == opVerticalBar && isCharClass(p.stack[n-1]) && isCharClass(p.stack[n-3]) {
    		re1 := p.stack[n-1]
    		re3 := p.stack[n-3]
    		// Make re3 the more complex of the two.
    		if re1.Op > re3.Op {
    			re1, re3 = re3, re1
    			p.stack[n-3] = re3
    		}
    		switch re3.Op {
    		case OpAnyChar:
    			// re1 doesn't add anything.
    		case OpAnyCharNotNL:
    			// re1 might add \n
    			if matchRune(re1, '\n') {
    				re3.Op = OpAnyChar
    			}
    		case OpCharClass:
    			// re1 is simpler, so either literal or char class
    			if re1.Op == OpLiteral {
    				re3.Rune = appendRange(re3.Rune, re1.Rune[0], re1.Rune[0])
    			} else {
    				re3.Rune = appendClass(re3.Rune, re1.Rune)
    			}
    		case OpLiteral:
    			// both literal
    			if re1.Rune[0] == re3.Rune[0] {
    				break
    			}
    			re3.Op = OpCharClass
    			re3.Rune = append(re3.Rune, re3.Rune[0])
    			re3.Rune = appendRange(re3.Rune, re1.Rune[0], re1.Rune[0])
    		}
    		p.reuse(re1)
    		p.stack = p.stack[:n-1]
    		return true
    	}
    
    	if n >= 2 {
    
    		re1 := p.stack[n-1]
    		re2 := p.stack[n-2]
    		if re2.Op == opVerticalBar {
    
    			if n >= 3 {
    				// Now out of reach.
    				// Clean opportunistically.
    				cleanAlt(p.stack[n-3])
    			}
    
    			p.stack[n-2] = re1
    			p.stack[n-1] = re2
    			return true
    		}
    	}
    	return false
    }
    
    // parseRightParen handles a ) in the input.
    func (p *parser) parseRightParen() os.Error {
    	p.concat()
    	if p.swapVerticalBar() {
    		// pop vertical bar
    		p.stack = p.stack[:len(p.stack)-1]
    	}
    	p.alternate()
    
    	n := len(p.stack)
    	if n < 2 {
    		return &Error{ErrInternalError, ""}
    	}
    	re1 := p.stack[n-1]
    	re2 := p.stack[n-2]
    	p.stack = p.stack[:n-2]
    	if re2.Op != opLeftParen {
    		return &Error{ErrMissingParen, p.wholeRegexp}
    	}
    	if re2.Cap == 0 {
    		// Just for grouping.
    		p.push(re1)
    	} else {
    		re2.Op = OpCapture
    		re2.Sub = re2.Sub0[:1]
    		re2.Sub[0] = re1
    		p.push(re2)
    	}
    	return nil
    }
    
    
    // parseEscape parses an escape sequence at the beginning of s
    // and returns the rune.
    func (p *parser) parseEscape(s string) (r int, rest string, err os.Error) {
    	t := s[1:]
    	if t == "" {
    		return 0, "", &Error{ErrTrailingBackslash, ""}
    	}
    	c, t, err := nextRune(t)
    	if err != nil {
    		return 0, "", err
    	}
    
    Switch:
    	switch c {
    	default:
    		if c < utf8.RuneSelf && !isalnum(c) {
    			// Escaped non-word characters are always themselves.
    			// PCRE is not quite so rigorous: it accepts things like
    			// \q, but we don't.  We once rejected \_, but too many
    			// programs and people insist on using it, so allow \_.
    			return c, t, nil
    		}
    
    	// Octal escapes.
    	case '1', '2', '3', '4', '5', '6', '7':
    		// Single non-zero digit is a backreference; not supported
    		if t == "" || t[0] < '0' || t[0] > '7' {
    			break
    		}
    		fallthrough
    	case '0':
    		// Consume up to three octal digits; already have one.
    		r = c - '0'
    		for i := 1; i < 3; i++ {
    			if t == "" || t[0] < '0' || t[0] > '7' {
    				break
    			}
    			r = r*8 + int(t[0]) - '0'
    			t = t[1:]
    		}
    		return r, t, nil
    
    	// Hexadecimal escapes.
    	case 'x':
    		if t == "" {
    			break
    		}
    		if c, t, err = nextRune(t); err != nil {
    			return 0, "", err
    		}
    		if c == '{' {
    			// Any number of digits in braces.
    			// Perl accepts any text at all; it ignores all text
    			// after the first non-hex digit.  We require only hex digits,
    			// and at least one.
    			nhex := 0
    			r = 0
    			for {
    				if t == "" {
    					break Switch
    				}
    				if c, t, err = nextRune(t); err != nil {
    					return 0, "", err
    				}
    				if c == '}' {
    					break
    				}
    				v := unhex(c)
    				if v < 0 {
    					break Switch
    				}
    				r = r*16 + v
    				if r > unicode.MaxRune {
    					break Switch
    				}
    
    			}
    			if nhex == 0 {
    				break Switch
    			}
    			return r, t, nil
    		}
    
    		// Easy case: two hex digits.
    		x := unhex(c)
    		if c, t, err = nextRune(t); err != nil {
    			return 0, "", err
    		}
    		y := unhex(c)
    		if x < 0 || y < 0 {
    			break
    		}
    		return x*16 + y, t, nil
    
    	// C escapes.  There is no case 'b', to avoid misparsing
    	// the Perl word-boundary \b as the C backspace \b
    	// when in POSIX mode.  In Perl, /\b/ means word-boundary
    	// but /[\b]/ means backspace.  We don't support that.
    	// If you want a backspace, embed a literal backspace
    	// character or use \x08.
    	case 'a':
    		return '\a', t, err
    	case 'f':
    		return '\f', t, err
    	case 'n':
    		return '\n', t, err
    	case 'r':
    		return '\r', t, err
    	case 't':
    		return '\t', t, err
    	case 'v':
    		return '\v', t, err
    	}
    	return 0, "", &Error{ErrInvalidEscape, s[:len(s)-len(t)]}
    }
    
    
    // parseClassChar parses a character class character at the beginning of s
    // and returns it.
    func (p *parser) parseClassChar(s, wholeClass string) (r int, rest string, err os.Error) {
    	if s == "" {
    		return 0, "", &Error{Code: ErrMissingBracket, Expr: wholeClass}
    	}
    
    
    	// Allow regular escape sequences even though
    	// many need not be escaped in this context.
    	if s[0] == '\\' {
    		return p.parseEscape(s)
    	}
    
    type charGroup struct {
    	sign  int
    	class []int
    }
    
    // parsePerlClassEscape parses a leading Perl character class escape like \d
    // from the beginning of s.  If one is present, it appends the characters to r
    // and returns the new slice r and the remainder of the string.
    func (p *parser) parsePerlClassEscape(s string, r []int) (out []int, rest string) {
    	if p.flags&PerlX == 0 || len(s) < 2 || s[0] != '\\' {
    		return
    	}
    	g := perlGroup[s[0:2]]
    	if g.sign == 0 {
    		return
    	}
    
    	return p.appendGroup(r, g), s[2:]
    
    }
    
    // parseNamedClass parses a leading POSIX named character class like [:alnum:]
    // from the beginning of s.  If one is present, it appends the characters to r
    // and returns the new slice r and the remainder of the string.