Socket
Book a DemoInstallSign in
Socket

antlr4-helper

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

antlr4-helper

A NPM library that assists in various Antlr4 parse, language analysis, and language manipulation tasks

1.0.56
latest
Source
npmnpm
Version published
Weekly downloads
25
-59.02%
Maintainers
1
Weekly downloads
 
Created
Source

Antlr4 Helper

Overview

The goal of this project is to provided classes and interfaces that assist with the interaction of Antlr parsers. Making things like language manipulation and analysis much easier.

Getting Started

npm install -S antlr4-helper

Antlr4 Helper Documentation

Documentation (Click Here)

JavaScript Examples

Parsing Text

const antlrHelper = require('antlr4-helper');
const TinycLexer = require('./parser/TinycLexer').TinycLexer;
const TinycParser = require('./parser/TinycParser').TinycParser;


const factory = antlrHelper.createFactoryBuilder()
    .lexer((input) => new TinycLexer(input))
    .parser(tokenStream => new TinycParser(tokenStream))
    .rootRule((parser) => parser.program())
    .build();

const parser = antlrHelper.createParser(factory);
parser.parse('variable = 100;');
parser.checkForErrors();

//
// Find only variables
//
parser.filter((rule) => rule.getName() === 'id')
    .forEach((rule) => {
        const ruleName = rule.getName();
        console.log(ruleName); // id
        console.log(rule.getText()); // variable
    });

Changing Parsed Text

const antlrHelper = require('antlr4-helper');
const TinycLexer = require('./parser/TinycLexer').TinycLexer;
const TinycParser = require('./parser/TinycParser').TinycParser;

const factory = antlrHelper.createFactoryBuilder()
    .lexer((input) => new TinycLexer(input))
    .parser(tokenStream => new TinycParser(tokenStream))
    .rootRule((parser) => parser.program())
    .build();

const parser = antlrHelper.createParser(factory);
parser.parse('a = 10;');
parser.checkForErrors(); // No parse errors

//
// Find the first rule
//
const rule = parser.findRuleByName('id');

rule.setText('var');
console.log("The changed text:");
console.log(parser.getText()); //var = 10;

console.log("The replaced variable:");
const varName = rule.getText();
console.log(varName); //var;

Keywords

ANTLR4

FAQs

Package last updated on 08 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.