All Projects → mcchatman8009 → antlr4-tool

mcchatman8009 / antlr4-tool

Licence: other
A useful Antlr4 tool with full TypeScript support

Programming Languages

typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to antlr4-tool

xqdoc
An Antlr4 implementation of xqDoc for XQuery
Stars: ✭ 14 (-58.82%)
Mutual labels:  antlr4, antlr4-grammar
yahdl
A programming language for FPGAs.
Stars: ✭ 20 (-41.18%)
Mutual labels:  antlr4
speedy-antlr-tool
Generate an accelerator extension that makes your Antlr parser in Python super-fast!
Stars: ✭ 22 (-35.29%)
Mutual labels:  antlr4
java-ast
Java Parser for JavaScript/TypeScript (based on antlr4ts)
Stars: ✭ 58 (+70.59%)
Mutual labels:  antlr4
Java-AgentSpeak
LightJason - AgentSpeak(L++) for Java
Stars: ✭ 21 (-38.24%)
Mutual labels:  antlr4
Antlr4
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
Stars: ✭ 11,227 (+32920.59%)
Mutual labels:  antlr4
snowstar
Here lies the code for the Snow* programming language, currently being rewritten.
Stars: ✭ 31 (-8.82%)
Mutual labels:  antlr4
kolasu
Kotlin Language Support – AST Library
Stars: ✭ 45 (+32.35%)
Mutual labels:  antlr4
Profane
Scripting language for derps
Stars: ✭ 18 (-47.06%)
Mutual labels:  antlr4
ANTLR4ParseTreeVisualizer
Visual Studio debugging visualizer, and .NET visualization controls, for ANTLR4 parse trees
Stars: ✭ 59 (+73.53%)
Mutual labels:  antlr4
MPL
A language to generate command blocks for Minecraft 1.9 and higher
Stars: ✭ 18 (-47.06%)
Mutual labels:  antlr4
bsl-parser
Коллекция парсеров языка 1С (BSL) в формате ANTLR4.
Stars: ✭ 23 (-32.35%)
Mutual labels:  antlr4
robots.txt
🤖 robots.txt as a service. Crawls robots.txt files, downloads and parses them to check rules through an API
Stars: ✭ 13 (-61.76%)
Mutual labels:  antlr4
parcera
Grammar-based Clojure(script) parser
Stars: ✭ 100 (+194.12%)
Mutual labels:  antlr4
libra
Java Predicate, supports SQL-like syntax
Stars: ✭ 30 (-11.76%)
Mutual labels:  antlr4

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].