Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Parser Combinators
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
yhkaplan
December 03, 2019
Programming
330
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.6k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
230
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.9k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
4k
Property Wrappers
yhkaplan
0
630
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
Groovy as a human and AI friendly programming language
paulk
0
120
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
150
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
260
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
960
Performance Optimization With LLMs
xleitix
0
210
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
340
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
3
1.3k
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
810
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
11
8k
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
590
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
7k
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
180
Featured
See All Featured
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
510
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
590
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
ラッコキーワード サービス紹介資料
rakko
4
5.5M
Between Models and Reality
mayunak
4
510
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Into the Great Unknown - MozCon
thekraken
41
2.8k
WCS-LA-2024
lcolladotor
0
920
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
580
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