From 262642438a20da76edd958b31a33fb3b4cbfef4d Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Sun, 12 May 2019 16:58:23 -0400 Subject: [PATCH] 57 --- .../Parsers.playground/Contents.swift | 78 +++++++++++++++++++ .../Parsers.playground/contents.xcplayground | 4 + 0057-what-is-a-parser-pt2/README.md | 5 ++ README.md | 1 + 4 files changed, 88 insertions(+) create mode 100644 0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift create mode 100644 0057-what-is-a-parser-pt2/Parsers.playground/contents.xcplayground create mode 100644 0057-what-is-a-parser-pt2/README.md diff --git a/0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift b/0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift new file mode 100644 index 00000000..082ece43 --- /dev/null +++ b/0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift @@ -0,0 +1,78 @@ +//typealias Parser = (String) -> A + +struct Parser { +// let run: (String) -> A? +// let run: (String) -> (match: A?, rest: String) +// let run: (inout String) -> A? + let run: (inout Substring) -> A? + + func run(_ str: String) -> (match: A?, rest: Substring) { + var str = str[...] + let match = self.run(&str) + return (match, str) + } +} + +let int = Parser { str in + let prefix = str.prefix(while: { $0.isNumber }) + guard let int = Int(prefix) else { return nil } + str.removeFirst(prefix.count) + return int +} + + +//Substring + + +int.run("42") +int.run("42 Hello World") +int.run("Hello World") + + +// (A) -> A +// (inout A) -> Void + +enum Route { + case home + case profile + case episodes + case episode(id: Int) +} + +let router = Parser { str in + fatalError() +} + +//router.run("/") // .home +//router.run("/episodes/42") // .episode(42) + +//switch router.run("/episodes/42") { +//case .none: +//case .some(.home): +//case .some(.profile): +//case .some(.episodes): +//case let .some(.episode(id)): +//} + +import Foundation + +enum EnumPropertyGenerator { + case help + case version + case invoke(urls: [URL], dryRun: Bool) +} + +let cli = Parser { str in + fatalError() +} + +//cli.run("generate-enum-properties --version") // .version +//cli.run("generate-enum-properties --help") // .help +//cli.run("generate-enum-properties --dry-run /path/to/file.swift") // .invoke(["/path/to/file.swift"], dryRun: true) +// +//switch cli.run("generate-enum-properties --dry-run /path/to/file.swift") { +//case .help: +//case .version: +//case .invoke: +//case nil: +//} diff --git a/0057-what-is-a-parser-pt2/Parsers.playground/contents.xcplayground b/0057-what-is-a-parser-pt2/Parsers.playground/contents.xcplayground new file mode 100644 index 00000000..63b6dd8d --- /dev/null +++ b/0057-what-is-a-parser-pt2/Parsers.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/0057-what-is-a-parser-pt2/README.md b/0057-what-is-a-parser-pt2/README.md new file mode 100644 index 00000000..b8271b45 --- /dev/null +++ b/0057-what-is-a-parser-pt2/README.md @@ -0,0 +1,5 @@ +## [Point-Free](https://www.pointfree.co) + +> #### This directory contains code from Point-Free Episode: [What Is a Parser?: Part 2](https://www.pointfree.co/episodes/ep57-what-is-a-parser-part-2) +> +> TODO diff --git a/README.md b/README.md index c39c9f9c..61a95746 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,4 @@ This repository is the home of code written on episodes of 1. [Advanced Swift Syntax Enum Properties](0054-advanced-swift-syntax-enum-properties) 1. [Swift Syntax Command Line Tool](0055-swift-syntax-command-line-tool) 1. [What Is a Parser?: Part 1](0056-what-is-a-parser-pt1) +1. [What Is a Parser?: Part 2](0057-what-is-a-parser-pt2)