Implement error emitter (Closes #431)
Along with commit 851d8ed
, this resolves #431 based on the fact that plugins can overwrite session methods, allowing use cases like multiple or limited errors (and/or warnings) to be an opt-in feature.
This commit is contained in:
parent
a11d217167
commit
ef0595596f
|
@ -3,9 +3,20 @@
|
|||
"use strict";
|
||||
|
||||
const ast = require( "../ast" );
|
||||
const GrammarError = require( "../grammar-error" );
|
||||
const opcodes = require( "./opcodes" );
|
||||
const parser = require( "../parser" );
|
||||
|
||||
function fatal( message, location ) {
|
||||
|
||||
if ( typeof location !== "undefined" )
|
||||
|
||||
throw new GrammarError( message, location );
|
||||
|
||||
throw new Error( message );
|
||||
|
||||
}
|
||||
|
||||
class Session {
|
||||
|
||||
constructor( options ) {
|
||||
|
@ -19,6 +30,9 @@ class Session {
|
|||
this.visitor = options.visitor || ast.visitor;
|
||||
|
||||
if ( typeof options.warn === "function" ) this.warn = options.warn;
|
||||
if ( typeof options.error === "function" ) this.error = options.error;
|
||||
|
||||
Object.defineProperty( this, "fatal", { value: fatal } );
|
||||
|
||||
}
|
||||
|
||||
|
@ -34,7 +48,13 @@ class Session {
|
|||
|
||||
}
|
||||
|
||||
warn( message, details ) {}
|
||||
warn( message, location ) {}
|
||||
|
||||
error( message, location ) {
|
||||
|
||||
fatal( message, location );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue