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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
yhkaplan
December 03, 2019
Programming
0
300
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
3k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
200
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.8k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.7k
Property Wrappers
yhkaplan
0
580
The Great Swift Migration
yhkaplan
1
4.2k
Speeding Up Your CI
yhkaplan
0
510
Automate All the Things
yhkaplan
4
2.6k
Other Decks in Programming
See All in Programming
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
110
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
160
CSC307 Lecture 03
javiergs
PRO
1
490
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
700
Architectural Extensions
denyspoltorak
0
280
CSC307 Lecture 07
javiergs
PRO
0
550
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
110
AtCoder Conference 2025
shindannin
0
1k
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
Package Management Learnings from Homebrew
mikemcquaid
0
210
Featured
See All Featured
Between Models and Reality
mayunak
1
180
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Designing for Timeless Needs
cassininazir
0
130
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
55
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
0
110
Into the Great Unknown - MozCon
thekraken
40
2.2k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
300
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
200
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
77
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