From 27a380c2360becb94585e77d48cf18b15c619aa3 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:23:31 +0100 Subject: [PATCH] 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*). --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 6067fa3..e4779bd 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -562,23 +562,3 @@ Met in concha's `Ccore`, in `bufVal` and `listVal`, and in `Sys` over C, before each call that takes a buffer. - -## An error told where the wrong token is - -A comment that quotes a regular expression, `(* matches (?>x*) *)`, -ends at the `*)` inside `x*)`, and the rest of it is read as code. -The compiler is right to refuse what follows; but it told the error -at the start of an earlier declaration, 40 lines from the token that -was wrong, and the comment that caused it was hard to find. - -The proposal: an error is told at the token where the parse failed, -as most already are; where the parser reports at the start of a -declaration instead, it reports the token as well. A comment that has -just closed on a line with more text after it could add a hint: -"a comment ended here". Nothing a program means changes. - -A comment can't quote `*)` at all. That is the report's rule, and -Oberon's, and stays; the report could say it in a line, with the -spelling to use instead (`x* )` or a word). - -Met in rrem's parser, documenting `(?>x*)`. diff --git a/doc/compiler.md b/doc/compiler.md index 2e5a5a2..c48d28e 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -937,7 +937,13 @@ `Diag.error` and `Diag.note`, then returns `Failed` itself. A message names what it found: `found` describes the current symbol and `symText` spells an operator or reserved word, so that the parser's `expect` and -`expected` say `expected ";", found "end"`. Errors in the text itself +`expected` say `expected ";", found "end"`. `comment` notes where the +last comment ended: `commentBetween` says whether one stands between +the symbol before and the current one, and `commentClosed` whether one +ended just before a place. With them the parser's `foundNotes` shows +the symbol found when a comment put lines between it and the missing +one's place, and `commentNote` the end of a comment that closed just +before the error, which most likely quoted `*)`. Errors in the text itself stop the compiler (`markBytes`, `lexError`): past a string left open, the symbols can't be trusted. A slip from another language, such as `!=` for `#`, is reported by `slip`, which reads on as if the right symbol diff --git a/src/Parse.raz b/src/Parse.raz index d2c63e5..32a49dc 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -173,13 +173,43 @@ return Diag.say("more than {} {}", [Fmt.int(n), Fmt.str(what)]); end; +(* After an error at p: if a comment closed just before it, it most + likely quoted the characters that end one, and ended early; where it + ended is shown. *) +proc commentNote(p: Diag.Pos); +var closed: Diag.Pos; ok: BOOL; +begin + if p.f # Scan.tokPos().f then return; end; + closed, ok := Scan.commentClosed(p.a); + if ok then + Diag.note(closed, "a comment ended here"); + Diag.help("a comment that quotes *) writes it apart: * )"); + end; +end; + +(* After an error at at about the symbol found: that symbol, if at is + above it with a comment between, as a missing symbol's place may + be (with only blanks between, the two are seen together); and the + comment, if it ended just before the symbol. *) +proc foundNotes(at: Diag.Pos); +begin + if (at.a # Scan.tokPos().a) and Scan.commentBetween() then + Diag.note(Scan.tokPos(), say1("found here: {}", Scan.found())); + end; + commentNote(Scan.tokPos()); +end; + (* A missing symbol is reported just after the one before it. *) proc expected(what: []U8): ERROR; +var at: Diag.Pos; begin if Scan.tok = Scan.sBlank then return Scan.mark("_ isn't a name: it is the blank, which stands only left of :="); end; - return Scan.markAt(Scan.missingPos(), say2("expected {}, found {}", what, Scan.found())); + at := Scan.missingPos(); + Diag.error(at, say2("expected {}, found {}", what, Scan.found())); + foundNotes(at); + return Scan.Failed; end; proc expect(s: Scan.Sym): ERROR; @@ -211,6 +241,7 @@ proc endMissing(p: Diag.Pos; msg: []U8): ERROR; begin Diag.error(p, msg); + foundNotes(p); if stray then Diag.note(strayAt, "is this statement's \"end\" missing? one further left closes it"); end; if elseIf then Diag.help("raz writes elsif: \"else if\" begins an if that needs its own \"end\""); end; return Scan.Failed; @@ -440,6 +471,7 @@ var found: BOOL; low: []U8; begin Diag.error(p, say1("{} isn't declared", nameOf(n))); + commentNote(p); _, _, found := moduleFile(n); low := lowerCase(nameOf(n)); if found then Diag.help(say2("{} is a module: import {}", nameOf(n), nameOf(n))); diff --git a/src/Scan.raz b/src/Scan.raz index 6202b00..7835c88 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -37,6 +37,7 @@ len, pos, line, lineStart: INT; tokPos, tokLine, tokLineStart: INT; prevEnd: INT; (* where the symbol before this one ends *) + commentEnd: INT; (* just after the end of the last comment, or 0 *) tok: Sym; ival: I64; rval: F64; @@ -260,6 +261,27 @@ return p; end; +(* Whether a comment stands between the symbol before the current one + and it, which may put many lines between the two *) +proc commentBetween*: BOOL; +begin + return cur.commentEnd > cur.prevEnd; +end; + +(* The end of a comment that closed just before byte a of the file + being read, with only blanks between, and true. A comment that + quotes the two characters that end one ends there, and what it went + on to say is read as code. *) +proc commentClosed*(a: INT): (Diag.Pos, BOOL); +var i: INT; +begin + if (cur.commentEnd = 0) or (cur.commentEnd > a) then return {}, false; end; + for i := cur.commentEnd to a - 1 do + if (cur.buf[i] # ' ') and (cur.buf[i] # 9) then return {}, false; end; + end; + return {f: cur.f, a: cur.commentEnd - 2, b: cur.commentEnd}, true; +end; + (* From the start of p to the end of the symbol before the current one: the whole of a construct that has just been read *) proc spanFrom*(p: Diag.Pos): Diag.Pos; @@ -668,6 +690,7 @@ INC(cur.pos); end; until depth = 0; + cur.commentEnd := cur.pos; end; (* Reading a decimal real exactly: the F64 nearest to digits times 10 to diff --git a/test/err/CommentEnded.err b/test/err/CommentEnded.err new file mode 100644 index 0000000..3dee50c --- /dev/null +++ b/test/err/CommentEnded.err @@ -0,0 +1,11 @@ +test/err/CommentEnded.raz:2:12: error: expected "end", found as + 2 | import Out; + | ^ +test/err/CommentEnded.raz:6:19: note: found here: as + 6 | Matches (?>x*) as one atom, which never gives back what it took, + | ^^ +test/err/CommentEnded.raz:6:16: note: a comment ended here + 6 | Matches (?>x*) as one atom, which never gives back what it took, + | ^^ +help: a comment that quotes *) writes it apart: * ) +raz: 1 error diff --git a/test/err/CommentEnded.raz b/test/err/CommentEnded.raz new file mode 100644 index 0000000..2df59f6 --- /dev/null +++ b/test/err/CommentEnded.raz @@ -0,0 +1,22 @@ +module CommentEnded; +import Out; +(* a comment that quotes the two characters that end one ends there *) + +(* Whether s matches the pattern. + Matches (?>x*) as one atom, which never gives back what it took, + so that a pattern stays linear. *) +proc matches(s: []U8): BOOL; +var i: INT; +begin + i := 0; + while i < LEN(s) do + INC(i); + end; + return i > 0; +end; + +proc main*; +begin + if matches("a") then Out.ln; end; +end; +end CommentEnded. diff --git a/test/err/CommentEndedBody.err b/test/err/CommentEndedBody.err new file mode 100644 index 0000000..5d88a72 --- /dev/null +++ b/test/err/CommentEndedBody.err @@ -0,0 +1,8 @@ +test/err/CommentEndedBody.raz:9:42: error: would isn't declared + 9 | (* skips spaces, as the pattern \s*) would, + | ^^^^^ +test/err/CommentEndedBody.raz:9:39: note: a comment ended here + 9 | (* skips spaces, as the pattern \s*) would, + | ^^ +help: a comment that quotes *) writes it apart: * ) +raz: 1 error diff --git a/test/err/CommentEndedBody.raz b/test/err/CommentEndedBody.raz new file mode 100644 index 0000000..889bb89 --- /dev/null +++ b/test/err/CommentEndedBody.raz @@ -0,0 +1,13 @@ +module CommentEndedBody; +(* the same inside a procedure, where the rest is read as statements *) +import Out; + +proc main*; +var i: INT; +begin + i := 0; + (* skips spaces, as the pattern \s*) would, + and stops at the first other byte *) + Out.int(i); +end; +end CommentEndedBody.