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

Compute the size of syntax trees.

This commit is contained in:
Rob Rix 2015-10-14 14:32:26 -04:00
parent 6b781622b6
commit 645e038982

View File

@ -71,6 +71,17 @@ extension JSON {
func annotate(json: Syntax<Term, JSONLeaf>) -> Term {
return Cofree(0, json)
}
func size(syntax: Syntax<Term, JSONLeaf>) -> Int {
switch syntax {
case .Leaf:
return 1
case let .Indexed(i):
return 1 + i.map { size($0.unwrap) }.reduce(0, combine: +)
case let .Keyed(i):
return 1 + i.values.map { size($0.unwrap) }.reduce(0, combine: +)
}
}
switch self {
case let .Array(a):
return annotate(.Indexed(a.map { $0.term }))