-
Notifications
You must be signed in to change notification settings - Fork 1
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
36ac47e
commit 3d802c0
Showing
4 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
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
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,31 @@ | ||
/// A combinator that applies a combinator and then, if successful, passes its result to a | ||
/// function returning the next combinator to apply. | ||
public struct Bind<Base: Combinator, Next: Combinator>: Combinator | ||
where Base.Context == Next.Context | ||
{ | ||
|
||
public typealias Context = Base.Context | ||
|
||
public typealias Element = Next.Element | ||
|
||
/// The base combinator. | ||
public let base: Base | ||
|
||
/// A function that accepts the result of `base` and returns a new combinator. | ||
public let makeNext: (Base.Element) throws -> Next | ||
|
||
/// Creates a combinator that applies `base` and then the combinator returned by `makeNext`. | ||
public init(_ base: Base, and makeNext: @escaping (Base.Element) throws -> Next) { | ||
self.base = base | ||
self.makeNext = makeNext | ||
} | ||
|
||
public func parse(_ context: inout Context) throws -> Next.Element? { | ||
if let a = try base.parse(&context) { | ||
return try makeNext(a).parse(&context) | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
} |
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
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