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

Request for feature: partial parsing #192

Open
pontaoski opened this issue Oct 16, 2021 · 3 comments
Open

Request for feature: partial parsing #192

pontaoski opened this issue Oct 16, 2021 · 3 comments

Comments

@pontaoski
Copy link

pontaoski commented Oct 16, 2021

Description:

A new method that starts the parser as usual, but returns:

  • (State, Resumption, Error)

State is either "Incomplete" (the structure isn't fully parsed) or "Complete" (the structure is fully parsed).
Resumption is something that will allow you to resume parsing with more tokens if State == Incomplete.

If it hits EOF while in the middle of parsing a structure, it pauses, returning something to allow you to resume the parsing from where it left off with new input instead of erroring.

Usecase:

REPLs.

Something like:

document := &ast{}
line := readline()
state, resumption, error := parser.ParsePartial(&document, line)

for {
  if err != nil {
    // input is malformed
  }
  if state == Complete {
    // evaluate code
  } else if state == Incomplete {
    line = readline()
    state, resumption, err = resumption.Resume(line)
  }
}
@alecthomas
Copy link
Owner

I do like the idea of incremental parsing. Most previous discussion has revolved around implementing a streaming lexer that blocks until the input is completely consumed. Would that satisfy your use case?

@pontaoski
Copy link
Author

Ideally, I'd like the following things:

  • being able to know if it's awaiting more input, so that I can signal such to the user with a new ... > in the repl
  • being able to know where in the parser it's currently parsing (e.g. to make REPL print differently for an incomplete pattern match or a function)

If a streaming parser can do that, then sounds good to me. Though, it seems like I would need to utilise goroutines with that to get the desired result, when it should probably be a single-goroutine affair.

@ysmood
Copy link

ysmood commented Jun 27, 2022

It's also useful for IDE syntax highlighting.

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

No branches or pull requests

3 participants