Registry / serialization / antlr4ng

antlr4ng

JSON →
library3.0.16jsnpmunverified

antlr4ng is an alternative, TypeScript-first runtime for ANTLR4 grammars, specifically designed to be used with the `antlr-ng` parser generation tool. Currently stable at version `3.0.16`, it receives frequent point releases addressing bugs and introducing minor enhancements. Unlike the original ANTLR4 JavaScript runtime, `antlr4ng` operates identically across Node.js and browser environments and is built with strict TypeScript nullability checks. It requires an ES2022 (ES6) compatible runtime for features like static initialization blocks and private class fields. While implementing nearly all features of the Java ANTLR4 runtime, it notably omits the `UnbufferedCharStream` class. Developers migrating from other ANTLR4 JavaScript runtimes will need to adjust code for stricter null handling and renamed internal members.

npm install antlr4ng
INSTALL
IMPORT
SIG · ANTLR4NG
A
antlr4ng
serializationjavascriptv3.0.16
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CharStream
import { CharStream } from "antlr4ng";
const { CharStream } = require("antlr4ng");
The `antlr4ng` package is an ES Module (ESM) and requires an `import` statement. It mandates ES2022+ features for its internal implementation.
CommonTokenStream
import { CommonTokenStream } from "antlr4ng";
const { CommonTokenStream } = require("antlr4ng");
Part of the core `antlr4ng` runtime for managing tokens from a lexer, typically used in conjunction with a `CharStream`.
ExpressionLexer
import { ExpressionLexer } from "./generated/ExpressionLexer.js";
import { ExpressionLexer } from "antlr4ng"; import { ExpressionLexer } from "./generated/ExpressionLexer";
Lexer, parser, and visitor classes are generated by the `antlr-ng` tool into user-defined paths. When using Node.js with ESM, the `.js` extension must be explicitly included in the import path.
Parser
import { Parser } from "antlr4ng";
const { Parser } = require("antlr4ng");
The base class for generated parsers, often extended for custom logic. It follows ESM import rules and ES2022+ requirements.

Demonstrates parsing a simple arithmetic expression using generated lexer and parser, highlighting basic setup with `CharStream` and `CommonTokenStream` from `antlr4ng`.

import { CharStream, CommonTokenStream } from "antlr4ng"; import { ExpressionLexer } from "./generated/ExpressionLexer.js"; import { ExpressionParser } from "./generated/ExpressionParser.js"; const input = "1 + 2 * 3"; const inputStream = CharStream.fromString(input); const lexer = new ExpressionLexer(inputStream); const tokenStream = new CommonTokenStream(lexer); const parser = new ExpressionParser(tokenStream); // Optionally configure error handling or listeners // parser.removeErrorListeners(); // parser.addErrorListener(new MyErrorListener()); const tree = parser.start(); // To demonstrate visitor usage (assuming MyVisitor is defined): // import { ExpressionVisitor } from "./generated/ExpressionVisitor.js"; // import { AddContext } from "./generated/ExpressionParser.js"; // Example context type // // class MyVisitor extends ExpressionVisitor<number> { // public visitAdd = (ctx: AddContext): number => { // // Assuming visit is implemented for other rule contexts // return this.visit(ctx.expression(0)) + this.visit(ctx.expression(1)); // }; // // ... implement other visit methods // protected defaultResult(): number { return 0; } // protected aggregateResult(aggregate: number, nextResult: number): number { return nextResult; } // } // const result = new MyVisitor().visit(tree); // console.log(`Parse tree successfully created. Visitor result: ${result}`); console.log("Parse tree successfully created.");
Debug
Known issues
breakingThe `antlr4ng` runtime requires ES2022 (ES13) or newer JavaScript environments, utilizing features like static initialization blocks and private class fields (`#field`). Older JavaScript runtimes (e.g., Node.js < 16) will fail.
fix
Ensure your Node.js version is 16.x or higher, or configure your bundler/TypeScript compiler to target `ES2022` or higher.
affects: >=3.0.0
breakingMigration from other ANTLR4 JavaScript runtimes requires adjusting code due to `antlr4ng`'s stricter TypeScript nullability. Many previously implicitly nullable members are now explicitly typed as nullable, aligning with Java runtime behavior.
fix
Implement explicit null checks (`if (member !== null)`) or use TypeScript's non-null assertion operator (`member!`) where appropriate, prioritizing safety.
affects: >=3.0.0
breakingSeveral internal parser and recognizer member variables have been renamed for consistency with TypeScript conventions (e.g., `_ctx` to `context`, `_errHandler` to `errorHandler`, `_input` to `inputStream`, `_interp` to `interpreter`).
fix
Update references to these renamed members throughout your custom parser logic, error handlers, or visitor/listener implementations.
affects: >=3.0.0
gotchaThis `antlr4ng` runtime is designed to be used with parsers generated by the `antlr-ng` tool. While it implements ANTLR4, direct compatibility with parsers generated by the original ANTLR4 JavaScript target might not be guaranteed due to numerous bug fixes and internal changes.
fix
Always use the `antlr-ng` tool to generate your lexer and parser files for optimal compatibility with the `antlr4ng` runtime.
affects: >=3.0.0
gotchaWhen importing generated lexer/parser/visitor files in an ESM Node.js environment, the `.js` extension must be explicitly included in the import path, even if the source file is `.ts`.
fix
Change `import { MyLexer } from './MyLexer';` to `import { MyLexer } from './MyLexer.js';` for generated files.
affects: >=3.0.0
Errors
Common errors & fixes
TS2339: Property '_ctx' does not exist on type 'Parser'. Did you mean 'context'?
Accessing an old, renamed internal member of `Parser` or `Recognizer`.
fix
Replace `parser._ctx` with `parser.context`. Similarly, change `_errHandler` to `errorHandler`, `_input` to `inputStream`, and `_interp` to `interpreter`.
TypeError: Class constructor CharStream cannot be invoked without 'new'
Attempting to use `require()` for the `antlr4ng` package, which is an ESM-only library and relies on ES2022+ features, or incorrect CJS interop.
fix
Ensure your project is configured for ES Modules and use `import { CharStream } from 'antlr4ng';` instead of `const { CharStream } = require('antlr4ng');`.
Error: Cannot find module './generated/ExpressionLexer'
The import path for generated files is incorrect, or the `.js` extension is missing when running in an ESM Node.js environment.
fix
Verify the relative path to your `generated` folder. For ESM in Node.js, ensure the import statement includes the `.js` extension: `import { ExpressionLexer } from './generated/ExpressionLexer.js';`
SyntaxError: Private field '#field' must be declared in an enclosing class
Running `antlr4ng` in a JavaScript environment that does not support ES2022 (ES13) features, specifically private class fields (`#field`).
fix
Upgrade your Node.js runtime to version 16.x or newer, or ensure your build tools (e.g., Babel, TypeScript compiler) are configured to transpile to a target that supports ES2022 or higher.
Upgrade
Version history
3.0.16latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources