Code Monkey home page Code Monkey logo

rrd-antlr4's Introduction

RRD for ANTLR4

Railroad Diagrams for ANTLR 4 grammar rules.

I won't be adding new features to this tool. If you're looking for a more fully featured tool (including LaTeX output, to name just one thing), and a more modular setup version of this tool, have a look at flashpixx' RRD-ANTLR4: https://github.com/flashpixx/RRD-ANTLR4

Introduction

This tool parses ANTLR 4 grammars and creates an html page containing all parser- and lexer rules from this grammar with accompanying railroad diagrams as well as creating separate railroad PNG images of all grammar rules.

Install

Clone this repository:

git clone https://github.com/bkiers/rrd-antlr4.git

Then build it:

cd rrd-antlr4
mvn clean package

The JAR file containing all dependencies can be found in the target folder.

To give it a test by parsing the JSON grammar found in the official ANTLR 4 repository, run the following command from the terminal:

cd target
java -jar rrd-antlr4-0.1.2.jar https://raw.github.com/antlr/grammars-v4/master/json/Json.g4

When the command above finishes, one html file and some png images will have been generated in the folder ./output/Json. The html file does not make use of the png files but uses SVG to display the railroad diagram and will look like this:

json diagrams

Using programmatically

To use this library in your own code, either stick the JAR file (rrd-antlr4-0.1.2.jar) in your classpath, or add the following dependency to your POM (after having done a mvn clean package!):

<dependency>
    <groupId>nl.big-o</groupId>
    <artifactId>rrd-antlr4</artifactId>
    <version>0.1.2</version>
</dependency>

and parse a grammar file as follows:

String grammarFileName = "/path/to/Json.g4";
DiagramGenerator generator = new DiagramGenerator(grammarFileName);

// Print all parsed rules
System.out.println("all parsed rules: " + generator.getRules().keySet());

// The name of the rule to create a railroad diagram of
String ruleName = "jsonObject";

// Get the SVG of the rule
String svg = generator.getSVG(ruleName);
System.out.println("the svg looks like this: " + svg);

// Create the PNG railroad diagram
boolean success = generator.createDiagram(ruleName);
System.out.println("successfully created diagram: " + success);

// Create an html file containing all rules
success = generator.createHtml("/path/to/file.html");
System.out.println("successfully created the html file: " + success);

Credits

  • railroad-diagrams to create the SVG diagrams of ANTLR 4's rules
  • ANTLR 4 grammar (and runtime) to parse ANTLR 4 grammars
  • Batik to convert SVG to PNG

License

The MIT License (MIT)

Copyright (c) 2014 Bart Kiers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

rrd-antlr4's People

Contributors

aatwi avatar aboualiaa avatar bkiers avatar itzg 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.