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.
This commit is contained in:
David Majda 2016-07-29 11:58:35 +02:00
parent e03d92488a
commit e9cb23608d
2 changed files with 18 additions and 14 deletions

View file

@ -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',

View file

@ -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