GitHub PullRequestReviewCommentEvent at 2014-04-10 17:25:10
Repository: mars
Actor: VladimirNik
Payload: {"comment":{"url":"https://api.github.com/repos/scalareflect/mars/pulls/comments/11495996","id":11495996,"diff_hunk":"@@ -0,0 +1,103 @@\n+package scala.reflect.macros.runtime.plugin\n+\n+import scala.tools.nsc.{ Global, Phase }\n+import scala.tools.nsc.plugins.{ Plugin, PluginComponent }\n+import scala.tools.nsc.typechecker.Macros\n+\n+object MarsPlugin {\n+  val expandNameOpt = \"expandName\"\n+}\n+\n+class MarsPlugin(val global: Global) extends Plugin {\n+  import MarsPlugin._\n+  import global._\n+  \n+  val name = \"marsplugin\"\n+  val description = \"runtime macro expansion plugin\"\n+  var expandName = \"test\"\n+  val components = List[PluginComponent](MarsComponent)\n+\n+  override def processOptions(options: List[String], error: String => Unit) {\n+    for (option <- options) {\n+      if (option.startsWith(expandNameOpt)) {\n+        expandName = option.substring(expandNameOpt.length)\n+      } else{\n+          error(\"Option not understood: \"+option)\n+      }\n+    }\n+  }\n+\n+  private object MarsComponent extends PluginComponent {\n+    val global = MarsPlugin.this.global\n+    import global._\n+    import scala.reflect.macros.runtime.internal.ContextFactory\n+\n+    override val runsAfter = List(\"typer\")\n+\n+    val phaseName = \"marsPhase\"\n+\n+    override def newPhase(prev: Phase): StdPhase = new StdPhase(prev) {\n+      override def apply(compUnit: CompilationUnit) {\n+        try {\n+          //process only .scala files\n+          val fileName = compUnit.source.file.name\n+          if (fileName.endsWith(\".scala\")) {\n+            println(sm\"\"\"\n+            |=================================================\n+            |=============== Context Creator =================  \n+            |=================================================\n+            |phase name: ${prev.name}\n+            |unit: $compUnit\"\"\")\n+            \n+            val contextFactory = new ContextFactory {\n+              val global = MarsComponent.this.global\n+            } \n+            \n+            import contextFactory.{global => compiler, _}\n+            import compiler.analyzer.Context\n+            \n+            val tree = compUnit.body.asInstanceOf[compiler.Tree]\n+            val initialContext = analyzer.rootContext(compUnit, EmptyTree, false).asInstanceOf[Context]\n+            \n+            ContextCreator(initialContext).contexted(tree)\n+            val contextInfo = applyContextInfo\n+            if (contextInfo.nonEmpty) {\n+              val (applyTree, resultedContext) = contextInfo.get\n+              val untypedInjectTree = q\"\"\"println(\"Hello: \" + a)\"\"\"\n+              val typechecker = global.analyzer.newTyper(resultedContext.asInstanceOf[global.analyzer.Context])\n+              val typedInjectTree = typechecker.typed(untypedInjectTree).asInstanceOf[compiler.Tree]\n+              val unitTree = compUnit.body.asInstanceOf[compiler.Tree]\n+              val transformedTree = RuntimeMacroInjector(unitTree, typedInjectTree).expandMacroTree.asInstanceOf[Tree]\n+              \n+              println(sm\"\"\"\n+              |applyTree: ${showRaw(applyTree)}\n+              |\n+              |===> resulted context: $resultedContext\n+              |\n+              |===> resulted scope: ${resultedContext.scope}\n+              |\n+              |resultedTree: ${showRaw(typedInjectTree)}\n+              |\n+              |transformedTree: ${showRaw(transformedTree)}\n+              |\n+              |transformedShowCode: ${showCode(transformedTree)}\n+              |\"\"\")\n+              \n+              compUnit.body = transformedTree\n+            } else {\n+              println(\"\\n===> tree is not found!!!\\n\")\n+            }\n+            //mode we should get from typer\n+            //global.analyzer.macroExpand(null, null, c.contextMode , null)","path":"mars/src/main/scala/scala/reflect/macros/runtime/plugin/MarsPlugin.scala","position":null,"original_position":91,"commit_id":"56db9a5318ea3c4a9af769708bc9987a2c5fcd79","original_commit_id":"f9674dc7109d6f6c831ef1ce9ae3cca4649c0eda","user":{"login":"VladimirNik","id":2835001,"avatar_url":"https://avatars.githubusercontent.com/u/2835001?","gravatar_id":"6db351d564ec84b44cb8aef30821964b","url":"https://api.github.com/users/VladimirNik","html_url":"https://github.com/VladimirNik","followers_url":"https://api.github.com/users/VladimirNik/followers","following_url":"https://api.github.com/users/VladimirNik/following{/other_user}","gists_url":"https://api.github.com/users/VladimirNik/gists{/gist_id}","starred_url":"https://api.github.com/users/VladimirNik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VladimirNik/subscriptions","organizations_url":"https://api.github.com/users/VladimirNik/orgs","repos_url":"https://api.github.com/users/VladimirNik/repos","events_url":"https://api.github.com/users/VladimirNik/events{/privacy}","received_events_url":"https://api.github.com/users/VladimirNik/received_events","type":"User","site_admin":false},"body":"Old comments, from ContextFactory, I removed it.","created_at":"2014-04-10T17:25:10Z","updated_at":"2014-04-10T17:25:10Z","html_url":"https://github.com/scalareflect/mars/pull/3#discussion_r11495996","pull_request_url":"https://api.github.com/repos/scalareflect/mars/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/scalareflect/mars/pulls/comments/11495996"},"html":{"href":"https://github.com/scalareflect/mars/pull/3#discussion_r11495996"},"pull_request":{"href":"https://api.github.com/repos/scalareflect/mars/pulls/3"}}}}