From dafe42af25856ce48e771937a8cce8d35b807a07 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 29 Oct 2026 09:11:35 +0000 Subject: [PATCH] raz -q def: a module is its name in its module line, and an import's name answers too So that hover on a module shows its "module X;" line and the comment above it, its opening comment, and go to definition lands there. Sym.Module.at holds the place; compileFile answers at an import. --- diff --git a/doc/compiler.md b/doc/compiler.md index c124a2c..be36d4b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 479 | 363 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,267 | 1,040 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 865 | 688 | Types, objects, scopes, layout | +| `src/Sym.raz` | 866 | 689 | Types, objects, scopes, layout | | `src/Chunk.raz` | 364 | 280 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,777 | 1,450 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,846 | 1,989 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,918 | 4,175 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,922 | 4,179 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 411 | 317 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | | `src/Host.raz` | 103 | 52 | The system running the compiler | @@ -2374,7 +2374,9 @@ `Scan.word` up there and writes its declaration's place (`Diag.writePlace`, which `-q check`'s rows use too) and what it is (`Sym.describe`, which `offer`'s rows use): a module's place is its - file, and a name of the universe or a built-in has none. A cursor inside a comment or a string + name in its `module` line (`Module.at`), and an import's name, read + in `compileFile`, answers so too; a name of the universe or a + built-in has none. A cursor inside a comment or a string is dropped, so that it gives no names. Since `cursor` is part of the scanner's state, a recovery or a read ahead that puts the state back puts the cursor back too, and it is met again. diff --git a/doc/raz.1 b/doc/raz.1 index 2f20d4b..613806d 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -335,8 +335,9 @@ are as .Cm words gives them. -A module's place is the start of its file; a name of the language's -own, as +A module's place is its name in its own +.Li module +line, on an import's name too; a name of the language's own, as .Cm INT or .Cm LEN , diff --git a/src/Parse.raz b/src/Parse.raz index 8fe6b62..14023d4 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -569,8 +569,8 @@ of scope s is written, or of any scope open if s < 0. words writes the names that start with what is typed there; def where the name written there is declared, its place and what it is: a module's - place is its file, and the language's own names have none. Then the - compile ends. *) + place is its name in its own file, and the language's own names + have none. Then the compile ends. *) proc answer(s: INT); var b: Buf.Buf; o: PObj; begin @@ -581,7 +581,7 @@ if s < 0 then o := Sym.lookup(internName(Scan.word)); else o := Sym.find(s, internName(Scan.word)); end; if (o # nil) and (o^.typ # Sym.tPoison) then if (o^.cls = Sym.oModule) and (o^.home # nil) and (o^.home^.path # "") then - Fmt.write(b, "{}\t1\t1\t1\t1", [Fmt.str(Diag.shownPath(o^.home^.path))]); + Diag.writePlace(b, o^.home^.at); elsif (o^.scope = 0) or (o^.cls = Sym.oBuiltin) then Diag.writePlace(b, {f: MAX(INT), a: 0, b: 0}); else @@ -4428,7 +4428,7 @@ st: Scan.State; saved: [Sym.MaxDepth]INT; d: INT; oldMod: PModule; oldCheck, oldChecks: BOOL; m: PModule; lib: ^Sym.Lib; start: Diag.Pos; alias, mname, line, oldPath: INT; o: PObj; bodyChunk: INT; iargs: Sym.Args; - generic: BOOL; argsAt: Scan.State; why: []const U8; firstDoubt: INT; + generic, atName: BOOL; argsAt: Scan.State; why: []const U8; firstDoubt: INT; begin firstDoubt := numDoubts; Scan.saveScan(st); @@ -4463,6 +4463,7 @@ if (Scan.tok # Scan.sIdent) or (Scan.id # n) then return nil, Scan.mark(say2("expected module {}, found {}", nameOf(n), Scan.found())); end; + m^.at := Scan.tokPos(); Scan.next; (* the module first pending is the one named on the command line, which nothing imports, so no arguments come with it *) @@ -4480,7 +4481,9 @@ if Scan.tok # Scan.sIdent then return nil, expected("a module name"); end; start := Scan.tokPos(); alias := Scan.id; - mname := Scan.id; + atName := define and (alias = Scan.atCursor); (* raz -q def on a module's name here *) + if atName then alias := internName(Scan.word); end; + mname := alias; iargs.n := 0; generic := false; Scan.next; @@ -4511,6 +4514,7 @@ elsif not generic then o^.home := try importModule(mname, iargs); addImport(m, o^.home); + if atName then answer(m^.scope); end; else o^.home := nil; (* until the instance is made, at its first use *) if numDeferred >= MaxDeferred then diff --git a/src/Sym.raz b/src/Sym.raz index c139c3e..048e43e 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -128,6 +128,7 @@ args*: Args; scope*: INT; path*: []const U8; + at*: Diag.Pos; (* its name in its "module" line, for raz -q def *) first*, last*: ^Obj; (* its top-level declarations, in order *) locals*, lastLocal*: ^Obj; (* its procedures' declarations, in order *) imports*: ^Import; diff --git a/test/check.sh b/test/check.sh index b10ca56..eaff622 100755 --- a/test/check.sh +++ b/test/check.sh @@ -171,12 +171,13 @@ # raz -q, for an editor: check's rows; the names words offers at a # cursor after a part of a name, after a module's dot, and none in a # comment or a string; and where def finds a local, another module's -# name, a module and a built-in, which has no place. The text is -# standard input: checking an empty one shows that the file on disk -# isn't read. +# name, a module, used and imported, and a built-in, which has no +# place. The text is standard input: checking an empty one shows +# that the file on disk isn't read. for t in "check Check.raz check" "words Words.raz:9:16 prefix" "words Words.raz:10:11 export" \ "words Words.raz:11:10 none" "words Words.raz:12:15 none" "def Def.raz:10:15 def-local" \ - "def Def.raz:11:9 def-export" "def Def.raz:11:6 def-module" "def Def.raz:10:24 def-builtin"; do + "def Def.raz:11:9 def-export" "def Def.raz:11:6 def-module" "def Def.raz:2:9 def-module" \ + "def Def.raz:10:24 def-builtin"; do set -- $t f=test/query/${2%%:*} $raz -q $1 test/query/$2 < $f > $out/query.out diff --git a/test/query/def-module.expect b/test/query/def-module.expect index 59e2a09..42f0da2 100644 --- a/test/query/def-module.expect +++ b/test/query/def-module.expect @@ -1 +1 @@ -lib/Out.raz 1 1 1 1 module Out +lib/Out.raz 9 8 9 11 module Out