Skip to content

Commit

Permalink
initial mill setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ailrst committed Jan 9, 2024
1 parent 2a7d05f commit a7a47ea
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
62 changes: 62 additions & 0 deletions antlr.sc
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)
}
47 changes: 47 additions & 0 deletions build.sc
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)
//}


}

0 comments on commit a7a47ea

Please sign in to comment.