From 04dfef3b63b4c707a4feba956b74e8a75fb3c04e Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Fri, 2 Feb 2018 04:32:39 +0000 Subject: [PATCH] Use error emitter (Closes #430) --- lib/compiler/index.js | 2 +- lib/compiler/passes/generate-js.js | 18 ++++++++++++------ lib/compiler/passes/inference-match-result.js | 6 ++---- lib/compiler/passes/report-duplicate-labels.js | 3 +-- lib/compiler/passes/report-duplicate-rules.js | 3 +-- .../passes/report-infinite-recursion.js | 4 +--- .../passes/report-infinite-repetition.js | 6 ++---- lib/compiler/passes/report-undefined-rules.js | 6 ++---- 8 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/compiler/index.js b/lib/compiler/index.js index 61ea4dc..bf6b5c9 100644 --- a/lib/compiler/index.js +++ b/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 }.` ); } diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index fcb4cc0..4eee73a 100644 --- a/lib/compiler/passes/generate-js.js +++ b/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 ] + "." ); diff --git a/lib/compiler/passes/inference-match-result.js b/lib/compiler/passes/inference-match-result.js index 2546c4d..47f984b 100644 --- a/lib/compiler/passes/inference-match-result.js +++ b/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 ); diff --git a/lib/compiler/passes/report-duplicate-labels.js b/lib/compiler/passes/report-duplicate-labels.js index 5cf796b..bc9e79b 100644 --- a/lib/compiler/passes/report-duplicate-labels.js +++ b/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 ); diff --git a/lib/compiler/passes/report-duplicate-rules.js b/lib/compiler/passes/report-duplicate-rules.js index 82da138..908ff99 100644 --- a/lib/compiler/passes/report-duplicate-rules.js +++ b/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 ); diff --git a/lib/compiler/passes/report-infinite-recursion.js b/lib/compiler/passes/report-infinite-recursion.js index 1f6c270..fab4109 100644 --- a/lib/compiler/passes/report-infinite-recursion.js +++ b/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 ); diff --git a/lib/compiler/passes/report-infinite-repetition.js b/lib/compiler/passes/report-infinite-repetition.js index 1125f53..ef6548d 100644 --- a/lib/compiler/passes/report-infinite-repetition.js +++ b/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 ); diff --git a/lib/compiler/passes/report-undefined-rules.js b/lib/compiler/passes/report-undefined-rules.js index 1b47f46..e26c092 100644 --- a/lib/compiler/passes/report-undefined-rules.js +++ b/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.` ); }