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,7 +909,8 @@ module.exports = function(ast, options) {
' peg$maxFailExpected.push(expected);', ' peg$maxFailExpected.push(expected);',
' }', ' }',
'', '',
' function peg$cleanupExpected(expected) {', ' function peg$buildException() {',
' function cleanupExpected(expected) {',
' var i = 1;', ' var i = 1;',
'', '',
' expected.sort(function(a, b) {', ' expected.sort(function(a, b) {',
@ -930,6 +931,21 @@ module.exports = function(ast, options) {
' }', ' }',
' }', ' }',
' }', ' }',
'',
' 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,7 +469,8 @@ module.exports = (function() {
peg$maxFailExpected.push(expected); peg$maxFailExpected.push(expected);
} }
function peg$cleanupExpected(expected) { function peg$buildException() {
function cleanupExpected(expected) {
var i = 1; var i = 1;
expected.sort(function(a, b) { expected.sort(function(a, b) {
@ -491,6 +492,21 @@ module.exports = (function() {
} }
} }
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() {
var s0, s1, s2, s3, s4; var s0, s1, s2, s3, s4;
@ -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