-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTable.swift
53 lines (46 loc) · 1.33 KB
/
Table.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Table.swift
// SwiftOrg
//
// Created by Xiaoxing Hu on 30/01/17.
// Copyright © 2017 Xiaoxing Hu. All rights reserved.
//
import Foundation
public struct Table: Node {
public struct Row {
public var cells: [String] = []
public var hasSeparator: Bool = false
init(_ theCells: [String], hasSeparator itHasSeparator: Bool = false) {
cells = theCells
hasSeparator = itHasSeparator
}
}
public var rows: [Row] = []
public var description: String {
let data = rows.map { row in
return "\(row)"
}.joined(separator: "\n")
return "Table(rows:\n\(data))"
}
}
extension OrgParser {
func parseTable() throws -> Table {
var table = Table()
while let (_, token) = tokens.peek() {
switch token {
case .tableRow(let cells):
_ = tokens.dequeue()
table.rows.append(Table.Row(cells))
case .horizontalSeparator:
_ = tokens.dequeue()
if !table.rows.isEmpty {
table.rows[table.rows.count - 1]
= Table.Row(table.rows.last!.cells, hasSeparator: true)
}
default:
return table
}
}
return table
}
}