Parser 2.0 (Major Delezer) #52
|
|
@ -390,7 +390,28 @@ export class Scanner {
|
|||
if (this.char === c`/` && this.peek() === c`/`) {
|
||||
this.next() // skip /
|
||||
this.next() // skip /
|
||||
this.push(TokenType.Regex)
|
||||
|
||||
// read regex flags
|
||||
while (this.char > 0 && isIdentStart(this.char))
|
||||
this.next()
|
||||
|
||||
// validate regex
|
||||
const to = this.pos - getCharSize(this.char)
|
||||
const regexText = this.input.slice(this.start, to)
|
||||
const [_, pattern, flags] = regexText.match(/^\/\/(.*)\/\/([gimsuy]*)$/) || []
|
||||
|
||||
if (pattern) {
|
||||
try {
|
||||
new RegExp(pattern, flags)
|
||||
this.push(TokenType.Regex)
|
||||
break
|
||||
} catch (e) {
|
||||
// invalid regex - fall through to Word
|
||||
}
|
||||
}
|
||||
|
||||
// invalid regex is treated as Word
|
||||
this.push(TokenType.Word)
|
||||
break
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user