Go on after an error: every error in a run, none twice Each statement and declaration is a recovery point (setjmp): an error jumps back, the scanner rereads the construct and skips to its end, and parsing goes on. Undeclared names become poison where used; errors in constructs that use poison aren't reported, nor a second one at one place. A symbol at a line's start ends a skip and gives up the proc. Lexical errors and a module with errors stop the run. tools/Mutate.raz mutates a symbol of each program; make check fails on a crash, a status > 1 or two errors at one place.
| @@ -227,12 +227,18 @@ | ||
| 227 | 227 | the previous symbol's end. The machinery, *main*; moving each |
| 228 | 228 | of the 200 `Mark` calls onto the right position, *agent*, one |
| 229 | 229 | per file. |
| 230 | -- [ ] 22. Going on after an error. A poison type that every check | |
| 231 | - accepts without a word, so one mistake makes one message; a name | |
| 232 | - not declared is declared as poison where it is first used; after | |
| 233 | - a syntax error the parser skips to the next `proc` or the end of | |
| 234 | - the module; code is still generated, then thrown away; at most | |
| 235 | - 20 errors. A module with errors stops the run after it. *main*. | |
| 230 | +- [x] 22. Going on after an error. Each statement and declaration is | |
| 231 | + a recovery point (`setjmp`): an error jumps back to it, the | |
| 232 | + scanner rereads it from its start and skips to its end, and the | |
| 233 | + parser carries on with the next; no error site goes on past its | |
| 234 | + error, so none needs reviewing. A name not declared is declared | |
| 235 | + with a poison type where it is first used, as are the names of a | |
| 236 | + declaration with an error, and errors in a construct that uses | |
| 237 | + poison aren't reported. A symbol at the start of a line ends a | |
| 238 | + skip: it starts the next declaration, and its procedure is given | |
| 239 | + up. Code is still generated, then thrown away; at most 20 errors; | |
| 240 | + an error in the text, or a module with errors, stops the run. | |
| 241 | + *main*. | |
| 236 | 242 | `tools/Mutate.raz`, which deletes, doubles and swaps symbols in |
| 237 | 243 | the tests that compile; `make check` runs it with fixed seeds |
| 238 | 244 | and fails on a crash or two messages at one place. Writing it |
| @@ -245,7 +251,8 @@ | ||
| 245 | 251 | first; *main* reviews the wording as a whole. |
| 246 | 252 | - [ ] 24. Notes and help. Objects remember where they are declared: |
| 247 | 253 | "declared here", "the first one is here", "the signature is |
| 248 | - here". Did you mean: the closest name by edit distance in the | |
| 254 | + here". The checks on `main` in `raz.pas` point at | |
| 255 | + it, so that every error has a place (the fuzzer finds them). Did you mean: the closest name by edit distance in the | |
| 249 | 256 | scopes in reach, in a module's exports, among the built-ins |
| 250 | 257 | (`len` for `LEN`). Slips from other languages: `!=`, `==`, `=` |
| 251 | 258 | for `:=`, `&&`, `||`, `!`, `elif`, a missing `then` or `do`, a |
| @@ -8,12 +8,12 @@ | ||
| 8 | 8 | |
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | -| `src/diag.pas` | 243 | 174 | Messages: positions, source excerpts, colour, counts | | |
| 12 | -| `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | | |
| 13 | -| `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | | |
| 11 | +| `src/diag.pas` | 248 | 177 | Messages: positions, source excerpts, colour, counts | | |
| 12 | +| `src/scan.pas` | 612 | 509 | Symbols, literals, comments, positions | | |
| 13 | +| `src/sym.pas` | 526 | 437 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | |
| 15 | 15 | | `src/gen.pas` | 1,468 | 1,281 | Items: loading, storing, copying, frames, calls | |
| 16 | -| `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | | |
| 16 | +| `src/parse.pas` | 2,053 | 1,873 | Declarations, statements, expressions, built-ins, recovery | | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | 19 | | `src/raz.pas` | 219 | 172 | The command line, the entry points | |
| @@ -144,7 +144,10 @@ | ||
| 144 | 144 | and `MissingPos` the place to say a symbol is missing: just after the |
| 145 | 145 | symbol before, when the current one starts a new line. Errors go |
| 146 | 146 | through `Mark`, at the current symbol, or `MarkAt`, at a position; each |
| 147 | -reports the error (`diag.Error`) and stops. There is no | |
| 147 | +reports the error (`diag.Error`) and jumps to `recovery`, the parser's | |
| 148 | +place to go on from (see parse.pas). Errors in the text itself stop the | |
| 149 | +compiler (`MarkBytes`, `LexError`): past a string left open, the | |
| 150 | +symbols can't be trusted. There is no | |
| 148 | 151 | recovery: the first error ends the compilation. `SaveScan` and `RestoreScan` keep |
| 149 | 152 | the scanner's state while an imported module is compiled. |
| 150 | 153 | |
| @@ -287,7 +290,8 @@ | ||
| 287 | 290 | `want` parameter carries the type an expression must have, where it is |
| 288 | 291 | known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build. |
| 289 | 292 | |
| 290 | -Then statements: `Statement` dispatches; `IfStatement`, | |
| 293 | +Then statements: `Statement` makes each one a recovery point (below) | |
| 294 | +and `OneStatement` dispatches; `IfStatement`, | |
| 291 | 295 | `WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps |
| 292 | 296 | with `x64.pas`'s labels. `Defer` is worth reading: a deferred statement |
| 293 | 297 | is compiled where it stands, as a little subroutine of the procedure |
| @@ -295,7 +299,8 @@ | ||
| 295 | 299 | way out of its statement list: the end, `return` and `break`. |
| 296 | 300 | |
| 297 | 301 | Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` |
| 298 | -and `extern`. `ProcDecl` handles forward declarations, code procedures | |
| 302 | +and `extern`, each one through `Declaration`, a recovery point, which | |
| 303 | +calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures | |
| 299 | 304 | (`CodeBody`, which just copies the bytes) and bodies (`Body`). |
| 300 | 305 | `ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`, |
| 301 | 306 | `EnumType`) build `TType`s. |
| @@ -308,6 +313,22 @@ | ||
| 308 | 313 | is compiled once per distinct list of arguments: `ParseArgs` reads |
| 309 | 314 | them at the import, `Params` binds them to the module's parameters, and |
| 310 | 315 | `IsModule` decides whether an instance already exists. |
| 316 | + | |
| 317 | +**Recovery.** After an error the parser carries on, so that one run | |
| 318 | +reports them all. `Guard` notes, at the start of a statement or | |
| 319 | +declaration, the scanner's state and the parser's; `setjmp` marks the | |
| 320 | +place, and `MarkAt` jumps back to it after reporting. `Restore` puts | |
| 321 | +the state back, the construct's first symbol current again, and | |
| 322 | +`SkipConstruct` reads past it, counting the words that open and close | |
| 323 | +blocks. A symbol at the start of a line ends the skip: by the usual | |
| 324 | +layout it starts the next declaration, so the statement gives up its | |
| 325 | +procedure (`GiveUp`), and `SkipProc` reads to the procedure's | |
| 326 | +`end Name;`. A name not declared is declared where it is used, with | |
| 327 | +the type `tPoison` (`Poison`), as are the names of a declaration that | |
| 328 | +had an error; a construct that uses poison sets `quiet`, and its errors | |
| 329 | +aren't reported, since they follow from one that was. Nothing breaks | |
| 330 | +for the half-made code: once there is an error, no executable is | |
| 331 | +written. | |
| 311 | 332 | |
| 312 | 333 | The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each |
| 313 | 334 | parses its own arguments, since `SIZE` takes a type and `INC` a |
| @@ -853,9 +853,11 @@ | ||
| 853 | 853 | where they help, notes pointing at other places, such as where a name |
| 854 | 854 | is declared, and a suggestion, such as a name in reach spelled almost |
| 855 | 855 | alike. It goes on after an error to find the others, up to 20, and |
| 856 | -writes no executable. After an error of syntax it skips to the next | |
| 857 | -procedure, so that one misplaced symbol brings no false reports after | |
| 858 | -it. A module with errors stops the compile after it. | |
| 856 | +writes no executable. After an error it skips the statement or | |
| 857 | +declaration the error is in and carries on with the next; a name that | |
| 858 | +isn't declared is reported once, and nothing that follows from it is | |
| 859 | +reported. An error in the text itself, such as a string left open, | |
| 860 | +stops the compile, and so does a module with errors, after it. | |
| 859 | 861 | |
| 860 | 862 | The compiler also warns of what is declared and never used: an import; |
| 861 | 863 | a constant, type, variable or procedure that isn't exported; a |
| @@ -60,6 +60,7 @@ | ||
| 60 | 60 | numFiles, cap: longint; |
| 61 | 61 | colour, colourKnown: boolean; |
| 62 | 62 | cwd: shortstring; { the working directory and a /, or '' if unknown } |
| 63 | + lastError: TPos; { where the last error was } | |
| 63 | 64 | |
| 64 | 65 | { Paths are shown from the working directory where they are under it, |
| 65 | 66 | so messages read the same wherever the compiler or its library is. } |
| @@ -189,6 +190,10 @@ | ||
| 189 | 190 | |
| 190 | 191 | procedure Error(const p: TPos; const msg: shortstring); |
| 191 | 192 | begin |
| 193 | + { a second error at one place follows from the first: when a symbol | |
| 194 | + ends a construct with an error, the one around it may stop there too } | |
| 195 | + if (errors > 0) and (p.f = lastError.f) and (p.a = lastError.a) then exit; | |
| 196 | + lastError := p; | |
| 192 | 197 | Show(p, 'error', Red, msg); |
| 193 | 198 | inc(errors); |
| 194 | 199 | if errors >= MaxErrors then begin |
| @@ -52,6 +52,7 @@ | ||
| 52 | 52 | checkMode: boolean; { reading a file only for its interface } |
| 53 | 53 | inTypes: boolean; { in a type section: ^T may name a later type } |
| 54 | 54 | fwdTypes: array[0..255] of PType; |
| 55 | + fwdPos: array[0..255] of TPos; { where each names its type } | |
| 55 | 56 | numFwd: longint; |
| 56 | 57 | declExported: boolean; { the export mark of the declaration being read } |
| 57 | 58 | curLoop: PLoop; { the innermost loop, or nil } |
| @@ -64,6 +65,9 @@ | ||
| 64 | 65 | inExtern: boolean; { reading an extern's signature: ... allowed } |
| 65 | 66 | tArgv, tBodySig: PType; |
| 66 | 67 | nameRt, nameFail, nameEq, nameMain, nameBody, nameStatic: longint; |
| 68 | + declaring: array[0..63] of longint; { the names the declaration being read declares } | |
| 69 | + numDeclaring: longint; | |
| 70 | + skipped: boolean; { the last statement had an error and was skipped } | |
| 67 | 71 | |
| 68 | 72 | procedure Expression(var x: TItem; want: PType); forward; |
| 69 | 73 | function ParseType: PType; forward; |
| @@ -106,7 +110,122 @@ | ||
| 106 | 110 | curModule^.last := o; |
| 107 | 111 | end; |
| 108 | 112 | end; |
| 113 | + | |
| 114 | +{ Recovery | |
| 109 | 115 | |
| 116 | + After an error the parser goes on with the next statement or | |
| 117 | + declaration, so that one run reports every error. Each statement and | |
| 118 | + declaration is a recovery point: it notes the state of the scanner | |
| 119 | + and the parser where it starts, and an error (MarkAt) jumps back to | |
| 120 | + it. The state is put back, the scanner reads the construct again | |
| 121 | + from its start, skipping to its end, and the parser carries on after | |
| 122 | + it. Nothing is generated after an error, so the code half made for | |
| 123 | + the construct does no harm. A name that isn't declared is declared | |
| 124 | + where it is used, with type tPoison, and what follows from it is not | |
| 125 | + reported again; so are the names of a declaration that had an error. | |
| 126 | + | |
| 127 | + Skipping counts the words that open and close blocks. A symbol at | |
| 128 | + the start of a line is taken to start a declaration of the module, | |
| 129 | + as layout has it: a statement skipped up to one gives up its | |
| 130 | + procedure, and the procedure is skipped up to its end. } | |
| 131 | + | |
| 132 | +type | |
| 133 | + TRecover = record | |
| 134 | + jb: jmp_buf; | |
| 135 | + outer: ^jmp_buf; | |
| 136 | + scan: TScanState; | |
| 137 | + scopes, depth, numDefers: longint; | |
| 138 | + loop: PLoop; | |
| 139 | + result: PType; | |
| 140 | + deferring, directDefer, quiet: boolean; | |
| 141 | + end; | |
| 142 | + | |
| 143 | +procedure Guard(var r: TRecover); | |
| 144 | +begin | |
| 145 | + r.outer := recovery; | |
| 146 | + SaveScan(r.scan); | |
| 147 | + r.scopes := ScopeDepth; r.depth := depth; r.numDefers := numDefers; | |
| 148 | + r.loop := curLoop; r.result := curResult; | |
| 149 | + r.deferring := deferring; r.directDefer := directDefer; r.quiet := quiet; | |
| 150 | + recovery := @r.jb; | |
| 151 | + quiet := false; | |
| 152 | +end; | |
| 153 | + | |
| 154 | +procedure Unguard(var r: TRecover); | |
| 155 | +begin | |
| 156 | + recovery := r.outer; | |
| 157 | + quiet := r.quiet; | |
| 158 | +end; | |
| 159 | + | |
| 160 | +{ After an error: the state as it was at the start of r's construct, | |
| 161 | + whose first symbol is again the current one } | |
| 162 | +procedure Restore(var r: TRecover); | |
| 163 | +begin | |
| 164 | + RestoreScan(r.scan); | |
| 165 | + CutScopes(r.scopes); depth := r.depth; numDefers := r.numDefers; | |
| 166 | + curLoop := r.loop; curResult := r.result; | |
| 167 | + deferring := r.deferring; directDefer := r.directDefer; | |
| 168 | + inTypes := false; declExported := false; inExtern := false; | |
| 169 | +end; | |
| 170 | + | |
| 171 | +{ Gives up the construct r is for, and the one around it } | |
| 172 | +procedure GiveUp(var r: TRecover); | |
| 173 | +begin | |
| 174 | + Unguard(r); | |
| 175 | + if recovery = nil then Finish; | |
| 176 | + longjmp(recovery^, 1); | |
| 177 | +end; | |
| 178 | + | |
| 179 | +{ Reads to the end of a statement or declaration, from its first | |
| 180 | + symbol: up to the ";" that ends it, not read, or to the "end", | |
| 181 | + "else", "elsif", "until" or "|" of the statement around it. False if | |
| 182 | + it stopped at a symbol that starts a line, or the end of the file. } | |
| 183 | +function SkipConstruct: boolean; | |
| 184 | +var d, p: longint; | |
| 185 | +begin | |
| 186 | + d := 0; p := 0; | |
| 187 | + repeat | |
| 188 | + case tok of | |
| 189 | + sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); | |
| 190 | + sEnd, sUntil: if d = 0 then exit(true) else dec(d); | |
| 191 | + sElse, sElsif, sBar: if (d = 0) and (p = 0) then exit(true); | |
| 192 | + sLparen, sLbrak, sLbrace: inc(p); | |
| 193 | + sRparen, sRbrak, sRbrace: if p > 0 then dec(p); | |
| 194 | + sSemicolon: if (d = 0) and (p = 0) then exit(true); | |
| 195 | + sEof: exit(false); | |
| 196 | + end; | |
| 197 | + Next; | |
| 198 | + until AtLineStart; | |
| 199 | + SkipConstruct := false; | |
| 200 | +end; | |
| 201 | + | |
| 202 | +{ Declares n where it is used, after it was reported as not declared, | |
| 203 | + or after the declaration of it had an error } | |
| 204 | +function Poison(n: longint): PObj; | |
| 205 | +var o: PObj; | |
| 206 | +begin | |
| 207 | + o := NewObj(oConst, n, tPoison); | |
| 208 | + Insert(o, TopScope); | |
| 209 | + Poison := o; | |
| 210 | +end; | |
| 211 | + | |
| 212 | +procedure WillDeclare(n: longint); | |
| 213 | +begin | |
| 214 | + if numDeclaring <= High(declaring) then declaring[numDeclaring] := n; | |
| 215 | + inc(numDeclaring); | |
| 216 | +end; | |
| 217 | + | |
| 218 | +{ After a declaration had an error: its names are poison, and reading | |
| 219 | + goes on after it } | |
| 220 | +procedure SkipDeclaration; | |
| 221 | +var i: longint; | |
| 222 | +begin | |
| 223 | + for i := 0 to numDeclaring - 1 do | |
| 224 | + if (i <= High(declaring)) and (Find(TopScope, declaring[i]) = nil) then | |
| 225 | + Poison(declaring[i]); | |
| 226 | + if SkipConstruct and (tok = sSemicolon) then Next; | |
| 227 | +end; | |
| 228 | + | |
| 110 | 229 | function ConstValue(var x: TItem): int64; |
| 111 | 230 | begin |
| 112 | 231 | if x.mode <> mConst then MarkAt(x.pos, 'constant expected'); |
| @@ -126,7 +245,12 @@ | ||
| 126 | 245 | begin |
| 127 | 246 | if tok <> sIdent then MarkAt(MissingPos, 'name expected'); |
| 128 | 247 | o := Lookup(id); |
| 129 | - if o = nil then Mark(NameOf(id) + ' isn''t declared'); | |
| 248 | + if o = nil then begin | |
| 249 | + Poison(id); | |
| 250 | + quiet := false; | |
| 251 | + Mark(NameOf(id) + ' isn''t declared'); | |
| 252 | + end; | |
| 253 | + if o^.typ = tPoison then quiet := true; | |
| 130 | 254 | Next; |
| 131 | 255 | if o^.cls = oModule then begin |
| 132 | 256 | Expect(sPeriod, '.'); |
| @@ -396,6 +520,7 @@ | ||
| 396 | 520 | t^.fwd := id; |
| 397 | 521 | if numFwd > High(fwdTypes) then Mark('too many forward pointers'); |
| 398 | 522 | fwdTypes[numFwd] := t; |
| 523 | + fwdPos[numFwd] := TokPos; | |
| 399 | 524 | inc(numFwd); |
| 400 | 525 | Next; |
| 401 | 526 | end else |
| @@ -1161,7 +1286,7 @@ | ||
| 1161 | 1286 | inc(numDefers); |
| 1162 | 1287 | end; |
| 1163 | 1288 | |
| 1164 | -procedure Statement; | |
| 1289 | +procedure OneStatement; | |
| 1165 | 1290 | var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; |
| 1166 | 1291 | begin |
| 1167 | 1292 | start := TokPos; |
| @@ -1218,11 +1343,30 @@ | ||
| 1218 | 1343 | Jmp(curLoop^.brk); |
| 1219 | 1344 | end; |
| 1220 | 1345 | sDefer: Defer; |
| 1346 | + else if AtLineStart then MarkAt(MissingPos, 'end expected') | |
| 1221 | 1347 | else MarkAt(MissingPos, 'statement expected'); |
| 1222 | 1348 | end; |
| 1223 | 1349 | ReleaseTemps(m); |
| 1224 | 1350 | end; |
| 1225 | 1351 | |
| 1352 | +procedure Statement; | |
| 1353 | +var r: TRecover; m: int64; | |
| 1354 | +begin | |
| 1355 | + m := TempMark; | |
| 1356 | + Guard(r); | |
| 1357 | + if setjmp(r.jb) = 0 then begin | |
| 1358 | + OneStatement; | |
| 1359 | + skipped := false; | |
| 1360 | + end else begin | |
| 1361 | + Restore(r); | |
| 1362 | + { a statement never starts a line: this is the next declaration } | |
| 1363 | + if AtLineStart or not SkipConstruct then GiveUp(r); | |
| 1364 | + ReleaseTemps(m); | |
| 1365 | + skipped := true; | |
| 1366 | + end; | |
| 1367 | + Unguard(r); | |
| 1368 | +end; | |
| 1369 | + | |
| 1226 | 1370 | procedure Statements; |
| 1227 | 1371 | var base: longint; old: boolean; |
| 1228 | 1372 | begin |
| @@ -1231,7 +1375,7 @@ | ||
| 1231 | 1375 | directDefer := false; |
| 1232 | 1376 | while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin |
| 1233 | 1377 | Statement; |
| 1234 | - Expect(sSemicolon, ';'); | |
| 1378 | + if not skipped or (tok = sSemicolon) then Expect(sSemicolon, ';'); | |
| 1235 | 1379 | end; |
| 1236 | 1380 | RunDefers(base); |
| 1237 | 1381 | numDefers := base; |
| @@ -1383,6 +1527,7 @@ | ||
| 1383 | 1527 | Next; |
| 1384 | 1528 | at := TokPos; |
| 1385 | 1529 | IdentDef(n, exp); |
| 1530 | + WillDeclare(n); | |
| 1386 | 1531 | at := SpanFrom(at); |
| 1387 | 1532 | inExtern := true; |
| 1388 | 1533 | t := Signature; |
| @@ -1399,6 +1544,7 @@ | ||
| 1399 | 1544 | if tok <> sVar then MarkAt(MissingPos, 'proc or var expected'); |
| 1400 | 1545 | Next; |
| 1401 | 1546 | IdentDef(n, exp); |
| 1547 | + WillDeclare(n); | |
| 1402 | 1548 | Expect(sColon, ':'); |
| 1403 | 1549 | declExported := exp; |
| 1404 | 1550 | t := ParseType; |
| @@ -1411,103 +1557,158 @@ | ||
| 1411 | 1557 | Declare(o); |
| 1412 | 1558 | end; |
| 1413 | 1559 | |
| 1414 | -procedure Declarations(top: boolean); | |
| 1560 | +procedure ConstDecl(top: boolean); | |
| 1561 | +var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start: TPos; | |
| 1562 | +begin | |
| 1563 | + IdentDef(n, exp); | |
| 1564 | + WillDeclare(n); | |
| 1565 | + t := nil; | |
| 1566 | + if tok = sColon then begin | |
| 1567 | + Next; | |
| 1568 | + start := TokPos; | |
| 1569 | + t := ParseType; | |
| 1570 | + if t <> tError then MarkAt(SpanFrom(start), 'only an ERROR constant has a type'); | |
| 1571 | + end; | |
| 1572 | + Expect(sEql, '='); | |
| 1573 | + Expression(x, nil); | |
| 1574 | + o := NewObj(oConst, n, x.typ); | |
| 1575 | + o^.exported := exp and top; | |
| 1576 | + if t = tError then begin | |
| 1577 | + o^.typ := tError; | |
| 1578 | + o^.val := ErrorChunk(x, curModule^.sname, n) + 1; | |
| 1579 | + end else if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 1580 | + else o^.val := ConstValue(x); | |
| 1581 | + Declare(o); | |
| 1582 | + Expect(sSemicolon, ';'); | |
| 1583 | +end; | |
| 1584 | + | |
| 1585 | +procedure TypeDecl(top: boolean); | |
| 1586 | +var n: longint; exp: boolean; o: PObj; t: PType; | |
| 1587 | +begin | |
| 1588 | + IdentDef(n, exp); | |
| 1589 | + WillDeclare(n); | |
| 1590 | + Expect(sEql, '='); | |
| 1591 | + declExported := exp and top; | |
| 1592 | + inTypes := true; | |
| 1593 | + t := ParseType; | |
| 1594 | + o := NewObj(oType, n, t); | |
| 1595 | + o^.exported := exp and top; | |
| 1596 | + if t^.obj = nil then t^.obj := o; | |
| 1597 | + Declare(o); | |
| 1598 | + Expect(sSemicolon, ';'); | |
| 1599 | +end; | |
| 1600 | + | |
| 1601 | +procedure VarDecl(top: boolean); | |
| 1415 | 1602 | var |
| 1416 | - n, i, k: longint; exp: boolean; x: TItem; o: PObj; t: PType; | |
| 1417 | - names: array[0..63] of longint; exps: array[0..63] of boolean; start: TPos; | |
| 1603 | + i, k: longint; o: PObj; t: PType; | |
| 1604 | + names: array[0..63] of longint; exps: array[0..63] of boolean; | |
| 1605 | +begin | |
| 1606 | + k := 0; | |
| 1607 | + repeat | |
| 1608 | + if k > High(names) then Mark('too many names'); | |
| 1609 | + IdentDef(names[k], exps[k]); | |
| 1610 | + WillDeclare(names[k]); | |
| 1611 | + inc(k); | |
| 1612 | + if tok <> sComma then break; | |
| 1613 | + Next; | |
| 1614 | + until false; | |
| 1615 | + Expect(sColon, ':'); | |
| 1616 | + declExported := top and exps[0]; | |
| 1617 | + t := ParseType; | |
| 1618 | + declExported := false; | |
| 1619 | + for i := 0 to k - 1 do begin | |
| 1620 | + o := NewObj(oVar, names[i], t); | |
| 1621 | + o^.exported := exps[i] and top; | |
| 1622 | + o^.isVar := false; | |
| 1623 | + if top then begin | |
| 1624 | + o^.lev := 0; | |
| 1625 | + o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); | |
| 1626 | + SetBss(o^.chunk, t^.size, t^.align); | |
| 1627 | + end else begin | |
| 1628 | + o^.lev := 1; | |
| 1629 | + o^.val := AllocPerm(t^.size, t^.align); | |
| 1630 | + end; | |
| 1631 | + Declare(o); | |
| 1632 | + end; | |
| 1633 | + Expect(sSemicolon, ';'); | |
| 1634 | +end; | |
| 1635 | + | |
| 1636 | +{ After a procedure had an error: its name is poison if it wasn't | |
| 1637 | + declared, and reading goes on after the "end Name;" that closes it, | |
| 1638 | + or at the next procedure if that is missing } | |
| 1639 | +procedure SkipProc; | |
| 1640 | +var st: TScanState; | |
| 1641 | +begin | |
| 1642 | + Next; | |
| 1643 | + if (tok = sIdent) and (Find(TopScope, id) = nil) then Poison(id); | |
| 1644 | + repeat | |
| 1645 | + repeat Next until AtLineStart or (tok = sEof); | |
| 1646 | + if tok in [sProc, sExtern, sEof] then exit; | |
| 1647 | + if tok = sEnd then begin | |
| 1648 | + SaveScan(st); | |
| 1649 | + Next; | |
| 1650 | + if tok = sIdent then Next; | |
| 1651 | + if tok = sSemicolon then begin Next; exit end; | |
| 1652 | + RestoreScan(st); | |
| 1653 | + exit; | |
| 1654 | + end; | |
| 1655 | + until false; | |
| 1656 | +end; | |
| 1657 | + | |
| 1658 | +{ One declaration, of the kind k, as a recovery point } | |
| 1659 | +procedure Declaration(k: TSym; top: boolean); | |
| 1660 | +var r: TRecover; | |
| 1661 | +begin | |
| 1662 | + Guard(r); | |
| 1663 | + numDeclaring := 0; | |
| 1664 | + if setjmp(r.jb) = 0 then | |
| 1665 | + case k of | |
| 1666 | + sConst: ConstDecl(top); | |
| 1667 | + sType: TypeDecl(top); | |
| 1668 | + sVar: VarDecl(top); | |
| 1669 | + sProc: begin | |
| 1670 | + if not top then Mark('procedures don''t nest'); | |
| 1671 | + ProcDecl; | |
| 1672 | + Expect(sSemicolon, ';'); | |
| 1673 | + end; | |
| 1674 | + sExtern: begin | |
| 1675 | + if not top then Mark('extern only at the top of a module'); | |
| 1676 | + ExternDecl; | |
| 1677 | + Expect(sSemicolon, ';'); | |
| 1678 | + end; | |
| 1679 | + end | |
| 1680 | + else begin | |
| 1681 | + Restore(r); | |
| 1682 | + if k = sProc then SkipProc else SkipDeclaration; | |
| 1683 | + end; | |
| 1684 | + Unguard(r); | |
| 1685 | +end; | |
| 1686 | + | |
| 1687 | +procedure Declarations(top: boolean); | |
| 1688 | +var i: longint; o: PObj; k: TSym; | |
| 1418 | 1689 | begin |
| 1419 | 1690 | repeat |
| 1420 | - case tok of | |
| 1421 | - sConst: begin | |
| 1691 | + k := tok; | |
| 1692 | + case k of | |
| 1693 | + sConst, sVar: begin | |
| 1422 | 1694 | Next; |
| 1423 | - while tok = sIdent do begin | |
| 1424 | - IdentDef(n, exp); | |
| 1425 | - t := nil; | |
| 1426 | - if tok = sColon then begin | |
| 1427 | - Next; | |
| 1428 | - start := TokPos; | |
| 1429 | - t := ParseType; | |
| 1430 | - if t <> tError then MarkAt(SpanFrom(start), 'only an ERROR constant has a type'); | |
| 1431 | - end; | |
| 1432 | - Expect(sEql, '='); | |
| 1433 | - Expression(x, nil); | |
| 1434 | - o := NewObj(oConst, n, x.typ); | |
| 1435 | - o^.exported := exp and top; | |
| 1436 | - if t = tError then begin | |
| 1437 | - o^.typ := tError; | |
| 1438 | - o^.val := ErrorChunk(x, curModule^.sname, n) + 1; | |
| 1439 | - end else if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 1440 | - else o^.val := ConstValue(x); | |
| 1441 | - Declare(o); | |
| 1442 | - Expect(sSemicolon, ';'); | |
| 1443 | - end; | |
| 1695 | + while tok = sIdent do Declaration(k, top); | |
| 1444 | 1696 | end; |
| 1445 | 1697 | sType: begin |
| 1446 | 1698 | Next; |
| 1447 | - inTypes := true; | |
| 1448 | 1699 | numFwd := 0; |
| 1449 | - while tok = sIdent do begin | |
| 1450 | - IdentDef(n, exp); | |
| 1451 | - Expect(sEql, '='); | |
| 1452 | - declExported := exp and top; | |
| 1453 | - t := ParseType; | |
| 1454 | - o := NewObj(oType, n, t); | |
| 1455 | - o^.exported := exp and top; | |
| 1456 | - if t^.obj = nil then t^.obj := o; | |
| 1457 | - Declare(o); | |
| 1458 | - Expect(sSemicolon, ';'); | |
| 1459 | - end; | |
| 1700 | + while tok = sIdent do Declaration(sType, top); | |
| 1701 | + inTypes := false; | |
| 1460 | 1702 | for i := 0 to numFwd - 1 do begin |
| 1461 | 1703 | o := Lookup(fwdTypes[i]^.fwd); |
| 1462 | - if (o = nil) or (o^.cls <> oType) then Mark(NameOf(fwdTypes[i]^.fwd) + ' isn''t declared'); | |
| 1463 | - fwdTypes[i]^.base := o^.typ; | |
| 1704 | + if (o = nil) or (o^.cls <> oType) then begin | |
| 1705 | + Error(fwdPos[i], NameOf(fwdTypes[i]^.fwd) + ' isn''t declared'); | |
| 1706 | + fwdTypes[i]^.base := tPoison; | |
| 1707 | + end else fwdTypes[i]^.base := o^.typ; | |
| 1464 | 1708 | fwdTypes[i]^.fwd := -1; |
| 1465 | 1709 | end; |
| 1466 | - inTypes := false; | |
| 1467 | - declExported := false; | |
| 1468 | 1710 | end; |
| 1469 | - sVar: begin | |
| 1470 | - Next; | |
| 1471 | - while tok = sIdent do begin | |
| 1472 | - k := 0; | |
| 1473 | - repeat | |
| 1474 | - if k > High(names) then Mark('too many names'); | |
| 1475 | - IdentDef(names[k], exps[k]); | |
| 1476 | - inc(k); | |
| 1477 | - if tok <> sComma then break; | |
| 1478 | - Next; | |
| 1479 | - until false; | |
| 1480 | - Expect(sColon, ':'); | |
| 1481 | - declExported := top and exps[0]; | |
| 1482 | - t := ParseType; | |
| 1483 | - declExported := false; | |
| 1484 | - for i := 0 to k - 1 do begin | |
| 1485 | - o := NewObj(oVar, names[i], t); | |
| 1486 | - o^.exported := exps[i] and top; | |
| 1487 | - o^.isVar := false; | |
| 1488 | - if top then begin | |
| 1489 | - o^.lev := 0; | |
| 1490 | - o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); | |
| 1491 | - SetBss(o^.chunk, t^.size, t^.align); | |
| 1492 | - end else begin | |
| 1493 | - o^.lev := 1; | |
| 1494 | - o^.val := AllocPerm(t^.size, t^.align); | |
| 1495 | - end; | |
| 1496 | - Declare(o); | |
| 1497 | - end; | |
| 1498 | - Expect(sSemicolon, ';'); | |
| 1499 | - end; | |
| 1500 | - end; | |
| 1501 | - sProc: begin | |
| 1502 | - if not top then Mark('procedures don''t nest'); | |
| 1503 | - ProcDecl; | |
| 1504 | - Expect(sSemicolon, ';'); | |
| 1505 | - end; | |
| 1506 | - sExtern: begin | |
| 1507 | - if not top then Mark('extern only at the top of a module'); | |
| 1508 | - ExternDecl; | |
| 1509 | - Expect(sSemicolon, ';'); | |
| 1510 | - end; | |
| 1711 | + sProc, sExtern: Declaration(k, top); | |
| 1511 | 1712 | else exit; |
| 1512 | 1713 | end; |
| 1513 | 1714 | until false; |
| @@ -1716,9 +1917,10 @@ | ||
| 1716 | 1917 | if tok <> sPeriod then MarkAt(MissingPos, '. expected'); |
| 1717 | 1918 | o := m^.first; |
| 1718 | 1919 | while o <> nil do begin |
| 1719 | - if (o^.cls = oProc) and o^.fwd then Mark(NameOf(o^.name) + ' is declared forward but never defined'); | |
| 1920 | + if (o^.cls = oProc) and o^.fwd then Error(TokPos, NameOf(o^.name) + ' is declared forward but never defined'); | |
| 1720 | 1921 | o := o^.link; |
| 1721 | 1922 | end; |
| 1923 | + if errors > 0 then Finish; { a module with errors stops the compiler after it } | |
| 1722 | 1924 | CloseScope; |
| 1723 | 1925 | m^.done := true; |
| 1724 | 1926 | if not checkOnly then begin |
| @@ -7,7 +7,9 @@ | ||
| 7 | 7 | current symbol is, for messages. |
| 8 | 8 | |
| 9 | 9 | Depends on: BaseUnix, to read files; diag, to report errors. |
| 10 | - Leaves out: recovery from errors. The first error stops the compiler. } | |
| 10 | + Leaves out: recovery from errors in the text itself, such as a string | |
| 11 | + not closed: the first such error stops the compiler. The parser goes | |
| 12 | + on after its own errors, through recovery below. } | |
| 11 | 13 | |
| 12 | 14 | unit scan; |
| 13 | 15 | |
| @@ -68,6 +70,15 @@ | ||
| 68 | 70 | procedure Next; |
| 69 | 71 | procedure Mark(const msg: shortstring); |
| 70 | 72 | procedure MarkAt(const p: TPos; const msg: shortstring); |
| 73 | +function AtLineStart: boolean; | |
| 74 | + | |
| 75 | +var | |
| 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: ^jmp_buf; | |
| 79 | + { The construct being read uses a name already reported as not | |
| 80 | + declared: its errors follow from that one, and aren't reported. } | |
| 81 | + quiet: boolean; | |
| 71 | 82 | procedure SaveScan(var st: TScanState); |
| 72 | 83 | procedure RestoreScan(const st: TScanState); |
| 73 | 84 | function TokPos: TPos; |
| @@ -219,20 +230,37 @@ | ||
| 219 | 230 | MarkAt(TokPos, msg); |
| 220 | 231 | end; |
| 221 | 232 | |
| 222 | -{ Report at the bytes a to b of the file being read } | |
| 233 | +{ An error in the parser's construct at p: reported, then the parser | |
| 234 | + goes on from the recovery point. } | |
| 235 | +procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 236 | +begin | |
| 237 | + if not quiet then Error(p, msg); | |
| 238 | + if recovery = nil then Finish; | |
| 239 | + longjmp(recovery^, 1); | |
| 240 | +end; | |
| 241 | + | |
| 242 | +{ An error in the text at bytes a to b of the file being read, which | |
| 243 | + stops the compiler: past it, the symbols can't be trusted. } | |
| 223 | 244 | procedure MarkBytes(a, b: longint; const msg: shortstring); |
| 224 | 245 | var p: TPos; |
| 225 | 246 | begin |
| 226 | 247 | p.f := cur.f; p.a := a; p.b := b; |
| 227 | - MarkAt(p, msg); | |
| 248 | + Error(p, msg); | |
| 249 | + Finish; | |
| 228 | 250 | end; |
| 229 | 251 | |
| 230 | -procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 252 | +procedure LexError(const msg: shortstring); | |
| 231 | 253 | begin |
| 232 | - Error(p, msg); | |
| 233 | - Finish; | |
| 254 | + MarkBytes(cur.tokPos, cur.pos, msg); | |
| 234 | 255 | end; |
| 235 | 256 | |
| 257 | +{ The current symbol is the first on its line: at the top level of a | |
| 258 | + module, by the usual layout, so where reading can start again. } | |
| 259 | +function AtLineStart: boolean; | |
| 260 | +begin | |
| 261 | + AtLineStart := cur.tokPos = cur.tokLineStart; | |
| 262 | +end; | |
| 263 | + | |
| 236 | 264 | { Files } |
| 237 | 265 | |
| 238 | 266 | function Open(const path: shortstring): boolean; |
| @@ -296,13 +324,13 @@ | ||
| 296 | 324 | $F4: begin n := 3; cp := 4; hi := $8F end; { up to 10FFFFH } |
| 297 | 325 | else |
| 298 | 326 | cur.tokPos := pos; |
| 299 | - Mark('bad UTF-8'); | |
| 327 | + LexError('bad UTF-8'); | |
| 300 | 328 | end; |
| 301 | 329 | for i := 1 to n do begin |
| 302 | 330 | b1 := byte(cur.buf[pos + i]); |
| 303 | 331 | if (b1 < lo) or (b1 > hi) or (pos + i >= cur.len) then begin |
| 304 | 332 | cur.tokPos := pos; |
| 305 | - Mark('bad UTF-8'); | |
| 333 | + LexError('bad UTF-8'); | |
| 306 | 334 | end; |
| 307 | 335 | cp := cp shl 6 or (b1 and $3F); |
| 308 | 336 | lo := $80; hi := $BF; |
| @@ -313,7 +341,7 @@ | ||
| 313 | 341 | |
| 314 | 342 | procedure Put(b: longint); |
| 315 | 343 | begin |
| 316 | - if slen >= MaxStr then Mark('string too long'); | |
| 344 | + if slen >= MaxStr then LexError('string too long'); | |
| 317 | 345 | sbuf[slen] := b; |
| 318 | 346 | inc(slen); |
| 319 | 347 | end; |
| @@ -385,7 +413,7 @@ | ||
| 385 | 413 | end else if byte(c) >= $80 then |
| 386 | 414 | v := Utf8(cur.pos) |
| 387 | 415 | else if (c < ' ') or (c = #127) then |
| 388 | - Mark('unterminated literal') | |
| 416 | + LexError('unterminated literal') | |
| 389 | 417 | else begin |
| 390 | 418 | v := ord(c); |
| 391 | 419 | inc(cur.pos); |
| @@ -431,40 +459,40 @@ | ||
| 431 | 459 | if cur.buf[cur.pos] = 'H' then begin |
| 432 | 460 | v := 0; |
| 433 | 461 | for i := start to cur.pos - 1 do begin |
| 434 | - if v shr 60 <> 0 then Mark('number too big'); | |
| 462 | + if v shr 60 <> 0 then LexError('number too big'); | |
| 435 | 463 | v := v * 16 + qword(HexVal(cur.buf[i])); |
| 436 | 464 | end; |
| 437 | 465 | inc(cur.pos); |
| 438 | 466 | tok := sInt; |
| 439 | 467 | ival := int64(v); { 16 digits are the bits of a 64-bit word } |
| 440 | 468 | end else if (cur.buf[cur.pos] = '.') and (cur.buf[cur.pos + 1] in ['0'..'9']) then begin |
| 441 | - if not allDec then Mark('bad number'); | |
| 469 | + if not allDec then LexError('bad number'); | |
| 442 | 470 | inc(cur.pos); |
| 443 | 471 | while cur.buf[cur.pos] in ['0'..'9'] do inc(cur.pos); |
| 444 | 472 | if cur.buf[cur.pos] = 'E' then begin |
| 445 | 473 | inc(cur.pos); |
| 446 | 474 | if cur.buf[cur.pos] in ['+', '-'] then inc(cur.pos); |
| 447 | - if not (cur.buf[cur.pos] in ['0'..'9']) then Mark('bad exponent'); | |
| 475 | + if not (cur.buf[cur.pos] in ['0'..'9']) then LexError('bad exponent'); | |
| 448 | 476 | while cur.buf[cur.pos] in ['0'..'9'] do inc(cur.pos); |
| 449 | 477 | end; |
| 450 | - if cur.pos - start > 255 then Mark('number too long'); | |
| 478 | + if cur.pos - start > 255 then LexError('number too long'); | |
| 451 | 479 | SetLength(s, cur.pos - start); |
| 452 | 480 | Move(cur.buf[start], s[1], cur.pos - start); |
| 453 | 481 | Val(s, rval, code); |
| 454 | - if code <> 0 then Mark('bad real'); | |
| 482 | + if code <> 0 then LexError('bad real'); | |
| 455 | 483 | tok := sReal; |
| 456 | 484 | end else begin |
| 457 | - if not allDec then Mark('hex number needs H'); | |
| 485 | + if not allDec then LexError('hex number needs H'); | |
| 458 | 486 | v := 0; |
| 459 | 487 | for i := start to cur.pos - 1 do begin |
| 460 | 488 | d := ord(cur.buf[i]) - ord('0'); |
| 461 | - if v > (qword(high(int64)) - qword(d)) div 10 then Mark('number too big'); | |
| 489 | + if v > (qword(high(int64)) - qword(d)) div 10 then LexError('number too big'); | |
| 462 | 490 | v := v * 10 + qword(d); |
| 463 | 491 | end; |
| 464 | 492 | tok := sInt; |
| 465 | 493 | ival := int64(v); |
| 466 | 494 | end; |
| 467 | - if cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '_', '0'..'9'] then Mark('bad number'); | |
| 495 | + if cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '_', '0'..'9'] then LexError('bad number'); | |
| 468 | 496 | end; |
| 469 | 497 | |
| 470 | 498 | procedure Next; |
| @@ -492,7 +520,7 @@ | ||
| 492 | 520 | start := cur.pos; |
| 493 | 521 | repeat inc(cur.pos) |
| 494 | 522 | until not (cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '0'..'9', '_']); |
| 495 | - if cur.pos - start > MaxIdent then Mark('name too long'); | |
| 523 | + if cur.pos - start > MaxIdent then LexError('name too long'); | |
| 496 | 524 | h := Intern(@cur.buf[start], cur.pos - start); |
| 497 | 525 | if h < NumWords then tok := TSym(ord(sAnd) + h) |
| 498 | 526 | else begin tok := sIdent; id := h end; |
| @@ -500,9 +528,9 @@ | ||
| 500 | 528 | '0'..'9': Number; |
| 501 | 529 | '''': begin |
| 502 | 530 | inc(cur.pos); |
| 503 | - if cur.buf[cur.pos] = '''' then Mark('empty character'); | |
| 531 | + if cur.buf[cur.pos] = '''' then LexError('empty character'); | |
| 504 | 532 | v := LitChar(false); |
| 505 | - if cur.buf[cur.pos] <> '''' then Mark('character not closed'); | |
| 533 | + if cur.buf[cur.pos] <> '''' then LexError('character not closed'); | |
| 506 | 534 | inc(cur.pos); |
| 507 | 535 | tok := sChar; |
| 508 | 536 | ival := v; |
| @@ -563,7 +591,7 @@ | ||
| 563 | 591 | end; |
| 564 | 592 | else |
| 565 | 593 | if byte(c) >= $80 then |
| 566 | - Mark('text outside strings, characters and comments must be ASCII'); | |
| 594 | + LexError('text outside strings, characters and comments must be ASCII'); | |
| 567 | 595 | MarkBytes(cur.pos, cur.pos + 1, 'unexpected character'); |
| 568 | 596 | end; |
| 569 | 597 | end; |
| @@ -124,6 +124,7 @@ | ||
| 124 | 124 | var |
| 125 | 125 | tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType; |
| 126 | 126 | tBool, tF32, tReal, tRConst, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType; |
| 127 | + tPoison: PType; { of a name reported as not declared } | |
| 127 | 128 | curModule: PModule; |
| 128 | 129 | |
| 129 | 130 | function NewType(form: TForm; size, align: int64): PType; |
| @@ -131,6 +132,8 @@ | ||
| 131 | 132 | function NewScope: longint; |
| 132 | 133 | procedure OpenScope(s: longint); |
| 133 | 134 | procedure CloseScope; |
| 135 | +function ScopeDepth: longint; | |
| 136 | +procedure CutScopes(d: longint); | |
| 134 | 137 | function TopScope: longint; |
| 135 | 138 | procedure SaveScopes(var d: longint; var saved: array of longint); |
| 136 | 139 | procedure RestoreScopes(d: longint; const saved: array of longint); |
| @@ -221,8 +224,19 @@ | ||
| 221 | 224 | begin |
| 222 | 225 | depth := d; |
| 223 | 226 | for i := 0 to depth - 1 do scopes[i] := saved[i]; |
| 227 | +end; | |
| 228 | + | |
| 229 | +function ScopeDepth: longint; | |
| 230 | +begin | |
| 231 | + ScopeDepth := depth; | |
| 224 | 232 | end; |
| 225 | 233 | |
| 234 | +{ Closes the scopes opened since there were d, after an error } | |
| 235 | +procedure CutScopes(d: longint); | |
| 236 | +begin | |
| 237 | + depth := d; | |
| 238 | +end; | |
| 239 | + | |
| 226 | 240 | function TopScope: longint; |
| 227 | 241 | begin |
| 228 | 242 | TopScope := scopes[depth - 1]; |
| @@ -490,6 +504,8 @@ | ||
| 490 | 504 | tPtrU8^.base := tU8; |
| 491 | 505 | tBytes := NewType(fSlice, 16, 8); |
| 492 | 506 | tBytes^.base := tU8; |
| 507 | + tPoison := NewType(fInt, 8, 8); | |
| 508 | + tPoison^.signed := true; | |
| 493 | 509 | Enter('true', oConst, tBool, 1); |
| 494 | 510 | Enter('false', oConst, tBool, 0); |
| 495 | 511 | Enter('nil', oConst, tNil, 0); |
| @@ -122,6 +122,37 @@ | ||
| 122 | 122 | echo "FAIL tools/Pretty.raz: $(cat $out/Pretty.cerr)"; fail=$((fail + 1)) |
| 123 | 123 | fi |
| 124 | 124 | |
| 125 | +# error recovery: tools/Mutate.raz changes one symbol of each program, | |
| 126 | +# and the compiler must not crash, must exit with status 0 or 1, must | |
| 127 | +# say "error:" when it exits with 1, and must not report one place twice. | |
| 128 | +# Reproduce a failure with: build/Mutate FILE SEED > M.raz | |
| 129 | +fuzz() { # fuzz file seed | |
| 130 | + f=$1; seed=$2; dir=$(dirname $f) | |
| 131 | + mkdir -p $out/fuzz; m=$out/fuzz/$(basename $f) | |
| 132 | + $out/Mutate $f $seed > $m | |
| 133 | + $raz -I $dir/mods -o $out/mutant $m 2>$out/mutant.err | |
| 134 | + status=$? | |
| 135 | + why= | |
| 136 | + if [ $status -gt 1 ]; then why="exit status $status" | |
| 137 | + elif grep -q 'internal error' $out/mutant.err; then why="internal error" | |
| 138 | + elif [ $status = 1 ] && ! grep -q ': error:' $out/mutant.err; then why="status 1 without an error" | |
| 139 | + elif [ -n "$(grep ': error:' $out/mutant.err | sed 's/: error:.*//' | sort | uniq -d)" ]; then | |
| 140 | + why="two errors at one place" | |
| 141 | + fi | |
| 142 | + if [ -n "$why" ]; then | |
| 143 | + echo "FAIL fuzz $f seed $seed: $why"; cat $out/mutant.err | |
| 144 | + fail=$((fail + 1)) | |
| 145 | + else pass=$((pass + 1)); fi | |
| 146 | +} | |
| 147 | +if ./raz -o $out/Mutate tools/Mutate.raz 2>$out/Mutate.cerr; then | |
| 148 | + for f in examples/*.raz test/run/*.raz; do | |
| 149 | + $raz -I $(dirname $f)/mods -o $out/mutant $f 2>/dev/null || continue | |
| 150 | + for seed in ${FUZZ_SEEDS:-1 2 3}; do fuzz $f $seed; done | |
| 151 | + done | |
| 152 | +else | |
| 153 | + echo "FAIL tools/Mutate.raz: $(cat $out/Mutate.cerr)"; fail=$((fail + 1)) | |
| 154 | +fi | |
| 155 | + | |
| 125 | 156 | # each library module's main is its test; Sys over C is tested in test/libc |
| 126 | 157 | for f in lib/*.raz; do |
| 127 | 158 | [ $f = lib/Sys.raz ] && continue |
| @@ -0,0 +1,31 @@ | ||
| 1 | +test/err/Recover.raz:6:27: error: Foo isn't declared | |
| 2 | + 6 | P = record a: I64; b: Foo; end; | |
| 3 | + | ^^^ | |
| 4 | +test/err/Recover.raz:9:8: error: Bar isn't declared | |
| 5 | + 9 | var g: Bar; | |
| 6 | + | ^^^ | |
| 7 | +test/err/Recover.raz:15:10: error: can't use I64 as BOOL | |
| 8 | + 15 | b := x; | |
| 9 | + | ^ | |
| 10 | +test/err/Recover.raz:16:10: error: y isn't declared | |
| 11 | + 16 | x := y + 1; | |
| 12 | + | ^ | |
| 13 | +test/err/Recover.raz:19:14: error: can't combine I64 and BOOL | |
| 14 | + 19 | x := (x + 2) * b; | |
| 15 | + | ^^^^^^^^^^^ | |
| 16 | +test/err/Recover.raz:20:17: error: z isn't declared | |
| 17 | + 20 | Out.int(z); | |
| 18 | + | ^ | |
| 19 | +test/err/Recover.raz:22:12: error: can't combine I64 and BOOL | |
| 20 | + 22 | return x + true; | |
| 21 | + | ^^^^^^^^ | |
| 22 | +test/err/Recover.raz:28:7: error: no field c | |
| 23 | + 28 | q.c := 1; | |
| 24 | + | ^ | |
| 25 | +test/err/Recover.raz:30:18: error: ; expected | |
| 26 | + 30 | Out.str("hi") | |
| 27 | + | ^ | |
| 28 | +test/err/Recover.raz:37:13: error: nope isn't declared | |
| 29 | + 37 | Out.int(nope(2)); | |
| 30 | + | ^^^^ | |
| 31 | +raz: 10 errors |
| @@ -0,0 +1,40 @@ | ||
| 1 | +module Recover; | |
| 2 | + | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +type | |
| 6 | + P = record a: I64; b: Foo; end; | |
| 7 | + Q = record a: I64; end; | |
| 8 | + | |
| 9 | +var g: Bar; | |
| 10 | + h: I64; | |
| 11 | + | |
| 12 | +proc f(x: I64): I64; | |
| 13 | +var b: BOOL; | |
| 14 | +begin | |
| 15 | + b := x; | |
| 16 | + x := y + 1; | |
| 17 | + x := y * 2; | |
| 18 | + if x = 1 then | |
| 19 | + x := (x + 2) * b; | |
| 20 | + Out.int(z); | |
| 21 | + end; | |
| 22 | + return x + true; | |
| 23 | +end f; | |
| 24 | + | |
| 25 | +proc g2(): Q; | |
| 26 | +var q: Q; | |
| 27 | +begin | |
| 28 | + q.c := 1; | |
| 29 | + h := g; | |
| 30 | + Out.str("hi") | |
| 31 | + return q; | |
| 32 | +end g2; | |
| 33 | + | |
| 34 | +proc main*(); | |
| 35 | +begin | |
| 36 | + Out.int(f(1)); | |
| 37 | + Out.int(nope(2)); | |
| 38 | +end main; | |
| 39 | + | |
| 40 | +end Recover. |
| @@ -0,0 +1,22 @@ | ||
| 1 | +test/err/Resync.raz:10:5: error: ; expected | |
| 2 | + 10 | end a; | |
| 3 | + | ^ | |
| 4 | +test/err/Resync.raz:14:31: error: ; expected | |
| 5 | + 14 | while x > 0 do x := x - 1 end; | |
| 6 | + | ^^^ | |
| 7 | +test/err/Resync.raz:15:10: error: expression expected | |
| 8 | + 15 | x := ) + 2; | |
| 9 | + | ^ | |
| 10 | +test/err/Resync.raz:16:11: error: match needs a variant or an enumeration | |
| 11 | + 16 | match x | |
| 12 | + | ^ | |
| 13 | +test/err/Resync.raz:23:13: error: expression expected | |
| 14 | + 23 | x := 1 +; | |
| 15 | + | ^ | |
| 16 | +test/err/Resync.raz:23:14: error: end expected | |
| 17 | + 23 | x := 1 +; | |
| 18 | + | ^ | |
| 19 | +test/err/Resync.raz:27:10: error: can't use BOOL as I64 | |
| 20 | + 27 | x := false; | |
| 21 | + | ^^^^^ | |
| 22 | +raz: 7 errors |
| @@ -0,0 +1,35 @@ | ||
| 1 | +module Resync; | |
| 2 | + | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +proc a(x: I64); | |
| 6 | +begin | |
| 7 | + if x = 1 then | |
| 8 | + x := 2; | |
| 9 | + Out.int(x); | |
| 10 | +end a; | |
| 11 | + | |
| 12 | +proc b(x: I64); | |
| 13 | +begin | |
| 14 | + while x > 0 do x := x - 1 end; | |
| 15 | + x := ) + 2; | |
| 16 | + match x | |
| 17 | + | 1: x := true; | |
| 18 | + end; | |
| 19 | +end b; | |
| 20 | + | |
| 21 | +proc c(x: I64); | |
| 22 | +begin | |
| 23 | + x := 1 +; | |
| 24 | + | |
| 25 | +proc d(x: I64); | |
| 26 | +begin | |
| 27 | + x := false; | |
| 28 | +end d; | |
| 29 | + | |
| 30 | +proc main*(); | |
| 31 | +begin | |
| 32 | + a(1); b(2); c(3); d(4); | |
| 33 | +end main; | |
| 34 | + | |
| 35 | +end Resync. |
| @@ -0,0 +1,96 @@ | ||
| 1 | +(* Mutate: a raz source with one random change, to try the compiler's | |
| 2 | + recovery from errors. | |
| 3 | + | |
| 4 | + Mutate File.raz seed | |
| 5 | + prints the file with one symbol deleted, duplicated, swapped with the | |
| 6 | + next, or replaced by another symbol of the file. The seed picks the | |
| 7 | + change: the same seed gives the same text. Comments and spacing stay | |
| 8 | + as they are around the change. *) | |
| 9 | +module Mutate; | |
| 10 | +import Out, Rt, Buf, Heap, Scan, Files; | |
| 11 | + | |
| 12 | +var | |
| 13 | + src: []U8; | |
| 14 | + state: U64; (* the generator: xorshift64 *) | |
| 15 | + | |
| 16 | +(* A number from the generator, below n. *) | |
| 17 | +proc random(n: I64): I64; | |
| 18 | +begin | |
| 19 | + state := state xor (state shl 13); | |
| 20 | + state := state xor (state shr 7); | |
| 21 | + state := state xor (state shl 17); | |
| 22 | + return I64(state mod U64(n)); | |
| 23 | +end; | |
| 24 | + | |
| 25 | +(* The seed, as a nonzero state. *) | |
| 26 | +proc seed(s: []U8); | |
| 27 | +var i: I64; | |
| 28 | +begin | |
| 29 | + state := 88172645463325252; | |
| 30 | + for i := 0 to LEN(s) - 1 do state := state * 31 + U64(s[i]); end; | |
| 31 | + if state = 0 then state := 1; end; | |
| 32 | + for i := 1 to 10 do random(2); end; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +(* The symbols that aren't comments. *) | |
| 36 | +proc count(): I64; | |
| 37 | +var sc: Scan.Scanner; n: I64; | |
| 38 | +begin | |
| 39 | + Scan.init(ADR(sc), src); | |
| 40 | + Scan.next(ADR(sc)); | |
| 41 | + n := 0; | |
| 42 | + while sc.kind # Scan.eof do | |
| 43 | + if sc.kind # Scan.comment then INC(n); end; | |
| 44 | + Scan.next(ADR(sc)); | |
| 45 | + end; | |
| 46 | + return n; | |
| 47 | +end; | |
| 48 | + | |
| 49 | +(* Symbol k, counting from 0 and leaving out comments. *) | |
| 50 | +proc symbol(k: I64): Scan.Scanner; | |
| 51 | +var sc: Scan.Scanner; | |
| 52 | +begin | |
| 53 | + Scan.init(ADR(sc), src); | |
| 54 | + repeat | |
| 55 | + Scan.next(ADR(sc)); | |
| 56 | + if sc.kind # Scan.comment then DEC(k); end; | |
| 57 | + until k < 0; | |
| 58 | + return sc; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +proc main*; | |
| 62 | +var buf: Buf.Buf; n, op, i: I64; a, b, c: Scan.Scanner; | |
| 63 | +begin | |
| 64 | + if ARGC() # 3 then Rt.stop("usage: Mutate File.raz seed"); end; | |
| 65 | + Buf.init(ADR(buf), Heap.heap); | |
| 66 | + if not Files.read(Files.arg(1), ADR(buf)) then Rt.stop("Mutate: can't read the file"); end; | |
| 67 | + src := Buf.bytes(ADR(buf)); | |
| 68 | + seed(Files.arg(2)); | |
| 69 | + n := count(); | |
| 70 | + if n < 2 then Out.str(src); return; end; | |
| 71 | + op := random(4); | |
| 72 | + i := random(n - 1); | |
| 73 | + a := symbol(i); | |
| 74 | + b := symbol(i + 1); | |
| 75 | + c := symbol(random(n)); | |
| 76 | + Out.str(src[0..a.start]); | |
| 77 | + if op = 0 then | |
| 78 | + Out.str(src[a.stop..b.start]); | |
| 79 | + elsif op = 1 then | |
| 80 | + Out.str(Scan.text(ADR(a))); | |
| 81 | + Out.str(" "); | |
| 82 | + Out.str(Scan.text(ADR(a))); | |
| 83 | + Out.str(src[a.stop..b.start]); | |
| 84 | + elsif op = 2 then | |
| 85 | + Out.str(Scan.text(ADR(b))); | |
| 86 | + Out.str(src[a.stop..b.start]); | |
| 87 | + Out.str(Scan.text(ADR(a))); | |
| 88 | + b.start := b.stop; (* b has been written, as the first *) | |
| 89 | + else | |
| 90 | + Out.str(Scan.text(ADR(c))); | |
| 91 | + Out.str(src[a.stop..b.start]); | |
| 92 | + end; | |
| 93 | + Out.str(src[b.start..LEN(src)]); | |
| 94 | +end; | |
| 95 | + | |
| 96 | +end Mutate. |