1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Compute the categories for each node.

This commit is contained in:
Rob Rix 2015-10-28 14:20:46 -04:00
parent c5685aa586
commit 83328cc0d0

View File

@ -41,25 +41,22 @@ func termWithInput(string: String) -> Term? {
ts_document_parse(document)
let root = ts_document_root_node(document)
var histogram: [String:Int] = [:]
struct E: ErrorType {}
let result: Term? = try? Cofree
.ana { node in
return try? Cofree
.ana { node, category in
let count = ts_node_named_child_count(node)
guard let name = String.fromCString(ts_node_name(node, document)) else { throw E() }
histogram[name] = (histogram[name] ?? 0) + 1
guard count > 0 else { return Syntax.Leaf(name) }
return .Indexed((0..<count).map { ts_node_named_child(node, $0) })
} (root)
.map {
let start = ts_node_pos($0).chars
return start..<(start + ts_node_size($0).chars)
guard count > 0 else { return Syntax.Leaf(category.rawValue) }
return try .Indexed((0..<count).map { index in
let child = ts_node_named_child(node, index)
guard let name = String.fromCString(ts_node_name(child, document)) else { throw E() }
guard let category = Category(rawValue: name) else { throw E() }
return (child, category)
})
} (root, Category.Program)
.map { node, category in
let start = ts_node_pos(node).chars
return start..<(start + ts_node_size(node).chars)
}
print(histogram)
return result
}
}