forked from pointfreeco/episode-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
725c5a2
commit ba6cd8c
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
4 changes: 4 additions & 0 deletions
4
0185-parser-printers-tour-pt1/Parsing.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos' buildActiveScheme='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters