From ba6cd8cf86e630aaf64a519cb0d33449ba95bad7 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 11 Apr 2022 14:54:52 -0400 Subject: [PATCH] 185 --- .../Parsing.playground/Contents.swift | 152 ++++++++++++++++++ .../Parsing.playground/contents.xcplayground | 4 + 0185-parser-printers-tour-pt1/README.md | 7 + README.md | 1 + 4 files changed, 164 insertions(+) create mode 100644 0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift create mode 100644 0185-parser-printers-tour-pt1/Parsing.playground/contents.xcplayground create mode 100644 0185-parser-printers-tour-pt1/README.md diff --git a/0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift b/0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift new file mode 100644 index 00000000..cae3d320 --- /dev/null +++ b/0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift @@ -0,0 +1,152 @@ +import Parsing + +let string = String(repeating: "A", count: 10_000) +let substring = string.dropFirst(10).dropLast(10) + +"café" +"\u{00E9}" +"e\u{0301}" + +"\u{00E9}" == "e\u{0301}" + +"\u{00E9}".utf8 +"e\u{0301}".utf8 +Array("\u{00E9}".utf8) +Array("e\u{0301}".utf8) +"\u{00E9}".utf8.elementsEqual("e\u{0301}".utf8) +"\u{00E9}".elementsEqual("e\u{0301}") + +struct Dot { + let x, y: Int +} +enum Direction: String, CaseIterable { + case x, y +} +struct Fold { + let direction: Direction + let position: Int +} +struct Instructions { + let dots: [Dot] + let folds: [Fold] +} + +let dotTuple = ParsePrint { + Digits() + "," + Digits() +} +try dotTuple.print((3, 1)) + +let dot = ParsePrint(.memberwise(Dot.init)) { + Digits() + "," + Digits() +} +try dot.print(Dot(x: 3, y: 1)) + +do { + try dot.parse("6,10") +} catch { + print(error) +} + +let dots = Many { + dot +} separator: { + "\n" +} + +try dots.parse(""" +6,10 +0,14 +9,10 +0,3 +""") +try dots.print([ + Dot(x: 3, y: 1), + Dot(x: 2, y: 0), + Dot(x: 1, y: 4), +]) + +// fold ➡️ y=7 +let fold = ParsePrint(.memberwise(Fold.init)) { + "fold ➡️ " + Direction.parser() + "=" + Digits() +} +do { + try fold.parse("fold ➡️ y=7") +} catch { + print(error) +} +try fold.print(Fold(direction: .x, position: 5)) + +let folds = Many { + fold +} separator: { + "\n" +} + +let instructions = ParsePrint(.memberwise(Instructions.init)) { + dots + "\n\n" + folds +} + +do { + let dot = ParsePrint(.memberwise(Dot.init)) { + Digits() + ",".utf8 + Digits() + } + let dots = Many { + dot + } separator: { + "\n".utf8 + } + let fold = ParsePrint(.memberwise(Fold.init)) { + "fold ➡️ ".utf8 + Direction.parser() + "=".utf8 + Digits() + } + let folds = Many { + fold + } separator: { + "\n".utf8 + } + let instructions = ParsePrint(.memberwise(Instructions.init)) { + dots + "\n\n".utf8 + folds + } +} + +let input = """ +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold ➡️ y=7 +fold ➡️ x=5 +""" + +let output = try instructions.parse(input) +try instructions.print(output) == input diff --git a/0185-parser-printers-tour-pt1/Parsing.playground/contents.xcplayground b/0185-parser-printers-tour-pt1/Parsing.playground/contents.xcplayground new file mode 100644 index 00000000..a8211e59 --- /dev/null +++ b/0185-parser-printers-tour-pt1/Parsing.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/0185-parser-printers-tour-pt1/README.md b/0185-parser-printers-tour-pt1/README.md new file mode 100644 index 00000000..9601e867 --- /dev/null +++ b/0185-parser-printers-tour-pt1/README.md @@ -0,0 +1,7 @@ +## [Point-Free](https://www.pointfree.co) + +> #### This directory contains code from Point-Free Episode: [Tour of Parser-Printers: Introduction](https://www.pointfree.co/episodes/ep185-tour-of-parser-printers-introduction) +> +> Today we celebrate a huge release of swift-parsing, which includes the ability to build invertible parser-printers with ease. We’ll demonstrate by using the library to build three different parser-printers, starting with a fun exercise from Advent of Code. + +The following code sample runs with [swift-parsing 0.9.0](https://github.com/pointfreeco/swift-parsing). diff --git a/README.md b/README.md index 446f3c91..12c47216 100644 --- a/README.md +++ b/README.md @@ -186,3 +186,4 @@ This repository is the home of code written on episodes of [Point-Free](https:// 1. [Parser Printers: Map](0182-parser-printers-pt5) 1. [Parser Printers: Bizarro Printing](0183-parser-printers-pt6) 1. [Parser Printers: The Point](0184-parser-printers-pt7) +1. [Tour of Parser-Printers: Introduction](0185-parser-printers-tour-pt1)