OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package syntax parses regular expressions into parse trees and compiles | 5 // Package syntax parses regular expressions into parse trees and compiles |
6 // parse trees into programs. Most clients of regular expressions will use | 6 // parse trees into programs. Most clients of regular expressions will use |
7 // the facilities of package regexp (such as Compile and Match) instead of | 7 // the facilities of package regexp (such as Compile and Match) instead of |
8 // this package. | 8 // this package. |
9 package syntax | 9 package syntax |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 ErrInvalidEscape ErrorCode = "invalid escape sequence" | 39 ErrInvalidEscape ErrorCode = "invalid escape sequence" |
40 ErrInvalidNamedCapture ErrorCode = "invalid named capture" | 40 ErrInvalidNamedCapture ErrorCode = "invalid named capture" |
41 ErrInvalidPerlOp ErrorCode = "invalid or unsupported Perl syntax
" | 41 ErrInvalidPerlOp ErrorCode = "invalid or unsupported Perl syntax
" |
42 ErrInvalidRepeatOp ErrorCode = "invalid nested repetition operator
" | 42 ErrInvalidRepeatOp ErrorCode = "invalid nested repetition operator
" |
43 ErrInvalidRepeatSize ErrorCode = "invalid repeat count" | 43 ErrInvalidRepeatSize ErrorCode = "invalid repeat count" |
44 ErrInvalidUTF8 ErrorCode = "invalid UTF-8" | 44 ErrInvalidUTF8 ErrorCode = "invalid UTF-8" |
45 ErrMissingBracket ErrorCode = "missing closing ]" | 45 ErrMissingBracket ErrorCode = "missing closing ]" |
46 ErrMissingParen ErrorCode = "missing closing )" | 46 ErrMissingParen ErrorCode = "missing closing )" |
47 ErrMissingRepeatArgument ErrorCode = "missing argument to repetition ope
rator" | 47 ErrMissingRepeatArgument ErrorCode = "missing argument to repetition ope
rator" |
48 ErrTrailingBackslash ErrorCode = "trailing backslash at end of expre
ssion" | 48 ErrTrailingBackslash ErrorCode = "trailing backslash at end of expre
ssion" |
| 49 ErrUnexpectedParen ErrorCode = "unexpected )" |
49 ) | 50 ) |
50 | 51 |
51 func (e ErrorCode) String() string { | 52 func (e ErrorCode) String() string { |
52 return string(e) | 53 return string(e) |
53 } | 54 } |
54 | 55 |
55 // Flags control the behavior of the parser and record information about regexp
context. | 56 // Flags control the behavior of the parser and record information about regexp
context. |
56 type Flags uint16 | 57 type Flags uint16 |
57 | 58 |
58 const ( | 59 const ( |
(...skipping 1102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 func (p *parser) parseRightParen() error { | 1162 func (p *parser) parseRightParen() error { |
1162 p.concat() | 1163 p.concat() |
1163 if p.swapVerticalBar() { | 1164 if p.swapVerticalBar() { |
1164 // pop vertical bar | 1165 // pop vertical bar |
1165 p.stack = p.stack[:len(p.stack)-1] | 1166 p.stack = p.stack[:len(p.stack)-1] |
1166 } | 1167 } |
1167 p.alternate() | 1168 p.alternate() |
1168 | 1169 |
1169 n := len(p.stack) | 1170 n := len(p.stack) |
1170 if n < 2 { | 1171 if n < 2 { |
1171 » » return &Error{ErrInternalError, ""} | 1172 » » return &Error{ErrUnexpectedParen, p.wholeRegexp} |
1172 } | 1173 } |
1173 re1 := p.stack[n-1] | 1174 re1 := p.stack[n-1] |
1174 re2 := p.stack[n-2] | 1175 re2 := p.stack[n-2] |
1175 p.stack = p.stack[:n-2] | 1176 p.stack = p.stack[:n-2] |
1176 if re2.Op != opLeftParen { | 1177 if re2.Op != opLeftParen { |
1177 » » return &Error{ErrMissingParen, p.wholeRegexp} | 1178 » » return &Error{ErrUnexpectedParen, p.wholeRegexp} |
1178 } | 1179 } |
1179 // Restore flags at time of paren. | 1180 // Restore flags at time of paren. |
1180 p.flags = re2.Flags | 1181 p.flags = re2.Flags |
1181 if re2.Cap == 0 { | 1182 if re2.Cap == 0 { |
1182 // Just for grouping. | 1183 // Just for grouping. |
1183 p.push(re1) | 1184 p.push(re1) |
1184 } else { | 1185 } else { |
1185 re2.Op = OpCapture | 1186 re2.Op = OpCapture |
1186 re2.Sub = re2.Sub0[:1] | 1187 re2.Sub = re2.Sub0[:1] |
1187 re2.Sub[0] = re1 | 1188 re2.Sub[0] = re1 |
(...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1858 return c - '0' | 1859 return c - '0' |
1859 } | 1860 } |
1860 if 'a' <= c && c <= 'f' { | 1861 if 'a' <= c && c <= 'f' { |
1861 return c - 'a' + 10 | 1862 return c - 'a' + 10 |
1862 } | 1863 } |
1863 if 'A' <= c && c <= 'F' { | 1864 if 'A' <= c && c <= 'F' { |
1864 return c - 'A' + 10 | 1865 return c - 'A' + 10 |
1865 } | 1866 } |
1866 return -1 | 1867 return -1 |
1867 } | 1868 } |
OLD | NEW |