Fix attribution of tokens.

This commit is contained in:
2023-09-02 17:01:56 -07:00
parent 941b201549
commit 62ba662a91
6 changed files with 81 additions and 28 deletions

View File

@ -0,0 +1,27 @@
package gay.pizza.pork.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.types.path
import gay.pizza.pork.ast.NodeCoalescer
import gay.pizza.pork.frontend.FileFrontend
import gay.pizza.pork.parse.TokenNodeAttribution
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
val path by argument("file").path(mustExist = true, canBeDir = false)
override fun run() {
val frontend = FileFrontend(path)
val attribution = TokenNodeAttribution()
val program = frontend.parse(attribution)
val coalescer = NodeCoalescer { node ->
val tokens = attribution.assembleTokens(node)
println("node ${node.toString().replace("\n", "^")}")
for (token in tokens) {
println("token $token")
}
}
coalescer.visit(program)
}
}

View File

@ -14,6 +14,7 @@ class RootCommand : CliktCommand(
TokenizeCommand(),
ReprintCommand(),
AstCommand(),
AttributeCommand(),
GenerateKotlinCommand(),
GenerateDartCommand()
)