MCPcopy Create free account
hub / github.com/andybalholm/cascadia / parseRegex

Method parseRegex

parser.go:222–252  ·  view source on GitHub ↗

parseRegex parses a regular expression; the end is defined by encountering an unmatched closing ')' or ']' which is not consumed

()

Source from the content-addressed store, hash-verified

220// parseRegex parses a regular expression; the end is defined by encountering an
221// unmatched closing ')' or ']' which is not consumed
222func (p *parser) parseRegex() (rx *regexp.Regexp, err error) {
223 i := p.i
224 if len(p.s) < i+2 {
225 return nil, errors.New("expected regular expression, found EOF instead")
226 }
227
228 // number of open parens or brackets;
229 // when it becomes negative, finished parsing regex
230 open := 0
231
232loop:
233 for i < len(p.s) {
234 switch p.s[i] {
235 case '(', '[':
236 open++
237 case ')', ']':
238 open--
239 if open < 0 {
240 break loop
241 }
242 }
243 i++
244 }
245
246 if i >= len(p.s) {
247 return nil, errors.New("EOF in regular expression")
248 }
249 rx, err = regexp.Compile(p.s[p.i:i])
250 p.i = i
251 return rx, err
252}
253
254// skipWhitespace consumes whitespace characters and comments.
255// It returns true if there was actually anything to skip.

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected