mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-14 04:24:05 +09:00
WIP new UI elem
This commit is contained in:
118
src/net/torvald/terrarum/Yaml.kt
Normal file
118
src/net/torvald/terrarum/Yaml.kt
Normal file
@@ -0,0 +1,118 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Simplified version of YAML, only for the representation of a text tree.
|
||||
*
|
||||
* Example code:
|
||||
* ```
|
||||
* - File
|
||||
* - New : Ctrl-N
|
||||
* - Open : Ctrl-O
|
||||
* - Open Recent
|
||||
* - yaml_example.yaml
|
||||
* - Yaml.kt
|
||||
* - Close : Ctrl-W
|
||||
* - Settings
|
||||
* - Line Separators
|
||||
* - CRLF
|
||||
* - CR
|
||||
* - LF
|
||||
* - Edit
|
||||
* - Undo : Ctrl-Z
|
||||
* - Redo : Shift-Ctrl-Z
|
||||
* - Cut : Ctrl-X
|
||||
* - Copy : Ctrl-C
|
||||
* - Paste : Ctrl-V
|
||||
* - Find
|
||||
* - Find : Ctrl-F
|
||||
* - Replace : Shift-Ctrl-F
|
||||
* - Convert Indents
|
||||
* - To Spaces
|
||||
* - Set Project Indentation
|
||||
* - To Tabs
|
||||
* - Refactor
|
||||
* - Refactor This
|
||||
* - Rename : Shift-Ctrl-R
|
||||
* - Extract
|
||||
* - Variable
|
||||
* - Property
|
||||
* - Function
|
||||
* ```
|
||||
*
|
||||
* - All lines are indented with one space
|
||||
* - All entries are preceded by '- ' (dash and a space)
|
||||
* - All propery are separated by ' : ' (space colon space)
|
||||
*
|
||||
* Any deviation to the above rule will cause a parse failure, because it's simple and dumb as that.
|
||||
*
|
||||
* Created by minjaesong on 2018-12-08.
|
||||
*/
|
||||
inline class Yaml(val text: String) {
|
||||
|
||||
companion object {
|
||||
val SEPARATOR = Regex(" : ")
|
||||
}
|
||||
|
||||
|
||||
fun parse(): QNDTreeNode<String> {
|
||||
var currentIndentLevel = -1
|
||||
val root = QNDTreeNode<String>()
|
||||
var currentNode = root
|
||||
val nodesStack = Stack<QNDTreeNode<String>>()
|
||||
|
||||
nodesStack.push(currentNode)
|
||||
|
||||
text.split('\n') .forEach {
|
||||
val indentLevel = it.countSpaces()
|
||||
val it = it.trimIndent()
|
||||
if (it.startsWith("- ")) {
|
||||
val nodeName = it.drop(2)
|
||||
|
||||
if (indentLevel == currentIndentLevel) {
|
||||
val sibling = QNDTreeNode(nodeName, currentNode.parent)
|
||||
currentNode.parent!!.children.add(sibling)
|
||||
currentNode = sibling
|
||||
}
|
||||
else if (indentLevel > currentIndentLevel) {
|
||||
val childNode = QNDTreeNode(nodeName, currentNode)
|
||||
currentNode.children.add(childNode)
|
||||
nodesStack.push(currentNode)
|
||||
currentNode = childNode
|
||||
currentIndentLevel = indentLevel
|
||||
}
|
||||
else {
|
||||
repeat(currentIndentLevel - indentLevel) { currentNode = nodesStack.pop() }
|
||||
currentIndentLevel = indentLevel
|
||||
val sibling = QNDTreeNode(nodeName, currentNode.parent)
|
||||
currentNode.parent!!.children.add(sibling)
|
||||
currentNode = sibling
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// test traverse resulting tree
|
||||
/*root.traversePreorder { node, depth ->
|
||||
repeat(depth + 1) { print("-") }
|
||||
println("${node.data} -> ${node.parent}")
|
||||
}*/
|
||||
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
private fun String.countSpaces(): Int {
|
||||
var c = 0
|
||||
while (c <= this.length) {
|
||||
if (this[c] == ' ')
|
||||
c++
|
||||
else
|
||||
break
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user