raz / commit 27a380c2

commit
27a380c2360becb94585e77d48cf18b15c619aa3
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f1077724
A comment that ended early is shown where it ended

A comment quoting *) ends there, and the rest is read as code; the
error was told just after the symbol before, which may be lines above.
Now, with a comment between, the symbol found is shown too
(foundNotes), and a comment that closed just before the error, as for
a name not declared, is shown with how to write *) (commentNote).
Scan notes where the last comment ended. Nothing a program means
changes. Met in rrem, documenting (?>x*).

patch · log

8 files changed, +117 -22

PROPOSALS.md modified +0 -20

@@ -562,23 +562,3 @@
562562
563563 Met in concha's `Ccore`, in `bufVal` and `listVal`, and in `Sys` over
564564 C, before each call that takes a buffer.
565-
566-## An error told where the wrong token is
567-
568-A comment that quotes a regular expression, `(* matches (?>x*) *)`,
569-ends at the `*)` inside `x*)`, and the rest of it is read as code.
570-The compiler is right to refuse what follows; but it told the error
571-at the start of an earlier declaration, 40 lines from the token that
572-was wrong, and the comment that caused it was hard to find.
573-
574-The proposal: an error is told at the token where the parse failed,
575-as most already are; where the parser reports at the start of a
576-declaration instead, it reports the token as well. A comment that has
577-just closed on a line with more text after it could add a hint:
578-"a comment ended here". Nothing a program means changes.
579-
580-A comment can't quote `*)` at all. That is the report's rule, and
581-Oberon's, and stays; the report could say it in a line, with the
582-spelling to use instead (`x* )` or a word).
583-
584-Met in rrem's parser, documenting `(?>x*)`.

doc/compiler.md (history) modified +7 -1

@@ -937,7 +937,13 @@
937937 `Diag.error` and `Diag.note`, then returns `Failed` itself. A message
938938 names what it found: `found` describes the current symbol and `symText`
939939 spells an operator or reserved word, so that the parser's `expect` and
940-`expected` say `expected ";", found "end"`. Errors in the text itself
940+`expected` say `expected ";", found "end"`. `comment` notes where the
941+last comment ended: `commentBetween` says whether one stands between
942+the symbol before and the current one, and `commentClosed` whether one
943+ended just before a place. With them the parser's `foundNotes` shows
944+the symbol found when a comment put lines between it and the missing
945+one's place, and `commentNote` the end of a comment that closed just
946+before the error, which most likely quoted `*)`. Errors in the text itself
941947 stop the compiler (`markBytes`, `lexError`): past a string left open,
942948 the symbols can't be trusted. A slip from another language, such as `!=`
943949 for `#`, is reported by `slip`, which reads on as if the right symbol

src/Parse.raz (history) modified +33 -1

@@ -173,13 +173,43 @@
173173 return Diag.say("more than {} {}", [Fmt.int(n), Fmt.str(what)]);
174174 end;
175175
176+(* After an error at p: if a comment closed just before it, it most
177+ likely quoted the characters that end one, and ended early; where it
178+ ended is shown. *)
179+proc commentNote(p: Diag.Pos);
180+var closed: Diag.Pos; ok: BOOL;
181+begin
182+ if p.f # Scan.tokPos().f then return; end;
183+ closed, ok := Scan.commentClosed(p.a);
184+ if ok then
185+ Diag.note(closed, "a comment ended here");
186+ Diag.help("a comment that quotes *) writes it apart: * )");
187+ end;
188+end;
189+
190+(* After an error at at about the symbol found: that symbol, if at is
191+ above it with a comment between, as a missing symbol's place may
192+ be (with only blanks between, the two are seen together); and the
193+ comment, if it ended just before the symbol. *)
194+proc foundNotes(at: Diag.Pos);
195+begin
196+ if (at.a # Scan.tokPos().a) and Scan.commentBetween() then
197+ Diag.note(Scan.tokPos(), say1("found here: {}", Scan.found()));
198+ end;
199+ commentNote(Scan.tokPos());
200+end;
201+
176202 (* A missing symbol is reported just after the one before it. *)
177203 proc expected(what: []U8): ERROR;
204+var at: Diag.Pos;
178205 begin
179206 if Scan.tok = Scan.sBlank then
180207 return Scan.mark("_ isn't a name: it is the blank, which stands only left of :=");
181208 end;
182- return Scan.markAt(Scan.missingPos(), say2("expected {}, found {}", what, Scan.found()));
209+ at := Scan.missingPos();
210+ Diag.error(at, say2("expected {}, found {}", what, Scan.found()));
211+ foundNotes(at);
212+ return Scan.Failed;
183213 end;
184214
185215 proc expect(s: Scan.Sym): ERROR;
@@ -211,6 +241,7 @@
211241 proc endMissing(p: Diag.Pos; msg: []U8): ERROR;
212242 begin
213243 Diag.error(p, msg);
244+ foundNotes(p);
214245 if stray then Diag.note(strayAt, "is this statement's \"end\" missing? one further left closes it"); end;
215246 if elseIf then Diag.help("raz writes elsif: \"else if\" begins an if that needs its own \"end\""); end;
216247 return Scan.Failed;
@@ -440,6 +471,7 @@
440471 var found: BOOL; low: []U8;
441472 begin
442473 Diag.error(p, say1("{} isn't declared", nameOf(n)));
474+ commentNote(p);
443475 _, _, found := moduleFile(n);
444476 low := lowerCase(nameOf(n));
445477 if found then Diag.help(say2("{} is a module: import {}", nameOf(n), nameOf(n)));

src/Scan.raz (history) modified +23 -0

@@ -37,6 +37,7 @@
3737 len, pos, line, lineStart: INT;
3838 tokPos, tokLine, tokLineStart: INT;
3939 prevEnd: INT; (* where the symbol before this one ends *)
40+ commentEnd: INT; (* just after the end of the last comment, or 0 *)
4041 tok: Sym;
4142 ival: I64;
4243 rval: F64;
@@ -260,6 +261,27 @@
260261 return p;
261262 end;
262263
264+(* Whether a comment stands between the symbol before the current one
265+ and it, which may put many lines between the two *)
266+proc commentBetween*: BOOL;
267+begin
268+ return cur.commentEnd > cur.prevEnd;
269+end;
270+
271+(* The end of a comment that closed just before byte a of the file
272+ being read, with only blanks between, and true. A comment that
273+ quotes the two characters that end one ends there, and what it went
274+ on to say is read as code. *)
275+proc commentClosed*(a: INT): (Diag.Pos, BOOL);
276+var i: INT;
277+begin
278+ if (cur.commentEnd = 0) or (cur.commentEnd > a) then return {}, false; end;
279+ for i := cur.commentEnd to a - 1 do
280+ if (cur.buf[i] # ' ') and (cur.buf[i] # 9) then return {}, false; end;
281+ end;
282+ return {f: cur.f, a: cur.commentEnd - 2, b: cur.commentEnd}, true;
283+end;
284+
263285 (* From the start of p to the end of the symbol before the current one:
264286 the whole of a construct that has just been read *)
265287 proc spanFrom*(p: Diag.Pos): Diag.Pos;
@@ -668,6 +690,7 @@
668690 INC(cur.pos);
669691 end;
670692 until depth = 0;
693+ cur.commentEnd := cur.pos;
671694 end;
672695
673696 (* Reading a decimal real exactly: the F64 nearest to digits times 10 to

test/err/CommentEnded.err (history) added +11 -0

@@ -0,0 +1,11 @@
1+test/err/CommentEnded.raz:2:12: error: expected "end", found as
2+ 2 | import Out;
3+ | ^
4+test/err/CommentEnded.raz:6:19: note: found here: as
5+ 6 | Matches (?>x*) as one atom, which never gives back what it took,
6+ | ^^
7+test/err/CommentEnded.raz:6:16: note: a comment ended here
8+ 6 | Matches (?>x*) as one atom, which never gives back what it took,
9+ | ^^
10+help: a comment that quotes *) writes it apart: * )
11+raz: 1 error

test/err/CommentEnded.raz (history) added +22 -0

@@ -0,0 +1,22 @@
1+module CommentEnded;
2+import Out;
3+(* a comment that quotes the two characters that end one ends there *)
4+
5+(* Whether s matches the pattern.
6+ Matches (?>x*) as one atom, which never gives back what it took,
7+ so that a pattern stays linear. *)
8+proc matches(s: []U8): BOOL;
9+var i: INT;
10+begin
11+ i := 0;
12+ while i < LEN(s) do
13+ INC(i);
14+ end;
15+ return i > 0;
16+end;
17+
18+proc main*;
19+begin
20+ if matches("a") then Out.ln; end;
21+end;
22+end CommentEnded.

test/err/CommentEndedBody.err (history) added +8 -0

@@ -0,0 +1,8 @@
1+test/err/CommentEndedBody.raz:9:42: error: would isn't declared
2+ 9 | (* skips spaces, as the pattern \s*) would,
3+ | ^^^^^
4+test/err/CommentEndedBody.raz:9:39: note: a comment ended here
5+ 9 | (* skips spaces, as the pattern \s*) would,
6+ | ^^
7+help: a comment that quotes *) writes it apart: * )
8+raz: 1 error

test/err/CommentEndedBody.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+module CommentEndedBody;
2+(* the same inside a procedure, where the rest is read as statements *)
3+import Out;
4+
5+proc main*;
6+var i: INT;
7+begin
8+ i := 0;
9+ (* skips spaces, as the pattern \s*) would,
10+ and stops at the first other byte *)
11+ Out.int(i);
12+end;
13+end CommentEndedBody.