Skip to content

Commit

Permalink
185
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Apr 11, 2022
1 parent 725c5a2 commit ba6cd8c
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
152 changes: 152 additions & 0 deletions 0185-parser-printers-tour-pt1/Parsing.playground/Contents.swift
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
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>
7 changes: 7 additions & 0 deletions 0185-parser-printers-tour-pt1/README.md
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).
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ba6cd8c

Please sign in to comment.