How to use the antlr4.error function in antlr4

To help you get started, we’ve selected a few antlr4 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github neo4j-contrib / cypher-editor / cypher-editor-support / src / errors / ErrorListener.js View on Github external
* it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

import antlr4 from 'antlr4';

export class ErrorListener extends antlr4.error.ErrorListener {
  errors = [];

  // eslint-disable-next-line no-unused-vars
  syntaxError(rec, sym, line, col, msg, e) {
    if (msg === "mismatched input '' expecting {';', SP}") {
        // suppress error about missing semicolon at the end of a query
      return;
    }
    if (msg === "missing ';' at ''") {
      return;
    }
    if (msg === "mismatched input '' expecting {':', CYPHER, EXPLAIN, PROFILE, USING, CREATE, DROP, LOAD, WITH, OPTIONAL, MATCH, UNWIND, MERGE, SET, DETACH, DELETE, REMOVE, FOREACH, RETURN, START, CALL}") {
      return;
    }
    this.errors.push({ line, col, msg });
  }
github kitspace / electro-grammar / js / lib / index.js View on Github external
function parse(input) {
    const chars = new antlr4.InputStream(input);
    const lexer = new ElectroGrammarLexer(chars);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new ElectroGrammarParser(tokens);

    // enable grammar ambiguity diagnostic output
    // see Antlr book ch 9.2, page 156
    // https://github.com/antlr/antlr4/issues/2206
    parser.removeErrorListeners();
    parser.addErrorListener(new antlr4.error.DiagnosticErrorListener());
    parser._interp.PredictionMode =
      antlr4.atn.PredictionMode.LL_EXACT_AMBIG_DETECTION;

    parser.buildParseTrees = true;

    const tree = parser[start_rule]();
    const listener = new ElectroGrammarToObjectListener();
    const walker = antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
    return listener.obj;
  }
  return parse;
github merklejerk / solpp / src / antlr-utils.js View on Github external
}

function createLocationString(loc, unit) {
	const {line, column} = loc;
	return `"${unit}" (${line}:${column})`;
}

function getNodeRuleName(node) {
	if (!node.getRuleContext)
		return;
	const ctx = node.getRuleContext();
	const cname = ctx.constructor.name;
	return cname.substr(cname, cname.length - 'Context'.length);
}

class ErrorListener extends antlr.error.ErrorListener {
	syntaxError(recognizer, offendingSymbol, line, column, message) {
		throw new Error(`${message} (${line}:${column})`);
	}
}

function createParseTree(LexerType, ParserType, startRule, text, mode=null) {
	const errors = new ErrorListener();
	const lexer = new LexerType(antlr.CharStreams.fromString(text));
	// If mode was passed, jump straight into that mode.
	if (_.isString(mode) && _.includes(lexer.modeNames, mode))
		lexer.mode(_.indexOf(lexer.modeNames, mode));
	const tokens = new antlr.CommonTokenStream(lexer);
	const parser = new ParserType(tokens);
	parser.removeErrorListeners();
	lexer.removeErrorListeners();
	parser.addErrorListener(errors);
github indeedeng / iql / iql2 / src / js / Parsers.js View on Github external
const antlr4 = require('antlr4');
const JQLLexer = require('./JQLLexer');
const JQLParser = require('./JQLParser');

let antlr = require('antlr4');
const ErrorListener = antlr.error.ErrorListener;
const Errors = antlr.error;

const moment = require('moment');

const DEFAULT_UTC_OFFSET = -6;

import autobind from 'autobind-decorator';

function failure(errors, expected, resultValue) {
    return {
        errors,
        expected,
        assumeSuccess: () => {throw errors[0].msg},
        resultValue,
    };
}
github hyperledger-labs / weaver-dlt-interoperability / common / policy-dsl / parser / PolicyParser.js View on Github external
this.state = 14;
	                    if (!( this.precpred(this._ctx, 4))) {
	                        throw new antlr4.error.FailedPredicateException(this, "this.precpred(this._ctx, 4)");
	                    }
	                    this.state = 15;
	                    this.match(PolicyParser.AND);
	                    this.state = 16;
	                    this.expression(5);
	                    break;

	                case 2:
	                    localctx = new BooleanOrExpressionContext(this, new ExpressionContext(this, _parentctx, _parentState));
	                    this.pushNewRecursionContext(localctx, _startState, PolicyParser.RULE_expression);
	                    this.state = 17;
	                    if (!( this.precpred(this._ctx, 3))) {
	                        throw new antlr4.error.FailedPredicateException(this, "this.precpred(this._ctx, 3)");
	                    }
	                    this.state = 18;
	                    this.match(PolicyParser.OR);
	                    this.state = 19;
	                    this.expression(4);
	                    break;

	                } 
	            }
	            this.state = 24;
	            this._errHandler.sync(this);
	            _alt = this._interp.adaptivePredict(this._input,2,this._ctx);
	        }

	    } catch( error) {
	        if(error instanceof antlr4.error.RecognitionException) {
github mongodb-js / bson-transpilers / codegeneration / ErrorListener.js View on Github external
const antlr4 = require('antlr4');

const { BsonTranspilersSyntaxError } = require('../helper/error');

/**
 * Custom Error Listener
 *
 * @returns {object}
 */
class ErrorListener extends antlr4.error.ErrorListener {
  /**
   * Checks syntax error
   *
   * @param {object} recognizer - The parsing support code essentially. Most of it is error recovery stuff
   * @param {object} symbol - Offending symbol
   * @param {int} line - Line of offending symbol
   * @param {int} column - Position in line of offending symbol
   * @param {string} message - Error message
   * @param {string} payload - Stack trace
   */
  syntaxError(recognizer, symbol, line, column, message, payload) {
    throw new BsonTranspilersSyntaxError(message, { symbol, line, column, payload });
  }
}

module.exports = ErrorListener;
github indeedeng / iql / iql2 / src / js / Parsers.js View on Github external
const antlr4 = require('antlr4');
const JQLLexer = require('./JQLLexer');
const JQLParser = require('./JQLParser');

let antlr = require('antlr4');
const ErrorListener = antlr.error.ErrorListener;
const Errors = antlr.error;

const moment = require('moment');

const DEFAULT_UTC_OFFSET = -6;

import autobind from 'autobind-decorator';

function failure(errors, expected, resultValue) {
    return {
        errors,
        expected,
        assumeSuccess: () => {throw errors[0].msg},
        resultValue,
    };
}
github wix / quix / quix-frontend / client / src / lib / language-parsers / sql-parser / parser / errors-listener.ts View on Github external
import antlr4 from 'antlr4';

export interface IErrorAnnotation {
  row: number;
  column: number;
  text: string;
  type: string;
}

export class PrestoErrorListener extends antlr4.error.ErrorListener {
  private readonly annotations: IErrorAnnotation[] = [];

  getErrors = () => this.annotations;

  syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
    this.annotations.push({
      row: line - 1,
      column,
      text: msg,
      type: 'error'
    });
  }
}
github jmandel / fhirpath.js / fhirpath.js View on Github external
var ErrorListener = function(errors) {
  antlr4.error.ErrorListener.call(this);
  this.errors = errors;
  return this;
};
github hyperledger-labs / weaver-dlt-interoperability / common / policy-dsl / parser / PolicyParser.js View on Github external
this.state = 28;
	            this.match(PolicyParser.INTLIT);
	            this.state = 29;
	            _la = this._input.LA(1);
	            if(!((((_la) & ~0x1f) == 0 && ((1 << _la) & ((1 << PolicyParser.GREATER_THAN) | (1 << PolicyParser.LESS_THAN) | (1 << PolicyParser.GREATER_THAN_EQUAL) | (1 << PolicyParser.LESS_THAN_EQUAL))) !== 0))) {
	            this._errHandler.recoverInline(this);
	            }
	            else {
	            	this._errHandler.reportMatch(this);
	                this.consume();
	            }
	            this.state = 30;
	            this.match(PolicyParser.COUNT);
	            break;
	        default:
	            throw new antlr4.error.NoViableAltException(this);
	        }
	    } catch (re) {
	    	if(re instanceof antlr4.error.RecognitionException) {
		        localctx.exception = re;
		        this._errHandler.reportError(this, re);
		        this._errHandler.recover(this, re);
		    } else {
		    	throw re;
		    }
	    } finally {
	        this.exitRule();
	    }
	    return localctx;
	}