Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Parser Combinators
Search
yhkaplan
December 03, 2019
Programming
0
290
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
2.8k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
190
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.8k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.7k
Property Wrappers
yhkaplan
0
570
The Great Swift Migration
yhkaplan
1
4.1k
Speeding Up Your CI
yhkaplan
0
500
Automate All the Things
yhkaplan
4
2.5k
Other Decks in Programming
See All in Programming
AIコーディングエージェント(Manus)
kondai24
0
200
AIコーディングエージェント(Gemini)
kondai24
0
260
ゆくKotlin くるRust
exoego
1
130
開発に寄りそう自動テストの実現
goyoki
2
1.3k
AI 駆動開発ライフサイクル(AI-DLC):ソフトウェアエンジニアリングの再構築 / AI-DLC Introduction
kanamasa
11
3.2k
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
170
これならできる!個人開発のすゝめ
tinykitten
PRO
0
120
チームをチームにするEM
hitode909
0
360
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
430
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.5k
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
130
AIコーディングエージェント(NotebookLM)
kondai24
0
220
Featured
See All Featured
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
1.9k
Documentation Writing (for coders)
carmenintech
77
5.2k
A Tale of Four Properties
chriscoyier
162
23k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
0
220
What the history of the web can teach us about the future of AI
inesmontani
PRO
0
370
A better future with KSS
kneath
240
18k
Speed Design
sergeychernyshev
33
1.4k
Chasing Engaging Ingredients in Design
codingconduct
0
77
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
0
98
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
0
60
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1k
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