Skip to content

Commit

Permalink
Allow closing brace and bracket inside of classes. Fixes #101
Browse files Browse the repository at this point in the history
  • Loading branch information
jviereck committed Mar 7, 2020
1 parent f989518 commit 2a2f090
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
8 changes: 4 additions & 4 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@
return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
}

function createCharacter(matches) {
function createCharacter(matches, insideClass) {
var _char = matches[0];
var first = _char.charCodeAt(0);
if (hasUnicodeFlag) {
if (_char === '}') {
if (!insideClass && _char === '}') {
bail("unescaped or unmatched closing brace");
}
if (_char === ']') {
if (!insideClass && _char === ']') {
bail("unescaped or unmatched closing bracket");
}
var second;
Expand Down Expand Up @@ -1102,7 +1102,7 @@

var res;
if (res = matchReg(/^[^\\\]-]/)) {
return createCharacter(res[0]);
return createCharacter(res[0], true);
} else if (match('\\')) {
res = parseClassEscape();
if (!res) {
Expand Down
52 changes: 50 additions & 2 deletions test/test-data-unicode.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 2a2f090

Please sign in to comment.