parseRegex parses a regular expression; the end is defined by encountering an unmatched closing ')' or ']' which is not consumed
()
| 195 | // parseRegex parses a regular expression; the end is defined by encountering an |
| 196 | // unmatched closing ')' or ']' which is not consumed |
| 197 | func (p *parser) parseRegex() (rx *regexp.Regexp, err error) { |
| 198 | i := p.i |
| 199 | if len(p.s) < i+2 { |
| 200 | return nil, errors.New("expected regular expression, found EOF instead") |
| 201 | } |
| 202 | |
| 203 | // number of open parens or brackets; |
| 204 | // when it becomes negative, finished parsing regex |
| 205 | open := 0 |
| 206 | |
| 207 | loop: |
| 208 | for i < len(p.s) { |
| 209 | switch p.s[i] { |
| 210 | case '(', '[': |
| 211 | open++ |
| 212 | case ')', ']': |
| 213 | open-- |
| 214 | if open < 0 { |
| 215 | break loop |
| 216 | } |
| 217 | } |
| 218 | i++ |
| 219 | } |
| 220 | |
| 221 | if i >= len(p.s) { |
| 222 | return nil, errors.New("EOF in regular expression") |
| 223 | } |
| 224 | rx, err = regexp.Compile(p.s[p.i:i]) |
| 225 | p.i = i |
| 226 | return rx, err |
| 227 | } |
| 228 | |
| 229 | // skipWhitespace consumes whitespace characters and comments. |
| 230 | // It returns true if there was actually anything to skip. |
no outgoing calls
no test coverage detected