npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

antlr4-tool

v1.1.1

Published

An Antlr4 tool that generates both Typescript and JavaScript Antlr Parsers (Current Antlr version: 4.8)

Downloads

1,382

Readme

antlr4-tool

Overview

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.

Requirements

  • Java Runtime Environment 1.6+ (1.8+ recommended)

Getting Stated

  • Install antlr4-tool, adding it to your package.json
npm install --save-dev antlr4-tool
  • Install the Antlr4 library
npm install -S antlr4

#
# (TypeScript Only)
#
npm install -S @types/antlr4
  • Add a grammar to your project, e.g. path/to/Grammar.g4
"scripts": {
  "generate-parser": "antlr4-tool -o parser path/to/Grammar.g4"
}
  • Run the NPM script command
npm run generate-parser
  • Use your generated 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);

TypeScript Notes

Add to your tsconfig.json:

"typeRoots": ["types"],

For Command-Line Use

Installation For Command-Line Use

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

Finding the Version

antlr4-tool -V

Example Creating a C Parser for TypeScript

antlr4-tool -l ts -o c-parser samples/c/C.g4

Example Creating a C Parser for JavaScript

antlr4-tool -l js -o c-parser samples/c/C.g4

Example

Using antlr4-tool as a Library

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);