Repository navigation
parsers: add Where for parsed value conditions - #35
Merged
Merged
Conversation
Add Where(predicate, expected) to reject parsed values with an ordinary parse failure. Restore the input position on predicate rejection and report the expected value at the start of the attempt.
While writing tests for Where, discovered that ParseContext.ToString() returned "(1:1) " even when no diagnostics had been recorded. Return an empty string in that case and document the actual behavior. Use "Parsing failed." without coordinates when parsing fails without diagnostics, so the failure is not mistaken for success.
Pin that a successful As preserves later diagnostics and that Eof reports its position after a consumed prefix. Both interactions surfaced while testing Where, which relies on them indirectly; dedicated tests make a failure point at the owning parser instead of showing up inside the Where suite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some grammar rules depend on the value they parsed.
Literal.Number<int>()reads256without error, but an IPv4 octet must be in0..255.MapandDocan transform the value, but they cannot reject it: aDothat returnsfalseis still a successful match.FatalErrordoes reject the value, but it stops the whole parse, so no alternative can run.Where(predicate, expected)turns such a check into an ordinary parser:What it gives:
Or,OptionalandRepeatkeep working as usual.expectedtext is reported through the regular diagnostics at the point where the rule started, and joins other expectations recorded there..Void()and with suppressed diagnostics; only the parsed value is discarded.Parsereturns them inParseResult.ExceptionandTryParsepasses them to the caller, consistent withDoandMap.Additional changes
The branch also includes a small diagnostics fix found while testing:
ParseContext.ToString()used to format"(1:1) "when no diagnostics had been recorded and now returns an empty string.Parsereports"Parsing failed."when a failure records no diagnostics. Tests for theAsandEofbehavior thatWhererelies on were added with it.Note
The text of this description and the tests in this pull request
were prepared with the help of DeepSeek V4.1 Flash.