2015-10-01 21:25:17 +03:00
|
|
|
/// Source info & categorization for nodes in a syntax tree.
|
2015-10-09 15:30:33 +03:00
|
|
|
public enum Info: AlgebraicHashable, CustomDebugStringConvertible, CustomJSONConvertible {
|
2015-09-30 23:01:47 +03:00
|
|
|
case Literal(String, Set<Category>)
|
2015-09-30 23:02:01 +03:00
|
|
|
|
|
|
|
public var categories: Set<Category> {
|
|
|
|
switch self {
|
|
|
|
case let .Literal(_, c):
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 16:46:02 +03:00
|
|
|
|
2015-10-09 15:25:26 +03:00
|
|
|
|
|
|
|
// MARK: AlgebraicHashable
|
|
|
|
|
2015-10-01 16:46:02 +03:00
|
|
|
public var hash: Hash {
|
|
|
|
switch self {
|
|
|
|
case let .Literal(source, categories):
|
|
|
|
return Hash("Literal", Hash(source), Hash(categories))
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 23:41:54 +03:00
|
|
|
|
2015-10-09 15:25:16 +03:00
|
|
|
|
|
|
|
// MARK: CustomDebugStringConvertible
|
|
|
|
|
2015-10-06 23:41:54 +03:00
|
|
|
public var debugDescription: String {
|
|
|
|
switch self {
|
2015-10-07 20:19:49 +03:00
|
|
|
case let .Literal(s, c) where c.isEmpty:
|
|
|
|
return s
|
2015-10-06 23:41:54 +03:00
|
|
|
case let .Literal(s, c):
|
2015-10-07 20:20:41 +03:00
|
|
|
return s + " (" + c.sort().map { String(reflecting: $0) }.joinWithSeparator(", ") + ")"
|
2015-10-06 23:41:54 +03:00
|
|
|
}
|
|
|
|
}
|
2015-10-09 15:30:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: CustomJSONConvertible
|
|
|
|
|
|
|
|
public var JSON: Doubt.JSON {
|
|
|
|
switch self {
|
|
|
|
case let .Literal(source, categories):
|
2015-10-09 15:43:43 +03:00
|
|
|
return [
|
|
|
|
"literal": [
|
2015-10-09 15:30:33 +03:00
|
|
|
"source": .String(source),
|
|
|
|
"categories": .Array(categories.map { $0.JSON })
|
2015-10-09 15:43:43 +03:00
|
|
|
]
|
|
|
|
]
|
2015-10-09 15:30:33 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-30 22:58:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-09 15:21:45 +03:00
|
|
|
// MARK: - Equality
|
|
|
|
|
2015-09-30 22:58:01 +03:00
|
|
|
public func == (left: Info, right: Info) -> Bool {
|
|
|
|
switch (left, right) {
|
|
|
|
case let (.Literal(s1, c1), .Literal(s2, c2)):
|
|
|
|
return s1 == s2 && c1 == c2
|
|
|
|
}
|
|
|
|
}
|