Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Partial Parsing #17

Open
quad opened this issue Sep 6, 2023 · 0 comments
Open

Feature Request: Partial Parsing #17

quad opened this issue Sep 6, 2023 · 0 comments

Comments

@quad
Copy link

quad commented Sep 6, 2023

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
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant