Replies: 1 comment
-
As far as I know, your best bet is to do what you already said; add that "where the prefix is not one of You could imagine an API where the struct KeyValueP: ParserPrinter {
var body: some ParserPrinter<Substring, MyEnum> {
OneOf(autoBacktrack: false) {
ParsePrint { "list="; ListP() }
ParsePrint { "string="; StringP() }
// fallback case
ParsePrint { PrefixUpTo("="); StringOrListP() }
}
}
} You could also imagine being able to fine-tune the backtracking with something like a struct KeyValueP: ParserPrinter {
var body: some ParserPrinter<Substring, MyEnum> {
OneOf(autoBacktrack: false) {
Try { "list="; ListP() }
ParsePrint { "string="; StringP() }
// fallback case
ParsePrint { PrefixUpTo("="); StringOrListP() }
}
}
} and it would move past the Something like that could maybe be useful to have in the library, but it's not possible today. |
Beta Was this translation helpful? Give feedback.
-
I'm parsing a structure that repeats many key/value pairs in arbitrary order, where several keys tell me what the shape of their values should be, and there's a fallback option that matches any key with a catchall value parser. I can do that with OneOf, but I've been having quite some trouble tracking down mistakes in my parsers based on the error messages. I think the problem may be that if the value-parser for a particular key fails,
OneOf
will go on and try all the other options as well: most of them will fail fast (because their keys don't match) but the catchall can succeed, which is not what I'm looking for.This won't compile but it gives you the idea I guess:
I guess I could handle this manually by making the fallback case explicitly say "where the prefix is not one of
list=
andstring=
", but I'm wondering if there's a solution that doesn't require the fallback knowing all the cases it should avoid. Something like Prolog'scut
goal, which commits to a backtracking option, and would be used in the more specific sub-parsers to say "once my key-parser has matched, if my value-parser fails to match you should fail the entire OneOf instead of trying the next alternative".Beta Was this translation helpful? Give feedback.
All reactions