Sharp-edge with a OneOf
printer ending with a "catch-all" fallback case.
#244
tgrapperon
started this conversation in
General
Replies: 1 comment 1 reply
-
@tgrapperon would something like this work? let someCase = ParsePrint(.case(Enum.someCase)) {
"someCase:"
Prefix { $0 != "-" }.map(.string)
Parse {
"-"
params
}
.replaceError(with: [])
} or this? let someCase = ParsePrint(.case(Enum.someCase)) {
"someCase:"
Prefix { $0 != "-" }.map(.string)
OneOf {
Parse {
"-"
params
}
End().map { [Param]() }
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I was recently working on a parser-printer that had to parse the enum:
When there are no parameters, the input looks like
and, with parameters:
Let say that
params
is aMany
parser of non-empty[Param]
, I can then write a parser for each configuration:I would then write this enum case as
It parses correctly the strings I'm sending to it. But when I try to print
someCase
with params, it generates asomeCase
without params. This is because the current form ofsomeCaseWithoutParams
is able to print a case with params, and since it's the first one tried, it's the one that is used to print the value.Fortunately, it is easy to fix: if I change for example
someCaseWithoutParams
to be…well, it doesn't work. This parser still manages to print a case with params because when it arrives at
Not
when printing, it has already written the whole string inRest
's, and it runs theNot
on an empty string.We can fix this by reversing the order of parsers:
And this parser printer works fine in both directions.
So in this case, we unfortunately had to:
The
Rest/Always
combination is the root of all evil here. I've inserted theAlways
because I wanted the parser to be homogenous and output(String, [Param])
, but all problematic behaviors are caused by their unconstrained nature.I have yet to find an elegant solution to this rather simple problem. I had to jump back into
Parsing
to solve a specific issue while working on another problem for days, so I'm maybe missing something obvious.Beta Was this translation helpful? Give feedback.
All reactions