How to use antlr4 - 10 common examples

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 mongodb-js / bson-transpilers / test / ECMAScriptTransformer.spec.js View on Github external
const generate = function(input) {
    const chars = new antlr4.InputStream(input);
    const lexer = new ECMAScriptLexer.ECMAScriptLexer(chars);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new ECMAScriptParser.ECMAScriptParser(tokens);
    parser.buildParseTrees = true;

    const tree = parser.expressionSequence();
    const transformer = new ECMAScriptTransformer();
    transformer.visitExpressionSequence(tree);

    // Print
    const listener = new PrintListener();
    return listener.buildAST(tree, parser.ruleNames);
  };
github Thomaash / me / src / importScript / index.js View on Github external
} catch (error) {
        Object.assign(error, { varName, funcName, args, code })
        console.warn(error)
        log.push({
          severity: 'warning',
          msg: `Can't process function call: “${code}”.`
        })
      }
    }
  })

  // Process the tree
  const tree = parse(input)
  const walker = new antlr4.tree.ParseTreeWalker()
  walker.walk(printer, tree)
  antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree)

  // Prepare ports without IPs
  const hostDevs = new Set()
  links.forEach(link => hostDevs.add(link.from).add(link.to))
  links.forEach(edge => {
    edge.from = fixNextHostDev(hostDevs, edge.from)
    edge.to = fixNextHostDev(hostDevs, edge.to)
  })
  links.forEach(edge => {
    ;[edge.from, edge.to].forEach(hostDev => {
      const [nodename, portname] = hostDev.split('-')

      ips.$push(nodename, portname, hostIPs[nodename] || [])
      delete hostIPs[nodename]
    })
  })
github ballerina-attic / composer / modules / web / js / ballerina / parser / antlr-gen / BallerinaVisitor.js View on Github external
// Generated from Ballerina.g4 by ANTLR 4.5.3
// jshint ignore: start
var antlr4 = require('antlr4/index');

// This class defines a complete generic visitor for a parse tree produced by BallerinaParser.

class BallerinaVisitor extends antlr4.tree.ParseTreeVisitor {
    constructor() {
        super();
        return this;
    }

    // Visit a parse tree produced by BallerinaParser#compilationUnit.
    visitCompilationUnit(ctx) {
    }

    // Visit a parse tree produced by BallerinaParser#packageDeclaration.
    visitPackageDeclaration(ctx) {
    }

    // Visit a parse tree produced by BallerinaParser#importDeclaration.
    visitImportDeclaration(ctx) {
    }
github ballerina-attic / composer / modules / web / js / ballerina / parser / antlr-gen / BallerinaListener.js View on Github external
// Generated from Ballerina.g4 by ANTLR 4.5.3
// jshint ignore: start
var antlr4 = require('antlr4/index');

// This class defines a complete listener for a parse tree produced by BallerinaParser.
class BallerinaListener extends antlr4.tree.ParseTreeListener {
    constructor() {
        super();
        return this;
    }

    // Enter a parse tree produced by BallerinaParser#compilationUnit.
    enterCompilationUnit(ctx) {
    }

    // Exit a parse tree produced by BallerinaParser#compilationUnit.
    exitCompilationUnit(ctx) {
    }

    // Enter a parse tree produced by BallerinaParser#packageDeclaration.
    enterPackageDeclaration(ctx) {
    }
github cancerberoSgx / univac / src / getAst.ts View on Github external
function getAst(options: GetAstOptions) {
  const input = options.input
  var MyGrammarLexer = require('./grammar/CLexer').CLexer
  var MyGrammarParser = require('./grammar/CParser').CParser
  // var MyGrammarListener = require('./grammar/CListener').CListener;
  var chars = new antlr4.InputStream(input)
  var lexer = new MyGrammarLexer(chars)
  var tokens = new antlr4.CommonTokenStream(lexer)
  var parser = new MyGrammarParser(tokens)
  //  parser.buildParseTrees = true;
  // //  parser.query()
  var tree = parser.compilationUnit()
  let n: Partial<Node> = {
    children: []
  }
  class Visitor {
    visitChildren(ctx: Ctx) {
      if (!ctx) {
        return
      }
      let node = this.getNode(ctx)
      n.children!.push(node)
      // n = mode
      // this.parentNode = node
github ballerina-attic / composer / modules / web / js / ballerina / parser / parser.js View on Github external
Parser.prototype.parse = function(input){
    // setup parser
    var chars = new antlr4.InputStream(input);
    var lexer = new BallerinaLexer.BallerinaLexer(chars);
    var tokens  = new antlr4.CommonTokenStream(lexer);
    var parser = new BallerinaParser.BallerinaParser(tokens);
    var listener = new BLangParserListener(parser);

    // set custom error listener for collecting syntax errors
    var errorListener = new BLangParserErrorListener();
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    // start parsing
    var tree = parser.compilationUnit();

    antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);

    // return collected errors
    return errorListener.getErrors();
};
github maiermic / antlr4-ace-ext / parser / SingleToken / SingleTokenLexer.js View on Github external
// Generated from grammar/SingleToken.g4 by ANTLR 4.5.3
// jshint ignore: start
var antlr4 = require('antlr4/index');


var serializedATN = ["\u0003\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd",
    "\u0002\u0003\u000b\b\u0001\u0004\u0002\t\u0002\u0003\u0002\u0003\u0002",
    "\u0003\u0002\u0003\u0002\u0003\u0002\u0003\u0002\u0002\u0002\u0003\u0003",
    "\u0003\u0003\u0002\u0002\n\u0002\u0003\u0003\u0002\u0002\u0002\u0003",
    "\u0005\u0003\u0002\u0002\u0002\u0005\u0006\u0007v\u0002\u0002\u0006",
    "\u0007\u0007q\u0002\u0002\u0007\b\u0007m\u0002\u0002\b\t\u0007g\u0002",
    "\u0002\t\n\u0007p\u0002\u0002\n\u0004\u0003\u0002\u0002\u0002\u0003",
    "\u0002\u0002"].join("");


var atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN);

var decisionsToDFA = atn.decisionToState.map( function(ds, index) { return new antlr4.dfa.DFA(ds, index); });

function SingleTokenLexer(input) {
	antlr4.Lexer.call(this, input);
    this._interp = new antlr4.atn.LexerATNSimulator(this, atn, decisionsToDFA, new antlr4.PredictionContextCache());
    return this;
}

SingleTokenLexer.prototype = Object.create(antlr4.Lexer.prototype);
SingleTokenLexer.prototype.constructor = SingleTokenLexer;

SingleTokenLexer.EOF = antlr4.Token.EOF;
SingleTokenLexer.TOKEN = 1;
github cancerberoSgx / univac / univac / src / grammar / golang / GolangLexer.js View on Github external
GolangLexer.prototype.nextToken = function() {
      let next = antlr4.Lexer.prototype.nextToken.apply(this, arguments)//.call(this, parent, invokingState);
  //     this.parser = parser;
  //     this.ruleIndex = GolangParser.RULE_importSpec;
  //     return this;
  // }
        // Get the next token.
        // let next = super.nextToken();

        if (next.getChannel && next.getChannel() == antlr4.Token.DEFAULT_CHANNEL) {
            // Keep track of the last token on the default channel.
            this.lastToken = next;
        }

        return next;
    }
github cancerberoSgx / univac / univac / src / grammar / antlr4 / LexerAdaptor.js View on Github external
// Adapted from LexerAdaptor.py by Sebastián Gurin

// From ANTLRv4Lexer.js, use it like this:
// ```
// function ANTLRv4Lexer(input) {
//   antlr4.Lexer.call(this, input) 
//   this._interp = new antlr4.atn.LexerATNSimulator(this, atn, decisionsToDFA, new antlr4.PredictionContextCache());
//   return this;
// }
// ANTLRv4Lexer.prototype = new LexerAdaptor()
// ANTLRv4Lexer.prototype.constructor = ANTLRv4Lexer;
// ```

var antlr4 = require('antlr4/index');

module.exports.LexerAdaptor = class LexerAdaptor extends antlr4.Lexer {

  getCurrentRuleType() {
    if (typeof this._currentRuleType === 'undefined') {
      this._currentRuleType = antlr4.Token.INVALID_TYPE;
    }
    return this._currentRuleType;
  }

  setCurrentRuleType(ruleType) {
    this._currentRuleType = ruleType;
  }

  handleBeginArgument() {
    if (this.inLexerRule()) {
      this.pushMode(require('./ANTLRv4Lexer').ANTLRv4Lexer.LexerCharSet);
      this.more();
github droplet-editor / droplet / antlr / JavaListener.js View on Github external
// Generated from Java.g4 by ANTLR 4.5
// jshint ignore: start
var antlr4 = require('antlr4/index');

// This class defines a complete listener for a parse tree produced by JavaParser.
function JavaListener() {
	antlr4.tree.ParseTreeListener.call(this);
	return this;
}

JavaListener.prototype = Object.create(antlr4.tree.ParseTreeListener.prototype);
JavaListener.prototype.constructor = JavaListener;

// Enter a parse tree produced by JavaParser#compilationUnit.
JavaListener.prototype.enterCompilationUnit = function(ctx) {
};

// Exit a parse tree produced by JavaParser#compilationUnit.
JavaListener.prototype.exitCompilationUnit = function(ctx) {
};


// Enter a parse tree produced by JavaParser#packageDeclaration.
JavaListener.prototype.enterPackageDeclaration = function(ctx) {
};

// Exit a parse tree produced by JavaParser#packageDeclaration.