Skip to content

Commit

Permalink
Adding parsing for ExtendedAtom. Remove parser logic from createChara…
Browse files Browse the repository at this point in the history
…cter.
  • Loading branch information
jviereck committed Mar 8, 2020
1 parent 2a2f090 commit 23d58b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
30 changes: 18 additions & 12 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,10 @@
return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
}

function createCharacter(matches, insideClass) {
function createCharacter(matches) {
var _char = matches[0];
var first = _char.charCodeAt(0);
if (hasUnicodeFlag) {
if (!insideClass && _char === '}') {
bail("unescaped or unmatched closing brace");
}
if (!insideClass && _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 Expand Up @@ -1102,7 +1108,7 @@

var res;
if (res = matchReg(/^[^\\\]-]/)) {
return createCharacter(res[0], true);
return createCharacter(res[0]);
} else if (match('\\')) {
res = parseClassEscape();
if (!res) {
Expand Down
4 changes: 2 additions & 2 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 Down

0 comments on commit 23d58b4

Please sign in to comment.