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
2.1k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
140
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.5k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.2k
Property Wrappers
yhkaplan
0
480
The Great Swift Migration
yhkaplan
1
3.9k
Speeding Up Your CI
yhkaplan
0
410
Automate All the Things
yhkaplan
4
2.2k
Other Decks in Programming
See All in Programming
長期運用プロダクトの開発速度を維持し続けるためのリファクタリング実践例
wataruss
8
2.7k
ドメイン駆動設計を実践するために必要なもの
bikisuke
4
330
watsonx.ai Dojo #2 生成AIを使ったアプリ開発入門編
oniak3ibm
PRO
0
230
A New Era of Testing
mannodermaus
2
510
o1モデルのプロンプトエンジニアリングって?
ktc_wada
0
280
2024 컴포즈 정원사
jisungbin
0
150
はじめてみよう量子プログラミング
itokoichi01
0
220
Increased Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
390
Pythonで改めて考える「クラス(class)」の使いどころ
os1ma
3
790
GraphQL あるいは React における自律的なデータ取得について
quramy
11
3k
Lessons by WebAssembly app in production on CDN Edge Computing Service
tetsuharuohzeki
0
210
From Idea to IDE: Developing Plugins for Android Studio
thisaay
1
220
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
78
8.6k
How To Stay Up To Date on Web Technology
chriscoyier
786
250k
What the flash - Photography Introduction
edds
67
11k
Embracing the Ebb and Flow
colly
83
4.4k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
A Tale of Four Properties
chriscoyier
155
22k
Scaling GitHub
holman
458
140k
A Modern Web Designer's Workflow
chriscoyier
692
190k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
41
6.5k
YesSQL, Process and Tooling at Scale
rocio
167
14k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.1k
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