63 lines
1.4 KiB
Plaintext
63 lines
1.4 KiB
Plaintext
// query.cm — Semantic queries over index data.
|
|
//
|
|
// All functions take an index object (from index.cm) and return arrays of hits.
|
|
|
|
var query = {}
|
|
|
|
// Find this references. scope: "top" (top-level only), "fn" (in functions), null (all).
|
|
query.find_this = function(idx, scope) {
|
|
var hits = []
|
|
var i = 0
|
|
var ref = null
|
|
while (i < length(idx.references)) {
|
|
ref = idx.references[i]
|
|
if (ref.name == "this") {
|
|
if (scope == null) {
|
|
hits[] = ref
|
|
} else if (scope == "top" && ref.enclosing == null) {
|
|
hits[] = ref
|
|
} else if (scope == "fn" && ref.enclosing != null) {
|
|
hits[] = ref
|
|
}
|
|
}
|
|
i = i + 1
|
|
}
|
|
return hits
|
|
}
|
|
|
|
// Intrinsic usage: find refs to a built-in name (e.g., print).
|
|
query.intrinsic = function(idx, name) {
|
|
var hits = []
|
|
var i = 0
|
|
var ref = null
|
|
if (idx.intrinsic_refs == null) return hits
|
|
while (i < length(idx.intrinsic_refs)) {
|
|
ref = idx.intrinsic_refs[i]
|
|
if (ref.name == name) {
|
|
hits[] = ref
|
|
}
|
|
i = i + 1
|
|
}
|
|
return hits
|
|
}
|
|
|
|
// Variable declarations matching a name and optional kind filter.
|
|
// kind is one of "var", "def", "fn", "param", or null (any).
|
|
query.find_decl = function(idx, name, kind) {
|
|
var hits = []
|
|
var i = 0
|
|
var sym = null
|
|
while (i < length(idx.symbols)) {
|
|
sym = idx.symbols[i]
|
|
if (sym.name == name) {
|
|
if (kind == null || sym.kind == kind) {
|
|
hits[] = sym
|
|
}
|
|
}
|
|
i = i + 1
|
|
}
|
|
return hits
|
|
}
|
|
|
|
return query
|