Migrate abstract iterable parser to Kotlin
This commit is contained in:
@@ -3,7 +3,8 @@ package dsl.ast.model.node
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
protected var children: MutableList<Any> = MutableList(numberOfChildren) { NONE }
|
||||
var children: MutableList<Any> = MutableList(numberOfChildren) { NONE }
|
||||
protected set
|
||||
|
||||
constructor(children: List<Any>, position: TokenPosition) : this(children.size, position) {
|
||||
this.children = children.toMutableList()
|
||||
@@ -22,12 +23,12 @@ abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
|
||||
println(newPrefix + (if (first) "" else if (newLast) "└─" else "├─") + nodeName + " " + position)
|
||||
newPrefix += if (newLast) " " else "│ "
|
||||
for((index, child) in children.withIndex()) {
|
||||
for ((index, child) in children.withIndex()) {
|
||||
newLast = index == children.size - 1
|
||||
if(child is Node) {
|
||||
if (child is Node) {
|
||||
child.pretty(newPrefix, newLast, false)
|
||||
} else {
|
||||
println(newPrefix + (if(newLast) "└ " else "├ ") + child)
|
||||
println(newPrefix + (if (newLast) "└ " else "├ ") + child)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
44
src/main/kotlin/dsl/ast/parser/AbstractIterableParser.kt
Normal file
44
src/main/kotlin/dsl/ast/parser/AbstractIterableParser.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package dsl.ast.parser
|
||||
|
||||
import dsl.ast.model.entity.ParserOutput
|
||||
import dsl.ast.model.node.Node
|
||||
import dsl.token.model.entity.TokenList
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
import dsl.token.model.enumeration.TokenType
|
||||
|
||||
class AbstractIterableNode(beginNode: Node, items: List<Node>) : Node(items, beginNode.position)
|
||||
|
||||
abstract class AbstractIterableParser(
|
||||
private val beginTokenType: TokenType,
|
||||
private val itemParser: Parser,
|
||||
private val endTokenType: TokenType,
|
||||
private val createNode: (List<Node>, TokenPosition) -> Node
|
||||
) : Parser() {
|
||||
override fun tryToParse(input: TokenList): ParserOutput {
|
||||
val emptyIterableParser = allOf(listOf(terminal(beginTokenType), terminal(endTokenType))) { object : Node(0, it[0].position) {} }
|
||||
|
||||
val nonEmptyIterableParser = allOf(
|
||||
listOf(terminal(beginTokenType),
|
||||
allOf(listOf(
|
||||
itemParser,
|
||||
optional(repeat(allOf(
|
||||
listOf(
|
||||
terminal(TokenType.COMMA),
|
||||
itemParser
|
||||
)
|
||||
) { it[1] }) { list, position -> object : Node(list, position) {} }
|
||||
))) { list ->
|
||||
object : Node(listOf(list[0]) + list[1].children, TokenPosition.NONE) {} },
|
||||
terminal(endTokenType)
|
||||
)
|
||||
) { createNode(it[1].children.toList() as List<Node>, it[0].position) }
|
||||
|
||||
return oneOf(
|
||||
listOf(
|
||||
emptyIterableParser,
|
||||
nonEmptyIterableParser
|
||||
)
|
||||
).parse(input)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user