Rework parsing structure
This commit is contained in:
parent
a4ec29f084
commit
b9f419a59d
17 changed files with 246 additions and 278 deletions
74
src/parser/grammars/TermsGrammar.kt
Normal file
74
src/parser/grammars/TermsGrammar.kt
Normal file
|
@ -0,0 +1,74 @@
|
|||
package parser.grammars
|
||||
|
||||
import com.github.h0tk3y.betterParse.combinators.or
|
||||
import com.github.h0tk3y.betterParse.combinators.separated
|
||||
import com.github.h0tk3y.betterParse.combinators.times
|
||||
import com.github.h0tk3y.betterParse.combinators.unaryMinus
|
||||
import com.github.h0tk3y.betterParse.combinators.use
|
||||
import com.github.h0tk3y.betterParse.grammar.parser
|
||||
import com.github.h0tk3y.betterParse.parser.Parser
|
||||
import prolog.ast.arithmetic.Float
|
||||
import prolog.ast.arithmetic.Integer
|
||||
import prolog.ast.logic.LogicOperand
|
||||
import prolog.ast.logic.LogicOperator
|
||||
import prolog.ast.terms.Atom
|
||||
import prolog.ast.terms.Body
|
||||
import prolog.ast.terms.Head
|
||||
import prolog.ast.terms.Operator
|
||||
import prolog.ast.terms.Structure
|
||||
import prolog.ast.terms.Term
|
||||
import prolog.ast.terms.Variable
|
||||
import prolog.builtins.Conjunction
|
||||
|
||||
open class TermsGrammar : Tokens() {
|
||||
// Basic named terms
|
||||
protected val variable: Parser<Variable> by variableToken use { Variable(text) }
|
||||
protected val atom: Parser<Atom> by nameToken use { Atom(text) }
|
||||
protected val compound: Parser<Structure> by (atom * -leftParenthesis * separated(
|
||||
parser(::term),
|
||||
comma,
|
||||
acceptZero = true
|
||||
) * -rightParenthesis) use {
|
||||
Structure(t1, t2.terms)
|
||||
}
|
||||
|
||||
// Basic arithmetic
|
||||
protected val int: Parser<Integer> by integerToken use { Integer(text.toInt()) }
|
||||
protected val float: Parser<Float> by floatToken use { Float(text.toFloat()) }
|
||||
|
||||
// Operators
|
||||
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 logicOperator: Parser<LogicOperator> by (simpleLogicOperand * -comma * logicOperand) use {
|
||||
Conjunction(t1, t2)
|
||||
}
|
||||
protected val operator: Parser<Operator> by (dummy
|
||||
or logicOperator
|
||||
)
|
||||
|
||||
// Parts
|
||||
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 }
|
||||
|
||||
protected val term: Parser<Term> by (dummy
|
||||
or float
|
||||
or int
|
||||
or variable
|
||||
or compound
|
||||
or atom
|
||||
)
|
||||
|
||||
override val rootParser: Parser<Any> by term
|
||||
}
|
Reference in a new issue