1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 17:59:10 +03:00

Branch has only one child.

This commit is contained in:
Rob Rix 2015-10-01 14:08:13 -04:00
parent 0e7bc6b2b3
commit 8961ee9168
3 changed files with 15 additions and 16 deletions

View File

@ -100,7 +100,7 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible
self = .Copy(.Leaf(v2))
case let (.Branch(v1), .Branch(v2)):
self = .Copy(.Branch(Diff.diff(v1, v2)))
self = .Copy(.Branch(Diff(v1, v2)))
default:
self = .Patch(a, b)

View File

@ -9,7 +9,7 @@ private func equals<F, A: Equatable>(left: Syntax<F, A>, _ right: Syntax<F, A>,
case let (.Leaf(l1), .Leaf(l2)):
return l1 == l2
case let (.Branch(v1), .Branch(v2)):
return v1.count == v2.count && zip(v1, v2).reduce(true) { $0 && recur($1.0, $1.1) }
return recur(v1, v2)
default:
return false
}

View File

@ -29,15 +29,15 @@ public enum Term<A: Equatable>: CustomDebugStringConvertible, CustomDocConvertib
return Term(.Leaf(a))
}
public static func Branch(terms: [Term]) -> Term {
return Term(.Branch(terms))
public static func Branch(term: Term) -> Term {
return Term(.Branch(term))
}
}
public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible {
case Empty
case Leaf(A)
case Branch([Recur])
case Branch(Recur)
public func map<T>(@noescape transform: Recur -> T) -> Syntax<T, A> {
switch self {
@ -45,15 +45,15 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return .Empty
case let .Leaf(n):
return .Leaf(n)
case let .Branch(vs):
return .Branch(vs.map(transform))
case let .Branch(x):
return .Branch(transform(x))
}
}
public func reduce<T>(initial: T, @noescape combine: (T, Recur) throws -> T) rethrows -> T {
switch self {
case let .Branch(xs):
return try xs.reduce(initial, combine: combine)
case let .Branch(x):
return try combine(initial, x)
default:
return initial
@ -66,9 +66,8 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return ".Empty"
case let .Leaf(n):
return ".Leaf(\(n))"
case let .Branch(vs):
let s = vs.map { String(reflecting: $0) }.joinWithSeparator(", ")
return ".Branch([ \(s) ])"
case let .Branch(x):
return ".Branch(\(String(reflecting: x)))"
}
}
@ -78,8 +77,8 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return .Empty
case let .Leaf(n):
return Doc(n)
case let .Branch(vs):
return vs.map(Doc.init).joinWithSeparator(",").bracket("{", "}")
case let .Branch(x):
return Doc(x)
}
}
}
@ -97,8 +96,8 @@ extension Syntax where A: Hashable {
return Hash("Empty")
case let .Leaf(n):
return Hash("Leaf", Hash(n))
case let .Branch(vs):
return Hash("Branch", .Ordered(vs.map(recur)))
case let .Branch(x):
return Hash("Branch", recur(x))
}
}
}