sbt and antlr, got simple example?

2019-07-01 19:26发布

问题:

Does anyone have an example of how to set up sbt to build an ANTLR file (to scala) and then compile the resulting code.

My file layout

 src/main/scala/Test.scala     // scala test rig
 src/main/scala/Test.g         // antlr grammar

 build/antlr/TestParser.scala  // antlr output files
 build/antlr/TestLexer.scala

What should my sbt contain? I know there's a plugin out there for pulling in the rules for ANTLR, but I haven't been able to make it work. (Still newbie to this world)

回答1:

I've written a sbt plugin to generate the parser and lexer code from a provided antlr grammer file. You can download the code on my github page http://github.com/stefri/sbt-antlr. It's also listed in the sbt plugin list https://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-list. The latest snapshot uses ANTLR 3.3 and is available via my github maven repository for the sbt 0.11.x series. If you need another ANTLR version it's easy to change and rebuild, I'm still working on the configuration options.

The usage is quite simple, just include the the plugin and my maven repository in ./project/plugins/build.sbt

resolvers += "stefri" at "http://stefri.github.com/repo/snapshots"

addSbtPlugin("com.github.stefri" % "sbt-antlr" % "0.2-SNAPSHOT")

then place your ANTLR3 grammar files in src/main/antlr3. They will be included in your next build.

Make sure you also include the plugins settings sbtantlr.SbtAntlrPlugin.antlrSettings in your project settings, e.g if you are using the simple configuration approach add the following line

seq(sbtantlr.SbtAntlrPlugin.antlrSettings: _*)

to your build.sbt file. Note, sbt-antlr generates the source code only once as long as your grammar file didn't change it does not re-generate the java source files.

The generated java files are spit out to target/scala-2.9.1/src_managed/main/antlr3, so make sure you inlcude that path in your IDE's build path. The plugin is still work in progress, but it already works quite nice with my grammars.



标签: scala sbt antlr3