mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
Enhanced indention support.
This commit is contained in:
parent
62ba662a91
commit
04c78c35e0
@ -1,23 +1,24 @@
|
||||
package gay.pizza.pork.ast
|
||||
|
||||
import gay.pizza.pork.ast.nodes.*
|
||||
import gay.pizza.pork.util.IndentPrinter
|
||||
import gay.pizza.pork.util.StringEscape
|
||||
|
||||
class Printer(private val buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
private var indent = 0
|
||||
class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
private val out = IndentPrinter(buffer)
|
||||
private var autoIndentState = false
|
||||
|
||||
private fun append(text: String) {
|
||||
buffer.append(text)
|
||||
if (autoIndentState) {
|
||||
out.emitIndent()
|
||||
autoIndentState = false
|
||||
}
|
||||
out.append(text)
|
||||
}
|
||||
|
||||
private fun appendLine() {
|
||||
buffer.appendLine()
|
||||
}
|
||||
|
||||
private fun indent() {
|
||||
repeat(indent) {
|
||||
append(" ")
|
||||
}
|
||||
out.appendLine()
|
||||
autoIndentState = true
|
||||
}
|
||||
|
||||
override fun visitIntLiteral(node: IntLiteral) {
|
||||
@ -40,11 +41,17 @@ class Printer(private val buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
|
||||
override fun visitListLiteral(node: ListLiteral) {
|
||||
append("[")
|
||||
for ((index, item) in node.items.withIndex()) {
|
||||
visit(item)
|
||||
if (index != node.items.size - 1) {
|
||||
append(", ")
|
||||
if (node.items.isNotEmpty()) {
|
||||
out.increaseIndent()
|
||||
appendLine()
|
||||
for ((index, item) in node.items.withIndex()) {
|
||||
visit(item)
|
||||
if (index != node.items.size - 1) {
|
||||
append(",")
|
||||
}
|
||||
appendLine()
|
||||
}
|
||||
out.decreaseIndent()
|
||||
}
|
||||
append("]")
|
||||
}
|
||||
@ -91,18 +98,16 @@ class Printer(private val buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
append(" ")
|
||||
}
|
||||
append("in")
|
||||
indent++
|
||||
out.increaseIndent()
|
||||
for (expression in node.expressions) {
|
||||
appendLine()
|
||||
indent()
|
||||
visit(expression)
|
||||
}
|
||||
|
||||
if (node.expressions.isNotEmpty()) {
|
||||
appendLine()
|
||||
}
|
||||
indent--
|
||||
indent()
|
||||
out.decreaseIndent()
|
||||
append("}")
|
||||
}
|
||||
|
||||
@ -120,11 +125,18 @@ class Printer(private val buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
override fun visitIf(node: If) {
|
||||
append("if ")
|
||||
visit(node.condition)
|
||||
append(" then ")
|
||||
append(" then")
|
||||
out.increaseIndent()
|
||||
appendLine()
|
||||
visit(node.thenExpression)
|
||||
out.decreaseIndent()
|
||||
if (node.elseExpression != null) {
|
||||
append(" else ")
|
||||
appendLine()
|
||||
append("else")
|
||||
out.increaseIndent()
|
||||
appendLine()
|
||||
visit(node.elseExpression)
|
||||
out.decreaseIndent()
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +150,7 @@ class Printer(private val buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
|
||||
override fun visitProgram(node: Program) {
|
||||
for (expression in node.expressions) {
|
||||
indent()
|
||||
out.emitIndent()
|
||||
visit(expression)
|
||||
appendLine()
|
||||
}
|
||||
|
25
src/main/kotlin/gay/pizza/pork/util/IndentPrinter.kt
Normal file
25
src/main/kotlin/gay/pizza/pork/util/IndentPrinter.kt
Normal file
@ -0,0 +1,25 @@
|
||||
package gay.pizza.pork.util
|
||||
|
||||
class IndentPrinter(
|
||||
val buffer: StringBuilder = StringBuilder(),
|
||||
val indent: String = " "
|
||||
) : Appendable by buffer, CharSequence by buffer {
|
||||
private var indentLevel: Int = 0
|
||||
private var indentLevelText: String = ""
|
||||
|
||||
fun emitIndent() {
|
||||
append(indentLevelText)
|
||||
}
|
||||
|
||||
fun increaseIndent() {
|
||||
indentLevel++
|
||||
indentLevelText = indent.repeat(indentLevel)
|
||||
}
|
||||
|
||||
fun decreaseIndent() {
|
||||
indentLevel--
|
||||
indentLevelText = indent.repeat(indentLevel)
|
||||
}
|
||||
|
||||
override fun toString(): String = buffer.toString()
|
||||
}
|
Loading…
Reference in New Issue
Block a user