Last active
December 17, 2015 04:59
-
-
Save jugyo/5554748 to your computer and use it in GitHub Desktop.
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
package foo { | |
import bar._; | |
object HelloWorld extends scala.ScalaObject { | |
def <init>() = { | |
super.<init>(); | |
() | |
}; | |
def main(args: Array[String]): scala.Unit = println("Hello, world!") | |
} | |
} | |
---- | |
PackageDef | |
Ident | |
Import | |
Ident | |
ModuleDef | |
Template | |
Select | |
Ident | |
emptyValDef$ | |
TypeTree | |
DefDef | |
TypeTree | |
Block | |
Apply | |
Select | |
Super | |
This | |
Literal | |
DefDef | |
ValDef | |
AppliedTypeTree | |
Ident | |
Ident | |
Select | |
Ident | |
Apply | |
Ident | |
Literal |
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
import scala.tools.nsc.{ast, Settings} | |
import scala.tools.nsc.interpreter._ | |
import scala.tools.nsc.util.BatchSourceFile | |
class ScalaParser { | |
val settings = new Settings() | |
settings.usejavacp.value = true | |
val main = new IMain(settings) | |
import main.global._ | |
import syntaxAnalyzer._ | |
def parse(code: String) = { | |
val parser = new SourceFileParser(new BatchSourceFile("<console>", code)) | |
parser.compilationUnit() | |
} | |
object traverser { | |
def traverse(trees: List[Tree], level: Int)(printTree: (Tree, Int) => Unit): Unit = { | |
for (tree <- trees) { | |
traverse(tree, level){printTree} | |
} | |
} | |
def traverse(tree: Tree, level: Int)(printTree: (Tree, Int) => Unit): Unit = { | |
printTree(tree, level) | |
traverse(tree.children, level + 1){printTree} | |
} | |
} | |
class TreePrinter { | |
def print(trees: List[Tree]): Unit = { | |
def printTree(tree: Tree, nest: Int) { | |
val indent = " " * nest | |
println(indent + tree.getClass.getSimpleName()) | |
} | |
traverser.traverse(trees, 0){printTree} | |
} | |
def print(tree: Tree): Unit = print(List(tree)) | |
} | |
} |
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
object ScalaParserMain { | |
def main(args: Array[String]) { | |
// TODO: deal a file via args | |
val code = | |
""" | |
|package foo | |
|import bar._ | |
|object HelloWorld { | |
| def main(args: Array[String]) { | |
| println("Hello, world!") | |
| } | |
|} | |
""".stripMargin | |
val parser = new ScalaParser() | |
val tree = parser.parse(code) | |
val printer = new parser.TreePrinter() | |
println(tree) | |
println("----") | |
printer.print(tree) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment