raz / commit d70fd8f0

commit
d70fd8f05460fc2cb12261be7a1e901bc8cd5280
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0cd48b0d
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).

patch · log

14 files changed, +182 -58

README.md (history) modified +1 -0

@@ -95,6 +95,7 @@
9595 raz run [option]... Main.raz [arg]...
9696 raz -q check [option]... Main.raz
9797 raz -q words [option]... Main.raz:LINE:COL
98+ raz -q def [option]... Main.raz:LINE:COL
9899 raz -h
99100 raz --version
100101

doc/compiler.md (history) modified +18 -12

@@ -16,18 +16,18 @@
1616
1717 | File | Lines | Code | What |
1818 |---|---|---|---|
19-| `src/Diag.raz` | 472 | 358 | Messages: positions, source excerpts, colour, counts |
20-| `src/Scan.raz` | 1,260 | 1,036 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 856 | 682 | Types, objects, scopes, layout |
19+| `src/Diag.raz` | 479 | 363 | Messages: positions, source excerpts, colour, counts |
20+| `src/Scan.raz` | 1,267 | 1,040 | Symbols, literals, comments, positions |
21+| `src/Sym.raz` | 865 | 688 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 364 | 280 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,777 | 1,450 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,846 | 1,989 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,892 | 4,154 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,918 | 4,175 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 411 | 317 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Host.raz` | 103 | 52 | The system running the compiler |
30-| `src/Raz.raz` | 488 | 381 | The command line, and `-q` for an editor |
30+| `src/Raz.raz` | 492 | 384 | The command line, and `-q` for an editor |
3131
3232 Every file opens with a comment that says what it is, what it depends
3333 on and what it leaves out. Read those first; they are the map.
@@ -2343,7 +2343,8 @@
23432343 `raz -q` answers an editor's language server about the text it holds,
23442344 saved or not, with the compiler itself, so that there is one grammar
23452345 and one set of rules. It is a compile that writes no program, with
2346-three changes, each in the unit it concerns.
2346+three changes, each in the unit it concerns. `-q def` answers an
2347+editor's hover and "go to definition" the same way.
23472348
23482349 - **The text from standard input.** `Raz.forEditor` gives `Scan` the
23492350 main module's path (`Scan.setStdin`); `Scan.open`, asked for that
@@ -2363,12 +2364,17 @@
23632364 cursor, it makes the symbol there a name, `Scan.atCursor`, which no
23642365 source can spell (`(cursor)`), and keeps what is typed of the name
23652366 in `Scan.prefix`: the bytes from the start of the name the cursor is
2366- in or just after, or none if it is between symbols. The parser reads
2367- on as for any name; where `qualIdent` would look a name up, it
2368- meets `atCursor` and calls `complete` instead, which writes the
2369- names that reach there (`Sym.offer`, over the same test as
2370- `closest`'s, `Sym.reaches`) and ends the compile. After `M.`, the
2371- names are what `M` exports. A cursor inside a comment or a string
2367+ in, just after or at the start of, or none if it is between
2368+ symbols; `Scan.word` is that name whole. The parser reads on as for
2369+ any name; where `qualIdent` would look a name up, it meets
2370+ `atCursor` and calls `answer` instead, which ends the compile. For
2371+ `-q words` it writes the names that reach there (`Sym.offer`, over
2372+ the same test as `closest`'s, `Sym.reaches`); after `M.`, the names
2373+ are what `M` exports. For `-q def` (`Parse.define`) it looks
2374+ `Scan.word` up there and writes its declaration's place
2375+ (`Diag.writePlace`, which `-q check`'s rows use too) and what it is
2376+ (`Sym.describe`, which `offer`'s rows use): a module's place is its
2377+ file, and a name of the universe or a built-in has none. A cursor inside a comment or a string
23722378 is dropped, so that it gives no names. Since `cursor` is part of the
23732379 scanner's state, a recovery or a read ahead that puts the state back
23742380 puts the cursor back too, and it is met again.

doc/raz.1 (history) modified +38 -0

@@ -22,6 +22,10 @@
2222 .Fl q Cm words
2323 .Op Ar option ...
2424 .Ar Main.raz : Ns Ar LINE : Ns Ar COL
25+.Nm
26+.Fl q Cm def
27+.Op Ar option ...
28+.Ar Main.raz : Ns Ar LINE : Ns Ar COL
2529 .Sh DESCRIPTION
2630 .Nm
2731 compiles the module in
@@ -189,6 +193,12 @@
189193 .Ar Main.raz ,
190194 write the names that can be typed at that cursor, a row each, and
191195 nothing else.
196+.It Fl q Cm def
197+For an editor: with
198+.Ar Main.raz : Ns Ar LINE : Ns Ar COL
199+in place of
200+.Ar Main.raz ,
201+write where the name at that cursor is declared, and what it is.
192202 .It Fl h , Fl \-help
193203 Say the options.
194204 .It Fl \-version
@@ -306,6 +316,34 @@
306316 The rows come in no order.
307317 Inside a comment or a string, after a record's dot, or where a name is
308318 declared, there are none.
319+It exits 0.
320+.Pp
321+.Fl q Cm def
322+reads the module up to the cursor as
323+.Cm words
324+does, and gives one row for the name the cursor is in, or just after:
325+.Bd -literal -offset indent
326+FILE LINE COL ENDLINE ENDCOL KIND NAME TYPE
327+.Ed
328+.Pp
329+where the place is that of the name in its declaration, as
330+.Cm check
331+gives places, and
332+.Ar KIND
333+and
334+.Ar TYPE
335+are as
336+.Cm words
337+gives them.
338+A module's place is the start of its file; a name of the language's
339+own, as
340+.Cm INT
341+or
342+.Cm LEN ,
343+has no file, and zeros.
344+Where
345+.Cm words
346+would give no names, it gives no row.
309347 It exits 0.
310348 .Sh LISTING
311349 The listing of

src/Diag.raz (history) modified +20 -13

@@ -247,23 +247,30 @@
247247 return n;
248248 end;
249249
250-(* A message as raz -q check gives it, a row for an editor: the file,
251- the line and the byte in the line where p starts, the same where it
252- ends, the kind and the message, split by tabs; all from 1. A message
253- about no place in a file has no file, and zeros. *)
254-proc row(p: Pos; kind, msg: []const U8);
250+(* p as raz -q gives a place, written to b: the file, the line and the
251+ byte in the line where p starts, the same where it ends, split by
252+ tabs; all from 1. A place in no file has no file, and zeros. *)
253+proc writePlace*(var b: Buf.Buf; p: Pos);
255254 var line, start, endLine, endStart: INT; e: Pos;
256255 begin
257256 if p.f >= numFiles then
258- Fmt.write(out, "\t0\t0\t0\t0\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]);
259- else
260- line, start := locate(p);
261- e := p;
262- e.a := p.b;
263- endLine, endStart := locate(e);
264- Fmt.write(out, "{}\t{}\t{}\t{}\t{}\t{}\t{}\n", [Fmt.str(files[p.f].path), Fmt.int(line),
265- Fmt.int(p.a - start + 1), Fmt.int(endLine), Fmt.int(p.b - endStart + 1), Fmt.str(kind), Fmt.str(msg)]);
257+ Buf.str(b, "\t0\t0\t0\t0");
258+ return;
266259 end;
260+ line, start := locate(p);
261+ e := p;
262+ e.a := p.b;
263+ endLine, endStart := locate(e);
264+ Fmt.write(b, "{}\t{}\t{}\t{}\t{}", [Fmt.str(files[p.f].path), Fmt.int(line), Fmt.int(p.a - start + 1),
265+ Fmt.int(endLine), Fmt.int(p.b - endStart + 1)]);
266+end;
267+
268+(* A message as raz -q check gives it, a row for an editor: its place,
269+ its kind and its text, split by tabs *)
270+proc row(p: Pos; kind, msg: []const U8);
271+begin
272+ writePlace(out, p);
273+ Fmt.write(out, "\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]);
267274 flush;
268275 end;
269276

src/Parse.raz (history) modified +34 -8

@@ -94,6 +94,7 @@
9494 firstModule*, lastModule*: PModule; (* in the order they were finished *)
9595
9696 checkMode: BOOL; (* reading a file only for its interface *)
97+ define: BOOL; (* raz -q def: the cursor asks where its name is declared *)
9798 inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *)
9899 laters: [MaxLater]Later;
99100 numLater: INT;
@@ -179,6 +180,12 @@
179180 thisVersion := v;
180181 end;
181182
183+(* raz -q def, not words: the cursor asks where its name is declared *)
184+proc setDefine*(yes: BOOL);
185+begin
186+ define := yes;
187+end;
188+
182189 proc nameOf(n: INT): []const U8;
183190 begin
184191 return Scan.nameOf(n);
@@ -558,14 +565,33 @@
558565 [Fmt.str(a), Fmt.str(nameOf(o^.name)), Fmt.str(b), Fmt.str(nameOf(o^.name))]));
559566 end;
560567
561-(* raz -q words: the current symbol is the cursor, where a name of
562- scope s can be typed, or of any scope open if s < 0. The names that
563- start with what is typed are written, and the compile ends. *)
564-proc complete(s: INT);
565-var b: Buf.Buf;
568+(* raz -q words or def: the current symbol is the cursor, where a name
569+ of scope s is written, or of any scope open if s < 0. words writes
570+ the names that start with what is typed there; def where the name
571+ written there is declared, its place and what it is: a module's
572+ place is its file, and the language's own names have none. Then the
573+ compile ends. *)
574+proc answer(s: INT);
575+var b: Buf.Buf; o: PObj;
566576 begin
567577 Buf.init(b, Heap.heap);
568- Sym.offer(s, Scan.prefix, b);
578+ if not define then
579+ Sym.offer(s, Scan.prefix, b);
580+ else
581+ if s < 0 then o := Sym.lookup(internName(Scan.word)); else o := Sym.find(s, internName(Scan.word)); end;
582+ if (o # nil) and (o^.typ # Sym.tPoison) then
583+ if (o^.cls = Sym.oModule) and (o^.home # nil) and (o^.home^.path # "") then
584+ Fmt.write(b, "{}\t1\t1\t1\t1", [Fmt.str(Diag.shownPath(o^.home^.path))]);
585+ elsif (o^.scope = 0) or (o^.cls = Sym.oBuiltin) then
586+ Diag.writePlace(b, {f: MAX(INT), a: 0, b: 0});
587+ else
588+ Diag.writePlace(b, o^.pos);
589+ end;
590+ Buf.add(b, '\t');
591+ Sym.describe(o, b);
592+ Buf.add(b, '\n');
593+ end;
594+ end;
569595 Out.str(Buf.bytes(b));
570596 Out.flush();
571597 Rt.exit(0);
@@ -575,7 +601,7 @@
575601 var o: PObj; m: PModule; at: Diag.Pos;
576602 begin
577603 if Scan.tok # Scan.sIdent then return nil, expected("a name"); end;
578- if Scan.id = Scan.atCursor then complete(-1); end;
604+ if Scan.id = Scan.atCursor then answer(-1); end;
579605 at := Scan.tokPos();
580606 o := Sym.lookup(Scan.id);
581607 if o = nil then
@@ -593,7 +619,7 @@
593619 try expect(Scan.sPeriod);
594620 if Scan.tok # Scan.sIdent then return nil, expected("a name"); end;
595621 m := o^.home;
596- if Scan.id = Scan.atCursor then complete(m^.scope); end;
622+ if Scan.id = Scan.atCursor then answer(m^.scope); end;
597623 o := Sym.find(m^.scope, Scan.id);
598624 if (o = nil) or not o^.exported then
599625 Diag.error(Scan.tokPos(), say2("{} isn't exported by {}", nameOf(Scan.id), nameOf(m^.name)));

src/Raz.raz (history) modified +12 -8

@@ -2,7 +2,7 @@
22
33 raz [-I dir]... [-o file] [-Werror] [-S] [-libc glibc|musl] [-base addr] [-bare] Main.raz
44 raz run [-I dir]... Main.raz [arg]...
5- raz -q check|words [-I dir]... Main.raz[:LINE:COL]
5+ raz -q check|words|def [-I dir]... Main.raz[:LINE:COL]
66 raz --version
77
88 Compiles the main module and every module it imports, writes the
@@ -13,8 +13,9 @@
1313 place with the arguments after Main.raz, so its status is the
1414 program's; its argv[0] is Main.raz. raz -q, for an editor, reads
1515 the main module's text from standard input and stops once it is
16- checked: check writes its messages as rows, and words the names
17- that can be typed at the cursor (forEditor).
16+ checked: check writes its messages as rows, words the names that
17+ can be typed at the cursor, and def where the name there is
18+ declared (forEditor).
1819
1920 The program starts by saving argc and argv, runs every module's body
2021 (those of the modules the program imports, imported modules first),
@@ -137,8 +138,8 @@
137138 end;
138139
139140 (* raz -q, for an editor: the main module's text is standard input,
140- and messages are rows (check) or not given (words, whose path names
141- the cursor). The result is the main module's path. *)
141+ and messages are rows (check) or not given (words and def, whose
142+ path names the cursor). The result is the main module's path. *)
142143 proc forEditor(query, path: []const U8): []const U8;
143144 var line, col: INT; ok: BOOL;
144145 begin
@@ -146,12 +147,13 @@
146147 col := 0;
147148 if query = "check" then
148149 Diag.setForm(Diag.Form.rows);
149- elsif query = "words" then
150+ elsif (query = "words") or (query = "def") then
150151 path, line, col, ok := cursorAt(path);
151- if not ok then Diag.fatal(say1("-q words {}: expected FILE:LINE:COL", path)); end;
152+ if not ok then Diag.fatal(Diag.say("-q {} {}: expected FILE:LINE:COL", [Fmt.str(query), Fmt.str(path)])); end;
152153 Diag.setForm(Diag.Form.silent);
154+ Parse.setDefine(query = "def");
153155 else
154- Diag.fatal(say1("-q {}: expected check or words", query));
156+ Diag.fatal(say1("-q {}: expected check, words or def", query));
155157 end;
156158 Scan.setStdin(path, line, col);
157159 return path;
@@ -182,6 +184,8 @@
182184 Out.str(" input, and writes each message as a row (see raz(1))\n");
183185 Out.str(" -q words with Main.raz:LINE:COL, writes the names that can be\n");
184186 Out.str(" typed at the cursor there, one row each\n");
187+ Out.str(" -q def with Main.raz:LINE:COL, writes where the name at the\n");
188+ Out.str(" cursor is declared, and what it is\n");
185189 Out.str(" -h, --help says this\n");
186190 Out.str(" --version says the version, and the report it follows\n");
187191 Out.str("see raz(1), and the report, doc/raz.md\n");

src/Scan.raz (history) modified +16 -9

@@ -9,8 +9,9 @@
99 For an editor (raz -q), one file's text may come from standard
1010 input instead (setStdin), and a place in it may be marked: the
1111 symbol there is a name, atCursor, standing for what is typed of the
12- name at the cursor, prefix, so that the parser can say what names
13- can be typed there.
12+ name at the cursor, prefix, and for the name written there whole,
13+ word, so that the parser can say what names can be typed there, or
14+ where the name there is declared.
1415
1516 Depends on: Sys, to read files; Diag, to report errors.
1617 Leaves out: recovery from errors in the text itself, such as a string
@@ -90,6 +91,7 @@
9091
9192 atCursor*: INT; (* the name the symbol at the cursor has *)
9293 prefix*: []const U8; (* what is typed of the name at the cursor *)
94+ word*: []const U8; (* the whole name at the cursor, as raz -q def asks of it *)
9395 stdinPath: []const U8; (* the file whose text is standard input, or "" *)
9496 cursorLine, cursorCol: INT; (* the cursor in it, from 1, or 0 for none *)
9597
@@ -1081,10 +1083,11 @@
10811083 end;
10821084
10831085 (* Makes the current symbol the name at the cursor, typed from start up
1084- to at; the cursor is passed then *)
1085-proc cursorName(start, at: INT);
1086+ to at, and written whole up to stop; the cursor is passed then *)
1087+proc cursorName(start, at, stop: INT);
10861088 begin
10871089 prefix := cur.buf[start..at];
1090+ word := cur.buf[start..stop];
10881091 cur.cursor := MAX(INT);
10891092 tok := sIdent;
10901093 id := atCursor;
@@ -1123,12 +1126,16 @@
11231126 cur.tokLineStart := cur.lineStart;
11241127 if p >= cur.cursor then
11251128 (* the cursor is before this symbol: if it was between symbols, not
1126- in a string or a comment, a name of nothing typed is made there *)
1127- if cur.prevEnd <= cur.cursor then
1128- cursorName(cur.cursor, cur.cursor);
1129+ in a string or a comment, a name of nothing typed is made there;
1130+ at the start of a name, the name is the cursor's, read below *)
1131+ if (p = cur.cursor) and (isLetter(c) or (c = '_')) then
1132+ (* the name's own branch makes it *)
1133+ elsif cur.prevEnd <= cur.cursor then
1134+ cursorName(cur.cursor, cur.cursor, cur.cursor);
11291135 return;
1136+ else
1137+ cur.cursor := MAX(INT);
11301138 end;
1131- cur.cursor := MAX(INT);
11321139 end;
11331140 if single[c] # sNull then
11341141 INC(cur.pos);
@@ -1141,7 +1148,7 @@
11411148 repeat INC(p); until not nameChar[buf[p]];
11421149 cur.pos := p;
11431150 if p >= cur.cursor then (* the name the cursor is in or just after *)
1144- cursorName(start, cur.cursor);
1151+ cursorName(start, cur.cursor, p);
11451152 return;
11461153 end;
11471154 if p - start > MaxIdent then

src/Sym.raz (history) modified +13 -4

@@ -438,20 +438,29 @@
438438 end;
439439 end;
440440
441+(* What o is, as raz -q says it: its kind, its name and its type,
442+ split by tabs, written to out *)
443+proc describe*(o: ^Obj; var out: Buf.Buf);
444+var t: []const U8;
445+begin
446+ t := "";
447+ if (o^.typ # nil) and (o^.cls # oModule) and (o^.cls # oBuiltin) then t := typeName(o^.typ); end;
448+ Fmt.write(out, "{}\t{}\t{}", [Fmt.str(kindOf(o)), Fmt.str(Scan.nameOf(o^.name)), Fmt.str(t)]);
449+end;
450+
441451 (* raz -q words: a row in out for each name in reach that starts with
442452 prefix, in scope s as closest has it: its kind, the name and its
443453 type, split by tabs. A name another declaration hides is left out. *)
444454 proc offer*(s: INT; prefix: []const U8; var out: Buf.Buf);
445-var i: INT; o: ^Obj; n, t: []const U8;
455+var i: INT; o: ^Obj; n: []const U8;
446456 begin
447457 for i := 0 to hashMask do
448458 o := hashTab[i];
449459 if reaches(o, s) and ((s >= 0) or (lookup(o^.name) = o)) then
450460 n := Scan.nameOf(o^.name);
451- t := "";
452- if (o^.typ # nil) and (o^.cls # oModule) and (o^.cls # oBuiltin) then t := typeName(o^.typ); end;
453461 if (LEN(n) >= LEN(prefix)) and (n[0..LEN(prefix)] = prefix) then
454- Fmt.write(out, "{}\t{}\t{}\n", [Fmt.str(kindOf(o)), Fmt.str(n), Fmt.str(t)]);
462+ describe(o, out);
463+ Buf.add(out, '\n');
455464 end;
456465 end;
457466 end;

test/check.sh (history) modified +7 -4

@@ -168,12 +168,15 @@
168168 && readelf -s $out/own/Own | grep -q ' Own.main$' && readelf --debug-dump=decodedline $out/own/Own | grep -q 'Own.raz'; then pass=$((pass + 1))
169169 else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi
170170
171-# raz -q, for an editor: check's rows, and the names words offers at a
171+# raz -q, for an editor: check's rows; the names words offers at a
172172 # cursor after a part of a name, after a module's dot, and none in a
173-# comment or a string. The text is standard input: checking an empty
174-# one shows that the file on disk isn't read.
173+# comment or a string; and where def finds a local, another module's
174+# name, a module and a built-in, which has no place. The text is
175+# standard input: checking an empty one shows that the file on disk
176+# isn't read.
175177 for t in "check Check.raz check" "words Words.raz:9:16 prefix" "words Words.raz:10:11 export" \
176- "words Words.raz:11:10 none" "words Words.raz:12:15 none"; do
178+ "words Words.raz:11:10 none" "words Words.raz:12:15 none" "def Def.raz:10:15 def-local" \
179+ "def Def.raz:11:9 def-export" "def Def.raz:11:6 def-module" "def Def.raz:10:24 def-builtin"; do
177180 set -- $t
178181 f=test/query/${2%%:*}
179182 $raz -q $1 test/query/$2 < $f > $out/query.out

test/query/Def.raz (history) added +19 -0

@@ -0,0 +1,19 @@
1+module Def;
2+import Out;
3+
4+var count: INT;
5+
6+(* Says hello. *)
7+proc greet(name: []const U8);
8+var total: INT;
9+begin
10+ total := count + LEN(name);
11+ Out.str(name);
12+end;
13+
14+proc main*;
15+begin
16+ greet("x");
17+end;
18+
19+end Def.

test/query/def-builtin.expect (history) added +1 -0

@@ -0,0 +1 @@
1+ 0 0 0 0 builtin LEN

test/query/def-export.expect (history) added +1 -0

@@ -0,0 +1 @@
1+lib/Out.raz 53 6 53 9 proc str proc([]const U8)

test/query/def-local.expect (history) added +1 -0

@@ -0,0 +1 @@
1+test/query/Def.raz 4 5 4 10 var count INT

test/query/def-module.expect (history) added +1 -0

@@ -0,0 +1 @@
1+lib/Out.raz 1 1 1 1 module Out