<commit_before>package org.plavelo.parser

sealed class Either<out L, out R> {
    abstract fun left(): L
    abstract fun right(): R
    open fun isLeft(): Boolean = false
    open fun isRight(): Boolean = false

    class Left<out L, out R>(private val l: L) : Either<L, R>() {
        override fun isLeft() = true
        override fun left(): L = l
        override fun right(): Nothing = throw RuntimeException()
    }

    class Right<out L, out R>(private val r: R) : Either<L, R>() {
        override fun isRight() = true
        override fun left(): Nothing = throw RuntimeException()
        override fun right(): R = r
    }
}

sealed class Value {
    abstract fun value(): Any
    abstract fun wrapped(): Collection<Any>
    class Empty : Value() {
        override fun value() = emptyList<Any>()
        override fun wrapped(): Collection<Any> = emptyList()
    }

    class Single(private val value: Any) : Value() {
        override fun value() = value
        override fun wrapped() = listOf(value)
    }

    class Multiple(private val value: Collection<Any>) : Value() {
        override fun value() = value
        override fun wrapped() = value
    }
}

sealed class Reply {
    abstract fun position(): Int
    abstract fun raw(): Value
    abstract fun value(): Any
    abstract fun farthest(): Int
    abstract fun expected(): Collection<String>
}

class Success(private val position: Int, private val value: Value) : Reply() {
    override fun position(): Int = position
    override fun raw(): Value = value
    override fun value(): Any = when (value) {
        is Value.Single -> value.value()
        is Value.Multiple -> value.value()
        is Value.Empty -> value
    }

    override fun farthest(): Int = -1
    override fun expected(): Collection<String> = emptyList()
}

class Failure(private val position: Int, private val expected: Collection<String>) : Reply() {
    override fun position(): Int = position
    override fun raw(): Value = throw RuntimeException()
    override fun value(): Any = throw RuntimeException()
    override fun farthest(): Int = position
    override fun expected(): Collection<String> = expected
}

fun Either<Failure, Success>.reply(): Reply = if (this.isRight()) this.right() else this.left()

private fun mergeReplies(reply: Either<Failure, Success>, last: Either<Failure, Success>?): Either<Failure, Success> {
    if (last == null) {
        return reply
    }
    if (reply.reply().farthest() > last.reply().farthest()) {
        return reply
    }
    if (reply.isRight()) {
        return Either.Right(Success(reply.reply().position(), reply.reply().raw()))
    }
    val expected = if (reply.reply().farthest() == last.reply().farthest()) {
        reply.reply().expected().union(last.reply().expected())
    } else {
        last.reply().expected()
    }
    return Either.Left(Failure(last.reply().position(), expected))
}

class Parser(private var action: (source: String, position: Int) -> Either<Failure, Success>) {
<commit_msg>changed the Reply class to interface<commit_after>package org.plavelo.parser

sealed class Either<out L, out R> {
    abstract fun left(): L
    abstract fun right(): R
    open fun isLeft(): Boolean = false
    open fun isRight(): Boolean = false

    class Left<out L, out R>(private val l: L) : Either<L, R>() {
        override fun isLeft() = true
        override fun left(): L = l
        override fun right(): Nothing = throw RuntimeException()
    }

    class Right<out L, out R>(private val r: R) : Either<L, R>() {
        override fun isRight() = true
        override fun left(): Nothing = throw RuntimeException()
        override fun right(): R = r
    }
}

sealed class Value {
    abstract fun value(): Any
    abstract fun wrapped(): Collection<Any>
    class Empty : Value() {
        override fun value() = emptyList<Any>()
        override fun wrapped(): Collection<Any> = emptyList()
    }

    class Single(private val value: Any) : Value() {
        override fun value() = value
        override fun wrapped() = listOf(value)
    }

    class Multiple(private val value: Collection<Any>) : Value() {
        override fun value() = value
        override fun wrapped() = value
    }
}

interface Reply {
    fun position(): Int
    fun raw(): Value
    fun value(): Any
    fun farthest(): Int
    fun expected(): Collection<String>
}

class Success(private val position: Int, private val value: Value) : Reply {
    override fun position(): Int = position
    override fun raw(): Value = value
    override fun value(): Any = when (value) {
        is Value.Single -> value.value()
        is Value.Multiple -> value.value()
        is Value.Empty -> value
    }
    override fun farthest(): Int = -1
    override fun expected(): Collection<String> = emptyList()
}

class Failure(private val position: Int, private val expected: Collection<String>) : Reply {
    override fun position(): Int = position
    override fun raw(): Value = throw RuntimeException()
    override fun value(): Any = throw RuntimeException()
    override fun farthest(): Int = position
    override fun expected(): Collection<String> = expected
}

fun Either<Failure, Success>.reply(): Reply = if (this.isRight()) this.right() else this.left()

private fun mergeReplies(reply: Either<Failure, Success>, last: Either<Failure, Success>?): Either<Failure, Success> {
    if (last == null) {
        return reply
    }
    if (reply.reply().farthest() > last.reply().farthest()) {
        return reply
    }
    if (reply.isRight()) {
        return Either.Right(Success(reply.reply().position(), reply.reply().raw()))
    }
    val expected = if (reply.reply().farthest() == last.reply().farthest()) {
        reply.reply().expected().union(last.reply().expected())
    } else {
        last.reply().expected()
    }
    return Either.Left(Failure(last.reply().position(), expected))
}

class Parser(private var action: (source: String, position: Int) -> Either<Failure, Success>) {

{'fraction_non_alphanumeric': 0.1342685370741483, 'fraction_numerical': 0.00033400133600534405, 'mean_word_length': 3.032996632996633, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3945855', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1606, 'n_words': 573}