1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 08:25:19 +03:00

Term initialization is partial.

This commit is contained in:
Rob Rix 2015-09-23 17:37:53 -04:00
parent 0a4dd8111f
commit 975ac8fc2f
2 changed files with 36 additions and 20 deletions

View File

@ -36,33 +36,49 @@ public enum Term: CustomDebugStringConvertible, CustomDocConvertible, CustomStri
// MARK: JSON representation. // MARK: JSON representation.
/// Constructs a Term representing the `JSON` in a file at `path`. /// Constructs a Term representing the `JSON` in a file at `path`.
public init(path: String, JSON: Doubt.JSON) { public init?(path: String, JSON: Doubt.JSON) {
struct E: ErrorType {}
func die<A>() throws -> A {
throw E()
}
do {
switch JSON.dictionary?["key.substructure"] { switch JSON.dictionary?["key.substructure"] {
case let .Some(.Array(a)): case let .Some(.Array(a)):
self = .Roll(.Group(.Roll(.Literal(path)), a.map(Term.init))) self = .Roll(.Group(.Roll(.Literal(path)), try a.map { try Term(JSON: $0) ?? die() }))
default: default:
self = .Empty return nil
}
} catch _ {
return nil
} }
} }
/// Constructs a Term representing `JSON`. /// Constructs a Term representing `JSON`.
public init(JSON: Doubt.JSON) { public init?(JSON: Doubt.JSON) {
struct E: ErrorType {}
func die<A>() throws -> A {
throw E()
}
do {
switch JSON.dictionary { switch JSON.dictionary {
case let .Some(d) where d["key.name"] != nil && d["key.substructure"] != nil: case let .Some(d) where d["key.name"] != nil && d["key.substructure"] != nil:
let name = d["key.name"]?.string ?? "" let name = d["key.name"]?.string ?? ""
let substructure = d["key.substructure"]?.array ?? [] let substructure = d["key.substructure"]?.array ?? []
switch d["key.kind"]?.string { switch d["key.kind"]?.string {
case .Some("source.lang.swift.decl.class"), .Some("source.lang.swift.decl.extension"): case .Some("source.lang.swift.decl.class"), .Some("source.lang.swift.decl.extension"):
self = .Group(.Literal(name), substructure.map(Term.init)) self = .Group(.Literal(name), try substructure.map { try Term(JSON: $0) ?? die() })
case .Some("source.lang.swift.decl.function.method.instance"), .Some("source.lang.swift.decl.function.free"): case .Some("source.lang.swift.decl.function.method.instance"), .Some("source.lang.swift.decl.function.free"):
self = .Assign(name, .Abstract([], substructure.map(Term.init))) self = .Assign(name, .Abstract([], try substructure.map { try Term(JSON: $0) ?? die() }))
default: default:
self = .Empty return nil
} }
default: default:
self = .Empty return nil
}
} catch _ {
return nil
} }
} }
} }

View File

@ -13,7 +13,7 @@ extension Term {
.map(Structure.init) .map(Structure.init)
.map({ $0.dictionary }) .map({ $0.dictionary })
.map(toAnyObject) .map(toAnyObject)
.flatMap({ JSON(object: $0).map { Term(path: path, JSON: $0) } }) else { return nil } .flatMap({ JSON(object: $0).flatMap { Term(path: path, JSON: $0) } }) else { return nil }
self = term self = term
} }
} }