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
300
Parser Combinators
yhkaplan
December 03, 2019
Tweet
Share
More Decks by yhkaplan
See All by yhkaplan
Using the latest UICollectionView APIs
yhkaplan
0
2.9k
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.7k
Property Wrappers
yhkaplan
0
570
The Great Swift Migration
yhkaplan
1
4.2k
Speeding Up Your CI
yhkaplan
0
500
Automate All the Things
yhkaplan
4
2.5k
Other Decks in Programming
See All in Programming
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
450
愛される翻訳の秘訣
kishikawakatsumi
3
360
チームをチームにするEM
hitode909
0
430
CSC307 Lecture 02
javiergs
PRO
1
710
AI 駆動開発ライフサイクル(AI-DLC):ソフトウェアエンジニアリングの再構築 / AI-DLC Introduction
kanamasa
11
4.7k
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
150
PC-6001でPSG曲を鳴らすまでを全部NetBSD上の Makefile に押し込んでみた / osc2025hiroshima
tsutsui
0
200
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
2k
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
330
Deno Tunnel を使ってみた話
kamekyame
0
290
開発に寄りそう自動テストの実現
goyoki
2
1.6k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
770
Rails Girls Zürich Keynote
gr2m
95
14k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
69
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
115
100k
Mobile First: as difficult as doing things right
swwweet
225
10k
A Tale of Four Properties
chriscoyier
162
23k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
110
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
40
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
33
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
110
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