mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
parser: various code and quality enhancements
This commit is contained in:
@ -8,6 +8,8 @@ plugins {
|
||||
dependencies {
|
||||
api(project(":minimal"))
|
||||
api("com.github.ajalt.clikt:clikt:4.2.0")
|
||||
|
||||
implementation(project(":common"))
|
||||
}
|
||||
|
||||
application {
|
||||
|
@ -2,14 +2,19 @@ package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.options.flag
|
||||
import com.github.ajalt.clikt.parameters.options.option
|
||||
import gay.pizza.dough.fs.PlatformFsProvider
|
||||
import gay.pizza.pork.ast.gen.Node
|
||||
import gay.pizza.pork.ast.gen.NodeCoalescer
|
||||
import gay.pizza.pork.ast.gen.visit
|
||||
import gay.pizza.pork.common.IndentPrinter
|
||||
import gay.pizza.pork.minimal.FileTool
|
||||
import gay.pizza.pork.parser.ParserAttributes
|
||||
import gay.pizza.pork.parser.ParserNodeAttribution
|
||||
|
||||
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
|
||||
val hierarchical by option("--hierarchical", help = "Print Hierarchical Output").flag(default = true)
|
||||
val path by argument("file")
|
||||
|
||||
override fun run() {
|
||||
@ -17,13 +22,27 @@ class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute"
|
||||
val attribution = ParserNodeAttribution()
|
||||
val compilationUnit = tool.parse(attribution)
|
||||
|
||||
val coalescer = NodeCoalescer { node ->
|
||||
val allTokens = ParserAttributes.recallAllTokens(node)
|
||||
println("node ${node.type.name}")
|
||||
for (token in allTokens) {
|
||||
println("token $token")
|
||||
if (hierarchical) {
|
||||
val output = IndentPrinter()
|
||||
fun visit(node: Node) {
|
||||
output.emitIndentedLine("${node.type.name} ->")
|
||||
output.indented {
|
||||
for (token in ParserAttributes.recallOwnedTokens(node)) {
|
||||
output.emitIndentedLine(token.toString())
|
||||
}
|
||||
node.visitChildren(NodeCoalescer(followChildren = false, handler = ::visit))
|
||||
}
|
||||
}
|
||||
visit(compilationUnit)
|
||||
} else {
|
||||
val coalescer = NodeCoalescer { node ->
|
||||
val allTokens = ParserAttributes.recallAllTokens(node)
|
||||
println("node ${node.type.name}")
|
||||
for (token in allTokens) {
|
||||
println("token $token")
|
||||
}
|
||||
}
|
||||
coalescer.visit(compilationUnit)
|
||||
}
|
||||
coalescer.visit(compilationUnit)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user