Error handling: Extract exception building into its own function

The exception-creating code will get somewhat hairy soon, so let's make
sure them mess will be contained.

Implements part of #198.
redux
David Majda 11 years ago
parent d96eb317fd
commit 3fe6aba7e2

@ -909,26 +909,42 @@ module.exports = function(ast, options) {
' peg$maxFailExpected.push(expected);',
' }',
'',
' function peg$cleanupExpected(expected) {',
' var i = 1;',
'',
' expected.sort(function(a, b) {',
' if (a.description < b.description) {',
' return -1;',
' } else if (a.description > b.description) {',
' return 1;',
' } else {',
' return 0;',
' }',
' });',
' function peg$buildException() {',
' function cleanupExpected(expected) {',
' var i = 1;',
'',
' expected.sort(function(a, b) {',
' if (a.description < b.description) {',
' return -1;',
' } else if (a.description > b.description) {',
' return 1;',
' } else {',
' return 0;',
' }',
' });',
'',
' while (i < expected.length) {',
' if (expected[i - 1].description === expected[i].description) {',
' expected.splice(i, 1);',
' } else {',
' i++;',
' while (i < expected.length) {',
' if (expected[i - 1].description === expected[i].description) {',
' expected.splice(i, 1);',
' } else {',
' i++;',
' }',
' }',
' }',
'',
' var pos = Math.max(peg$currPos, peg$maxFailPos),',
' posDetails = peg$computePosDetails(pos),',
' found = pos < input.length ? input.charAt(pos) : null;',
'',
' cleanupExpected(peg$maxFailExpected);',
'',
' return new SyntaxError(',
' peg$maxFailExpected,',
' found,',
' pos,',
' posDetails.line,',
' posDetails.column',
' );',
' }',
''
].join('\n'));
@ -959,16 +975,7 @@ module.exports = function(ast, options) {
' if (peg$result !== peg$FAILED && peg$currPos === input.length) {',
' return peg$result;',
' } else {',
' peg$cleanupExpected(peg$maxFailExpected);',
' peg$reportedPos = Math.max(peg$currPos, peg$maxFailPos);',
'',
' throw new SyntaxError(',
' peg$maxFailExpected,',
' peg$reportedPos < input.length ? input.charAt(peg$reportedPos) : null,',
' peg$reportedPos,',
' peg$computePosDetails(peg$reportedPos).line,',
' peg$computePosDetails(peg$reportedPos).column',
' );',
' throw peg$buildException();',
' }',
' }',
'',

@ -469,26 +469,42 @@ module.exports = (function() {
peg$maxFailExpected.push(expected);
}
function peg$cleanupExpected(expected) {
var i = 1;
function peg$buildException() {
function cleanupExpected(expected) {
var i = 1;
expected.sort(function(a, b) {
if (a.description < b.description) {
return -1;
} else if (a.description > b.description) {
return 1;
} else {
return 0;
}
});
expected.sort(function(a, b) {
if (a.description < b.description) {
return -1;
} else if (a.description > b.description) {
return 1;
} else {
return 0;
}
});
while (i < expected.length) {
if (expected[i - 1].description === expected[i].description) {
expected.splice(i, 1);
} else {
i++;
while (i < expected.length) {
if (expected[i - 1].description === expected[i].description) {
expected.splice(i, 1);
} else {
i++;
}
}
}
var pos = Math.max(peg$currPos, peg$maxFailPos),
posDetails = peg$computePosDetails(pos),
found = pos < input.length ? input.charAt(pos) : null;
cleanupExpected(peg$maxFailExpected);
return new SyntaxError(
peg$maxFailExpected,
found,
pos,
posDetails.line,
posDetails.column
);
}
function peg$parsegrammar() {
@ -3109,16 +3125,7 @@ module.exports = (function() {
if (peg$result !== peg$FAILED && peg$currPos === input.length) {
return peg$result;
} else {
peg$cleanupExpected(peg$maxFailExpected);
peg$reportedPos = Math.max(peg$currPos, peg$maxFailPos);
throw new SyntaxError(
peg$maxFailExpected,
peg$reportedPos < input.length ? input.charAt(peg$reportedPos) : null,
peg$reportedPos,
peg$computePosDetails(peg$reportedPos).line,
peg$computePosDetails(peg$reportedPos).column
);
throw peg$buildException();
}
}

Loading…
Cancel
Save