aboutsummaryrefslogtreecommitdiff
path: root/examples/new-style-macros-example/macros/Main.scala
diff options
context:
space:
mode:
authorJan Christopher Vogt <oss.nsp@cvogt.org>2017-06-13 11:24:39 -0400
committerGitHub <noreply@github.com>2017-06-13 11:24:39 -0400
commit00840fe6888b231c70e95ecf6feaeba89b42e218 (patch)
tree7a0cdec601addcfe487d299dcad25dc85c3dd7e8 /examples/new-style-macros-example/macros/Main.scala
parentace672254b31d7e6203b0c5a22f1c4e6dfe13f42 (diff)
parent38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f (diff)
downloadcbt-00840fe6888b231c70e95ecf6feaeba89b42e218.tar.gz
cbt-00840fe6888b231c70e95ecf6feaeba89b42e218.tar.bz2
cbt-00840fe6888b231c70e95ecf6feaeba89b42e218.zip
Merge pull request #515 from megri/master
Rudimentary Macroparadise plugin
Diffstat (limited to 'examples/new-style-macros-example/macros/Main.scala')
-rw-r--r--examples/new-style-macros-example/macros/Main.scala25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/new-style-macros-example/macros/Main.scala b/examples/new-style-macros-example/macros/Main.scala
new file mode 100644
index 0000000..ecdce34
--- /dev/null
+++ b/examples/new-style-macros-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 }"
+ }
+}
+