-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d5e250
commit 2ce2aa9
Showing
11 changed files
with
1,317 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlCommentNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
import dev.vighnesh153.xml.formatter.lexer.Token | ||
|
||
class XmlCommentNode(val comment: Token) : XmlExpression { | ||
override fun toString(indentation: Int): String { | ||
return "${buildIndentation(indentation)}<!-- ${comment.tokenLiteral.trim()} -->" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ols/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlElementAttribute.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
import dev.vighnesh153.xml.formatter.lexer.Token | ||
|
||
class XmlElementAttribute( | ||
val namespaces: List<Token>, | ||
val value: Token, | ||
) { | ||
override fun toString(): String { | ||
val key = namespaces.joinToString(":") { it.tokenLiteral } | ||
return "$key=\"${value.tokenLiteral}\"" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
sealed interface XmlExpression { | ||
fun toString(indentation: Int): String | ||
} |
14 changes: 14 additions & 0 deletions
14
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlProgram.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
class XmlProgram : XmlExpression { | ||
private val mutableStatements = mutableListOf<XmlExpression>() | ||
val statements: List<XmlExpression> | ||
get() = mutableStatements.toList() | ||
|
||
fun addStatement(stmt: XmlExpression) { | ||
mutableStatements.add(stmt) | ||
} | ||
|
||
override fun toString(indentation: Int): String = | ||
mutableStatements.joinToString("\n") { it.toString(indentation) } | ||
} |
23 changes: 23 additions & 0 deletions
23
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlPrologNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
class XmlPrologNode : XmlExpression { | ||
private val mutableAttributes = mutableListOf<XmlElementAttribute>() | ||
val attributes: List<XmlElementAttribute> | ||
get() = mutableAttributes.toList() | ||
|
||
fun addAttribute(attr: XmlElementAttribute) { | ||
mutableAttributes.add(attr) | ||
} | ||
|
||
override fun toString(indentation: Int): String { | ||
val builder = StringBuilder() | ||
builder.append(buildIndentation(indentation)) | ||
builder.append("<?xml") | ||
for (attr in mutableAttributes) { | ||
builder.append(" ") | ||
builder.append(attr.toString()) | ||
} | ||
builder.append("?>") | ||
return builder.toString() | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlTagNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
import dev.vighnesh153.xml.formatter.lexer.Token | ||
|
||
class XmlTagNode( | ||
) : XmlExpression { | ||
private val mutableNamespaces = mutableListOf<Token>() | ||
val namespaces: List<Token> | ||
get() = mutableNamespaces.toList() | ||
|
||
private val mutableAttributes = mutableListOf<XmlElementAttribute>() | ||
val attributes: List<XmlElementAttribute> | ||
get() = mutableAttributes.toList() | ||
|
||
private val mutableChildren = mutableListOf<XmlExpression>() | ||
val children: List<XmlExpression> | ||
get() = mutableChildren.toList() | ||
|
||
fun addNamespace(ns: Token) { | ||
mutableNamespaces.add(ns) | ||
} | ||
|
||
fun addAttribute(attr: XmlElementAttribute) { | ||
mutableAttributes.add(attr) | ||
} | ||
|
||
fun addChild(child: XmlExpression) { | ||
mutableChildren.add(child) | ||
} | ||
|
||
override fun toString(indentation: Int): String { | ||
val builder = mutableListOf<String>() | ||
|
||
val tag = mutableNamespaces.joinToString(":") { it.tokenLiteral } | ||
|
||
builder.add("${buildIndentation(indentation)}<$tag") | ||
if (mutableAttributes.size == 1) { | ||
val serializedAttrs = mutableAttributes.joinToString(" ") { it.toString() } | ||
builder[builder.lastIndex] += " $serializedAttrs" | ||
} else if (mutableAttributes.size > 1) { | ||
for (attr in mutableAttributes) { | ||
builder.add("${buildIndentation(indentation + 1)}$attr") | ||
} | ||
} | ||
|
||
if (mutableChildren.isEmpty()) { | ||
builder[builder.lastIndex] += " />" | ||
} else { | ||
builder[builder.lastIndex] += ">" | ||
} | ||
|
||
// if only a single text node child | ||
if (mutableChildren.size == 1 && mutableChildren.first() is XmlTextNode) { | ||
return builder.joinToString("\n") + mutableChildren.first() | ||
.toString(0) + "</$tag>" | ||
} | ||
|
||
for (child in mutableChildren) { | ||
builder.add(child.toString(indentation + 1)) | ||
} | ||
|
||
if (mutableChildren.isNotEmpty()) { | ||
builder.add("${buildIndentation(indentation)}</$tag>") | ||
} | ||
|
||
return builder.joinToString("\n") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/XmlTextNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
import dev.vighnesh153.xml.formatter.lexer.Token | ||
|
||
class XmlTextNode(val text: Token) : XmlExpression { | ||
override fun toString(indentation: Int): String { | ||
return "${buildIndentation(indentation)}${text.tokenLiteral.trim()}" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
jvm-tools/XmlFormatter/src/main/kotlin/dev/vighnesh153/xml/formatter/ast/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.vighnesh153.xml.formatter.ast | ||
|
||
internal fun buildIndentation(indentation: Int): String = " ".repeat(indentation * 4) |
Oops, something went wrong.