Add grammar compatibility option to the formatter (#1249)

This commit is contained in:
Islon Scherer
2025-10-28 13:29:08 +01:00
committed by GitHub
parent ef4989aa35
commit be0142d46b
9 changed files with 108 additions and 16 deletions

View File

@@ -33,7 +33,7 @@ import org.pkl.parser.syntax.Operator
import org.pkl.parser.syntax.generic.Node
import org.pkl.parser.syntax.generic.NodeType
internal class Builder(sourceText: String) {
internal class Builder(sourceText: String, private val grammarVersion: GrammarVersion) {
private var id: Int = 0
private val source: CharArray = sourceText.toCharArray()
private var prevNode: Node? = null
@@ -542,7 +542,11 @@ internal class Builder(sourceText: String) {
if (prev.isTerminal("(") || next.isTerminal(")")) {
if (next.isTerminal(")")) {
// trailing comma
ifWrap(groupId, nodes(Text(","), line()), line())
if (grammarVersion == GrammarVersion.V1) {
line()
} else {
ifWrap(groupId, nodes(Text(","), line()), line())
}
} else line()
} else spaceOrLine()
}
@@ -561,7 +565,11 @@ internal class Builder(sourceText: String) {
val node = if (hasTrailingLambda) Empty else line()
if (next.isTerminal(")") && !hasTrailingLambda) {
// trailing comma
ifWrap(groupId, nodes(Text(","), node), node)
if (grammarVersion == GrammarVersion.V1) {
node
} else {
ifWrap(groupId, nodes(Text(","), node), node)
}
} else node
} else spaceOrLine()
},
@@ -664,7 +672,11 @@ internal class Builder(sourceText: String) {
if (prev.isTerminal("<") || next.isTerminal(">")) {
if (next.isTerminal(">")) {
// trailing comma
ifWrap(id, nodes(Text(","), line()), line())
if (grammarVersion == GrammarVersion.V1) {
Line
} else {
ifWrap(id, nodes(Text(","), line()), line())
}
} else line()
} else spaceOrLine()
}

View File

@@ -27,22 +27,24 @@ class Formatter {
* Formats a Pkl file from the given file path.
*
* @param path the path to the Pkl file to format
* @param grammarVersion grammar compatibility version
* @return the formatted Pkl source code as a string
* @throws java.io.IOException if the file cannot be read
*/
fun format(path: Path): String {
return format(Files.readString(path))
fun format(path: Path, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
return format(Files.readString(path), grammarVersion)
}
/**
* Formats the given Pkl source code text.
*
* @param text the Pkl source code to format
* @param grammarVersion grammar compatibility version
* @return the formatted Pkl source code as a string
*/
fun format(text: String): String {
fun format(text: String, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
val parser = GenericParser()
val builder = Builder(text)
val builder = Builder(text, grammarVersion)
val gen = Generator()
val ast = parser.parseModule(text)
val formatAst = builder.format(ast)
@@ -51,3 +53,13 @@ class Formatter {
return gen.toString()
}
}
/** Grammar compatibility version. */
enum class GrammarVersion(val version: Int, val versionSpan: String) {
V1(1, "0.25 - 0.29"),
V2(2, "0.30+");
companion object {
fun latest(): GrammarVersion = entries.maxBy { it.version }
}
}