From 3fe6aba7e2cdebf4b9c748274024e6fe24773f48 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 30 Nov 2013 17:04:33 +0100 Subject: [PATCH] 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. --- lib/compiler/passes/generate-javascript.js | 61 ++++++++++++---------- lib/parser.js | 59 ++++++++++++--------- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 1d27101..1e177d3 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -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();', ' }', ' }', '', diff --git a/lib/parser.js b/lib/parser.js index e110efb..ce98ba0 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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(); } }