PrefixUpTo end of line. #206
-
Maybe a silly question, but when writing parsers I often find myself struggling with this specific change If I want to take the input until the end of the line: PrefixUpTo("\n") that works fine but sometimes I want to support the multiple newlines from other OSs. From a little exploration of the API I see PrefixUpTo(Whitespace(.vertical)) But that provokes a compiler error, a very confusing one by the way. It seems that PrefixUpTo doesn't accept other Parsers so this won't work. But the compiler error put me on the road of trying conversions From substring/utf8 (or maybe it was because i encountered more problems with that in previous versions so I immediately thought it was that ^^') So is there a way to make that prefix work with another parser? Or is there another obvious way I'm missing to parse until the end of the line? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @alexito4! The main reason Depending on your use case and needs for your parser, you could still implement these parsers to take parsers themselves, and if you can benchmark/share and determine that for common cases such parsers are not slow, we could discuss bringing them into the main library. As for an alternative spelling of the same problem. I believe you can achieve it with the following: Parse {
Prefix { !$0.isNewline }
Whitespace(.vertical)
} |
Beta Was this translation helpful? Give feedback.
Hi @alexito4! The main reason
PrefixThrough
andPrefixUpTo
take sequences and not parsers is that checking a prefix can be guaranteed to have a particular performance profile, but using any arbitrary parser can introduce something that is quite inefficient to run against each element/character of a collection/string.Depending on your use case and needs for your parser, you could still implement these parsers to take parsers themselves, and if you can benchmark/share and determine that for common cases such parsers are not slow, we could discuss bringing them into the main library.
As for an alternative spelling of the same problem. I believe you can achieve it with the following: