From 8ccc8cb96fbbf70d185155ee0e46f2aef6b624b9 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sat, 11 Jun 2022 10:39:34 -0600 Subject: [PATCH] Ensure Unicode escapes can't be used in character classes yet --- lib/parser.js | 112 +++++++++++++++++++++++++++++++---------------- src/parser.pegjs | 13 ++++-- 2 files changed, 85 insertions(+), 40 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 62bdb216..d05730a9 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -2145,6 +2145,28 @@ function peg$parse(input, options) { peg$currPos = s0; s0 = peg$FAILED; } + if (s0 === peg$FAILED) { + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 92) { + s1 = peg$c29; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e35); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parseExtendedUnicodeEscapeSequence(); + if (s2 !== peg$FAILED) { + s0 = s2; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } } } } @@ -2395,7 +2417,7 @@ function peg$parse(input, options) { if (peg$silentFails === 0) { peg$fail(peg$e35); } } if (s1 !== peg$FAILED) { - s2 = peg$parseEscapeSequence(); + s2 = peg$parseExtendedEscapeSequence(); if (s2 !== peg$FAILED) { s0 = s2; } else { @@ -2475,7 +2497,7 @@ function peg$parse(input, options) { if (peg$silentFails === 0) { peg$fail(peg$e35); } } if (s1 !== peg$FAILED) { - s2 = peg$parseEscapeSequence(); + s2 = peg$parseExtendedEscapeSequence(); if (s2 !== peg$FAILED) { s0 = s2; } else { @@ -2708,6 +2730,17 @@ function peg$parse(input, options) { return s0; } + function peg$parseExtendedEscapeSequence() { + var s0; + + s0 = peg$parseEscapeSequence(); + if (s0 === peg$FAILED) { + s0 = peg$parseExtendedUnicodeEscapeSequence(); + } + + return s0; + } + function peg$parseEscapeSequence() { var s0, s1, s2, s3; @@ -3060,47 +3093,49 @@ function peg$parse(input, options) { peg$currPos = s0; s0 = peg$FAILED; } - if (s0 === peg$FAILED) { - s0 = peg$currPos; - if (input.substr(peg$currPos, 2) === peg$c48) { - s1 = peg$c48; - peg$currPos += 2; + + return s0; + } + + function peg$parseExtendedUnicodeEscapeSequence() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 2) === peg$c48) { + s1 = peg$c48; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e57); } + } + if (s1 !== peg$FAILED) { + s2 = peg$currPos; + s3 = []; + s4 = peg$parseHexDigit(); + if (s4 !== peg$FAILED) { + while (s4 !== peg$FAILED) { + s3.push(s4); + s4 = peg$parseHexDigit(); + } } else { - s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e57); } + s3 = peg$FAILED; } - if (s1 !== peg$FAILED) { - s2 = peg$currPos; - s3 = []; - s4 = peg$parseHexDigit(); - if (s4 !== peg$FAILED) { - while (s4 !== peg$FAILED) { - s3.push(s4); - s4 = peg$parseHexDigit(); - } + if (s3 !== peg$FAILED) { + s2 = input.substring(s2, peg$currPos); + } else { + s2 = s3; + } + if (s2 !== peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 125) { + s3 = peg$c1; + peg$currPos++; } else { s3 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e1); } } if (s3 !== peg$FAILED) { - s2 = input.substring(s2, peg$currPos); - } else { - s2 = s3; - } - if (s2 !== peg$FAILED) { - if (input.charCodeAt(peg$currPos) === 125) { - s3 = peg$c1; - peg$currPos++; - } else { - s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e1); } - } - if (s3 !== peg$FAILED) { - peg$savedPos = s0; - s0 = peg$f32(s2); - } else { - peg$currPos = s0; - s0 = peg$FAILED; - } + peg$savedPos = s0; + s0 = peg$f32(s2); } else { peg$currPos = s0; s0 = peg$FAILED; @@ -3109,6 +3144,9 @@ function peg$parse(input, options) { peg$currPos = s0; s0 = peg$FAILED; } + } else { + peg$currPos = s0; + s0 = peg$FAILED; } return s0; diff --git a/src/parser.pegjs b/src/parser.pegjs index 88f263cd..5b230960 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -286,6 +286,7 @@ IdentifierStart / "$" / "_" / "\\" @UnicodeEscapeSequence + / "\\" @ExtendedUnicodeEscapeSequence IdentifierPart = IdentifierStart @@ -329,12 +330,12 @@ StringLiteral "string" DoubleStringCharacter = $(!('"' / "\\" / LineTerminator) SourceCharacter) - / "\\" @EscapeSequence + / "\\" @ExtendedEscapeSequence / LineContinuation SingleStringCharacter = $(!("'" / "\\" / LineTerminator) SourceCharacter) - / "\\" @EscapeSequence + / "\\" @ExtendedEscapeSequence / LineContinuation CharacterClassMatcher "character class" @@ -372,6 +373,10 @@ ClassCharacter LineContinuation = "\\" LineTerminatorSequence { return ""; } +ExtendedEscapeSequence + = EscapeSequence + / ExtendedUnicodeEscapeSequence + EscapeSequence = CharacterEscapeSequence / "0" !DecimalDigit { return "\0"; } @@ -411,7 +416,9 @@ UnicodeEscapeSequence = "u" digits:$(HexDigit HexDigit HexDigit HexDigit) { return String.fromCharCode(parseInt(digits, 16)); } - / "u{" digits:$HexDigit+ "}" { + +ExtendedUnicodeEscapeSequence + = "u{" digits:$HexDigit+ "}" { // String.fromCodePoint() is not supported in IE11. let codepoint = parseInt(digits, 16); if (codepoint > 0x10FFFF) {