|
| 1 | +import Swiftx |
| 2 | +import Operadics |
| 3 | + |
| 4 | +typealias Width = Int |
| 5 | +// TODO: What does this int mean, really |
| 6 | +typealias ColumnCount = Int |
| 7 | +typealias IndentLevel = Int |
| 8 | +/// The ribbon width is the maximal amount of non-indentation characters on a line |
| 9 | +typealias RibbonWidth = Int |
| 10 | + |
| 11 | +indirect enum Doc { |
| 12 | + case empty |
| 13 | + /// Invariant: char != '\n' |
| 14 | + case _char(Character) |
| 15 | + /// Invariant: '\n' ∉ text |
| 16 | + case _text(length: Int, String) |
| 17 | + case _line |
| 18 | + /// If flattened then whenFlattened else primary |
| 19 | + case flatAlt(primary: Doc, whenFlattened: Doc) |
| 20 | + case concat(Doc, Doc) |
| 21 | + /// Renders Doc with an increased indent level |
| 22 | + /// Note: This only affects line after the first newline |
| 23 | + case nest(IndentLevel, Doc) |
| 24 | + /// Invariant: longerLines.count >= shorterLines.split('\n').first.count |
| 25 | + case union(longerLines: Doc, shorterLines: Doc) |
| 26 | + // No support for Annotations for now, I don't think Swift's generics would take kindly to a Doc<A> |
| 27 | + // case annotate(A, Doc) |
| 28 | + case column((ColumnCount) -> Doc) |
| 29 | + case nesting((IndentLevel) -> Doc) |
| 30 | + case columns((ColumnCount?) -> Doc) |
| 31 | + case ribbon((RibbonWidth?) -> Doc) |
| 32 | + |
| 33 | + static func char(_ c: Character) -> Doc { |
| 34 | + return c == "\n" ? ._line : ._char(c) |
| 35 | + } |
| 36 | + |
| 37 | + static func text(_ str: String) -> Doc { |
| 38 | + return str == "" ? .empty : ._text(length: str.characters.count, str) |
| 39 | + } |
| 40 | + |
| 41 | + static var line: Doc { |
| 42 | + return .flatAlt(primary: ._line, whenFlattened: .space) |
| 43 | + } |
| 44 | + |
| 45 | + static var linebreak: Doc { |
| 46 | + return .flatAlt(primary: ._line, whenFlattened: .zero) |
| 47 | + } |
| 48 | + |
| 49 | + static var hardline: Doc { |
| 50 | + return ._line |
| 51 | + } |
| 52 | + |
| 53 | + /// Used to specify alternative layouts |
| 54 | + /// `doc.grouped` removes all line breaks in `doc`. The resulting line |
| 55 | + /// is added if it fits on the page. If it doesn't, it's rendered as is |
| 56 | + var grouped: Doc { |
| 57 | + return .union(longerLines: flattened, shorterLines: self) |
| 58 | + } |
| 59 | + |
| 60 | + var flattened: Doc { |
| 61 | + switch self { |
| 62 | + case .empty: return self |
| 63 | + case ._char(_): return self |
| 64 | + case ._text(length: _, _): return self |
| 65 | + case ._line: return self |
| 66 | + case let .flatAlt(_, whenFlattened): return whenFlattened |
| 67 | + case let .concat(x, y): return .concat(x.flattened, y.flattened) |
| 68 | + case let .nest(i, x): return .nest(i, x.flattened) |
| 69 | + case let .union(x, _): return x.flattened |
| 70 | + case let .column(f): return .column { f($0).flattened } |
| 71 | + case let .nesting(f): return .nesting { f($0).flattened } |
| 72 | + case let .columns(f): return .columns { f($0).flattened } |
| 73 | + case let .ribbon(f): return .ribbon { f($0).flattened } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
0 commit comments