From e9cb23608d13c6278b176d1bb2ad0d21cdb574fb Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 29 Jul 2016 11:58:35 +0200 Subject: [PATCH] Split peg$buildException into two separate functions This change reflects the fact that PEG.js-generated parsers really produce two kinds of syntax errors: Structured errors Caused by match failures, trailing input, or calls of the "expected" function in parser code. Their messages have fixed format ("Expected ... but ... found."). Simple errors Caused by calls of the "error" function in parser code. Their messages don't have any fixed format. Each kind of error now has a separate helper function which builds its instances. --- lib/compiler/passes/generate-js.js | 16 +++++++++------- lib/parser.js | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 13fd70f..e70d99e 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1068,8 +1068,7 @@ function generateJS(ast, options) { ' function expected(description, location) {', ' location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos)', '', - ' throw peg$buildException(', - ' null,', + ' throw peg$buildStructuredError(', ' [peg$otherExpectation(description)],', ' input.substring(peg$savedPos, peg$currPos),', ' location', @@ -1079,7 +1078,7 @@ function generateJS(ast, options) { ' function error(message, location) {', ' location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos)', '', - ' throw peg$buildException(message, null, null, location);', + ' throw peg$buildSimpleError(message, location);', ' }', '', ' function peg$literalExpectation(text, ignoreCase) {', @@ -1164,9 +1163,13 @@ function generateJS(ast, options) { ' peg$maxFailExpected.push(expected);', ' }', '', - ' function peg$buildException(message, expected, found, location) {', + ' function peg$buildSimpleError(message, location) {', + ' return new peg$SyntaxError(message, null, null, location);', + ' }', + '', + ' function peg$buildStructuredError(expected, found, location) {', ' return new peg$SyntaxError(', - ' message !== null ? message : peg$SyntaxError.buildMessage(expected, found),', + ' peg$SyntaxError.buildMessage(expected, found),', ' expected,', ' found,', ' location', @@ -1205,8 +1208,7 @@ function generateJS(ast, options) { ' peg$fail(peg$endExpectation());', ' }', '', - ' throw peg$buildException(', - ' null,', + ' throw peg$buildStructuredError(', ' peg$maxFailExpected,', ' peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null,', ' peg$maxFailPos < input.length', diff --git a/lib/parser.js b/lib/parser.js index d6b5f9b..666f2f5 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -523,8 +523,7 @@ function peg$parse(input, options) { function expected(description, location) { location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos) - throw peg$buildException( - null, + throw peg$buildStructuredError( [peg$otherExpectation(description)], input.substring(peg$savedPos, peg$currPos), location @@ -534,7 +533,7 @@ function peg$parse(input, options) { function error(message, location) { location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos) - throw peg$buildException(message, null, null, location); + throw peg$buildSimpleError(message, location); } function peg$literalExpectation(text, ignoreCase) { @@ -619,9 +618,13 @@ function peg$parse(input, options) { peg$maxFailExpected.push(expected); } - function peg$buildException(message, expected, found, location) { + function peg$buildSimpleError(message, location) { + return new peg$SyntaxError(message, null, null, location); + } + + function peg$buildStructuredError(expected, found, location) { return new peg$SyntaxError( - message !== null ? message : peg$SyntaxError.buildMessage(expected, found), + peg$SyntaxError.buildMessage(expected, found), expected, found, location @@ -5021,8 +5024,7 @@ function peg$parse(input, options) { peg$fail(peg$endExpectation()); } - throw peg$buildException( - null, + throw peg$buildStructuredError( peg$maxFailExpected, peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, peg$maxFailPos < input.length