Skip to content

yegor256/antlr2ebnf-maven-plugin

Repository files navigation

mvn Maven Central Javadoc codecov Hits-of-Code License

This Maven plugin takes your ANTLR4 grammar .g4 files and generates EBNF in the format expected by the naive-ebnf LaTeX package. Then, using pdflatex installed on your computer, the plugin renders the generated EBNF as a PDF document (you can skip that with the skipLatex configuration option). Then, you can transform this PDF to SVG or PNG formats, using the tools explained below.

The plugin expects you to have ANTLR-to-XML converter made by Gunther Rademacher, in the target/convert directory (normally, there should be five .jar files).

Just add it to pom.xml:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>com.yegor256</groupId>
        <artifactId>antlr2ebnf-maven-plugin</artifactId>
        <version>0.0.7</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

If your ANTLR4 grammar file is in the src/main/antlr4/foo/bar/Sample.g4 path, the EBNF will be generated in the target/ebnf/foo/bar/Sample.txt path and the PDF will be in the target/ebnf/foo/bar/Sample.pdf file.

You can also run this plugin in one line, without adding it to any pom.xml (Maven is still used, but no project is required, just a directory with .g4 files):

$ mvn com.yegor256:antlr2ebnf-maven-plugin:generate \
  -Dantlr2ebnf.sourceDir=/tmp/antlr4 \
  -Dantlr2ebnf.targetDir=/tmp/ebnf \
  -Dantlr2ebnf.convertDir=/tmp/convert-jars

Then, if you need PNG and SVG, use pdfcrop, pdf2svg, and convert:

$ pdfcrop --margins '10 10 10 10' Sample.pdf crop.pdf
$ pdf2svg crop.pdf Sample.svg
$ convert -density 300 -quality 100 -transparent white -colorspace RGB crop.pdf Sample.png

Should work. If it doesn't, submit a ticket, I will try to help.

Options

Here is the full list of options that you may use in the <configuration> of the plugin:

  • skip — disables the execution of the plugin
  • skipLatex — skips PDF generation step, just generates the .txt file
  • sourceDir — the directory with .g4 files
  • targetDir — the directory where .txt and .pdf files will be generated
  • convertDir — the directory with .jar files of the "convert" tool
  • pdflatex - the name of the pdflatex binary
  • specials — the list of terms that will be converted to ENBF specials
  • latexDir — the directory with temporary LaTeX files

More of them you can find in GenerateMojo.java.