From d70fd8f05460fc2cb12261be7a1e901bc8cd5280 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:59:20 +0000 Subject: [PATCH] raz -q def: where the name at the cursor is declared raz -q def Main.raz:LINE:COL reads to the cursor as words does and writes one row: the place of the name's declaration (as check gives places), its kind, name and type. A module's place is its file; a name of the universe or a built-in has none. For an editor's hover and go to definition (raz-lsp). Scan: word, the cursor's name whole; a cursor at a name's start is in that name. Diag: writePlace, out of row. Sym: describe, out of offer. Parse: answer, for words and def (setDefine). --- diff --git a/README.md b/README.md index ceb76a0..a3811d0 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ raz run [option]... Main.raz [arg]... raz -q check [option]... Main.raz raz -q words [option]... Main.raz:LINE:COL + raz -q def [option]... Main.raz:LINE:COL raz -h raz --version diff --git a/doc/compiler.md b/doc/compiler.md index 8d9fc35..c124a2c 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -16,18 +16,18 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/Diag.raz` | 472 | 358 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,260 | 1,036 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 856 | 682 | Types, objects, scopes, layout | +| `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/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,892 | 4,154 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,918 | 4,175 | 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 | -| `src/Raz.raz` | 488 | 381 | The command line, and `-q` for an editor | +| `src/Raz.raz` | 492 | 384 | The command line, and `-q` for an editor | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -2343,7 +2343,8 @@ `raz -q` answers an editor's language server about the text it holds, saved or not, with the compiler itself, so that there is one grammar and one set of rules. It is a compile that writes no program, with -three changes, each in the unit it concerns. +three changes, each in the unit it concerns. `-q def` answers an +editor's hover and "go to definition" the same way. - **The text from standard input.** `Raz.forEditor` gives `Scan` the main module's path (`Scan.setStdin`); `Scan.open`, asked for that @@ -2363,12 +2364,17 @@ cursor, it makes the symbol there a name, `Scan.atCursor`, which no source can spell (`(cursor)`), and keeps what is typed of the name in `Scan.prefix`: the bytes from the start of the name the cursor is - in or just after, or none if it is between symbols. The parser reads - on as for any name; where `qualIdent` would look a name up, it - meets `atCursor` and calls `complete` instead, which writes the - names that reach there (`Sym.offer`, over the same test as - `closest`'s, `Sym.reaches`) and ends the compile. After `M.`, the - names are what `M` exports. A cursor inside a comment or a string + in, just after or at the start of, or none if it is between + symbols; `Scan.word` is that name whole. The parser reads on as for + any name; where `qualIdent` would look a name up, it meets + `atCursor` and calls `answer` instead, which ends the compile. For + `-q words` it writes the names that reach there (`Sym.offer`, over + the same test as `closest`'s, `Sym.reaches`); after `M.`, the names + are what `M` exports. For `-q def` (`Parse.define`) it looks + `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 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 0540f8c..2f20d4b 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -22,6 +22,10 @@ .Fl q Cm words .Op Ar option ... .Ar Main.raz : Ns Ar LINE : Ns Ar COL +.Nm +.Fl q Cm def +.Op Ar option ... +.Ar Main.raz : Ns Ar LINE : Ns Ar COL .Sh DESCRIPTION .Nm compiles the module in @@ -189,6 +193,12 @@ .Ar Main.raz , write the names that can be typed at that cursor, a row each, and nothing else. +.It Fl q Cm def +For an editor: with +.Ar Main.raz : Ns Ar LINE : Ns Ar COL +in place of +.Ar Main.raz , +write where the name at that cursor is declared, and what it is. .It Fl h , Fl \-help Say the options. .It Fl \-version @@ -306,6 +316,34 @@ The rows come in no order. Inside a comment or a string, after a record's dot, or where a name is declared, there are none. +It exits 0. +.Pp +.Fl q Cm def +reads the module up to the cursor as +.Cm words +does, and gives one row for the name the cursor is in, or just after: +.Bd -literal -offset indent +FILE LINE COL ENDLINE ENDCOL KIND NAME TYPE +.Ed +.Pp +where the place is that of the name in its declaration, as +.Cm check +gives places, and +.Ar KIND +and +.Ar TYPE +are as +.Cm words +gives them. +A module's place is the start of its file; a name of the language's +own, as +.Cm INT +or +.Cm LEN , +has no file, and zeros. +Where +.Cm words +would give no names, it gives no row. It exits 0. .Sh LISTING The listing of diff --git a/src/Diag.raz b/src/Diag.raz index de22f72..1589455 100644 --- a/src/Diag.raz +++ b/src/Diag.raz @@ -247,23 +247,30 @@ return n; end; -(* A message as raz -q check gives it, a row for an editor: the file, - the line and the byte in the line where p starts, the same where it - ends, the kind and the message, split by tabs; all from 1. A message - about no place in a file has no file, and zeros. *) -proc row(p: Pos; kind, msg: []const U8); +(* p as raz -q gives a place, written to b: the file, the line and the + byte in the line where p starts, the same where it ends, split by + tabs; all from 1. A place in no file has no file, and zeros. *) +proc writePlace*(var b: Buf.Buf; p: Pos); var line, start, endLine, endStart: INT; e: Pos; begin if p.f >= numFiles then - Fmt.write(out, "\t0\t0\t0\t0\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]); - else - line, start := locate(p); - e := p; - e.a := p.b; - endLine, endStart := locate(e); - Fmt.write(out, "{}\t{}\t{}\t{}\t{}\t{}\t{}\n", [Fmt.str(files[p.f].path), Fmt.int(line), - Fmt.int(p.a - start + 1), Fmt.int(endLine), Fmt.int(p.b - endStart + 1), Fmt.str(kind), Fmt.str(msg)]); + Buf.str(b, "\t0\t0\t0\t0"); + return; end; + line, start := locate(p); + e := p; + e.a := p.b; + endLine, endStart := locate(e); + Fmt.write(b, "{}\t{}\t{}\t{}\t{}", [Fmt.str(files[p.f].path), Fmt.int(line), Fmt.int(p.a - start + 1), + Fmt.int(endLine), Fmt.int(p.b - endStart + 1)]); +end; + +(* A message as raz -q check gives it, a row for an editor: its place, + its kind and its text, split by tabs *) +proc row(p: Pos; kind, msg: []const U8); +begin + writePlace(out, p); + Fmt.write(out, "\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]); flush; end; diff --git a/src/Parse.raz b/src/Parse.raz index 6bdde4c..8fe6b62 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -94,6 +94,7 @@ firstModule*, lastModule*: PModule; (* in the order they were finished *) checkMode: BOOL; (* reading a file only for its interface *) + define: BOOL; (* raz -q def: the cursor asks where its name is declared *) inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *) laters: [MaxLater]Later; numLater: INT; @@ -179,6 +180,12 @@ thisVersion := v; end; +(* raz -q def, not words: the cursor asks where its name is declared *) +proc setDefine*(yes: BOOL); +begin + define := yes; +end; + proc nameOf(n: INT): []const U8; begin return Scan.nameOf(n); @@ -558,14 +565,33 @@ [Fmt.str(a), Fmt.str(nameOf(o^.name)), Fmt.str(b), Fmt.str(nameOf(o^.name))])); end; -(* raz -q words: the current symbol is the cursor, where a name of - scope s can be typed, or of any scope open if s < 0. The names that - start with what is typed are written, and the compile ends. *) -proc complete(s: INT); -var b: Buf.Buf; +(* raz -q words or def: the current symbol is the cursor, where a name + 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. *) +proc answer(s: INT); +var b: Buf.Buf; o: PObj; begin Buf.init(b, Heap.heap); - Sym.offer(s, Scan.prefix, b); + if not define then + Sym.offer(s, Scan.prefix, b); + else + 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))]); + elsif (o^.scope = 0) or (o^.cls = Sym.oBuiltin) then + Diag.writePlace(b, {f: MAX(INT), a: 0, b: 0}); + else + Diag.writePlace(b, o^.pos); + end; + Buf.add(b, '\t'); + Sym.describe(o, b); + Buf.add(b, '\n'); + end; + end; Out.str(Buf.bytes(b)); Out.flush(); Rt.exit(0); @@ -575,7 +601,7 @@ var o: PObj; m: PModule; at: Diag.Pos; begin if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; - if Scan.id = Scan.atCursor then complete(-1); end; + if Scan.id = Scan.atCursor then answer(-1); end; at := Scan.tokPos(); o := Sym.lookup(Scan.id); if o = nil then @@ -593,7 +619,7 @@ try expect(Scan.sPeriod); if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; m := o^.home; - if Scan.id = Scan.atCursor then complete(m^.scope); end; + if Scan.id = Scan.atCursor then answer(m^.scope); end; o := Sym.find(m^.scope, Scan.id); if (o = nil) or not o^.exported then Diag.error(Scan.tokPos(), say2("{} isn't exported by {}", nameOf(Scan.id), nameOf(m^.name))); diff --git a/src/Raz.raz b/src/Raz.raz index 50137b1..64ada60 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -2,7 +2,7 @@ raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] [-bare] Main.raz raz run [-I dir]... Main.raz [arg]... - raz -q check|words [-I dir]... Main.raz[:LINE:COL] + raz -q check|words|def [-I dir]... Main.raz[:LINE:COL] raz --version Compiles the main module and every module it imports, writes the @@ -13,8 +13,9 @@ place with the arguments after Main.raz, so its status is the program's; its argv[0] is Main.raz. raz -q, for an editor, reads the main module's text from standard input and stops once it is - checked: check writes its messages as rows, and words the names - that can be typed at the cursor (forEditor). + checked: check writes its messages as rows, words the names that + can be typed at the cursor, and def where the name there is + declared (forEditor). The program starts by saving argc and argv, runs every module's body (those of the modules the program imports, imported modules first), @@ -137,8 +138,8 @@ end; (* raz -q, for an editor: the main module's text is standard input, - and messages are rows (check) or not given (words, whose path names - the cursor). The result is the main module's path. *) + and messages are rows (check) or not given (words and def, whose + path names the cursor). The result is the main module's path. *) proc forEditor(query, path: []const U8): []const U8; var line, col: INT; ok: BOOL; begin @@ -146,12 +147,13 @@ col := 0; if query = "check" then Diag.setForm(Diag.Form.rows); - elsif query = "words" then + elsif (query = "words") or (query = "def") then path, line, col, ok := cursorAt(path); - if not ok then Diag.fatal(say1("-q words {}: expected FILE:LINE:COL", path)); end; + if not ok then Diag.fatal(Diag.say("-q {} {}: expected FILE:LINE:COL", [Fmt.str(query), Fmt.str(path)])); end; Diag.setForm(Diag.Form.silent); + Parse.setDefine(query = "def"); else - Diag.fatal(say1("-q {}: expected check or words", query)); + Diag.fatal(say1("-q {}: expected check, words or def", query)); end; Scan.setStdin(path, line, col); return path; @@ -182,6 +184,8 @@ Out.str(" input, and writes each message as a row (see raz(1))\n"); Out.str(" -q words with Main.raz:LINE:COL, writes the names that can be\n"); Out.str(" typed at the cursor there, one row each\n"); + Out.str(" -q def with Main.raz:LINE:COL, writes where the name at the\n"); + Out.str(" cursor is declared, and what it is\n"); Out.str(" -h, --help says this\n"); Out.str(" --version says the version, and the report it follows\n"); Out.str("see raz(1), and the report, doc/raz.md\n"); diff --git a/src/Scan.raz b/src/Scan.raz index 7e49e0f..b554993 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -9,8 +9,9 @@ For an editor (raz -q), one file's text may come from standard input instead (setStdin), and a place in it may be marked: the symbol there is a name, atCursor, standing for what is typed of the - name at the cursor, prefix, so that the parser can say what names - can be typed there. + name at the cursor, prefix, and for the name written there whole, + word, so that the parser can say what names can be typed there, or + where the name there is declared. Depends on: Sys, to read files; Diag, to report errors. Leaves out: recovery from errors in the text itself, such as a string @@ -90,6 +91,7 @@ atCursor*: INT; (* the name the symbol at the cursor has *) prefix*: []const U8; (* what is typed of the name at the cursor *) + word*: []const U8; (* the whole name at the cursor, as raz -q def asks of it *) stdinPath: []const U8; (* the file whose text is standard input, or "" *) cursorLine, cursorCol: INT; (* the cursor in it, from 1, or 0 for none *) @@ -1081,10 +1083,11 @@ end; (* Makes the current symbol the name at the cursor, typed from start up - to at; the cursor is passed then *) -proc cursorName(start, at: INT); + to at, and written whole up to stop; the cursor is passed then *) +proc cursorName(start, at, stop: INT); begin prefix := cur.buf[start..at]; + word := cur.buf[start..stop]; cur.cursor := MAX(INT); tok := sIdent; id := atCursor; @@ -1123,12 +1126,16 @@ cur.tokLineStart := cur.lineStart; if p >= cur.cursor then (* the cursor is before this symbol: if it was between symbols, not - in a string or a comment, a name of nothing typed is made there *) - if cur.prevEnd <= cur.cursor then - cursorName(cur.cursor, cur.cursor); + in a string or a comment, a name of nothing typed is made there; + at the start of a name, the name is the cursor's, read below *) + if (p = cur.cursor) and (isLetter(c) or (c = '_')) then + (* the name's own branch makes it *) + elsif cur.prevEnd <= cur.cursor then + cursorName(cur.cursor, cur.cursor, cur.cursor); return; + else + cur.cursor := MAX(INT); end; - cur.cursor := MAX(INT); end; if single[c] # sNull then INC(cur.pos); @@ -1141,7 +1148,7 @@ repeat INC(p); until not nameChar[buf[p]]; cur.pos := p; if p >= cur.cursor then (* the name the cursor is in or just after *) - cursorName(start, cur.cursor); + cursorName(start, cur.cursor, p); return; end; if p - start > MaxIdent then diff --git a/src/Sym.raz b/src/Sym.raz index 83656ed..c139c3e 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -438,20 +438,29 @@ end; end; +(* What o is, as raz -q says it: its kind, its name and its type, + split by tabs, written to out *) +proc describe*(o: ^Obj; var out: Buf.Buf); +var t: []const U8; +begin + t := ""; + if (o^.typ # nil) and (o^.cls # oModule) and (o^.cls # oBuiltin) then t := typeName(o^.typ); end; + Fmt.write(out, "{}\t{}\t{}", [Fmt.str(kindOf(o)), Fmt.str(Scan.nameOf(o^.name)), Fmt.str(t)]); +end; + (* raz -q words: a row in out for each name in reach that starts with prefix, in scope s as closest has it: its kind, the name and its type, split by tabs. A name another declaration hides is left out. *) proc offer*(s: INT; prefix: []const U8; var out: Buf.Buf); -var i: INT; o: ^Obj; n, t: []const U8; +var i: INT; o: ^Obj; n: []const U8; begin for i := 0 to hashMask do o := hashTab[i]; if reaches(o, s) and ((s >= 0) or (lookup(o^.name) = o)) then n := Scan.nameOf(o^.name); - t := ""; - if (o^.typ # nil) and (o^.cls # oModule) and (o^.cls # oBuiltin) then t := typeName(o^.typ); end; if (LEN(n) >= LEN(prefix)) and (n[0..LEN(prefix)] = prefix) then - Fmt.write(out, "{}\t{}\t{}\n", [Fmt.str(kindOf(o)), Fmt.str(n), Fmt.str(t)]); + describe(o, out); + Buf.add(out, '\n'); end; end; end; diff --git a/test/check.sh b/test/check.sh index 8b062a2..b10ca56 100755 --- a/test/check.sh +++ b/test/check.sh @@ -168,12 +168,15 @@ && readelf -s $out/own/Own | grep -q ' Own.main$' && readelf --debug-dump=decodedline $out/own/Own | grep -q 'Own.raz'; then pass=$((pass + 1)) else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi -# raz -q, for an editor: check's rows, and the names words offers at a +# 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. The text is standard input: checking an empty -# one shows that the file on disk isn't read. +# 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. 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"; do + "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 set -- $t f=test/query/${2%%:*} $raz -q $1 test/query/$2 < $f > $out/query.out diff --git a/test/query/Def.raz b/test/query/Def.raz new file mode 100644 index 0000000..bc2e9ed --- /dev/null +++ b/test/query/Def.raz @@ -0,0 +1,19 @@ +module Def; +import Out; + +var count: INT; + +(* Says hello. *) +proc greet(name: []const U8); +var total: INT; +begin + total := count + LEN(name); + Out.str(name); +end; + +proc main*; +begin + greet("x"); +end; + +end Def. diff --git a/test/query/def-builtin.expect b/test/query/def-builtin.expect new file mode 100644 index 0000000..0bb833a --- /dev/null +++ b/test/query/def-builtin.expect @@ -0,0 +1 @@ + 0 0 0 0 builtin LEN diff --git a/test/query/def-export.expect b/test/query/def-export.expect new file mode 100644 index 0000000..50c04d0 --- /dev/null +++ b/test/query/def-export.expect @@ -0,0 +1 @@ +lib/Out.raz 53 6 53 9 proc str proc([]const U8) diff --git a/test/query/def-local.expect b/test/query/def-local.expect new file mode 100644 index 0000000..6d2e596 --- /dev/null +++ b/test/query/def-local.expect @@ -0,0 +1 @@ +test/query/Def.raz 4 5 4 10 var count INT diff --git a/test/query/def-module.expect b/test/query/def-module.expect new file mode 100644 index 0000000..59e2a09 --- /dev/null +++ b/test/query/def-module.expect @@ -0,0 +1 @@ +lib/Out.raz 1 1 1 1 module Out