-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//package net.mlbox.millantlr | ||
// https://github.com/da-tubi/antlr4-scala-example/blob/master/antlr.sc | ||
import mill._ | ||
import mill.define._ | ||
import mill.scalalib._ | ||
import $ivy.`org.antlr:antlr4:4.9.3` | ||
import org.antlr.v4.tool.{ANTLRMessage, ANTLRToolListener} | ||
|
||
import scala.collection.mutable | ||
|
||
trait AntlrModule extends JavaModule { | ||
def antlrGrammarSources: Sources | ||
def antlrPackage: Option[String] = None | ||
def antlrGenerateVisitor: Boolean = false | ||
def antlrGenerateListener: Boolean = false | ||
|
||
def antlrGrammarSourceFiles = T { | ||
antlrGrammarSources().flatMap { source => | ||
if (os.isDir(source.path)) { | ||
os.walk(source.path) | ||
} else { | ||
Seq(source.path) | ||
} | ||
}.filter { path => | ||
os.isFile(path) && path.ext == "g4" | ||
}.map(PathRef(_)) | ||
} | ||
|
||
def antlrGenerate = T { | ||
val antlrToolArgs = mutable.ArrayBuffer.empty[String] | ||
|
||
antlrToolArgs.appendAll(antlrGrammarSourceFiles().map(_.path.relativeTo(os.pwd).toString)) | ||
antlrToolArgs.append("-o") | ||
antlrToolArgs.append(s"${T.dest}") | ||
if (antlrGenerateVisitor) { | ||
antlrToolArgs.append("-visitor") | ||
} | ||
if (antlrGenerateListener) { | ||
antlrToolArgs.append("-listener") | ||
} | ||
if (antlrPackage.isDefined) { | ||
antlrToolArgs.append("-package") | ||
antlrToolArgs.append(antlrPackage.get) | ||
} | ||
|
||
val antlrTool = new org.antlr.v4.Tool(antlrToolArgs.toArray) | ||
antlrTool.addListener(new ToolListener()) | ||
antlrTool.processGrammarsOnCommandLine() | ||
|
||
os.walk(T.dest).filter(path => os.isFile(path) && path.ext == "java").map(PathRef(_)) | ||
} | ||
|
||
override def generatedSources = T { | ||
super.generatedSources() ++ antlrGenerate() | ||
} | ||
} | ||
|
||
class ToolListener extends ANTLRToolListener { | ||
override def info(msg: String): Unit = throw new RuntimeException(msg) | ||
override def error(msg: ANTLRMessage): Unit = throw new RuntimeException(msg.toString) | ||
override def warning(msg: ANTLRMessage): Unit = throw new RuntimeException(msg.toString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import mill._, mill.define._, scalalib._ | ||
|
||
// https://index.scala-lang.org/ml86/mill-antlr | ||
import scalafmt._ | ||
import $file.antlr | ||
|
||
|
||
object basil extends RootModule with ScalaModule with antlr.AntlrModule { | ||
def scalaVersion = "3.3.1" | ||
|
||
val javaTests = ivy"com.novocode:junit-interface:0.11" | ||
val scalaTests = ivy"org.scalatest::scalatest:3.2.10" | ||
val scalactic = ivy"org.scalactic::scalactic:3.2.10" | ||
val antlrRuntime = ivy"org.antlr:antlr4-runtime:4.9" | ||
val sourceCode = ivy"com.lihaoyi::sourcecode:0.3.0" | ||
val mainArgs = ivy"com.lihaoyi::mainargs:0.5.1" | ||
|
||
def mainClass = Some("Main") | ||
|
||
|
||
def millSourcePath = super.millSourcePath / "src" / "main" | ||
def ivyDeps = Agg(scalactic, antlrRuntime, sourceCode, mainArgs) | ||
def sources = T.sources {Seq(PathRef(this.millSourcePath / "scala" ))} | ||
|
||
|
||
override def antlrPackage: Option[String] = Some("Parsers") | ||
override def antlrGenerateVisitor = true | ||
override def antlrGrammarSources = T.sources { | ||
Seq(PathRef(millSourcePath / "antlr4")) | ||
} | ||
|
||
object test extends ScalaTests with TestModule.ScalaTest { | ||
def ivyDeps = Agg(scalaTests, javaTests) | ||
def sources = T.sources {Seq(PathRef(this.millSourcePath / "scala" ))} | ||
|
||
//def millSourcePath = super.millSourcePath / "src" / "test" | ||
} | ||
|
||
|
||
// antlr | ||
|
||
//object test extends JavaModuleTests { | ||
// def ivyDeps = Agg(javaTests, scalaTests) | ||
//} | ||
|
||
|
||
} |