Giter Club home page Giter Club logo

antlr4-tool's Introduction

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

antlr4-tool's People

Contributors

alwonder avatar johnholliday avatar jonfreedman avatar mcchatman8009 avatar patrick-remy avatar scorsi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.