Skip to content

Commit

Permalink
Fix references, rename ref -> reference and remove matchIdx from the …
Browse files Browse the repository at this point in the history
…AST. Closes #57
  • Loading branch information
jviereck committed Aug 29, 2014
1 parent 9654a8d commit eea23f5
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 436 deletions.
23 changes: 9 additions & 14 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@
function parse(str, flags) {
var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
var pos = 0;
var lastMatchIdx = 0;
var lastMatchClosed = 0;
var closedCaptureCounter = 0;

function addRaw(node) {
node.raw = str.substring(node.range[0], node.range[1]);
Expand Down Expand Up @@ -206,9 +205,9 @@
});
}

function createRef(ref) {
function createReference(ref) {
return addRaw({
type: 'ref',
type: 'reference',
ref: parseInt(ref, 10),

This comment has been minimized.

Copy link
@mathiasbynens

mathiasbynens Aug 31, 2014

Collaborator

Ah, shouldn’t we rename this ref to reference too?

range: [
pos - 1 - ref.length,
Expand Down Expand Up @@ -418,12 +417,6 @@
return false;
}

// Need to set the `matchIdx` here before calling `parseDisjunction()`.
var matchIdx;
if (type === 'normal') {
matchIdx = ++lastMatchIdx;
}

var body = parseDisjunction();
if (!body) {
throw SyntaxError('Expected disjunction');
Expand All @@ -432,7 +425,9 @@
var group = createGroup(type, flattenBody(body), from, pos);

if (type == 'normal') {
group.matchIdx = matchIdx;
// Keep track of the number of closed groups. This is required for
// parseDecimalEscape().
closedCaptureCounter++;
}
return group;
}
Expand Down Expand Up @@ -624,10 +619,10 @@
if (res = matchReg(/^(?!0)\d+/)) {
match = res[0];
var refIdx = parseInt(res[0], 10);
if (refIdx <= lastMatchClosed) {
// If the number is smaller than the matching-groups found so
if (refIdx <= closedCaptureCounter) {
// If the number is smaller than the normal-groups found so
// far, then it is a reference...
return createRef(res[0]);
return createReference(res[0]);
} else {
// ... otherwise it needs to be interpreted as a octal (if the
// number is in an octal format). If it is NOT octal format,
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ var runTests = function(data, flags) {

runTests(require('./test-data.json'), '');
runTests(require('./test-data-unicode.json'), 'u');
runTests(require('./test-data-nonstandard.json'), '');
52 changes: 52 additions & 0 deletions test/test-data-nonstandard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"\\1": {
"type": "value",
"kind": "octal",
"codePoint": 1,
"range": [
0,
2
],
"raw": "\\1"
},
"a\\91": {
"type": "alternative",
"body": [
{
"type": "value",
"kind": "symbol",
"codePoint": 97,
"range": [
0,
1
],
"raw": "a"
},
{
"type": "value",
"kind": "symbol",
"codePoint": 57,
"range": [
2,
3
],
"raw": "9"
},
{
"type": "value",
"kind": "symbol",
"codePoint": 49,
"range": [
3,
4
],
"raw": "1"
}
],
"range": [
0,
4
],
"raw": "a\\91"
}
}
Loading

0 comments on commit eea23f5

Please sign in to comment.