forked from pointfreeco/episode-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f83bbf
commit 2626424
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//typealias Parser<A> = (String) -> A | ||
|
||
struct Parser<A> { | ||
// 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<Int> { 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<Route> { 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<EnumPropertyGenerator> { 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: | ||
//} |
4 changes: 4 additions & 0 deletions
4
0057-what-is-a-parser-pt2/Parsers.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters