Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Parser Combinators

yhkaplan
December 03, 2019

Parser Combinators

yhkaplan

December 03, 2019
Tweet

More Decks by yhkaplan

Other Decks in Programming

Transcript

  1. Self Intro » Name: Joshua Kaplan » Interests: ! λ

    " » Company: GMO Pepabo » Service: minne 2
  2. » Higher-order function that takes multiple parsers and /combines/ them

    into a new parser » CS theory » Parsec, Haskell 4
  3. Use let int = Parser<Int> { input in let num

    = input.prefix(while: { $0.isNumber }) guard let number = Int(num) else { throw ParserError.IntError.notANumber(num) } input.removeFirst(num.count) return number } 7
  4. func removingLiteral(_ string: String) -> Parser<Void> { return Parser<Void> {

    input in guard input.hasPrefix(string) else { throw ParserError.StringError.literalNotFound(string[...]) } input.removeFirst(string.count) } } 8
  5. struct Coordinate { let x, y: Int } let str

    = "1,2" let coordinateParser = zip( int, removingLiteral(","), int ).map { x, _, y in Coordinate(x: x, y: y) } let (coordinate, _) = try coordinateParser.run(str[...]) ▿ Coordinate - x: 1 - y: 2 10
  6. func substring(while predicate: @escaping (Character) -> Bool) -> Parser<Substring> {

    return Parser<Substring> { input in let p = input.prefix(while: predicate) input.removeFirst(p.count) return p } } 11
  7. Let's make another parser! struct Person { let name: String;

    let age: Int } let str = "name: John, age: 90" 12
  8. Name and age parsers let nameParser = zip( removingLiteral("name: "),

    substring(while: { $0.isLetter }) ).map { _, name in return String(name) } let ageParser = zip( removingLiteral("age: "), int ).map { _, age in return age } 13
  9. Person parser let personParser = zip( nameParser, removingLiteral(", "), ageParser

    ).map { name, _, age in return Person(name: name, age: age) } let (person, _) = try personParser.run(str[...]) ▿ Person - name: "John" - age: 90 14