package parser

import com.github.h0tk3y.betterParse.combinators.use
import com.github.h0tk3y.betterParse.grammar.Grammar
import com.github.h0tk3y.betterParse.grammar.parseToEnd
import com.github.h0tk3y.betterParse.parser.Parser
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import parser.grammars.TermsGrammar
import prolog.ast.terms.Atom
import prolog.ast.terms.CompoundTerm
import prolog.ast.terms.Operator
import prolog.ast.terms.Structure

class OperatorParserTests {
    class OperatorParser: TermsGrammar() {
        override val rootParser: Parser<CompoundTerm> by body use { this as CompoundTerm }
    }

    private var parser = OperatorParser() as Grammar<CompoundTerm>

    @Test
    fun `parse conjunction`() {
        val input = "a, b"

        val result = parser.parseToEnd(input)

        assertEquals(Structure(Atom(","), listOf(Atom("a"), Atom("b"))), result, "Expected atom 'a, b'")
    }

    class BodyParser : TermsGrammar() {
        override val rootParser: Parser<Any> by body
    }

    @Test
    fun `parse equality`() {
        val input = "a = b"

        val result = BodyParser().parseToEnd(input)

        assertEquals(Structure(Atom("="), listOf(Atom("a"), Atom("b"))), result, "Expected atom 'a = b'")
    }
}