Replies: 2 comments 2 replies
-
@liscio Some good thoughts here! A few immediate thoughts:
|
Beta Was this translation helpful? Give feedback.
2 replies
-
Alright, I have determined a workable solution, and put together a repo to demonstrate the approach. Here's what's neat about what I've done:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Context
I am trying to parse functions with a similar syntax to Swift. Imagine the following toy example function definition:
measureDistance(to: {start,end}, using: {inches, centimeters, …}, percentAccuracy: [1-95])
An example call would look like this:
measureDistance(to: start, using: inches, percentAccuracy: 3)
Curly braces indicate enum values, and the square brackets represent an allowed range of values.
I have already captured much of the repetitive bits with my own
Parser
conformances, so each parser definition looks like this:Now let's assume I have 100 such functions defined. For now, I have such an arrangement working with nested
OneOf
parsers:It's gross, but it works. 😅
The Problem
The reported errors stink right now. When I mis-spell one of the commands, I am greeted by a massive error that lists all the commands:
Ideally, I'd like to surface something akin to Swift's own reporting:
How do I solve this?
I feel like there are a few possible ways forward here:
OneOf
parsers, and implement atry/catch
to generate a better error. The trouble here is that I'd have to somehow figure out a way to "walk all theOneOf
parsers" to find theFunction
s, and do the "spell-checking" using that list. Further, I'm not sure how I might inspect theParsingError
to decide when it's a spelling mistake, and not a legitInt.parser()
failure—ParsingError
is not public, unfortunately.Function
parsers in my ownParser
conformance, but each thought along these lines so far has resulted in varied levels of wonkiness. Array ofAny
s that I force-cast toFunction
? maybe doing something usingAnyParser
and introspection, … ? I don't think either are possible due to the deeply-nested generics involved. Further, I don't think that you can replicate the nestedtry
/catch
blocks ofOneOf
with an arbitrary array.Any tips on a workable solution would be great!
PS: Before anyone asks, I am not trying to implement/define a programming language using
swift-parser
. Instead, I'm only interested in one-shot commands. Imagine the target/action pattern from Cocoa(Touch), but the action carries specific parameter values. These actions need to be serialized, and are ideally human-readable. JSON/plist/etc representations would be far too nested, and highly awkward for a user to modify.Beta Was this translation helpful? Give feedback.
All reactions