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
270
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
2.4k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
170
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.7k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.5k
Property Wrappers
yhkaplan
0
540
The Great Swift Migration
yhkaplan
1
4.1k
Speeding Up Your CI
yhkaplan
0
480
Automate All the Things
yhkaplan
4
2.4k
Other Decks in Programming
See All in Programming
複数アプリケーションを育てていくための共通化戦略
irof
0
540
DevTalks 25 - Create your own AI-infused Java apps with ease
kdubois
2
120
DevDay2025-OracleDatabase-kernel-addressing-history
oracle4engineer
PRO
7
1.6k
Perlで痩せる
yuukis
1
660
推論された型の移植性エラーTS2742に挑む
teamlab
PRO
0
150
從零到一:搭建你的第一個 Observability 平台
blueswen
0
220
Feature Flag 自動お掃除のための TypeScript プログラム変換
azrsh
PRO
4
630
ソフトウェア品質特性、意識してますか?AIの真の力を引き出す活用事例 / ai-and-software-quality
minodriven
19
6.7k
TypeScript だけを書いて Tauri でデスクトップアプリを作ろう / Tauri with only TypeScript
tris5572
2
530
イベントソーシングとAIの親和性ー物語とLLMに理解できるデータ
tomohisa
1
160
「兵法」から見る質とスピード
ickx
0
200
TSConfig Solution Style & subpath imports to switch types on a per-file basis
maminami373
1
180
Featured
See All Featured
How GitHub (no longer) Works
holman
314
140k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Become a Pro
speakerdeck
PRO
28
5.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Speed Design
sergeychernyshev
30
970
We Have a Design System, Now What?
morganepeng
52
7.6k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Why Our Code Smells
bkeepers
PRO
336
57k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.3k
The World Runs on Bad Software
bkeepers
PRO
68
11k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
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