
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Part of the Next Generation ANTLR Project
This package is a fork of the official ANTLR4 JavaScript runtime and has been fully transformed to TypeScript. Other improvements are:
antlr4ng-cli
tool to generate parser files compatible with this runtime. This tool uses a custom build of the ANTLR4 tool.This package is a blend of the original JS implementation and antlr4ts, which is a TypeScript implementation of the ANTLR4 runtime, but was abandoned. It tries to keep the best of both worlds, while following the Java runtime as close as possible. It's a bit slower than the JS runtime, but faster than antlr4ts.
To install the package, run the following command:
npm install antlr4ng
This package has a peer dependency to antlr4ng-cli
, which is the tool to generate parser files compatible with this runtime, so it is strongly recommended to install this one too:
npm install --save-dev antlr4ng-cli
See its readme for more information.
If you come from one of the other JS/TS runtimes, you may have to adjust your code a bit. The antlr4ng package more strictly exposes the Java nullability for certain members. This will require that you either use the non-null assertion operator to force the compiler to accept your code, or you have to check for null values before accessing a member. The latter is the recommended way, as it is safer.
Additionally, some members have been renamed to more TypeScript like names. The following table shows the most important changes:
Old Name | New Name |
---|---|
Parser._ctx | Parser.context |
Parser._errHandler | Parser.errorHandler |
Parser._input | Parser.inputStream |
Recognizer._interp | Recognizer.interpreter |
The package requires ES2022 or newer, for features like static initialization blocks in classes and private fields (#field
). It is recommended to use the latest TypeScript version.
The following example shows how to use the runtime to parse a simple expression, assuming you have generated the lexer and parser files for the grammar and placed them in a subfolder folder called generated
. The readme file of the antlr4ng-cli tool shows how to do that. Assume we have this expression grammar:
grammar Expression;
start: multiply | divide | add | subtract;
expression: '(' expression ')' | number;
multiply: expression '*' expression;
divide: expression '/' expression;
add: expression '+' expression;
subtract: expression '-' expression;
number: NUMBER;
NUMBER: [0-9]+;
WS: [ \t\r\n]+ -> skip;
import { CharStreams, CommonTokenStream } from "antlr4ng";
import { ExpressionLexer } from "./generated/ExpressionLexer.js";
import { ExpressionParser } from "./generated/ExpressionParser.js";
const input = "1 + 2 * 3";
const inputStream = CharStreams.fromString(input);
const lexer = new ExpressionLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new ExpressionParser(tokenStream);
const tree = parser.start();
You can then use the generated parser to walk the parse tree, for example with a visitor to evaluate the expression:
import { ExpressionVisitor } from "./generated/ExpressionVisitor.js";
class MyVisitor extends ExpressionVisitor<number> {
visitAdd(ctx: AddContext): number {
return this.visit(ctx.expression(0)) + this.visit(ctx.expression(1));
}
visitMultiply(ctx: MultiplyContext): number {
return this.visit(ctx.expression(0)) * this.visit(ctx.expression(1));
}
visitNumber(ctx: NumberContext): number {
return Number.parseInt(ctx.NUMBER().text);
}
}
const visitor = new MyVisitor();
const result = visitor.visit(tree);
This runtime is monitored for performance regressions. The following tables show the results of the benchmarks previously run on the JS runtime and on last release of this one. Warm times were taken from 5 runs with the 2 slowest stripped off and averaged.
Pure JavaScript release (with type definitions):
Test | Cold Run | Warm Run |
---|---|---|
Query Collection | 8464 ms | 230 ms |
Example File | 1043 ms | 112 ms |
Large Inserts | 11022 ms | 10616 ms |
Total | 20599 ms | 10978 ms |
Last release (pure TypeScript):
Test | Cold Run | Warm Run |
---|---|---|
Query Collection | 4724 ms | 337 ms |
Example File | 672 ms | 192 ms |
Large Inserts | 15144 ms | 15039 ms |
Total | 20600 ms | 15592 ms |
The numbers are interesting. While the cold run for the query collection is almost 3 seconds faster with pure TS, the overall numbers in warm state are worse. So it's not a pure JS vs. TS situation, but something else must have additional influence and this will be investigated. After all the TypeScript code is ultimately transpiled to JS, so it's probably a matter of how effective the TS code is translated to JS.
Overall the numbers in the pure TS runtime are pretty good, especially when comparing them with antlr4ts.
The benchmarks consist of a set of query files, which are parsed by a MySQL parser. The MySQL grammar is one of the largest and most complex grammars you can find for ANTLR4, which, I think, makes it a perfect test case for parser tests.
The query collection file contains more than 900 MySQL queries of all kinds, from very simple comments-only statements to complex stored procedures, including some deeply nested select queries that can easily exhaust the available stack space (in certain situations, such as parsing in a thread with default stack size). The minimum MySQL server version used was 8.0.0.
The large binary inserts file contains only a few dozen queries, but they are really large with deep recursions, so they stress the prediction engine of the parser. In addition, one query contains binary (image) data containing input characters from the entire UTF-8 range.
The example file is a copy of the largest test file in this repository, and is known to be very slow to parse with other MySQL grammars. The one used here, however, is fast.
ParserRuleContext
for retrieving sub contexts to accept any constructor parameter, for more flexibility.ParserRuleContext.getChild
now accepts any constructor type.The entire runtime now exclusively uses TypeScript. It was tested with the standard ANTLR4 runtime tests and completed the test suite successfully.
These releases contain mostly internal changes and bug fixes. The antlr4ng-cli tool dependency has been updated to the latest version and build + test processes has been improved (esbuild instead of webpack, Jest instead of Jasmine).
There are also some smaller fixes in Interval
and ParseTreeVisitor
. The latter now has the same implementation as the Java runtime.
numberOfSyntaxErrors
now.Bug fix releases. They contain many bugs found while integrating the runtime into a large project.
This release includes a lot of cleanup.
__list()
appendix in their name. Instead method overloading is used now to distinguish between the two cases.getTypedRuleContext
and getTypedRuleContexts
have been renamed to getRuleContext
and getRuleContexts
respectively, just as in the Java runtime._errHandler
-> 'errorHandler,
_input->
inputStream` and more).BitSet
to use less memory.getTokens()
in BufferedTokenStream
.antlr4ng-cli
, which is the tool to generate parser files compatible with this runtime.IntStream
interface as the base for CharStream
and TokenStream
. This avoids duplicate code in the stream type definitions.FileStream
as a preparation to get rid of the separate package files for node and browser. If something needs to be loaded from a file, the particular environment should provide the code for that.ErrorNode
, InputMismatchException
antlr4.atn
. Everything is available under a top level import.FAQs
Alternative JavaScript/TypeScript runtime for ANTLR4
The npm package antlr4ng receives a total of 42,655 weekly downloads. As such, antlr4ng popularity was classified as popular.
We found that antlr4ng demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.