diff --git a/index.cm b/index.cm index 3b3e00d7..81874b63 100644 --- a/index.cm +++ b/index.cm @@ -261,8 +261,12 @@ var index_ast = function(ast, tokens, filename) { // Function / arrow function expression — walk body. if (node.kind == "function" || node.kind == "arrow function") { enc = enclosing - if (node.name != null && node.function_nr != null) { - enc = filename + ":" + node.name + ":fn" + if (node.function_nr != null) { + if (node.name != null) { + enc = filename + ":" + node.name + ":fn" + } else { + enc = filename + ":anon_" + text(node.function_nr) + ":fn" + } } // Record params as symbols. if (node.list != null) { diff --git a/query.ce b/query.ce index ea7a790e..490f93d0 100644 --- a/query.ce +++ b/query.ce @@ -1,10 +1,10 @@ // cell query — Semantic queries across packages. // // Usage: -// cell query --this [] Top-level this references -// cell query --intrinsic [] Find built-in intrinsic usage -// cell query --decl [] Variable declarations by name -// cell query --help Show usage +// cell query --this [--top|--fn] [] this references +// cell query --intrinsic [] Find built-in intrinsic usage +// cell query --decl [] Variable declarations by name +// cell query --help Show usage var shop = use('internal/shop') var query_mod = use('query') @@ -12,6 +12,7 @@ var fd = use('fd') var mode = null var name = null +var this_scope = null var pkg_filter = null var show_help = false var i = 0 @@ -19,6 +20,10 @@ var i = 0 for (i = 0; i < length(args); i++) { if (args[i] == '--this') { mode = "this" + } else if (args[i] == '--top') { + this_scope = "top" + } else if (args[i] == '--fn') { + this_scope = "fn" } else if (args[i] == '--intrinsic') { mode = "intrinsic" if (i + 1 < length(args) && !starts_with(args[i + 1], '-')) { @@ -65,7 +70,9 @@ if (show_help) { log.console("Semantic queries across packages.") log.console("") log.console("Options:") - log.console(" --this Top-level this references") + log.console(" --this All this references") + log.console(" --this --top Top-level this only (not inside functions)") + log.console(" --this --fn this inside functions only") log.console(" --intrinsic Find built-in intrinsic usage (e.g., print)") log.console(" --decl Variable declarations by name") log.console("") @@ -93,7 +100,7 @@ if (show_help) { hits = null if (mode == "this") { - hits = query_mod.top_level_this(idx) + hits = query_mod.find_this(idx, this_scope) } else if (mode == "intrinsic") { hits = query_mod.intrinsic(idx, name) } else if (mode == "decl") { diff --git a/query.cm b/query.cm index 66fd6413..96d30be1 100644 --- a/query.cm +++ b/query.cm @@ -4,15 +4,21 @@ var query = {} -// Top-level this: references where name=="this" and enclosing==null. -query.top_level_this = function(idx) { +// 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" && ref.enclosing == null) { - hits[] = ref + 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 }