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

commonMain.piacenti.dslmaker.GenericParser2.kt Maven / Gradle / Ivy

Go to download

Kotlin multiplatform library to facilitate creation of DSLs with ANTLR or a simple built in parser

There is a newer version: 1.1.55
Show newest version
package piacenti.dslmaker

import piacenti.dslmaker.abstraction.ProductionStep
import piacenti.dslmaker.errors.ParserException
import piacenti.dslmaker.interfaces.GrammarInventory
import piacenti.dslmaker.interfaces.Parser
import piacenti.dslmaker.structures.ASTNode
import piacenti.dslmaker.structures.Grammar
import piacenti.dslmaker.structures.derivationgraph.LeftRecursionRemover

class GenericParser2(grammar: Grammar): Parser {
    private val leftRecursionRemover = LeftRecursionRemover()
    var grammar: Grammar = leftRecursionRemover.removeLeftRecursion(grammar)
        set(grammar) {
            field = grammar
            leftRecursionRemover.removeLeftRecursion(field)
        }

    private var matcher: ExpressionMatcher2? = null

    override val highestIndexParsed: Int
        get() = matcher!!.trackData.highestSuccessfulIndex

    override val possibleValidTokensIfContinuingParsing: Set
        get() = matcher!!.trackData.possibleValidTokensIfContinuingParsing.toSet()

    override val expectedTokenNotMatched: Set
        get() = matcher!!.trackData.expectedTokenNotMatched.toSet()


    private fun getCustomEnumInstance(): List {
        return grammar.container.tokens + grammar.container.productions

    }

    /**
     * @param text
     * @param matchEntireText
     * @return
     * @throws ParserException
     */
    override fun parse(text: String, matchEntireText: Boolean , tokenizeFirst: Boolean): ASTNode {
        matcher = ExpressionMatcher2(tokenizeFirst)
        return matcher!!.match(grammar, text, matchEntireText)
    }


    @Suppress("MemberVisibilityCanBePrivate")
    fun parseFind(text: String, regexDelimiter: String?): List {
        matcher = ExpressionMatcher2()
        return matcher!!.find(grammar, text, regexDelimiter)

    }

    fun parseFind(text: String): List {
        return parseFind(text, null)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy