From 2bbb132d7e8346f3da36c19b1fd9f792c5d97bad Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 11 Sep 2015 09:59:40 -0400 Subject: [PATCH] Parsing is public. --- Doubt/Parse.swift | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Doubt/Parse.swift b/Doubt/Parse.swift index 69e291853..4ebec5df0 100644 --- a/Doubt/Parse.swift +++ b/Doubt/Parse.swift @@ -1,15 +1,15 @@ -struct State { +public struct State { let rest: String let value: T } -prefix func ^(string: String)(_ input: String) -> State? { +public prefix func ^(string: String)(_ input: String) -> State? { return input.characters.startsWith(string.characters) ? State(rest: input.from(string.characters.count), value: string) : nil } -prefix func ^(strings: [String])(_ input: String) -> State? { +public prefix func ^(strings: [String])(_ input: String) -> State? { return strings.indexOf { input.characters.startsWith($0.characters) } .flatMap { index in let string = strings[index] @@ -17,25 +17,25 @@ prefix func ^(strings: [String])(_ input: String) -> State? { } } -func parseWhile(predicate: Character -> Bool)(_ input: String) -> State? { +public func parseWhile(predicate: Character -> Bool)(_ input: String) -> State? { return input.characters.count > 0 && predicate(input.characters[input.startIndex]) ? parseWhile(predicate)(input.from(1)).map { State(rest: $0.rest, value: input.to(1) + $0.value) } ?? State(rest: input.from(1), value: input.to(1)) : nil } -postfix func * (parser: String -> State?) -> String -> State<[T]>? { +public postfix func * (parser: String -> State?) -> String -> State<[T]>? { return (Array.cons <^> (parser <*> { (parser*)($0) })) <|> pure([]) } -postfix func + (parser: String -> State?) -> String -> State<[T]>? { +public postfix func + (parser: String -> State?) -> String -> State<[T]>? { return (Array.cons <^> (parser <*> parser*)) } -func interpolate(element: String -> State?, _ separator: String -> State?) -> String -> State<[Element]>? { +public func interpolate(element: String -> State?, _ separator: String -> State?) -> String -> State<[Element]>? { return Array.cons <^> (element <*> (separator *> element)*) <|> pure([]) } -func <*> (left: String -> State?, right: String -> State?)(_ input: String) -> State<(T, U)>? { +public func <*> (left: String -> State?, right: String -> State?)(_ input: String) -> State<(T, U)>? { return left(input).flatMap { l in right(l.rest).map { r in State(rest: r.rest, value: (l.value, r.value)) @@ -43,26 +43,26 @@ func <*> (left: String -> State?, right: String -> State?)(_ input: } } -func <* (left: String -> State?, right: String -> State?) -> String -> State? { +public func <* (left: String -> State?, right: String -> State?) -> String -> State? { return left >>- { const($0) <^> right } } -func *> (left: String -> State?, right: String -> State?) -> String -> State? { +public func *> (left: String -> State?, right: String -> State?) -> String -> State? { return left >>- const(right) } -func <|> (left: String -> State?, right: String -> State?)(_ input: String) -> State? { +public func <|> (left: String -> State?, right: String -> State?)(_ input: String) -> State? { return left(input) ?? right(input) } -func <^> (left: T -> U, right: String -> State?) -> String -> State? { +public func <^> (left: T -> U, right: String -> State?) -> String -> State? { return right >>- { pure(left($0)) } } -func >>- (left: String -> State?, right: T -> String -> State?)(_ input: String) -> State? { +public func >>- (left: String -> State?, right: T -> String -> State?)(_ input: String) -> State? { return left(input).flatMap { state in right(state.value)(state.rest) } } -func pure(value: A)(_ input: String) -> State? { +public func pure(value: A)(_ input: String) -> State? { return State(rest: input, value: value) }