Browse Source

Use error emitter (Closes #430)

master
Futago-za Ryuu 5 years ago
parent
commit
04dfef3b63
  1. 2
      lib/compiler/index.js
  2. 18
      lib/compiler/passes/generate-js.js
  3. 6
      lib/compiler/passes/inference-match-result.js
  4. 3
      lib/compiler/passes/report-duplicate-labels.js
  5. 3
      lib/compiler/passes/report-duplicate-rules.js
  6. 4
      lib/compiler/passes/report-infinite-recursion.js
  7. 6
      lib/compiler/passes/report-infinite-repetition.js
  8. 6
      lib/compiler/passes/report-undefined-rules.js

2
lib/compiler/index.js

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

18
lib/compiler/passes/generate-js.js

@ -59,12 +59,14 @@ function generateJS( ast, session, options ) {
case "rule":
return `peg$otherExpectation("${ js.stringEscape( e.value ) }")`;
case "literal":
return "peg$literalExpectation(\""
+ js.stringEscape( e.value )
+ "\", "
+ e.ignoreCase
+ ")";
case "class": {
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
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 ) {
// 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;
@ -657,7 +663,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore if
if ( thenSp !== elseSp ) {
throw new Error(
session.fail(
"Rule '" + rule.name + "', position " + pos + ": "
+ "Branches of a condition can't move the stack pointer differently "
+ "(before: " + baseSp + ", after then: " + thenSp + ", after else: " + elseSp + ")."
@ -695,7 +701,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore if
if ( bodySp !== baseSp ) {
throw new Error(
session.fail(
"Rule '" + rule.name + "', position " + pos + ": "
+ "Body of a loop can't move the stack pointer "
+ "(before: " + baseSp + ", after: " + bodySp + ")."
@ -921,7 +927,7 @@ function generateJS( ast, session, options ) {
// istanbul ignore next
default:
throw new Error(
session.fail(
"Rule '" + rule.name + "', position " + ip + ": "
+ "Invalid opcode " + bc[ ip ] + "."
);

6
lib/compiler/passes/inference-match-result.js

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

3
lib/compiler/passes/report-duplicate-labels.js

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

3
lib/compiler/passes/report-duplicate-rules.js

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

4
lib/compiler/passes/report-infinite-recursion.js

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

6
lib/compiler/passes/report-infinite-repetition.js

@ -1,7 +1,5 @@
"use strict";
const GrammarError = require( "../../grammar-error" );
// Reports expressions that don't consume any input inside |*| or |+| in the
// grammar, which prevents infinite loops in the generated parser.
function reportInfiniteRepetition( ast, session ) {
@ -11,7 +9,7 @@ function reportInfiniteRepetition( ast, session ) {
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).",
node.location
);
@ -24,7 +22,7 @@ function reportInfiniteRepetition( ast, session ) {
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).",
node.location
);

6
lib/compiler/passes/report-undefined-rules.js

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

Loading…
Cancel
Save