Skip to content

Feature Request: Partial Parsing #17

Description

@quad

Context: I'm porting an existing handwritten parser to use parsus.

Right now, parsus only supports fully parsing a string. If the string doesn't completely parse, it returns an error noting an index noting how far it was able to parse.

It would be very helpful if it also returned what it had succesfully parsed up until that point.

Because it doesn't have that feature, I've resorted to this hack:

class Grammar<Expression>() {
    // ... the grammar I've ported over

    abstract val bind: Parser<T>
    private val any by token { input, fromIndex -> input.length - fromIndex }
    val remain by parser {
        val v = bind() to currentOffset
        while (true) {
            when (val t = currentToken?.token) {
                EofToken, null -> break
                else -> skip(t)
            }
        }
        v
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions