]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: reject bare ?
authorBen Lynn <benlynn@gmail.com>
Wed, 19 Jan 2011 18:47:04 +0000 (13:47 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 19 Jan 2011 18:47:04 +0000 (13:47 -0500)
Minor cleanup:
  - removed a duplicate test case
  - added a function to remove repeated code
  - for consistency, replaced "return nil" with a panic at an
    unreachable point

Fixes #1428.

R=golang-dev, r, rsc
CC=golang-dev
https://golang.org/cl/4057042

src/pkg/regexp/all_test.go
src/pkg/regexp/regexp.go

index 3b2c489bcef5f105f5d9d8b002c5b5649985683c..aed73306454ab9a03b7e8099b44d0ae733080a6b 100644 (file)
@@ -38,6 +38,8 @@ type stringError struct {
 
 var bad_re = []stringError{
        {`*`, ErrBareClosure},
+       {`+`, ErrBareClosure},
+       {`?`, ErrBareClosure},
        {`(abc`, ErrUnmatchedLpar},
        {`abc)`, ErrUnmatchedRpar},
        {`x[a-z`, ErrUnmatchedLbkt},
@@ -47,7 +49,6 @@ var bad_re = []stringError{
        {`a**`, ErrBadClosure},
        {`a*+`, ErrBadClosure},
        {`a??`, ErrBadClosure},
-       {`*`, ErrBareClosure},
        {`\x`, ErrBadBackslash},
 }
 
index 2e03b798ab620ee2632391dfa5a5572c0204cf27..d274ccdf5a578089e081139c9ff7e4a376ccfdd8 100644 (file)
@@ -283,6 +283,24 @@ func escape(c int) int {
        return -1
 }
 
+func (p *parser) checkBackslash() int {
+       c := p.c()
+       if c == '\\' {
+               c = p.nextc()
+               switch {
+               case c == endOfFile:
+                       p.error(ErrExtraneousBackslash)
+               case ispunct(c):
+                       // c is as delivered
+               case escape(c) >= 0:
+                       c = int(escaped[escape(c)])
+               default:
+                       p.error(ErrBadBackslash)
+               }
+       }
+       return c
+}
+
 func (p *parser) charClass() *instr {
        i := newCharClass()
        cc := i.cclass
@@ -314,20 +332,8 @@ func (p *parser) charClass() *instr {
                        return i
                case '-': // do this before backslash processing
                        p.error(ErrBadRange)
-               case '\\':
-                       c = p.nextc()
-                       switch {
-                       case c == endOfFile:
-                               p.error(ErrExtraneousBackslash)
-                       case ispunct(c):
-                               // c is as delivered
-                       case escape(c) >= 0:
-                               c = int(escaped[escape(c)])
-                       default:
-                               p.error(ErrBadBackslash)
-                       }
-                       fallthrough
                default:
+                       c = p.checkBackslash()
                        p.nextc()
                        switch {
                        case left < 0: // first of pair
@@ -345,14 +351,14 @@ func (p *parser) charClass() *instr {
                        }
                }
        }
-       return nil
+       panic("unreachable")
 }
 
 func (p *parser) term() (start, end *instr) {
        switch c := p.c(); c {
        case '|', endOfFile:
                return nil, nil
-       case '*', '+':
+       case '*', '+', '?':
                p.error(ErrBareClosure)
        case ')':
                if p.nlpar == 0 {
@@ -407,20 +413,8 @@ func (p *parser) term() (start, end *instr) {
                }
                bra.next = start
                return bra, ebra
-       case '\\':
-               c = p.nextc()
-               switch {
-               case c == endOfFile:
-                       p.error(ErrExtraneousBackslash)
-               case ispunct(c):
-                       // c is as delivered
-               case escape(c) >= 0:
-                       c = int(escaped[escape(c)])
-               default:
-                       p.error(ErrBadBackslash)
-               }
-               fallthrough
        default:
+               c = p.checkBackslash()
                p.nextc()
                start = &instr{kind: iChar, char: c}
                p.re.add(start)