2023-09-04 01:56:24 -07:00
|
|
|
package gay.pizza.pork.tool
|
2023-08-21 23:08:56 -07:00
|
|
|
|
|
|
|
import com.github.ajalt.clikt.core.CliktCommand
|
|
|
|
import com.github.ajalt.clikt.parameters.arguments.argument
|
2023-09-05 14:04:39 -07:00
|
|
|
import gay.pizza.dough.fs.PlatformFsProvider
|
2023-09-11 02:34:28 -04:00
|
|
|
import gay.pizza.pork.minimal.FileTool
|
2023-08-21 23:08:56 -07:00
|
|
|
|
2023-09-02 20:22:08 -07:00
|
|
|
class TokenizeCommand : CliktCommand(help = "Tokenize Compilation Unit", name = "tokenize") {
|
2023-09-05 14:04:39 -07:00
|
|
|
val path by argument("file")
|
2023-08-21 23:08:56 -07:00
|
|
|
|
|
|
|
override fun run() {
|
2023-09-05 14:04:39 -07:00
|
|
|
val tool = FileTool(PlatformFsProvider.resolve(path))
|
2023-09-03 01:11:27 -07:00
|
|
|
val tokenStream = tool.tokenize()
|
2023-08-21 23:08:56 -07:00
|
|
|
for (token in tokenStream.tokens) {
|
2023-09-18 01:07:28 -07:00
|
|
|
println("${token.sourceIndex.index} ${token.type.name} '${sanitize(token.text)}'")
|
2023-08-21 23:08:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sanitize(input: String): String =
|
|
|
|
input
|
|
|
|
.replace("\n", "\\n")
|
|
|
|
.replace("\r", "\\r")
|
|
|
|
}
|