Self: Scan reports an error and returns Failed; Jump goes mark, markAt and markHelp show the error and return Scan.Failed, for the parser to pass up with try; no setjmp, no assembly, no recovery variable. Recover tests the pattern.
| @@ -18,8 +18,8 @@ | ||
| 18 | 18 | names, and has its own targets; nothing in `src/` or `lib/` changes. |
| 19 | 19 | |
| 20 | 20 | The language gained `try` (report 2026-10.20) for this: the parser's |
| 21 | -errors will return an `ERROR` that `try` passes up, and `self/Jump.raz`, | |
| 22 | -which the scanner's `fail` uses now, goes when step 4 lands. | |
| 21 | +errors return an `ERROR` (`Scan.Failed`) that `try` passes up to where | |
| 22 | +it reads on, in place of the seed's `longjmp`. | |
| 23 | 23 | |
| 24 | 24 | Each unit is ported in the seed's order (leaves first) and tested on |
| 25 | 25 | its own against the seed's, on the same inputs, since no program can |
| @@ -1,56 +0,0 @@ | ||
| 1 | -(* Jump: setjmp and longjmp, for a compiler that goes on after an error. | |
| 2 | - The parser reads on from the statement or declaration after one it | |
| 3 | - found wrong; raz has no exceptions, so the scanner's Fail, which the | |
| 4 | - parser's errors end in, jumps back to where the parser saved its | |
| 5 | - place. Written in assembly, since no raz code can return twice. | |
| 6 | - | |
| 7 | - Depends on: nothing. | |
| 8 | - Leaves out: saving anything but what System V says a procedure must | |
| 9 | - keep (rbx, rbp, rsp, r12 to r15) and the place to go on at. A | |
| 10 | - variable changed between save and jump may be held in a register | |
| 11 | - that jump restores, so a variable that is read after the jump is | |
| 12 | - written before save, or lives in memory the compiler can't reach | |
| 13 | - (a field of a record by pointer, a module's variable). *) | |
| 14 | -module Jump; | |
| 15 | - | |
| 16 | -type | |
| 17 | - Env* = record | |
| 18 | - rbx, rbp, rsp, r12, r13, r14, r15, rip: INT; | |
| 19 | - end; | |
| 20 | - | |
| 21 | -(* Saves the place after the call in e, and returns 0. When jump(e) is | |
| 22 | - called later, from any procedure that save's caller is still under, | |
| 23 | - save returns again, with 1. *) | |
| 24 | -proc save*(e: ^Env): INT; | |
| 25 | -code | |
| 26 | - mov rax, [rsp]; | |
| 27 | - lea rcx, [rsp + 8]; | |
| 28 | - mov [rdi], rbx; | |
| 29 | - mov [rdi + 8], rbp; | |
| 30 | - mov [rdi + 16], rcx; | |
| 31 | - mov [rdi + 24], r12; | |
| 32 | - mov [rdi + 32], r13; | |
| 33 | - mov [rdi + 40], r14; | |
| 34 | - mov [rdi + 48], r15; | |
| 35 | - mov [rdi + 56], rax; | |
| 36 | - xor eax, eax; | |
| 37 | - ret; | |
| 38 | -end save; | |
| 39 | - | |
| 40 | -(* Goes back to the save that filled e, which returns 1. *) | |
| 41 | -proc jump*(e: ^Env); | |
| 42 | -code | |
| 43 | - mov rbx, [rdi]; | |
| 44 | - mov rbp, [rdi + 8]; | |
| 45 | - mov rsp, [rdi + 16]; | |
| 46 | - mov r12, [rdi + 24]; | |
| 47 | - mov r13, [rdi + 32]; | |
| 48 | - mov r14, [rdi + 40]; | |
| 49 | - mov r15, [rdi + 48]; | |
| 50 | - mov rcx, [rdi + 56]; | |
| 51 | - mov eax, 1; | |
| 52 | - push rcx; (* a jump to rcx, for which the assembler has no form: *) | |
| 53 | - ret; (* push the place, and return to it *) | |
| 54 | -end jump; | |
| 55 | - | |
| 56 | -end Jump. |
| @@ -25,12 +25,12 @@ | ||
| 25 | 25 | what the parser sets in the seed's `diag` and `scan` (`quiet`, |
| 26 | 26 | `werror`, `recovery`, `skipping`) have setters. The `sbuf` array, |
| 27 | 27 | which can't be sliced outside, is `strBytes`. |
| 28 | -- **Going on after an error.** raz has no `setjmp`: `Jump.raz` is the | |
| 29 | - two in assembly. The parser's 200 or so error sites are deep in its | |
| 30 | - recursion and all end in a jump back to the statement or declaration | |
| 31 | - being read, which is how the seed reports up to 20 errors in a run | |
| 32 | - (15 of the 267 tests of messages show more than one). The assembler | |
| 33 | - has no `jmp reg`, so `jump` is `push rcx; ret`. | |
| 28 | +- **Going on after an error.** raz has no `setjmp`, and the seed's parser | |
| 29 | + ends its errors in a jump back to the statement or declaration being | |
| 30 | + read, which is how it reports up to 20 errors in a run. The language | |
| 31 | + gained `try` (report 2026-10.20) for this: `Scan.mark` and the others | |
| 32 | + show the error and return `Scan.Failed`, which the parser passes up | |
| 33 | + with `try` to where it reads on. `test/Recover.raz` is the pattern. | |
| 34 | 34 | - **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that |
| 35 | 35 | is unset and for one that is empty, so the port reads the second as |
| 36 | 36 | the first. no-color.org says a variable that is empty doesn't turn |
| @@ -6,13 +6,13 @@ | ||
| 6 | 6 | compares names as small integers. The scanner also says where the |
| 7 | 7 | current symbol is, for messages. |
| 8 | 8 | |
| 9 | - Depends on: Sys, to read files; Diag, to report errors; Jump, to go | |
| 10 | - on after the parser's errors. | |
| 9 | + Depends on: Sys, to read files; Diag, to report errors. | |
| 11 | 10 | Leaves out: recovery from errors in the text itself, such as a string |
| 12 | 11 | not closed: the first such error stops the compiler. The parser goes |
| 13 | - on after its own errors, through recovery below. *) | |
| 12 | + on after its own errors: mark and the others report one and give | |
| 13 | + back Failed, which it passes up with try to where it can read on. *) | |
| 14 | 14 | module Scan; |
| 15 | -import Sys, Heap, Mem, Fmt, Str, Diag, Jump; | |
| 15 | +import Sys, Heap, Mem, Fmt, Str, Diag; | |
| 16 | 16 | |
| 17 | 17 | type |
| 18 | 18 | NamePtr = ^U8; |
| @@ -45,6 +45,7 @@ | ||
| 45 | 45 | end; |
| 46 | 46 | |
| 47 | 47 | const |
| 48 | + Failed*: ERROR = "reported"; (* an error has been shown: the parser returns it *) | |
| 48 | 49 | MaxIdent* = 63; |
| 49 | 50 | MaxStr* = 65536; |
| 50 | 51 | NumWords = 43; |
| @@ -73,9 +74,6 @@ | ||
| 73 | 74 | slen*: INT; (* sString: how many bytes *) |
| 74 | 75 | sbuf: [MaxStr + 1]U8; (* sString: its bytes, then a zero *) |
| 75 | 76 | |
| 76 | - (* Where the parser goes on after an error: the innermost statement or | |
| 77 | - declaration being read. nil stops the compiler at the first error. *) | |
| 78 | - recovery: ^Jump.Env; | |
| 79 | 77 | (* The parser is skipping what it has read once already, after an |
| 80 | 78 | error, so slips in it aren't reported again. *) |
| 81 | 79 | skipping: BOOL; |
| @@ -102,11 +100,6 @@ | ||
| 102 | 100 | tableMask: INT; |
| 103 | 101 | |
| 104 | 102 | (* Exported variables can't be set from outside, so these do it. *) |
| 105 | -proc setRecovery*(r: ^Jump.Env); | |
| 106 | -begin | |
| 107 | - recovery := r; | |
| 108 | -end; | |
| 109 | - | |
| 110 | 103 | proc setSkipping*(yes: BOOL); |
| 111 | 104 | begin |
| 112 | 105 | skipping := yes; |
| @@ -290,33 +283,25 @@ | ||
| 290 | 283 | return col; |
| 291 | 284 | end; |
| 292 | 285 | |
| 293 | -(* After an error in the parser's construct, reported with its notes | |
| 294 | - and help: the parser goes on from the recovery point. *) | |
| 295 | -proc fail*; | |
| 296 | -begin | |
| 297 | - if recovery = nil then Diag.finish; end; | |
| 298 | - Jump.jump(recovery); | |
| 299 | -end; | |
| 300 | - | |
| 301 | -(* An error at p: reported, then the parser goes on from its recovery | |
| 302 | - point. *) | |
| 303 | -proc markAt*(p: Diag.Pos; msg: []U8); | |
| 286 | +(* An error at p, shown: the parser returns Failed, which try passes up | |
| 287 | + to where it goes on. *) | |
| 288 | +proc markAt*(p: Diag.Pos; msg: []U8): ERROR; | |
| 304 | 289 | begin |
| 305 | 290 | Diag.error(p, msg); |
| 306 | - fail; | |
| 291 | + return Failed; | |
| 307 | 292 | end; |
| 308 | 293 | |
| 309 | -proc mark*(msg: []U8); | |
| 294 | +proc mark*(msg: []U8): ERROR; | |
| 310 | 295 | begin |
| 311 | - markAt(tokPos(), msg); | |
| 296 | + return markAt(tokPos(), msg); | |
| 312 | 297 | end; |
| 313 | 298 | |
| 314 | 299 | (* markAt, with a line of help after the message. *) |
| 315 | -proc markHelp*(p: Diag.Pos; msg, hint: []U8); | |
| 300 | +proc markHelp*(p: Diag.Pos; msg, hint: []U8): ERROR; | |
| 316 | 301 | begin |
| 317 | 302 | Diag.error(p, msg); |
| 318 | 303 | Diag.help(hint); |
| 319 | - fail; | |
| 304 | + return Failed; | |
| 320 | 305 | end; |
| 321 | 306 | |
| 322 | 307 | (* An error in the text at bytes a to b of the file being read, which |
| @@ -32,4 +32,4 @@ | ||
| 32 | 32 | ./raz -Werror -I self -o $b/recover self/test/Recover.raz |
| 33 | 33 | printf 'a bad b bad bad c\nd bad\n' >$b/rec.raz |
| 34 | 34 | r=$($b/recover $b/rec.raz 2>/dev/null) || test $? = 1 |
| 35 | -test "$r" = "8 names, 4 jumps, 4 errors" && echo "recover ok" | |
| 35 | +test "$r" = "8 names, 4 failed, 4 errors" && echo "recover ok" |
| @@ -1,36 +1,38 @@ | ||
| 1 | -(* Tests Jump through Scan.fail: a driver reads a file's symbols and | |
| 2 | - reports each name `bad` as an error, which jumps back to where it | |
| 3 | - saved its place and goes on, as the parser does after an error. The | |
| 4 | - count lives in a module variable, which a jump leaves as it was. *) | |
| 1 | +(* Tests going on after an error, as the parser does with try: a driver | |
| 2 | + reads a file's symbols and reports each name `bad` as an error, | |
| 3 | + several calls deep; the error comes back up through them, and the | |
| 4 | + driver reads on from the next symbol. *) | |
| 5 | 5 | module Recover; |
| 6 | -import Out, Files, Diag, Scan, Jump; | |
| 6 | +import Out, Files, Diag, Scan; | |
| 7 | 7 | |
| 8 | -var | |
| 9 | - env: Jump.Env; | |
| 10 | - jumps, names: INT; | |
| 8 | +var failed, names: INT; | |
| 11 | 9 | |
| 12 | -proc deep(n: INT); | |
| 10 | +proc deep(n: INT): ERROR; | |
| 13 | 11 | begin |
| 14 | - if n > 0 then deep(n - 1); else Scan.mark("a bad name"); end; | |
| 12 | + if n > 0 then | |
| 13 | + try deep(n - 1); | |
| 14 | + return nil; | |
| 15 | + end; | |
| 16 | + return Scan.mark("a bad name"); | |
| 15 | 17 | end; |
| 16 | 18 | |
| 17 | 19 | proc main*; |
| 20 | +var err: ERROR; | |
| 18 | 21 | begin |
| 19 | 22 | if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; |
| 20 | - Scan.setRecovery(ADR(env)); | |
| 21 | - if Jump.save(ADR(env)) # 0 then INC(jumps); end; | |
| 22 | 23 | while Scan.tok # Scan.sEof do |
| 23 | 24 | Scan.next; |
| 24 | 25 | if Scan.tok = Scan.sIdent then |
| 25 | 26 | INC(names); |
| 26 | - if Scan.nameOf(Scan.id) = "bad" then deep(5); end; | |
| 27 | + if Scan.nameOf(Scan.id) = "bad" then | |
| 28 | + err := deep(5); | |
| 29 | + if err # nil then INC(failed); end; | |
| 30 | + end; | |
| 27 | 31 | end; |
| 28 | 32 | end; |
| 29 | - Out.int(names); Out.str(" names, "); Out.int(jumps); Out.str(" jumps, "); | |
| 33 | + Out.int(names); Out.str(" names, "); Out.int(failed); Out.str(" failed, "); | |
| 30 | 34 | Out.int(Diag.errors); Out.str(" errors\n"); |
| 31 | - Scan.setRecovery(nil); | |
| 32 | - Scan.mark("last"); | |
| 33 | - Out.str("not reached\n"); | |
| 35 | + Diag.finish; | |
| 34 | 36 | end; |
| 35 | 37 | |
| 36 | 38 | end Recover. |