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
230
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
1.8k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
130
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.5k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.1k
Property Wrappers
yhkaplan
0
420
The Great Swift Migration
yhkaplan
1
3.7k
Speeding Up Your CI
yhkaplan
0
380
Automate All the Things
yhkaplan
4
2k
Other Decks in Programming
See All in Programming
CDKコントリビュートの最初の壁を越えよう! -簡単issueの見つけ方-
badmintoncryer
2
160
Fast JSX: Don't clone props object #28768
yossydev
1
150
AppRouter Panel Talk
yosuke_furukawa
PRO
1
430
Anthropic Cookbook のおすすめレシピ
schroneko
7
1.1k
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
新宿ダンジョンを可視化してみた
satoshi7190
2
280
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
240
ゆるい個人開発のススメ
kuroppe1819
10
1k
Implementing Design Systems in Swift
seyfoyun
0
370
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1k
Elm Form Validation
bkuhlmann
0
510
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
19k
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
44
6.8k
From Idea to $5000 a Month in 5 Months
shpigford
378
45k
A better future with KSS
kneath
231
16k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
19
6.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
Building Effective Engineering Teams - LeadDev
addyosmani
31
1.9k
Music & Morning Musume
bryan
41
5.6k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Raft: Consensus for Rubyists
vanstee
133
6.3k
Ruby is Unlike a Banana
tanoku
96
10k
Six Lessons from altMBA
skipperchong
22
3k
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