mirror of
https://github.com/github/semantic.git
synced 2024-11-29 02:44:36 +03:00
Merge branch 'master' into output-source-range-annotations-to-json
This commit is contained in:
commit
fbd7c1f742
@ -4,17 +4,17 @@
|
||||
///
|
||||
/// This type is dual to `Free`. Where `Free` is inhabited by syntax trees where some terms are replaced with `B`s, `Cofree` is inhabited by syntax trees where all terms are annotated with `B`s. In Doubt, this allows us to e.g. annotate terms with source range information, categorization, etc.
|
||||
public enum Cofree<A, B> {
|
||||
case Unroll(B, () -> Syntax<Cofree, A>)
|
||||
indirect case Unroll(B, Syntax<Cofree, A>)
|
||||
|
||||
public var unwrap: Syntax<Cofree, A> {
|
||||
switch self {
|
||||
case let .Unroll(_, rest):
|
||||
return rest()
|
||||
return rest
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public init(_ annotation: B, @autoclosure(escaping) _ syntax: () -> Syntax<Cofree, A>) {
|
||||
public init(_ annotation: B, _ syntax: Syntax<Cofree, A>) {
|
||||
self = .Unroll(annotation, syntax)
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ public enum Cofree<A, B> {
|
||||
///
|
||||
/// As this is the dual of `Free.iterate`, it’s unsurprising that we have a similar guarantee: coiteration is linear in the size of the constructed tree.
|
||||
public static func coiterate(annotate: B -> Syntax<B, A>)(_ seed: B) -> Cofree {
|
||||
return .Unroll(seed, { annotate(seed).map(coiterate(annotate)) })
|
||||
return .Unroll(seed, annotate(seed).map(coiterate(annotate)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ extension Cofree: CustomDebugStringConvertible {
|
||||
|
||||
extension Cofree {
|
||||
public func map<Other>(transform: B -> Other) -> Cofree<A, Other> {
|
||||
return .Unroll(transform(extract), { self.unwrap.map { $0.map(transform) } })
|
||||
return .Unroll(transform(extract), unwrap.map { $0.map(transform) })
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ extension Cofree {
|
||||
|
||||
/// Returns a new `Cofree` by recursively applying `transform` to each node, producing the annotations for the copy.
|
||||
public func extend<Other>(transform: Cofree -> Other) -> Cofree<A, Other> {
|
||||
return .Unroll(transform(self), { self.unwrap.map { $0.extend(transform) } })
|
||||
return .Unroll(transform(self), unwrap.map { $0.extend(transform) })
|
||||
}
|
||||
|
||||
/// Returns a new `Cofree` constructed by recursively annotating each subtree with itself.
|
||||
|
Loading…
Reference in New Issue
Block a user