Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Oct 5, 2018
1 parent 813344e commit 0dc0b65
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/validator.ts
Expand Up @@ -902,6 +902,9 @@ export class RegExpValidator {
if (cp === RightParenthesis) {
this.raise("Unmatched ')'")
}
if (cp === ReverseSolidus) {
this.raise("\\ at end of pattern")
}
if (cp === RightSquareBracket || cp === RightCurlyBracket) {
this.raise("Lone quantifier brackets")
}
Expand Down Expand Up @@ -1176,6 +1179,7 @@ export class RegExpValidator {
return (
this.eatDot() ||
this.eatReverseSolidusAtomEscape() ||
this.eatReverseSolidusFollowedByC() ||
this.eatCharacterClass() ||
this.eatUncapturingGroup() ||
this.eatCapturingGroup() ||
Expand All @@ -1184,6 +1188,20 @@ export class RegExpValidator {
)
}

// \ [lookahead = c]
private eatReverseSolidusFollowedByC(): boolean {
if (
this.currentCodePoint === ReverseSolidus &&
this.nextCodePoint === LatinSmallLetterC
) {
this._lastIntValue = this.currentCodePoint
this.advance()
this.onCharacter(this.index - 1, this.index, ReverseSolidus)
return true
}
return false
}

// https://www.ecma-international.org/ecma-262/8.0/#prod-strict-InvalidBracedQuantifier
private eatInvalidBracedQuantifier(): boolean {
if (this.eatBracedQuantifier(true)) {
Expand Down Expand Up @@ -1222,6 +1240,7 @@ export class RegExpValidator {
cp !== -1 &&
cp !== CircumflexAccent &&
cp !== DollarSign &&
cp !== ReverseSolidus &&
cp !== FullStop &&
cp !== Asterisk &&
cp !== PlusSign &&
Expand Down Expand Up @@ -1457,6 +1476,7 @@ export class RegExpValidator {
}

// https://www.ecma-international.org/ecma-262/8.0/#prod-RegExpUnicodeEscapeSequence
//eslint-disable-next-line complexity
private eatRegExpUnicodeEscapeSequence(): boolean {
const start = this.index

Expand Down
11 changes: 11 additions & 0 deletions test/parser.ts
Expand Up @@ -74,3 +74,14 @@ describe("parseRegExpLiteral function:", () => {
assert.deepStrictEqual(actual, expected)
})
})

describe("RegExpParser:", () => {
describe("parsePattern function", () => {
it("should throw syntax error on '\\'.", () => {
assert.throws(
() => new RegExpParser().parsePattern("\\"),
/\\ at end of pattern/,
)
})
})
})

0 comments on commit 0dc0b65

Please sign in to comment.