
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
antlr4-tool
Advanced tools
An Antlr4 tool that generates both Typescript and JavaScript Antlr Parsers (Current Antlr version: 4.8)
The purpose and intent of this project was to create and generate Typescript declaration files for the Antlr4 JavaScript target. Allowing for generated Antlr Parsers and Lexers that have full ES5 browser support. This tool also allows for pure JavaScript implementations, if needed.
npm install --save-dev antlr4-tool
npm install -S antlr4
#
# (TypeScript Only)
#
npm install -S @types/antlr4
"scripts": {
"generate-parser": "antlr4-tool -o parser path/to/Grammar.g4"
}
npm run generate-parser
JavaScript
const antlr4 = require('antlr4')
const InputStream = antlr4.InputStream;
const CommonTokenStream = antlr4.CommonTokenStream;
const GrammarParser = require('./parser/GrammarParser').GrammarParser;
const GrammarLexer = require('./parser/GrammarLexer').GrammarLexer;
const inputStream = new InputStream('int x = 10;');
const lexer = new GrammarLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new GrammarParser(tokenStream);
// Parse the input, where `compilationUnit` is whatever entry point you defined
const tree = parser.compilationUnit();
console.log(tree);
TypeScript
import {InputStream, CommonTokenStream} from 'antlr4';
import {GrammarParser} from './parser/GrammarParser';
import {GrammarLexer} from './parser/GrammarLexer';
const inputStream = new InputStream('enter you grammar here');
const lexer = new GrammarLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new GrammarParser(tokenStream);
// Parse the input, where `compilationUnit` is whatever entry point you defined
const tree = parser.compilationUnit();
console.log(tree);
Add to your tsconfig.json:
"typeRoots": ["types"],
npm install -g antlr4-tool
Usage: antlr4-tool [options] <grammars...>
Options:
-V, --version output the version number
-o --output-dir [output_dir] Output Directory (Default: Current Directory)
-l --language [language] Antlr Language Target: ts, typescript, js, javascript (Default: typescript)
--listener Generate parse tree listener (Default)
--no-listener Don't generate parse tree listener
--visitor Generate parse tree visitor (Default)
--no-visitor Don't generate parse tree visitor
-h, --help output usage information
antlr4-tool -V
antlr4-tool -l ts -o c-parser samples/c/C.g4
antlr4-tool -l js -o c-parser samples/c/C.g4
const tool = require('antlr4-tool');
const opts = {
language: 'TypeScript', // Only support for JavaScript & TypeScript
grammarFiles: ['samples/c/C.g4'],
outputDirectory: 'c-parser'
};
const compiledResults = tool.compile(opts);
console.log(compiledResults);
FAQs
An Antlr4 tool that generates both Typescript and JavaScript Antlr Parsers (Current Antlr version: 4.8)
We found that antlr4-tool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.