peg: optional* word for deciding between f and failed match#3172
Open
olus2000 wants to merge 1 commit intofactor:masterfrom
Open
peg: optional* word for deciding between f and failed match#3172olus2000 wants to merge 1 commit intofactor:masterfrom
olus2000 wants to merge 1 commit intofactor:masterfrom
Conversation
Contributor
|
There is another solution, that optional takes a default value to return in case it doesn't match. Then the user could select something suitable for their case. |
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.
Currently there is no way to differentiate between the AST
fand a failed match in the AST produced byoptional. I present an alternative that allows to distinguish these two cases. Naming scheme followsat*. Several behaviors were considered:value torf fin the parent AST.{ value }orf(implemented by the PR).{ value t }or{ f f }so it can be destructured and treated similarly asat*.{ value }or{ }so it can be branched on with if-empty family.valuewrapped in a special tuple orf.valueor a special failure value.Option 1 is impossible to implement in current PEG vocab implementation since it requires returning an AST of two elements.
Option 2 has been chosen because it retains the property of
optionalthat a truthful value is returned on successful match allowing branching using usual conditional combinators, while also allowing a distinction for when the enclosed value has beenignored(an empty array is returned in such case).Option 3 is the closest in behavior to
at*but requires manual checking forignorewhich can't be dropped from the output since a variable amount of elements here would cause problems.Option 4 has the benefit of returning the same type of value every time, but requires manual checking for
ignore.Option 5 is very similar to option 2, but requires implementing a new type which only purpose will be to be destructured ASAP, and requires manual handling of
ignore.Option 6 doesn't always return a truthy value on success and never returns a falsy value on failure, which breaks similarity with
optional. Otherwise it's a good solution.