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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
yhkaplan
December 03, 2019
Programming
320
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Parser Combinators
yhkaplan
December 03, 2019
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
3.5k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
220
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.9k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.9k
Property Wrappers
yhkaplan
0
620
The Great Swift Migration
yhkaplan
1
4.3k
Speeding Up Your CI
yhkaplan
0
520
Automate All the Things
yhkaplan
4
2.7k
Other Decks in Programming
See All in Programming
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
110
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
110
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
490
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
100
MySQLとPostgreSQLって何が違うの?
akagami
PRO
0
160
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
PyO3 で既存 Python 評価器を Rust core 化する ー wasm-bindgen でブラウザにも配るための設計
kdash
1
350
初めての模倣学習とVLA
natsutan
0
450
「AI時代、配布するPythonコードをどう守るか: 難読化の実験と判断軸」 #PyconJP2026
pkshadeck
PRO
2
140
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
960
FDEとは、何者なのか?
masapyon1212
0
130
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
660
Featured
See All Featured
Practical Orchestrator
shlominoach
191
12k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Marketing to machines
jonoalderson
1
5.7k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
990
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
Deep Space Network (abreviated)
tonyrice
0
290
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
390
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
680
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