Super combinators
-
Upload
cocoaheads-france -
Category
Software
-
view
228 -
download
0
Transcript of Super combinators
![Page 1: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/1.jpg)
SUPERCOMBINATORS A "SWIFTY" PARSER COMBINATOR
FRAMEWORK
![Page 2: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/2.jpg)
SWIFTY
Use term with cautionStill being defined
Should mean "familiar"Can mean baggage !
![Page 3: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/3.jpg)
WHY?
Why do this presentation?Why parser combinators?
Why SuperCombinators?
![Page 4: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/4.jpg)
STRINGS
Two potential intended users of a string
1. !2. "
![Page 5: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/5.jpg)
SNIPS
A lot of work on taking text for humans"Show me walking directions home"
Transform into something for machineschmod +x query.txt
![Page 6: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/6.jpg)
ONE APPROACH
Context-free grammar
Assign semantic function to nonterminals
This is not easy to express cleanly in Swift
![Page 7: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/7.jpg)
ONE SOLUTION
Define the semantic function as a String
Transform into a closure
More advanced ExpressibleByStringLiteral
![Page 8: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/8.jpg)
PARSER COMBINATORS
Conceptually: typealias Parser<Value, Collection> = (Collection) -> (Value, Collection)?
Made to be composable
Unlike familiar Cocoa devices
![Page 9: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/9.jpg)
EXISTING SOLUTIONS
A number of frameworks available already
All had downsides▸ Too many custom operators
▸ Memory leaks▸ Swift 2.2
![Page 10: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/10.jpg)
EXISTING SOLUTIONS
Demo/teaching tool
Not for production
![Page 11: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/11.jpg)
CLONE
First we cloned an existing solution
Migration ❤
Became intimately familiar with every line
![Page 12: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/12.jpg)
REIMPLEMENT
What could be made better?▸ String-specific▸ Declarative API
▸ Memory-safe recursion
Other goodies
![Page 13: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/13.jpg)
STRING
String can also be a "substring"let substring: String = text.substring(from: substringIndex)
Completely opaque
Captures text
Cheap to get substring with full String API
![Page 14: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/14.jpg)
SWIFT TYPE CONVEYS SEMANTICS
Collection takes this to an extreme
Helps compiler and programmer
![Page 15: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/15.jpg)
Pattern AND Parser<Value>
Pattern just traverses String
Parser extracts Value
Makes composition implicit while intuitive
![Page 16: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/16.jpg)
OPERATORS
Distinction in types allows for simplification
Just || and & for almost all composition
mirrored by explicit .or(_:) and .and(_:) instance methods
![Page 17: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/17.jpg)
EXTENSIONS
Distinction in types allows for useful extensionsExpressibleByStringLiteral
Swift 3.1:extension Parser where Value == String {}
![Page 18: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/18.jpg)
RECURSIVE PARSERS
Reference cycles
Usually avoidable
Not always obvious how
![Page 19: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/19.jpg)
RECURSIVE PARSERS
Use additional object with unowned reference back
lazily generate parsing function
take Parser -> Parser
![Page 20: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/20.jpg)
EXAMPLElet digits = Pattern.characters(in: .decimalDigits)let int = digits.stringParser.map { Int($0)! }
let sum = Parser<Int>.recursive { sum in return (int & "+" & sum).map(+) || int}
print(sum.parse("1+2+3")!)// prints 6
![Page 21: Super combinators](https://reader033.fdocuments.net/reader033/viewer/2022051706/58e4d8421a28abf5048b592d/html5/thumbnails/21.jpg)
THANK YOU!
github.com/snipsco/SuperCombinators
Sasha Lopoukhine
github.com/superlopuh@superlopuh