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
yhkaplan
December 03, 2019
Programming
310
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.4k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
210
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.2k
Speeding Up Your CI
yhkaplan
0
520
Automate All the Things
yhkaplan
4
2.6k
Other Decks in Programming
See All in Programming
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
190
はてなアカウント基盤 State of the Union
cockscomb
0
180
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
260
Performance Engineering for Everyone
elenatanasoiu
0
210
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
210
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.7k
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.5k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
160
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
220
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
JavaDoc 再入門
nagise
1
410
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Music & Morning Musume
bryan
47
7.2k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
330
The World Runs on Bad Software
bkeepers
PRO
72
12k
Utilizing Notion as your number one productivity tool
mfonobong
4
330
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
540
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
260
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Exploring anti-patterns in Rails
aemeredith
3
420
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
140
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