2015-10-07 17:39:51 +03:00
|
|
|
public enum Doc: CustomDebugStringConvertible, CustomDocConvertible, Equatable {
|
2015-09-29 00:06:52 +03:00
|
|
|
case Empty
|
2015-09-29 05:36:22 +03:00
|
|
|
indirect case Concat(Doc, Doc)
|
|
|
|
indirect case Union(Doc, Doc)
|
2015-07-18 22:43:49 +03:00
|
|
|
case Text(String)
|
2015-09-29 05:36:22 +03:00
|
|
|
indirect case Nest(Int, Doc)
|
2015-09-29 00:06:52 +03:00
|
|
|
case Line
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 20:55:09 +03:00
|
|
|
public init<T>(_ value: T) {
|
2015-09-23 00:33:56 +03:00
|
|
|
self = (value as? CustomDocConvertible)?.doc ?? .Text(String(value))
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
|
2015-09-29 05:58:10 +03:00
|
|
|
public func group() -> Doc {
|
|
|
|
return Union(self.flattened, self)
|
|
|
|
}
|
|
|
|
|
2015-09-29 05:56:18 +03:00
|
|
|
public func bracket(left: String, _ right: String) -> Doc {
|
2015-10-08 00:47:45 +03:00
|
|
|
switch self {
|
|
|
|
case .Empty:
|
|
|
|
return Text(left) <> Text(right)
|
|
|
|
default:
|
|
|
|
return (Text(left)
|
|
|
|
<> Nest(2, Line <> self)
|
|
|
|
<> Line
|
|
|
|
<> Text(right)).group()
|
|
|
|
}
|
2015-09-29 05:56:18 +03:00
|
|
|
}
|
|
|
|
|
2015-09-29 05:36:22 +03:00
|
|
|
public var flattened: Doc {
|
2015-07-18 22:43:49 +03:00
|
|
|
switch self {
|
2015-09-29 00:06:52 +03:00
|
|
|
case .Empty, .Text:
|
|
|
|
return self
|
|
|
|
case let .Concat(a, b):
|
|
|
|
return .Concat(a.flattened, b.flattened)
|
|
|
|
case let .Union(a, _):
|
|
|
|
return a.flattened
|
2015-10-08 00:25:37 +03:00
|
|
|
case let .Nest(i, doc):
|
|
|
|
return .Nest(i, doc.flattened)
|
2015-09-29 00:06:52 +03:00
|
|
|
case .Line:
|
|
|
|
return .Text(" ")
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 00:13:10 +03:00
|
|
|
|
|
|
|
public func pretty(width: Int) -> String {
|
2015-09-29 05:16:19 +03:00
|
|
|
return best(width).description
|
2015-09-29 00:13:10 +03:00
|
|
|
}
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-29 05:59:24 +03:00
|
|
|
private func best(width: Int, placed: Int = 0) -> Layout {
|
2015-09-29 05:28:37 +03:00
|
|
|
return Layout(width: width, placed: placed, alternatives: .pure((0, self)))
|
2015-09-29 00:06:52 +03:00
|
|
|
}
|
2015-09-29 05:33:18 +03:00
|
|
|
|
2015-09-29 05:36:22 +03:00
|
|
|
public var doc: Doc {
|
2015-09-29 05:33:18 +03:00
|
|
|
return self
|
|
|
|
}
|
2015-10-07 17:39:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: CustomDebugStringConvertible
|
|
|
|
|
|
|
|
public var debugDescription: String {
|
|
|
|
switch self {
|
|
|
|
case .Empty:
|
|
|
|
return ".Empty"
|
|
|
|
case let .Concat(a, b):
|
|
|
|
return ".Concat(\(String(reflecting: a)), \(String(reflecting: b)))"
|
|
|
|
case let .Union(a, b):
|
|
|
|
return ".Union(\(String(reflecting: a)), \(String(reflecting: b)))"
|
|
|
|
case let .Text(s):
|
|
|
|
return ".Text(\(String(reflecting: s)))"
|
|
|
|
case let .Nest(i, x):
|
|
|
|
return ".Nest(\(String(reflecting: i)), \(String(reflecting: x)))"
|
|
|
|
case .Line:
|
|
|
|
return ".Line"
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 00:06:52 +03:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:17:07 +03:00
|
|
|
|
|
|
|
public func == (left: Doc, right: Doc) -> Bool {
|
|
|
|
switch (left, right) {
|
|
|
|
case (.Empty, .Empty), (.Line, .Line):
|
|
|
|
return true
|
|
|
|
case let (.Text(a), .Text(b)):
|
|
|
|
return a == b
|
|
|
|
case let (.Nest(i, a), .Nest(j, b)):
|
|
|
|
return i == j && a == b
|
|
|
|
case let (.Concat(l1, r1), .Concat(l2, r2)):
|
|
|
|
return l1 == l2 && r1 == r2
|
|
|
|
case let (.Union(l1, r1), .Union(l2, r2)):
|
|
|
|
return l1 == l2 && r1 == r2
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-29 05:47:02 +03:00
|
|
|
extension SequenceType where Generator.Element == Doc {
|
|
|
|
public func fold(combine: (Doc, Doc) -> Doc) -> Doc {
|
|
|
|
func fold(docs: Stream<Doc>) -> Doc {
|
|
|
|
switch docs {
|
|
|
|
case .Nil:
|
|
|
|
return .Empty
|
|
|
|
case let .Cons(x, rest) where rest.value.isEmpty:
|
|
|
|
return x
|
|
|
|
case let .Cons(x, rest):
|
|
|
|
return combine(x, fold(rest.value))
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 07:51:43 +03:00
|
|
|
return fold(Stream(self))
|
2015-09-29 05:47:02 +03:00
|
|
|
}
|
2015-09-29 05:53:15 +03:00
|
|
|
|
|
|
|
public func spread() -> Doc {
|
|
|
|
return fold(<+>)
|
|
|
|
}
|
|
|
|
|
2015-09-29 05:53:23 +03:00
|
|
|
public func stack() -> Doc {
|
|
|
|
return fold(</>)
|
|
|
|
}
|
|
|
|
|
2015-09-29 05:49:41 +03:00
|
|
|
public func joinWithSeparator(separator: String) -> Doc {
|
|
|
|
return fold {
|
2015-10-08 00:31:07 +03:00
|
|
|
$0 <> .Text(separator) </> $1
|
2015-09-29 05:49:41 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 05:47:02 +03:00
|
|
|
}
|
|
|
|
|
2015-09-11 17:17:19 +03:00
|
|
|
public protocol CustomDocConvertible: CustomStringConvertible {
|
2015-09-29 05:36:22 +03:00
|
|
|
var doc: Doc { get }
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
extension CustomDocConvertible {
|
2015-09-11 17:17:19 +03:00
|
|
|
public var description: String {
|
2015-09-29 00:13:10 +03:00
|
|
|
return doc.pretty(70)
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 00:06:52 +03:00
|
|
|
|
|
|
|
|
2015-09-29 05:36:22 +03:00
|
|
|
public func <> (left: Doc, right: Doc) -> Doc {
|
2015-09-29 00:06:52 +03:00
|
|
|
return .Concat(left, right)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-29 05:36:22 +03:00
|
|
|
public func <+> (left: Doc, right: Doc) -> Doc {
|
2015-09-29 00:06:52 +03:00
|
|
|
return left <> .Text(" ") <> right
|
|
|
|
}
|
|
|
|
|
2015-09-29 05:36:22 +03:00
|
|
|
public func </> (left: Doc, right: Doc) -> Doc {
|
2015-09-29 00:06:52 +03:00
|
|
|
return left <> .Line <> right
|
|
|
|
}
|
2015-09-29 06:33:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
private enum Layout: CustomStringConvertible, Equatable {
|
|
|
|
case Empty
|
|
|
|
indirect case Text(String, Layout)
|
|
|
|
indirect case Line(Int, Layout)
|
|
|
|
|
|
|
|
init(width: Int, placed: Int, alternatives: Stream<(Int, Doc)>) {
|
|
|
|
switch alternatives {
|
|
|
|
case .Nil:
|
|
|
|
self = .Empty
|
|
|
|
case let .Cons((_, .Empty), rest):
|
|
|
|
self = Layout(width: width, placed: placed, alternatives: rest.value)
|
|
|
|
case let .Cons((i, .Concat(x, y)), rest):
|
|
|
|
self = Layout(width: width, placed: placed, alternatives: .Cons((i, x), Memo(evaluated: .Cons((i, y), rest))))
|
|
|
|
case let .Cons((i, .Nest(j, x)), rest):
|
|
|
|
self = Layout(width: width, placed: placed, alternatives: .Cons((i + j, x), rest))
|
|
|
|
case let .Cons((_, .Text(s)), rest):
|
|
|
|
self = .Text(s, Layout(width: width, placed: placed + Int(s.characters.count), alternatives: rest.value))
|
|
|
|
case let .Cons((i, .Line), rest):
|
|
|
|
self = .Line(i, Layout(width: width, placed: i, alternatives: rest.value))
|
|
|
|
case let .Cons((i, .Union(x, y)), z):
|
|
|
|
self = .better(width, placed, Layout(width: width, placed: placed, alternatives: .Cons((i, x), z)), Layout(width: width, placed: placed, alternatives: .Cons((i, y), z)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
switch self {
|
|
|
|
case .Empty:
|
|
|
|
return ""
|
|
|
|
case let .Text(s, doc):
|
|
|
|
return s + doc.description
|
|
|
|
case let .Line(n, doc):
|
|
|
|
return "\n" + String(count: n, repeatedValue: " " as Character) + doc.description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fits(width: Int) -> Bool {
|
|
|
|
guard width >= 0 else { return false }
|
|
|
|
switch self {
|
|
|
|
case .Empty, .Line:
|
|
|
|
return true
|
|
|
|
case let .Text(s, x):
|
|
|
|
return x.fits(width - Int(s.characters.count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 00:36:59 +03:00
|
|
|
static func better(width: Int, _ placed: Int, _ x: Layout, @autoclosure _ y: () -> Layout) -> Layout {
|
|
|
|
return x.fits(width - placed) ? x : y()
|
2015-09-29 06:33:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func == (left: Layout, right: Layout) -> Bool {
|
|
|
|
switch (left, right) {
|
|
|
|
case (.Empty, .Empty):
|
|
|
|
return true
|
|
|
|
case let (.Text(a, x), .Text(b, y)):
|
|
|
|
return a == b && x == y
|
|
|
|
case let (.Line(i, x), .Line(j, y)):
|
|
|
|
return i == j && x == y
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 07:51:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
import Memo
|
|
|
|
import Stream
|