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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
yhkaplan
December 03, 2019
Programming
0
300
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
3.1k
Swift and Concurrency: The Plan for World Domination
yhkaplan
0
200
Backend-Driven UI: Making Screens Dynamic
yhkaplan
1
1.8k
Migrating from UIKit to SwiftUI efficiently
yhkaplan
4
3.8k
Property Wrappers
yhkaplan
0
590
The Great Swift Migration
yhkaplan
1
4.2k
Speeding Up Your CI
yhkaplan
0
510
Automate All the Things
yhkaplan
4
2.6k
Other Decks in Programming
See All in Programming
Nuxt Server Components
wattanx
0
160
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
240
Codex の「自走力」を高める
yorifuji
0
1.3k
それはエンジニアリングの糧である:AI開発のためにAIのOSSを開発する現場より / It serves as fuel for engineering: insights from the field of developing open-source AI for AI development.
nrslib
1
640
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
170
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
280
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
1.2k
車輪の再発明をしよう!PHP で実装して学ぶ、Web サーバーの仕組みと HTTP の正体
h1r0
2
440
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
1
670
AI活用のコスパを最大化する方法
ochtum
0
350
存在論的プログラミング: 時間と存在を記述する
koriym
5
560
Cyrius ーLinux非依存にコンテナをネイティブ実行する専用OSー
n4mlz
0
260
Featured
See All Featured
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
100
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
300
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
880
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
310
The browser strikes back
jonoalderson
0
850
Music & Morning Musume
bryan
47
7.1k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
200
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
130
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