regex flags, bad regexs become Words
This commit is contained in:
parent
5010a9584c
commit
52e100cab3
|
|
@ -390,8 +390,29 @@ export class Scanner {
|
||||||
if (this.char === c`/` && this.peek() === c`/`) {
|
if (this.char === c`/` && this.peek() === c`/`) {
|
||||||
this.next() // skip /
|
this.next() // skip /
|
||||||
this.next() // skip /
|
this.next() // skip /
|
||||||
|
|
||||||
|
// 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)
|
this.push(TokenType.Regex)
|
||||||
break
|
break
|
||||||
|
} catch (e) {
|
||||||
|
// invalid regex - fall through to Word
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// invalid regex is treated as Word
|
||||||
|
this.push(TokenType.Word)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
this.next()
|
this.next()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user