1
0
mirror of https://github.com/golang/go synced 2024-11-25 04:57:56 -07:00

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
This commit is contained in:
Jan Mercl 2012-05-14 11:50:25 -07:00 committed by Rob Pike
parent 2a47e0444c
commit bd13f6ff8e
3 changed files with 12 additions and 2 deletions

View File

@ -5484,6 +5484,7 @@ pkg regexp/syntax, const ErrMissingBracket ErrorCode
pkg regexp/syntax, const ErrMissingParen ErrorCode
pkg regexp/syntax, const ErrMissingRepeatArgument ErrorCode
pkg regexp/syntax, const ErrTrailingBackslash ErrorCode
pkg regexp/syntax, const ErrUnexpectedParen ErrorCode
pkg regexp/syntax, const FoldCase Flags
pkg regexp/syntax, const InstAlt InstOp
pkg regexp/syntax, const InstAltMatch InstOp

View File

@ -46,6 +46,7 @@ const (
ErrMissingParen ErrorCode = "missing closing )"
ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator"
ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression"
ErrUnexpectedParen ErrorCode = "unexpected )"
)
func (e ErrorCode) String() string {
@ -1168,13 +1169,13 @@ func (p *parser) parseRightParen() error {
n := len(p.stack)
if n < 2 {
return &Error{ErrInternalError, ""}
return &Error{ErrUnexpectedParen, p.wholeRegexp}
}
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}
return &Error{ErrUnexpectedParen, p.wholeRegexp}
}
// Restore flags at time of paren.
p.flags = re2.Flags

View File

@ -441,10 +441,18 @@ var invalidRegexps = []string{
`(`,
`)`,
`(a`,
`a)`,
`(a))`,
`(a|b|`,
`a|b|)`,
`(a|b|))`,
`(a|b`,
`a|b)`,
`(a|b))`,
`[a-z`,
`([a-z)`,
`[a-z)`,
`([a-z]))`,
`x{1001}`,
`x{9876543210}`,
`x{2,1}`,