Whiteknight / ParserObjects

C# library for parser combinators
https://whiteknight.github.io/ParserObjects
Apache License 2.0
6 stars 0 forks source link

ParseResultSequence should implement an end sentinel #181

Closed Whiteknight closed 1 year ago

Whiteknight commented 1 year ago

There are two options:

  1. Returns a Failure result with some kind of message saying we're at the end, or
  2. Returns some kind of custom success result with an end sentinel value

In either case we probably want to change the setup of parse result sequences to give the user a bit more easy control over what gets returned, and what happens in other cases (what if the inner parser fails? Throw an exception? Return the failure as-is?) etc.

Since this type is so critical for 2-stage parsers, and we have several examples in the test suite which showcase it, we need to make sure whatever we put in supports downstream parsing efforts.

Whiteknight commented 1 year ago

If the ParseResultSequence returns a failure result, the underlying sequence will be in the same position so all subsequent reads from that same position will produce the same failure. So, once we hit a failure, we're done. We should be able to detect this situation and return a cached error value which is similar but distinct from an end sentinel.

GetNext() should be able to detect .IsAtEnd for the underlying sequence and choose what to do:

  1. Invoke the parser and see what result it produces for EOF (and then cache that, because it's going to be the same every time) or,
  2. Refuse to invoke the parser and instead get some kind of user-supplied sentinel value
Whiteknight commented 1 year ago

This is done now. There's a callback that can be passed to the FromParseResult() method which can be used to construct a result. +tests.