Quoted atoms

This commit is contained in:
Tibo De Peuter 2025-04-30 12:08:36 +02:00
parent 1e087c8339
commit 43b364044e
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
5 changed files with 65 additions and 32 deletions

View file

@ -7,7 +7,6 @@ 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.ArithmeticOperator
import prolog.ast.arithmetic.Expression
import prolog.ast.arithmetic.Float
import prolog.ast.arithmetic.Integer
@ -15,9 +14,16 @@ import prolog.ast.logic.LogicOperand
import prolog.ast.terms.*
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 simpleAtom: Parser<Atom> by nameToken use { Atom(text) }
protected val quotedAtom: Parser<Atom> by (dummy
or ticked
or doubleTicked
or backTicked
) use { Atom(text.substring(1, text.length - 1)) }
protected val atom: Parser<Atom> by (quotedAtom or simpleAtom)
protected val compound: Parser<Structure> by (atom * -leftParenthesis * separated(
parser(::term),
comma,

View file

@ -30,5 +30,10 @@ abstract class Tokens : Grammar<Any>() {
protected val singleLineComment: Token by regexToken("%[^\\n]*", ignore = true)
protected val multiLineComment: Token by regexToken("/\\*.*?\\*/", ignore = true)
protected val ticked: Token by regexToken("'[^']*'")
protected val doubleTicked: Token by regexToken("\"[^\"]*\"")
protected val backTicked: Token by regexToken("`[^`]*`")
// Helper
protected val dummy by token { _, _ -> -1 } use { throw IllegalStateException("This parser should not be used") }
}