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
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
800
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
310
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
480
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
170
関数型プログラミングのメリットって何だろう?
wanko_it
0
160
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
460
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
140
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
200
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.4k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
【やさしく解説 設計編 #1】「ドメイン駆動」と「実装駆動」ってなに? 〜設計の考え方を、たとえ話で学ぼう〜
panda728
PRO
1
110
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Featured
See All Featured
So, you think you're a good person
axbom
PRO
2
2.1k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.6k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
800
Claude Code のすすめ
schroneko
67
230k
KATA
mclloyd
PRO
35
15k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
Design in an AI World
tapps
1
260
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Code Review Best Practice
trishagee
74
20k
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
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