Migrate extend statement parser to Kotlin
This commit is contained in:
20
src/main/kotlin/dsl/ast/model/node/ExtendNode.kt
Normal file
20
src/main/kotlin/dsl/ast/model/node/ExtendNode.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class ExtendNode(type: Node, identifier: Node, functions: Node, position: TokenPosition) : Node(3, position) {
|
||||
val type: Node
|
||||
get() = children[0]
|
||||
|
||||
val identifier: Node
|
||||
get() = children[1]
|
||||
|
||||
val functions: Node
|
||||
get() = children[2]
|
||||
|
||||
init {
|
||||
children[0] = type
|
||||
children[1] = identifier
|
||||
children[2] = functions
|
||||
}
|
||||
}
|
||||
40
src/main/kotlin/dsl/ast/parser/ExtendParser.kt
Normal file
40
src/main/kotlin/dsl/ast/parser/ExtendParser.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package dsl.ast.parser
|
||||
|
||||
import dsl.ast.model.entity.ParserOutput
|
||||
import dsl.ast.model.node.BlockNode
|
||||
import dsl.ast.model.node.ExtendNode
|
||||
import dsl.ast.model.node.Node
|
||||
import dsl.token.model.entity.TokenList
|
||||
import dsl.token.model.enumeration.TokenType
|
||||
|
||||
class ExtendParser : Parser() {
|
||||
override fun tryToParse(input: TokenList): ParserOutput {
|
||||
val simpleExtendParser = allOf(
|
||||
terminal(TokenType.EXTEND),
|
||||
assert(TypeParser(), "type to be extended"),
|
||||
assert(terminal(TokenType.AS), "'as' keyword with identifier"),
|
||||
assert(SimpleIdentifierParser(), "identifier"),
|
||||
terminal(TokenType.WITH),
|
||||
assert(mapNode(FunctionDefinitionParser()) { BlockNode(Node.NONE, listOf(it), Node.NONE) }, "method definition")
|
||||
) {
|
||||
ExtendNode(it[1], it[3], it[5], it[0].position)
|
||||
}
|
||||
|
||||
val complexExtendParser = allOf(
|
||||
terminal(TokenType.EXTEND),
|
||||
assert(TypeParser(), "type to be extended"),
|
||||
assert(terminal(TokenType.AS), "'as' keyword with identifier"),
|
||||
assert(SimpleIdentifierParser(), "identifier"),
|
||||
assert(loop(terminal(TokenType.OPEN_CURLY), assert(FunctionDefinitionParser(), "method definition or }"), terminal(TokenType.CLOSE_CURLY)) {
|
||||
begin, methods, end -> BlockNode(begin, methods, end)
|
||||
}, "block with methods' definitions or 'with' keyword with single method definition")
|
||||
) {
|
||||
ExtendNode(it[1], it[3], it[4], it[0].position)
|
||||
}
|
||||
|
||||
return oneOf(
|
||||
simpleExtendParser,
|
||||
complexExtendParser
|
||||
).parse(input)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user