1
1
mirror of https://github.com/github/semantic.git synced 2025-01-01 11:46:14 +03:00

Rename DOC to Doc.

This commit is contained in:
Rob Rix 2015-09-28 22:36:22 -04:00
parent f00eebca59
commit f4f5ad1e86
4 changed files with 37 additions and 37 deletions

View File

@ -20,13 +20,13 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible
} }
} }
public var doc: DOC { public var doc: Doc {
switch self { switch self {
case .Empty: case .Empty:
return .Empty return .Empty
case let .Patch(a, b): case let .Patch(a, b):
return .bracket("{-", DOC(a), "-}") return .bracket("{-", Doc(a), "-}")
<> .bracket("{+", DOC(b), "+}") <> .bracket("{+", Doc(b), "+}")
case let .Copy(a): case let .Copy(a):
return a.doc return a.doc
} }

View File

@ -3,7 +3,7 @@ public enum Layout: CustomStringConvertible, Equatable {
indirect case Text(String, Layout) indirect case Text(String, Layout)
indirect case Line(Int, Layout) indirect case Line(Int, Layout)
public init(width: Int, placed: Int, alternatives: Stream<(Int, DOC)>) { public init(width: Int, placed: Int, alternatives: Stream<(Int, Doc)>) {
switch alternatives { switch alternatives {
case .Nil: case .Nil:
self = .Empty self = .Empty
@ -48,29 +48,29 @@ public enum Layout: CustomStringConvertible, Equatable {
} }
} }
public enum DOC: CustomDocConvertible, Equatable { public enum Doc: CustomDocConvertible, Equatable {
case Empty case Empty
indirect case Concat(DOC, DOC) indirect case Concat(Doc, Doc)
indirect case Union(DOC, DOC) indirect case Union(Doc, Doc)
case Text(String) case Text(String)
indirect case Nest(Int, DOC) indirect case Nest(Int, Doc)
case Line case Line
public init<T>(_ value: T) { public init<T>(_ value: T) {
self = (value as? CustomDocConvertible)?.doc ?? .Text(String(value)) self = (value as? CustomDocConvertible)?.doc ?? .Text(String(value))
} }
public static func group(doc: DOC) -> DOC { public static func group(doc: Doc) -> Doc {
return Union(doc.flattened, doc) return Union(doc.flattened, doc)
} }
public static func bracket(l: String, _ x: DOC, _ r: String) -> DOC { public static func bracket(l: String, _ x: Doc, _ r: String) -> Doc {
return group(Concat(Text(l), Concat(Nest(2, Concat(Line, x)), Concat(Line, Text(r))))) return group(Concat(Text(l), Concat(Nest(2, Concat(Line, x)), Concat(Line, Text(r)))))
} }
public static func folddoc<C: CollectionType where C.Generator.Element == DOC>(docs: C, combine: (DOC, DOC) -> DOC) -> DOC { public static func foldDoc<C: CollectionType where C.Generator.Element == Doc>(Docs: C, combine: (Doc, Doc) -> Doc) -> Doc {
func fold(docs: Stream<DOC>) -> DOC { func fold(Docs: Stream<Doc>) -> Doc {
switch docs { switch Docs {
case .Nil: case .Nil:
return .Empty return .Empty
case let .Cons(x, rest) where rest.value.isEmpty: case let .Cons(x, rest) where rest.value.isEmpty:
@ -79,24 +79,24 @@ public enum DOC: CustomDocConvertible, Equatable {
return combine(x, fold(rest.value)) return combine(x, fold(rest.value))
} }
} }
return fold(Stream(sequence: docs)) return fold(Stream(sequence: Docs))
} }
public static func spread<C: CollectionType where C.Generator.Element == DOC>(docs: C) -> DOC { public static func spread<C: CollectionType where C.Generator.Element == Doc>(Docs: C) -> Doc {
return folddoc(docs, combine: <+>) return foldDoc(Docs, combine: <+>)
} }
public static func stack<C: CollectionType where C.Generator.Element == DOC>(docs: C) -> DOC { public static func stack<C: CollectionType where C.Generator.Element == Doc>(Docs: C) -> Doc {
return folddoc(docs, combine: </>) return foldDoc(Docs, combine: </>)
} }
public static func join<C: CollectionType where C.Generator.Element == DOC>(separator: String, _ docs: C) -> DOC { public static func join<C: CollectionType where C.Generator.Element == Doc>(separator: String, _ Docs: C) -> Doc {
return folddoc(docs) { return foldDoc(Docs) {
$0 <> Text(separator) <+> $1 $0 <> Text(separator) <+> $1
} }
} }
public var flattened: DOC { public var flattened: Doc {
switch self { switch self {
case .Empty, .Text: case .Empty, .Text:
return self return self
@ -104,8 +104,8 @@ public enum DOC: CustomDocConvertible, Equatable {
return .Concat(a.flattened, b.flattened) return .Concat(a.flattened, b.flattened)
case let .Union(a, _): case let .Union(a, _):
return a.flattened return a.flattened
case let .Nest(_, doc): case let .Nest(_, Doc):
return doc.flattened return Doc.flattened
case .Line: case .Line:
return .Text(" ") return .Text(" ")
} }
@ -119,13 +119,13 @@ public enum DOC: CustomDocConvertible, Equatable {
return Layout(width: width, placed: placed, alternatives: .pure((0, self))) return Layout(width: width, placed: placed, alternatives: .pure((0, self)))
} }
public var doc: DOC { public var doc: Doc {
return self return self
} }
} }
public protocol CustomDocConvertible: CustomStringConvertible { public protocol CustomDocConvertible: CustomStringConvertible {
var doc: DOC { get } var doc: Doc { get }
} }
extension CustomDocConvertible { extension CustomDocConvertible {
@ -135,15 +135,15 @@ extension CustomDocConvertible {
} }
public func <> (left: DOC, right: DOC) -> DOC { public func <> (left: Doc, right: Doc) -> Doc {
return .Concat(left, right) return .Concat(left, right)
} }
public func <+> (left: DOC, right: DOC) -> DOC { public func <+> (left: Doc, right: Doc) -> Doc {
return left <> .Text(" ") <> right return left <> .Text(" ") <> right
} }
public func </> (left: DOC, right: DOC) -> DOC { public func </> (left: Doc, right: Doc) -> Doc {
return left <> .Line <> right return left <> .Line <> right
} }

View File

@ -55,7 +55,7 @@ public func == (left: Layout, right: Layout) -> Bool {
} }
} }
public func == (left: DOC, right: DOC) -> Bool { public func == (left: Doc, right: Doc) -> Bool {
switch (left, right) { switch (left, right) {
case (.Empty, .Empty), (.Line, .Line): case (.Empty, .Empty), (.Line, .Line):
return true return true

View File

@ -15,7 +15,7 @@ public enum Term: CustomDebugStringConvertible, CustomDocConvertible, CustomStri
} }
} }
public var doc: DOC { public var doc: Doc {
switch self { switch self {
case .Empty: case .Empty:
return .Empty return .Empty
@ -182,25 +182,25 @@ public enum Syntax<Payload>: CustomDebugStringConvertible, CustomDocConvertible
} }
} }
public var doc: DOC { public var doc: Doc {
switch self { switch self {
case let .Apply(f, vs): case let .Apply(f, vs):
return DOC(f) <> .bracket("(", .join(", ", vs.map(DOC.init)), ")") return Doc(f) <> .bracket("(", .join(", ", vs.map(Doc.init)), ")")
case let .Abstract(parameters, body): case let .Abstract(parameters, body):
return .folddoc([ return .foldDoc([
.Text("λ"), .Text("λ"),
.join(", ", parameters.map(DOC.init)), .join(", ", parameters.map(Doc.init)),
.Text("."), .Text("."),
.stack(body.map(DOC.init)) .stack(body.map(Doc.init))
], combine: <>) ], combine: <>)
case let .Assign(n, v): case let .Assign(n, v):
return .spread([ .Text(n), .Text("="), DOC(v) ]) return .spread([ .Text(n), .Text("="), Doc(v) ])
case let .Variable(n): case let .Variable(n):
return .Text(n) return .Text(n)
case let .Literal(s): case let .Literal(s):
return .Text(s) return .Text(s)
case let .Group(n, vs): case let .Group(n, vs):
return DOC(n) <> .bracket("{", .stack(vs.map(DOC.init)), "}") return Doc(n) <> .bracket("{", .stack(vs.map(Doc.init)), "}")
} }
} }
} }