Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Parser Combinators
yhkaplan
December 03, 2019
Programming
0
180
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
940
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
76
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.1k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
2.2k
Property Wrappers
yhkaplan
0
270
The Great Swift Migration
yhkaplan
1
3k
Speeding Up Your CI
yhkaplan
0
280
Automate All the Things
yhkaplan
4
1.7k
Other Decks in Programming
See All in Programming
kintoneでランダム取得を作ってみた(imoniCamp 2022-07-27)
shokun1108
0
150
クラウド KMS の活用 / TOKYO BLOCKCHAIN TECH MEETUP 2022
odanado
PRO
0
190
테라폼으로 ECR 관리하기 (How to Manage ECR with Terraform)
posquit0
0
530
Edge Side Frontend という新領域
mizchi
22
10k
Git操作編
smt7174
2
250
More Than Micro Frontends: 3 Further Use Cases for Module Federation @DWX 2022
manfredsteyer
PRO
0
380
VIMRC 2022
achimnol
0
130
パラメタライズドテスト
ledsun
0
220
Regular expressions basics/正規表現の基本
kishikawakatsumi
6
260
Dagger, la CI, autrement
guikingone
1
110
OSS貢献を気軽にしたい Let's Go Talk #1
yuyaabo
2
240
2022年のモダンCSS改
tonkotsuboy_com
24
17k
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
269
12k
JazzCon 2018 Closing Keynote - Leadership for the Reluctant Leader
reverentgeek
173
8.6k
Code Review Best Practice
trishagee
44
9.7k
From Idea to $5000 a Month in 5 Months
shpigford
373
44k
The Invisible Side of Design
smashingmag
290
48k
KATA
mclloyd
7
8.8k
Faster Mobile Websites
deanohume
294
28k
The Pragmatic Product Professional
lauravandoore
19
3.1k
Navigating Team Friction
lara
175
11k
Done Done
chrislema
174
14k
What’s in a name? Adding method to the madness
productmarketing
11
1.6k
Optimizing for Happiness
mojombo
364
64k
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