Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

antlr4ts

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

antlr4ts

ANTLR 4 runtime for JavaScript written in Typescript

  • 0.5.0-alpha.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
378K
increased by2.11%
Maintainers
2
Weekly downloads
 
Created

What is antlr4ts?

The antlr4ts npm package is a TypeScript target for ANTLR (Another Tool for Language Recognition), which is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It is widely used for building languages, tools, and frameworks.

What are antlr4ts's main functionalities?

Grammar Definition

Define a grammar using ANTLR syntax. This example defines a simple grammar that recognizes the word 'hello' followed by an identifier.

grammar Hello;

r  : 'hello' ID;
ID : [a-z]+;
WS : [ \t\r\n]+ -> skip;

Generating Parser and Lexer

Generate a lexer and parser from the defined grammar and use them to parse an input string. This example shows how to create a lexer and parser for the 'Hello' grammar and parse the input 'hello world'.

const antlr4ts = require('antlr4ts');
const HelloLexer = require('./HelloLexer');
const HelloParser = require('./HelloParser');

const input = 'hello world';
const inputStream = antlr4ts.CharStreams.fromString(input);
const lexer = new HelloLexer.HelloLexer(inputStream);
const tokenStream = new antlr4ts.CommonTokenStream(lexer);
const parser = new HelloParser.HelloParser(tokenStream);
const tree = parser.r();
console.log(tree.toStringTree(parser));

Tree Walking

Walk the parse tree using a custom listener. This example shows how to create a custom listener that logs when entering a rule and how to walk the parse tree using this listener.

const antlr4ts = require('antlr4ts');
const HelloLexer = require('./HelloLexer');
const HelloParser = require('./HelloParser');
const HelloListener = require('./HelloListener');

class CustomHelloListener extends HelloListener.HelloListener {
  enterR(ctx) {
    console.log('Entering R: ' + ctx.getText());
  }
}

const input = 'hello world';
const inputStream = antlr4ts.CharStreams.fromString(input);
const lexer = new HelloLexer.HelloLexer(inputStream);
const tokenStream = new antlr4ts.CommonTokenStream(lexer);
const parser = new HelloParser.HelloParser(tokenStream);
const tree = parser.r();

const listener = new CustomHelloListener();
antlr4ts.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);

Other packages similar to antlr4ts

Keywords

FAQs

Package last updated on 01 Jan 2021

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc