Skip to content

Commit

Permalink
57
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed May 12, 2019
1 parent 6f83bbf commit 2626424
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
78 changes: 78 additions & 0 deletions 0057-what-is-a-parser-pt2/Parsers.playground/Contents.swift
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:
//}
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>
5 changes: 5 additions & 0 deletions 0057-what-is-a-parser-pt2/README.md
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2626424

Please sign in to comment.