48 lines
No EOL
1.6 KiB
Kotlin
48 lines
No EOL
1.6 KiB
Kotlin
package better_parser
|
|
|
|
import com.github.h0tk3y.betterParse.combinators.*
|
|
import com.github.h0tk3y.betterParse.grammar.parser
|
|
import com.github.h0tk3y.betterParse.lexer.literalToken
|
|
import com.github.h0tk3y.betterParse.parser.Parser
|
|
import prolog.ast.arithmetic.ArithmeticOperator
|
|
import prolog.ast.logic.*
|
|
import prolog.ast.terms.*
|
|
import prolog.builtins.Conjunction
|
|
import prolog.builtins.Disjunction
|
|
|
|
open class SimpleSourceParser : SimplePrologParser() {
|
|
protected val simpleLogicOperand: Parser<LogicOperand> by (dummy
|
|
or compound
|
|
or atom
|
|
)
|
|
protected val logicOperand: Parser<LogicOperand> by (dummy
|
|
or parser(::operator)
|
|
or simpleLogicOperand
|
|
)
|
|
|
|
protected val arithmeticOperator: Parser<ArithmeticOperator> by dummy
|
|
protected val logicOperator: Parser<LogicOperator> by (simpleLogicOperand * comma * logicOperand) use {
|
|
Conjunction(t1, t3)
|
|
}
|
|
|
|
protected val operator: Parser<Operator> by (arithmeticOperator or logicOperator) use { this as Operator }
|
|
|
|
protected val head: Parser<Head> by (dummy
|
|
or compound
|
|
or atom
|
|
)
|
|
protected val body: Parser<Body> by (dummy
|
|
or operator
|
|
or head
|
|
) use { this as Body }
|
|
|
|
// ----
|
|
|
|
private val rule: Parser<Rule> by (head * -neck * body) use { Rule(t1, t2) }
|
|
private val fact: Parser<Fact> by head use { Fact(this) }
|
|
|
|
private val clause: Parser<Clause> by ((rule or fact) * -dot)
|
|
private val clauses: Parser<List<Clause>> by zeroOrMore(clause)
|
|
|
|
override val rootParser: Parser<Any> by clauses
|
|
} |