Use error emitter (Closes #430)

master
Futago-za Ryuu 6 years ago
parent ef0595596f
commit 04dfef3b63

@ -92,7 +92,7 @@ const compiler = {
return ast.code; return ast.code;
default: default:
throw new Error( `Invalid output format: ${ options.output }.` ); session.error( `Invalid output format: ${ options.output }.` );
} }

@ -59,12 +59,14 @@ function generateJS( ast, session, options ) {
case "rule": case "rule":
return `peg$otherExpectation("${ js.stringEscape( e.value ) }")`; return `peg$otherExpectation("${ js.stringEscape( e.value ) }")`;
case "literal": case "literal":
return "peg$literalExpectation(\"" return "peg$literalExpectation(\""
+ js.stringEscape( e.value ) + js.stringEscape( e.value )
+ "\", " + "\", "
+ e.ignoreCase + e.ignoreCase
+ ")"; + ")";
case "class": { case "class": {
const parts = e.value.map( part => const parts = e.value.map( part =>
@ -80,9 +82,13 @@ function generateJS( ast, session, options ) {
+ ")"; + ")";
} }
case "any": return "peg$anyExpectation()";
case "any":
return "peg$anyExpectation()";
// istanbul ignore next // istanbul ignore next
default: throw new Error( `Unknown expectation type (${ JSON.stringify( e ) })` ); default:
session.fatal( `Unknown expectation type (${ JSON.stringify( e ) })` );
} }
@ -578,7 +584,7 @@ function generateJS( ast, session, options ) {
function s( i ) { function s( i ) {
// istanbul ignore next // istanbul ignore next
if ( i < 0 ) throw new Error( "Rule '" + rule.name + "': Var stack underflow: attempt to use var at index " + i ); if ( i < 0 ) session.fail( "Rule '" + rule.name + "': Var stack underflow: attempt to use var at index " + i );
return "s" + i; return "s" + i;
@ -657,7 +663,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore if // istanbul ignore if
if ( thenSp !== elseSp ) { if ( thenSp !== elseSp ) {
throw new Error( session.fail(
"Rule '" + rule.name + "', position " + pos + ": " "Rule '" + rule.name + "', position " + pos + ": "
+ "Branches of a condition can't move the stack pointer differently " + "Branches of a condition can't move the stack pointer differently "
+ "(before: " + baseSp + ", after then: " + thenSp + ", after else: " + elseSp + ")." + "(before: " + baseSp + ", after then: " + thenSp + ", after else: " + elseSp + ")."
@ -695,7 +701,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore if // istanbul ignore if
if ( bodySp !== baseSp ) { if ( bodySp !== baseSp ) {
throw new Error( session.fail(
"Rule '" + rule.name + "', position " + pos + ": " "Rule '" + rule.name + "', position " + pos + ": "
+ "Body of a loop can't move the stack pointer " + "Body of a loop can't move the stack pointer "
+ "(before: " + baseSp + ", after: " + bodySp + ")." + "(before: " + baseSp + ", after: " + bodySp + ")."
@ -921,7 +927,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore next // istanbul ignore next
default: default:
throw new Error( session.fail(
"Rule '" + rule.name + "', position " + ip + ": " "Rule '" + rule.name + "', position " + ip + ": "
+ "Invalid opcode " + bc[ ip ] + "." + "Invalid opcode " + bc[ ip ] + "."
); );

@ -1,7 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
// Inference match result of the rule. Can be: // Inference match result of the rule. Can be:
// -1: negative result, always fails // -1: negative result, always fails
// 0: neutral result, may be fail, may be match // 0: neutral result, may be fail, may be match
@ -90,8 +88,8 @@ function inferenceMatchResult( ast, session ) {
// istanbul ignore next // istanbul ignore next
if ( ++count > 6 ) { if ( ++count > 6 ) {
throw new GrammarError( session.error(
"Infinity cycle detected when trying evaluate node match result", "Infinity cycle detected when trying to evaluate node match result",
node.location node.location
); );

@ -1,6 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
const util = require( "../../util" ); const util = require( "../../util" );
const __hasOwnProperty = Object.prototype.hasOwnProperty; const __hasOwnProperty = Object.prototype.hasOwnProperty;
@ -42,7 +41,7 @@ function reportDuplicateLabels( ast, session ) {
const start = env[ label ].start; const start = env[ label ].start;
throw new GrammarError( session.error(
`Label "${ label }" is already defined at line ${ start.line }, column ${ start.column }.`, `Label "${ label }" is already defined at line ${ start.line }, column ${ start.column }.`,
node.location node.location
); );

@ -1,6 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
const __hasOwnProperty = Object.prototype.hasOwnProperty; const __hasOwnProperty = Object.prototype.hasOwnProperty;
// Checks that each rule is defined only once. // Checks that each rule is defined only once.
@ -17,7 +16,7 @@ function reportDuplicateRules( ast, session ) {
const start = rules[ name ].start; const start = rules[ name ].start;
throw new GrammarError( session.error(
`Rule "${ name }" is already defined at line ${ start.line }, column ${ start.column }.`, `Rule "${ name }" is already defined at line ${ start.line }, column ${ start.column }.`,
node.location node.location
); );

@ -1,7 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
// Reports left recursion in the grammar, which prevents infinite recursion in // Reports left recursion in the grammar, which prevents infinite recursion in
// the generated parser. // the generated parser.
// //
@ -44,7 +42,7 @@ function reportInfiniteRecursion( ast, session ) {
visitedRules.push( node.name ); visitedRules.push( node.name );
const rulePath = visitedRules.join( " -> " ); const rulePath = visitedRules.join( " -> " );
throw new GrammarError( session.error(
`Possible infinite loop when parsing (left recursion: ${ rulePath }).`, `Possible infinite loop when parsing (left recursion: ${ rulePath }).`,
node.location node.location
); );

@ -1,7 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
// Reports expressions that don't consume any input inside |*| or |+| in the // Reports expressions that don't consume any input inside |*| or |+| in the
// grammar, which prevents infinite loops in the generated parser. // grammar, which prevents infinite loops in the generated parser.
function reportInfiniteRepetition( ast, session ) { function reportInfiniteRepetition( ast, session ) {
@ -11,7 +9,7 @@ function reportInfiniteRepetition( ast, session ) {
if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) { if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) {
throw new GrammarError( session.error(
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).", "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
node.location node.location
); );
@ -24,7 +22,7 @@ function reportInfiniteRepetition( ast, session ) {
if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) { if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) {
throw new GrammarError( session.error(
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).", "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
node.location node.location
); );

@ -1,7 +1,5 @@
"use strict"; "use strict";
const GrammarError = require( "../../grammar-error" );
// Checks that all referenced rules exist. // Checks that all referenced rules exist.
function reportUndefinedRules( ast, session, options ) { function reportUndefinedRules( ast, session, options ) {
@ -10,7 +8,7 @@ function reportUndefinedRules( ast, session, options ) {
if ( ! ast.findRule( node.name ) ) { if ( ! ast.findRule( node.name ) ) {
throw new GrammarError( session.error(
`Rule "${ node.name }" is not defined.`, `Rule "${ node.name }" is not defined.`,
node.location node.location
); );
@ -26,7 +24,7 @@ function reportUndefinedRules( ast, session, options ) {
if ( ! ast.findRule( rule ) ) { if ( ! ast.findRule( rule ) ) {
throw new GrammarError( `Start rule "${ rule }" is not defined.` ); session.error( `Start rule "${ rule }" is not defined.` );
} }

Loading…
Cancel
Save