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
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.7k
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.6k
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
Rails Girls Sapporo 2ndの裏側―準備の日々から見えた、私が得たもの / SAPPORO ENGINEER BASE #11
lemonade_37
2
160
複数チーム並行開発下でのコード移行アプローチ ~手動 Codemod から「生成AI 活用」への進化
andpad
0
170
2026年向け会社紹介資料
misu
0
190
What’s Fair is FAIR: A Decentralised Future for WordPress Distribution
rmccue
0
180
Register is more than clipboard
satorunooshie
1
470
Tangible Code
chobishiba
3
560
関数の挙動書き換える
takatofukui
1
120
Atomics APIを知る / Understanding Atomics API
ssssota
1
150
開発生産性が組織文化になるまでの軌跡
tonegawa07
0
160
組織もソフトウェアも難しく考えない、もっとシンプルな考え方で設計する #phpconfuk
o0h
PRO
10
4.3k
PyCon mini 東海 2025「個人ではじめるマルチAIエージェント入門 〜LangChain × LangGraphでアイデアを形にするステップ〜」
komofr
3
990
CloudNative Days Winter 2025: 一週間で作る低レイヤコンテナランタイム
ternbusty
4
680
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
272
21k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Statistics for Hackers
jakevdp
799
220k
How to train your dragon (web standard)
notwaldorf
97
6.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
How to Think Like a Performance Engineer
csswizardry
28
2.3k
Documentation Writing (for coders)
carmenintech
76
5.1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Speed Design
sergeychernyshev
32
1.2k
Gamification - CAS2011
davidbonilla
81
5.5k
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