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);', ' peg$maxFailExpected.push(expected);',
' }', ' }',
'', '',
' function peg$cleanupExpected(expected) {', ' function peg$buildException() {',
' var i = 1;', ' function cleanupExpected(expected) {',
'', ' var i = 1;',
' expected.sort(function(a, b) {', '',
' if (a.description < b.description) {', ' expected.sort(function(a, b) {',
' return -1;', ' if (a.description < b.description) {',
' } else if (a.description > b.description) {', ' return -1;',
' return 1;', ' } else if (a.description > b.description) {',
' } else {', ' return 1;',
' return 0;', ' } else {',
' }', ' return 0;',
' });', ' }',
' });',
'', '',
' while (i < expected.length) {', ' while (i < expected.length) {',
' if (expected[i - 1].description === expected[i].description) {', ' if (expected[i - 1].description === expected[i].description) {',
' expected.splice(i, 1);', ' expected.splice(i, 1);',
' } else {', ' } else {',
' i++;', ' 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')); ].join('\n'));
@ -959,16 +975,7 @@ module.exports = function(ast, options) {
' if (peg$result !== peg$FAILED && peg$currPos === input.length) {', ' if (peg$result !== peg$FAILED && peg$currPos === input.length) {',
' return peg$result;', ' return peg$result;',
' } else {', ' } else {',
' peg$cleanupExpected(peg$maxFailExpected);', ' throw peg$buildException();',
' 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',
' );',
' }', ' }',
' }', ' }',
'', '',

@ -469,26 +469,42 @@ module.exports = (function() {
peg$maxFailExpected.push(expected); peg$maxFailExpected.push(expected);
} }
function peg$cleanupExpected(expected) { function peg$buildException() {
var i = 1; function cleanupExpected(expected) {
var i = 1;
expected.sort(function(a, b) { expected.sort(function(a, b) {
if (a.description < b.description) { if (a.description < b.description) {
return -1; return -1;
} else if (a.description > b.description) { } else if (a.description > b.description) {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
}); });
while (i < expected.length) { while (i < expected.length) {
if (expected[i - 1].description === expected[i].description) { if (expected[i - 1].description === expected[i].description) {
expected.splice(i, 1); expected.splice(i, 1);
} else { } else {
i++; 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() { function peg$parsegrammar() {
@ -3109,16 +3125,7 @@ module.exports = (function() {
if (peg$result !== peg$FAILED && peg$currPos === input.length) { if (peg$result !== peg$FAILED && peg$currPos === input.length) {
return peg$result; return peg$result;
} else { } else {
peg$cleanupExpected(peg$maxFailExpected); throw peg$buildException();
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
);
} }
} }

Loading…
Cancel
Save