![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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.
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);
PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. Unlike ANTLR, which uses LL(*) parsing, PEG.js uses Parsing Expression Grammars (PEGs). PEG.js is simpler to use for smaller projects but may not handle complex grammars as efficiently as ANTLR.
Nearley is a fast, powerful parser toolkit for JavaScript. It uses Earley parsing, which can parse all context-free grammars, including ambiguous ones. Nearley is more flexible than ANTLR but can be slower for certain types of grammars.
Jison is a parser generator that converts a context-free grammar into a JavaScript parser. It is similar to Bison but for JavaScript. Jison is easier to integrate with JavaScript projects but may not offer the same level of optimization and features as ANTLR.
This project has separate requirements for developers and end users.
:bulb: The requirements listed on this page only cover user scenarios - that is, scenarios where developers wish to use ANTLR 4 for parsing tasks inside of a TypeScript application. If you are interested in contributing to ANTLR 4 itself, see CONTRIBUTING.md for contributor documentation.
Parsers generated by the ANTLR 4 TypeScript target have a runtime dependency on the antlr4ts package. The package is tested and known to work with Node.js 6.7.
The tool used to generate TypeScript code from an ANTLR 4 grammar is written in Java. To fully utilize the ANTLR 4 TypeScript target (including the ability to regenerate code from a grammar file after changes are made), a Java Runtime Environment (JRE) needs to be installed on the developer machine. The generated code itself uses several features new to TypeScript 2.0.
antlr4ts
as a runtime dependency using your preferred package manager.npm install antlr4ts --save
yarn add antlr4ts
antlr4ts-cli
as a development dependency using your preferred package manager.npm install antlr4ts-cli --save-dev
yarn add -D antlr4ts-cli
Add a grammar to your project, e.g. path/to/MyGrammar.g4
Add a script to package.json for compiling your grammar to TypeScript
"scripts": {
// ...
"antlr4ts": "antlr4ts -visitor path/to/MyGrammar.g4"
}
Use your grammar in TypeScript
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
// Create the lexer and parser
let inputStream = new ANTLRInputStream("text");
let lexer = new MyGrammarLexer(inputStream);
let tokenStream = new CommonTokenStream(lexer);
let parser = new MyGrammarParser(tokenStream);
// Parse the input, where `compilationUnit` is whatever entry point you defined
let result = parser.compilationUnit();
FAQs
ANTLR 4 runtime for JavaScript written in Typescript
The npm package antlr4ts receives a total of 138,034 weekly downloads. As such, antlr4ts popularity was classified as popular.
We found that antlr4ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.