Skip to content

Commit

Permalink
Fix for incompatibility with the TypeScript transpiler (GitHub issue a…
Browse files Browse the repository at this point in the history
  • Loading branch information
octogonz committed Jan 20, 2021
1 parent d34d2e1 commit 53220f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
39 changes: 37 additions & 2 deletions runtime/JavaScript/src/antlr4/error/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,42 @@

const {PredicateTransition} = require('./../atn/Transition')

class RecognitionException extends Error {
/**
* The base class for error classes. This is used instead of the system Error class to avoid
* incompatibilities with transpilers targeting ECMASCript 5.
*/
class AntlrError {
constructor(message) {
const systemError = new Error(message)
this.name = systemError.name;
this.message = systemError.message;
this.stack = systemError.stack;
}

toString() {
var obj = Object(this);
if (obj !== this) {
throw new TypeError();
}

var name = this.name;
name = (name === undefined) ? 'Error' : String(name);

var msg = this.message;
msg = (msg === undefined) ? '' : String(msg);

if (name === '') {
return msg;
}
if (msg === '') {
return name;
}

return name + ': ' + msg;
}
}

class RecognitionException extends AntlrError {
constructor(params) {
super(params.message);
if (!!Error.captureStackTrace) {
Expand Down Expand Up @@ -156,7 +191,7 @@ class FailedPredicateException extends RecognitionException {
}


class ParseCancellationException extends Error{
class ParseCancellationException extends AntlrError {
constructor() {
super()
Error.captureStackTrace(this, ParseCancellationException);
Expand Down
1 change: 1 addition & 0 deletions runtime/JavaScript/src/antlr4/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

module.exports.AntlrError = require('./Errors').AntlrError;
module.exports.RecognitionException = require('./Errors').RecognitionException;
module.exports.NoViableAltException = require('./Errors').NoViableAltException;
module.exports.LexerNoViableAltException = require('./Errors').LexerNoViableAltException;
Expand Down

0 comments on commit 53220f2

Please sign in to comment.