-
I've got input that I don't want to round-trip completely: there are some optionals I'm happy to remove (e.g. an optional trailing comma in a list of items), and some variable input that's just plain irrelevant for my use case (think
I can get the commas with OneOf {
","
Always(())
} which seems awfully roundabout to me but does work. I don't know any way at all to drop the variable stuff, so for the moment I'm just carrying it around as unnecessary I'm sure there are better patterns than these, what am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sounds like you should be able to make use of the Can be used like this: let input = """
don't care don't care don't care
don't care really don't care
:123 don't care again
"""
struct MyParser: ParserPrinter {
var body: some ParserPrinter<Substring, Int> {
Skip { PrefixThrough(":") }.printing { _, _ in } // don't print anything
Int.parser()
Skip { Rest() }.printing { _, _ in } // don't print anything
}
}
// parsing
let output = try MyParser().parse(input)
output // 123
// printing
let printedValue = try MyParser().print(100)
printedValue // "100" |
Beta Was this translation helpful? Give feedback.
Sounds like you should be able to make use of the
.printing(_:)
operator onParser
. It allows you to turn anyParser
into aParserPrinter
, by explicitly telling it how to print.Can be used like this: