Skip to content

Skip/Optionally in ParserPrinter #300

Answered by oskarek
tikitu asked this question in Q&A
Discussion options

You must be logged in to vote

Sounds like you should be able to make use of the .printing(_:) operator on Parser. It allows you to turn any Parser into a ParserPrinter, by explicitly telling it how to print.

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"

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tikitu
Comment options

Answer selected by tikitu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants