commit 088701da0b2c1d66a07ac20d4d0c356f9a8a1f34
parent f2a4324bb3ea0f7db3c87445a3e37c4927e77a3a
Author: Lou Woell <lou.woell@posteo.de>
Date:   Thu,  4 Sep 2025 15:25:40 +0200

Implement decl locator

Diffstat:
Acmd/harehelper/locate.ha | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)

diff --git a/cmd/harehelper/locate.ha b/cmd/harehelper/locate.ha @@ -0,0 +1,78 @@ +use hare::module; +use hare::lex; +use hare::ast; +use getopt :: { command }; +use hare::parse; +use fmt; +use path; +use os; + +fn locate (cmd: command, ctx: *module::context) void = { + + let id = match (parse::identstr(cmd.args[0])) { + case parse::error => + return void; + case let id: ast::ident => + yield id; + }; + + match (locate_symbol(ctx, id)) { + case void => + return void; + case let d: lex::location => + fmt::printfln("{}:{}", d.path, d.line)!; + }; +}; + +fn locate_symbol (ctx: *module::context, id: ast::ident) (lex::location | void) = { + + let ns: module::location = ident_ns(id); + if(len(id) <= 1){ + let p = path::init(os::getcwd())!; + ns = &p; + }; + + let (path, src) = match(module::find(ctx, ns)){ + case module::error => + return void; + case let r: (str, module::srcset) => + yield r; + }; + + id = ident_last(id); + + for (let s .. src.ha) { + let decls = scan(s)!; + defer for (let d .. decls) { + ast::decl_finish(d); + }; + + for (let decl .. decls) { + match(decl.decl){ + case let d: []ast::decl_const => + for (let t .. d) { + if(ast::ident_eq(id, t.ident)){ + return decl.start; + }; + }; + case let d: []ast::decl_global => + for (let t .. d) { + if(ast::ident_eq(id, t.ident)){ + return decl.start; + }; + }; + case let d: []ast::decl_type => + for (let t .. d) { + if(ast::ident_eq(id, t.ident)){ + return decl.start; + }; + }; + case let d: ast::decl_func => + if(ast::ident_eq(id, d.ident)){ + return decl.start; + }; + case => void; + }; + }; + }; +};