aboutsummaryrefslogtreecommitdiff
path: root/examples/macroparadise-example/macros/Main.scala
diff options
context:
space:
mode:
authormegri <teknocide@gmail.com>2017-06-12 23:55:20 +0200
committerMartin Egri <meg@speedledger.se>2017-06-13 14:12:50 +0200
commit7d8d01dfb5dc934dc57f08d9e24be38c67270ea3 (patch)
treee0e768daf8ef1d0f3d7fabfd13937f8f5183eea3 /examples/macroparadise-example/macros/Main.scala
parentace672254b31d7e6203b0c5a22f1c4e6dfe13f42 (diff)
downloadcbt-7d8d01dfb5dc934dc57f08d9e24be38c67270ea3.tar.gz
cbt-7d8d01dfb5dc934dc57f08d9e24be38c67270ea3.tar.bz2
cbt-7d8d01dfb5dc934dc57f08d9e24be38c67270ea3.zip
Rudimentary Macroparadise plugin, example and test
Diffstat (limited to 'examples/macroparadise-example/macros/Main.scala')
-rw-r--r--examples/macroparadise-example/macros/Main.scala25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/macroparadise-example/macros/Main.scala b/examples/macroparadise-example/macros/Main.scala
new file mode 100644
index 0000000..ecdce34
--- /dev/null
+++ b/examples/macroparadise-example/macros/Main.scala
@@ -0,0 +1,25 @@
+package macroparadise_example.macros
+
+import scala.meta._
+import scala.collection.immutable.Seq
+
+class Main extends scala.annotation.StaticAnnotation {
+ inline def apply(defn: Any): Any = meta {
+ defn match {
+ case q"object $name { ..$stats }" =>
+ MainMacroImpl.expand(name, stats)
+ case _ =>
+ abort("@main must annotate an object.")
+ }
+ }
+}
+
+// This is an example how we can refactor the macro implementation into a utility
+// function which can be used for unit testing, see MainUnitTest.
+object MainMacroImpl {
+ def expand(name: Term.Name, stats: Seq[Stat]): Defn.Object = {
+ val main = q"def main(args: Array[String]): Unit = { ..$stats }"
+ q"object $name { $main }"
+ }
+}
+