Skip to content

Commit

Permalink
Merge pull request #102 from jviereck/jviereck/fix101
Browse files Browse the repository at this point in the history
Fix regression with using closing brackets inside of class range in non-unicode mode
  • Loading branch information
jviereck authored Mar 11, 2020
2 parents 0f22de3 + e929267 commit 1325cad
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "regjsparser",
"version": "0.6.2",
"version": "0.6.3",
"author": "'Julian Viereck' <[email protected]>",
"license": "BSD-2-Clause",
"main": "./parser",
Expand Down
26 changes: 16 additions & 10 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,6 @@
var _char = matches[0];
var first = _char.charCodeAt(0);
if (hasUnicodeFlag) {
if (_char === '}') {
bail("unescaped or unmatched closing brace");
}
if (_char === ']') {
bail("unescaped or unmatched closing bracket");
}
var second;
if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
second = lookahead().charCodeAt(0);
Expand Down Expand Up @@ -487,7 +481,7 @@
return anchor;
}

var atom = parseAtom();
var atom = parseAtomAndExtendedAtom();
if (!atom) {
bail('Expected atom');
}
Expand Down Expand Up @@ -611,24 +605,36 @@
return quantifier;
}

function parseAtom() {
function parseAtomAndExtendedAtom() {
// Parsing Atom and ExtendedAtom together due to redundancy.
// ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
//
// SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
//
// Atom ::
// PatternCharacter
// .
// \ AtomEscape
// CharacterClass
// ( GroupSpecifier Disjunction )
// ( ? : Disjunction )
// ExtendedAtom ::
// ExtendedPatternCharacter
// ExtendedPatternCharacter ::
// SourceCharacter but not one of ^$\.*+?()[|

var res;

// jviereck: allow ']', '}' here as well to be compatible with browser's
// implementations: ']'.match(/]/);
// if (res = matchReg(/^[^^$\\.*+?()[\]{}|]/)) {
if (res = matchReg(/^[^^$\\.*+?(){[|]/)) {
if (res = matchReg(/^[^^$\\.*+?()[\]{}|]/)) {
// PatternCharacter
return createCharacter(res);
}
else if (!hasUnicodeFlag && (res = matchReg(/^(?:]|})/))) {
// ExtendedPatternCharacter
return createCharacter(res);
}
else if (match('.')) {
// .
return createDot();
Expand Down
56 changes: 52 additions & 4 deletions test/test-data-unicode.json
Original file line number Diff line number Diff line change
Expand Up @@ -914,13 +914,13 @@
"}": {
"type": "error",
"name": "SyntaxError",
"message": "unescaped or unmatched closing brace at position 1\n }\n ^",
"message": "Expected atom at position 0\n }\n ^",
"input": "}"
},
"]": {
"type": "error",
"name": "SyntaxError",
"message": "unescaped or unmatched closing bracket at position 1\n ]\n ^",
"message": "Expected atom at position 0\n ]\n ^",
"input": "]"
},
"[\\-]": {
Expand All @@ -930,12 +930,60 @@
"type": "value",
"kind": "singleEscape",
"codePoint": 45,
"range": [1, 3],
"range": [
1,
3
],
"raw": "\\-"
}
],
"negative": false,
"range": [0, 4],
"range": [
0,
4
],
"raw": "[\\-]"
},
"[}]": {
"type": "characterClass",
"body": [
{
"type": "value",
"kind": "symbol",
"codePoint": 125,
"range": [
1,
2
],
"raw": "}"
}
],
"negative": false,
"range": [
0,
3
],
"raw": "[}]"
},
"[^}]": {
"type": "characterClass",
"body": [
{
"type": "value",
"kind": "symbol",
"codePoint": 125,
"range": [
2,
3
],
"raw": "}"
}
],
"negative": true,
"range": [
0,
4
],
"raw": "[^}]"
}
}
41 changes: 41 additions & 0 deletions test/test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -37472,5 +37472,46 @@
2
],
"raw": "a."
},
"}": {
"type": "value",
"kind": "symbol",
"codePoint": 125,
"range": [
0,
1
],
"raw": "}"
},
"]": {
"type": "value",
"kind": "symbol",
"codePoint": 93,
"range": [
0,
1
],
"raw": "]"
},
"[}]": {
"type": "characterClass",
"body": [
{
"type": "value",
"kind": "symbol",
"codePoint": 125,
"range": [
1,
2
],
"raw": "}"
}
],
"negative": false,
"range": [
0,
3
],
"raw": "[}]"
}
}

0 comments on commit 1325cad

Please sign in to comment.