All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.prime.combinator.pasers.implementations.RepeatableBetween.kt Maven / Gradle / Ivy

package prime.combinator.pasers.implementations

import prime.combinator.pasers.Parsed
import prime.combinator.pasers.ParsedResult
import prime.combinator.pasers.Parser
import prime.combinator.pasers.ParsingError
import prime.combinator.pasers.implementations.RepeatableBetween.RepeatableBetweenParsed
import java.util.*

/**
 * RepeatableBetween allows repeat parser included between 2 others.
 * Example:
 *  aim: we want parse all letters between braces, "[Na]"
 *  how to reach:   RepeatableBetween(Str("["), EnglishLetter(), Str("]")).parse("[Na]").get()
 *  result: successfully parsed letters "N","a" cause the located between braces.
 *  Parser has joinRepeaters which allows to join repeated parser and get in the example above string "Na" instead
 *  of set of letters. See TestParsers#testRepeatableBetweenJoined
 *
 * @License: Apache-2.0
 * @source: https://github.com/fantaevroman/primeCombinator
 * @author: Roman Fantaev
 * @contact: [email protected]
 * @since 2021
 */
class RepeatableBetween(
    private val left: Parser,
    private val between: Parser,
    private val right: Parser
) : Parser.RepeatableBetweenParsed> {

    inner class RepeatableBetweenParsed(
        val mappedFrom: Parsed,
        val left: L,
        val between: List,
        val right: R
    ) : Parsed(mappedFrom)

    inner class RepeatableBetweenJoinedParsed(
        val mapFrom: Parsed,
        val left: L,
        val between: T,
        val right: R
    ) : Parsed(mapFrom)

    override fun parse(previous: Parsed): ParsedResult {
        return Between(left, Repeat(between), right).map {
            RepeatableBetweenParsed(
                it,
                it.left,
                it.between.repeatersParsed,
                it.right
            )
        }.parse(previous)
    }

    fun  joinRepeaters(joinBetween: (parsed: List) -> K): Parser> {
        return this.map {
            RepeatableBetweenJoinedParsed(it, it.left, joinBetween(it.between), it.right)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy