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*).
| @@ -562,23 +562,3 @@ | ||
| 562 | 562 | |
| 563 | 563 | Met in concha's `Ccore`, in `bufVal` and `listVal`, and in `Sys` over |
| 564 | 564 | 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*)`. |
| @@ -937,7 +937,13 @@ | ||
| 937 | 937 | `Diag.error` and `Diag.note`, then returns `Failed` itself. A message |
| 938 | 938 | names what it found: `found` describes the current symbol and `symText` |
| 939 | 939 | 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 | |
| 941 | 947 | stop the compiler (`markBytes`, `lexError`): past a string left open, |
| 942 | 948 | the symbols can't be trusted. A slip from another language, such as `!=` |
| 943 | 949 | for `#`, is reported by `slip`, which reads on as if the right symbol |
| @@ -173,13 +173,43 @@ | ||
| 173 | 173 | return Diag.say("more than {} {}", [Fmt.int(n), Fmt.str(what)]); |
| 174 | 174 | end; |
| 175 | 175 | |
| 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 | + | |
| 176 | 202 | (* A missing symbol is reported just after the one before it. *) |
| 177 | 203 | proc expected(what: []U8): ERROR; |
| 204 | +var at: Diag.Pos; | |
| 178 | 205 | begin |
| 179 | 206 | if Scan.tok = Scan.sBlank then |
| 180 | 207 | return Scan.mark("_ isn't a name: it is the blank, which stands only left of :="); |
| 181 | 208 | 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; | |
| 183 | 213 | end; |
| 184 | 214 | |
| 185 | 215 | proc expect(s: Scan.Sym): ERROR; |
| @@ -211,6 +241,7 @@ | ||
| 211 | 241 | proc endMissing(p: Diag.Pos; msg: []U8): ERROR; |
| 212 | 242 | begin |
| 213 | 243 | Diag.error(p, msg); |
| 244 | + foundNotes(p); | |
| 214 | 245 | if stray then Diag.note(strayAt, "is this statement's \"end\" missing? one further left closes it"); end; |
| 215 | 246 | if elseIf then Diag.help("raz writes elsif: \"else if\" begins an if that needs its own \"end\""); end; |
| 216 | 247 | return Scan.Failed; |
| @@ -440,6 +471,7 @@ | ||
| 440 | 471 | var found: BOOL; low: []U8; |
| 441 | 472 | begin |
| 442 | 473 | Diag.error(p, say1("{} isn't declared", nameOf(n))); |
| 474 | + commentNote(p); | |
| 443 | 475 | _, _, found := moduleFile(n); |
| 444 | 476 | low := lowerCase(nameOf(n)); |
| 445 | 477 | if found then Diag.help(say2("{} is a module: import {}", nameOf(n), nameOf(n))); |
| @@ -37,6 +37,7 @@ | ||
| 37 | 37 | len, pos, line, lineStart: INT; |
| 38 | 38 | tokPos, tokLine, tokLineStart: INT; |
| 39 | 39 | prevEnd: INT; (* where the symbol before this one ends *) |
| 40 | + commentEnd: INT; (* just after the end of the last comment, or 0 *) | |
| 40 | 41 | tok: Sym; |
| 41 | 42 | ival: I64; |
| 42 | 43 | rval: F64; |
| @@ -260,6 +261,27 @@ | ||
| 260 | 261 | return p; |
| 261 | 262 | end; |
| 262 | 263 | |
| 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 | + | |
| 263 | 285 | (* From the start of p to the end of the symbol before the current one: |
| 264 | 286 | the whole of a construct that has just been read *) |
| 265 | 287 | proc spanFrom*(p: Diag.Pos): Diag.Pos; |
| @@ -668,6 +690,7 @@ | ||
| 668 | 690 | INC(cur.pos); |
| 669 | 691 | end; |
| 670 | 692 | until depth = 0; |
| 693 | + cur.commentEnd := cur.pos; | |
| 671 | 694 | end; |
| 672 | 695 | |
| 673 | 696 | (* Reading a decimal real exactly: the F64 nearest to digits times 10 to |
| @@ -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 |
| @@ -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. |
| @@ -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 |
| @@ -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. |