Commit bd13f6ff authored by Jan Mercl's avatar Jan Mercl Committed by Rob Pike

regexp/syntax: replace internal error on unexpected ) w/ ErrUnexpectedParen

Unbalanced extra right parenthesis produced an internal error instead of
a more descriptive one.

Fixes #3406.

R=r, rsc
CC=golang-dev
https://golang.org/cl/6201063
parent 2a47e044
...@@ -5484,6 +5484,7 @@ pkg regexp/syntax, const ErrMissingBracket ErrorCode ...@@ -5484,6 +5484,7 @@ pkg regexp/syntax, const ErrMissingBracket ErrorCode
pkg regexp/syntax, const ErrMissingParen ErrorCode pkg regexp/syntax, const ErrMissingParen ErrorCode
pkg regexp/syntax, const ErrMissingRepeatArgument ErrorCode pkg regexp/syntax, const ErrMissingRepeatArgument ErrorCode
pkg regexp/syntax, const ErrTrailingBackslash ErrorCode pkg regexp/syntax, const ErrTrailingBackslash ErrorCode
pkg regexp/syntax, const ErrUnexpectedParen ErrorCode
pkg regexp/syntax, const FoldCase Flags pkg regexp/syntax, const FoldCase Flags
pkg regexp/syntax, const InstAlt InstOp pkg regexp/syntax, const InstAlt InstOp
pkg regexp/syntax, const InstAltMatch InstOp pkg regexp/syntax, const InstAltMatch InstOp
...@@ -46,6 +46,7 @@ const ( ...@@ -46,6 +46,7 @@ const (
ErrMissingParen ErrorCode = "missing closing )" ErrMissingParen ErrorCode = "missing closing )"
ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator" ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator"
ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression" ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression"
ErrUnexpectedParen ErrorCode = "unexpected )"
) )
func (e ErrorCode) String() string { func (e ErrorCode) String() string {
...@@ -1168,13 +1169,13 @@ func (p *parser) parseRightParen() error { ...@@ -1168,13 +1169,13 @@ func (p *parser) parseRightParen() error {
n := len(p.stack) n := len(p.stack)
if n < 2 { if n < 2 {
return &Error{ErrInternalError, ""} return &Error{ErrUnexpectedParen, p.wholeRegexp}
} }
re1 := p.stack[n-1] re1 := p.stack[n-1]
re2 := p.stack[n-2] re2 := p.stack[n-2]
p.stack = p.stack[:n-2] p.stack = p.stack[:n-2]
if re2.Op != opLeftParen { if re2.Op != opLeftParen {
return &Error{ErrMissingParen, p.wholeRegexp} return &Error{ErrUnexpectedParen, p.wholeRegexp}
} }
// Restore flags at time of paren. // Restore flags at time of paren.
p.flags = re2.Flags p.flags = re2.Flags
......
...@@ -441,10 +441,18 @@ var invalidRegexps = []string{ ...@@ -441,10 +441,18 @@ var invalidRegexps = []string{
`(`, `(`,
`)`, `)`,
`(a`, `(a`,
`a)`,
`(a))`,
`(a|b|`, `(a|b|`,
`a|b|)`,
`(a|b|))`,
`(a|b`, `(a|b`,
`a|b)`,
`(a|b))`,
`[a-z`, `[a-z`,
`([a-z)`, `([a-z)`,
`[a-z)`,
`([a-z]))`,
`x{1001}`, `x{1001}`,
`x{9876543210}`, `x{9876543210}`,
`x{2,1}`, `x{2,1}`,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment