Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Parser Combinators
Search
yhkaplan
December 03, 2019
Programming
0
230
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
2k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
140
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.5k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.1k
Property Wrappers
yhkaplan
0
460
The Great Swift Migration
yhkaplan
1
3.8k
Speeding Up Your CI
yhkaplan
0
400
Automate All the Things
yhkaplan
4
2.1k
Other Decks in Programming
See All in Programming
OpenAI/Gemini APIを使って EPUBを翻訳するCLIツールをつくってみた
tomiyan
0
790
最近追加した型の紹介とその振り返り
aki19035vc
0
180
feature環境をGitHub ActionsとCloudFormationでいい感じに管理する
nealle
2
310
Polarsの成長: v0.14からv1.0までの変遷と今後の展望
zerebom
1
350
3 Effective Rules for Success with Signals in Angular
manfredsteyer
PRO
0
120
Prompt FlowによるLLMアプリケーション開発
yuto2000
1
1k
How to use Macrobenchmark
veronikapj
0
160
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Berlin
prof18
0
110
しくじり先生 Image Matching Challenge 2024 編
goosehaaan
0
810
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
15
8.5k
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
140
I/O Extended Android in Korea 2024 ~ Whats new in Android development tools
pluu
0
250
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
21
2.9k
What the flash - Photography Introduction
edds
65
11k
Navigating Team Friction
lara
181
13k
What's in a price? How to price your products and services
michaelherold
239
11k
Automating Front-end Workflow
addyosmani
1362
200k
KATA
mclloyd
20
13k
Bash Introduction
62gerente
607
210k
[RailsConf 2023] Rails as a piece of cake
palkan
35
4.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
357
18k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
18
1.2k
Principles of Awesome APIs and How to Build Them.
keavy
124
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
262
13k
Transcript
Parser Combinators 1
Self Intro » Name: Joshua Kaplan » Interests: ! λ
" » Company: GMO Pepabo » Service: minne 2
What are Parser Combinators? 3
» Higher-order function that takes multiple parsers and /combines/ them
into a new parser » CS theory » Parsec, Haskell 4
In Swift struct Parser<A> { let run: (inout Substring) throws
-> A } 5
Characteristics » Monadic » Composable » Generic » Immutable 6
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
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
Higher order functions » map » flatMap (bind, >>=) »
zip 9
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
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
Let's make another parser! struct Person { let name: String;
let age: Int } let str = "name: John, age: 90" 12
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
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
Comparison » By hand » Scanner 15
Why and when? 16
References » https://github.com/pointfreeco/episode-code- samples/tree/master/0064-parser-combinators-pt3 » https://talk.objc.io/episodes/S01E13-parsing- techniques » https://github.com/johnpatrickmorgan/Sparse »
https://github.com/davedufresne/SwiftParsec » https://github.com/thoughtbot/Argo » https://github.com/tryswift/TryParsec 17