diag.pas: one place for messages, with the source underlined Positions are a file and a byte range; line/col worked out only when printing. Colour on a tty unless NO_COLOR. Counts, summary, -Werror. test/err compares the whole message now.
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | FORCE: |
| 17 | 17 | |
| 18 | 18 | units: src/*.pas | build |
| 19 | - for u in scan sym x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done | |
| 19 | + for u in diag scan sym x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done | |
| 20 | 20 | |
| 21 | 21 | # tests of the parts below the parser, built from test/ |
| 22 | 22 | tests: units |
| @@ -191,7 +191,7 @@ | ||
| 191 | 191 | T.raz:3:5: note: x is declared here |
| 192 | 192 | 3 | var x: I64; |
| 193 | 193 | | ^ |
| 194 | -T.raz: 1 error | |
| 194 | +raz: 1 error | |
| 195 | 195 | ``` |
| 196 | 196 | |
| 197 | 197 | How messages are worded, for every step that writes one: lowercase, |
| @@ -211,9 +211,9 @@ | ||
| 211 | 211 | - [x] 19. The report first, for review before any code: messages, |
| 212 | 212 | warnings and `-Werror` in section 10, run-time messages in |
| 213 | 213 | sections 6.5 and 10, report version `2026-10.1`. *main* |
| 214 | -- [ ] 20. `src/diag.pas`, the one place messages are made: a position | |
| 215 | - (file, line, first and last column) and a message built in | |
| 216 | - pieces, past shortstring's 255 characters; the source excerpt | |
| 214 | +- [x] 20. `src/diag.pas`, the one place messages are made: a position | |
| 215 | + (a file and a stretch of bytes, its line and columns worked out | |
| 216 | + only for a message); the source excerpt | |
| 217 | 217 | and its underline, colour, notes and help; counting errors and |
| 218 | 218 | warnings, the summary, `-Werror`, the exit status. `Mark` and |
| 219 | 219 | `Fatal` move there. `test/err` and `test/pretty` keep working: |
| @@ -68,8 +68,8 @@ | ||
| 68 | 68 | |
| 69 | 69 | ## Using it |
| 70 | 70 | |
| 71 | - raz [-I dir]... [-o file] Main.raz | |
| 72 | - raz run [-I dir]... Main.raz [arg]... | |
| 71 | + raz [-I dir]... [-o file] [-Werror] Main.raz | |
| 72 | + raz run [-I dir]... [-Werror] Main.raz [arg]... | |
| 73 | 73 | raz --version |
| 74 | 74 | |
| 75 | 75 | `raz Main.raz` compiles module `Main` and every module it imports, and |
| @@ -77,7 +77,8 @@ | ||
| 77 | 77 | in the main module's directory, then in each `-I` directory, then in |
| 78 | 78 | `lib`. A module whose code depends on the system comes in one file per |
| 79 | 79 | target, `Name.linux-x86_64.raz` before `Name.raz`; the compiler checks |
| 80 | -that all of them export the same things. | |
| 80 | +that all of them export the same things. `-Werror` makes warnings | |
| 81 | +errors. | |
| 81 | 82 | |
| 82 | 83 | `raz run Main.raz a b` compiles the same way, then runs the program |
| 83 | 84 | with the arguments `a b`, writing no file: its output and exit status |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eight files of Free Pascal, under 5,000 lines of code. | |
| 3 | +The compiler is nine files of Free Pascal, under 6,000 lines of code. | |
| 4 | 4 | This guide walks through them in the order they are best read: from the |
| 5 | 5 | characters of a source file to the bytes of an executable. It assumes |
| 6 | 6 | you know the language from `doc/raz.md`, and enough x86-64 to read a |
| @@ -8,14 +8,15 @@ | ||
| 8 | 8 | |
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | -| `src/scan.pas` | 548 | 471 | Symbols, literals, comments, error messages | | |
| 11 | +| `src/diag.pas` | 227 | 162 | Messages: positions, source excerpts, colour, counts | | |
| 12 | +| `src/scan.pas` | 535 | 459 | Symbols, literals, comments, positions | | |
| 12 | 13 | | `src/sym.pas` | 508 | 422 | Types, objects, scopes, layout | |
| 13 | 14 | | `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder | |
| 14 | 15 | | `src/gen.pas` | 1,414 | 1,240 | Items: loading, storing, copying, frames, calls | |
| 15 | 16 | | `src/parse.pas` | 1,816 | 1,685 | Declarations, statements, expressions, built-ins | |
| 16 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 17 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 18 | -| `src/raz.pas` | 216 | 169 | The command line, the entry points | | |
| 19 | +| `src/raz.pas` | 219 | 172 | The command line, the entry points | | |
| 19 | 20 | |
| 20 | 21 | Every file opens with a comment that says what it is, what it depends |
| 21 | 22 | on and what it leaves out. Read those first; they are the map. |
| @@ -111,6 +112,18 @@ | ||
| 111 | 112 | is not fast code. It is code whose every instruction you can account |
| 112 | 113 | for, which is the point. |
| 113 | 114 | |
| 115 | +## diag.pas: messages | |
| 116 | + | |
| 117 | +Every message the compiler prints is made here. A position, `TPos`, is | |
| 118 | +a file and a stretch of bytes in it; `Error`, `Warning` and `Note` show | |
| 119 | +it as `file:line:column`, then the line of source with the stretch | |
| 120 | +underlined, and `Help` adds a line of advice. The line and column are | |
| 121 | +worked out only when a message is printed (`Locate`, `Chars`), so | |
| 122 | +positions cost nothing while compiling. `AddFile` keeps every source | |
| 123 | +file the scanner opens, so a note can show a line of any of them. | |
| 124 | +`Finish` prints the count of errors and warnings and stops if there | |
| 125 | +were errors; `Fatal` is for errors with no place in the source. | |
| 126 | + | |
| 114 | 127 | ## scan.pas: the scanner |
| 115 | 128 | |
| 116 | 129 | The scanner turns bytes into symbols (`TSym`). The current symbol is in |
| @@ -132,9 +145,9 @@ | ||
| 132 | 145 | Source is UTF-8, checked by `Utf8` wherever a byte above 127 appears: |
| 133 | 146 | in strings, characters and comments. Anywhere else it is an error. |
| 134 | 147 | |
| 135 | -Errors go through `Mark`, which prints `file:line:column` (the column | |
| 136 | -counted in characters, by `Column`) and stops. There is no recovery: | |
| 137 | -the first error ends the compilation. `SaveScan` and `RestoreScan` keep | |
| 148 | +`TokPos` is the position of the current symbol. Errors go through | |
| 149 | +`Mark`, which reports one there (`diag.Error`) and stops. There is no | |
| 150 | +recovery: the first error ends the compilation. `SaveScan` and `RestoreScan` keep | |
| 138 | 151 | the scanner's state while an imported module is compiled. |
| 139 | 152 | |
| 140 | 153 | ## sym.pas: types, objects and scopes |
| @@ -0,0 +1,227 @@ | ||
| 1 | +{ diag.pas: messages. | |
| 2 | + | |
| 3 | + The one place the compiler's messages are made. A message is an | |
| 4 | + error or a warning at a position, which it shows as the file, line | |
| 5 | + and column, then the line of source with the position underlined; | |
| 6 | + notes, each at a position of its own, and help, a line of advice, | |
| 7 | + may follow it. Colour is used when standard error is a terminal and | |
| 8 | + NO_COLOR isn't set. diag keeps every source file it is given, so | |
| 9 | + that a note can show a line of a module compiled earlier. | |
| 10 | + | |
| 11 | + Depends on: BaseUnix and termio, to write and to ask about the | |
| 12 | + terminal. | |
| 13 | + Leaves out: wording. The callers say what is wrong; a message is at | |
| 14 | + most 255 characters, which every message so far keeps well within. } | |
| 15 | + | |
| 16 | +unit diag; | |
| 17 | + | |
| 18 | +{$mode objfpc}{$H-} | |
| 19 | + | |
| 20 | +interface | |
| 21 | + | |
| 22 | +type | |
| 23 | + { A stretch of a source file: bytes a up to but not including b. | |
| 24 | + a = b marks the place between two characters. } | |
| 25 | + TPos = record | |
| 26 | + f, a, b: longint; | |
| 27 | + end; | |
| 28 | + | |
| 29 | +const | |
| 30 | + MaxErrors = 20; | |
| 31 | + | |
| 32 | +var | |
| 33 | + errors, warnings: longint; | |
| 34 | + werror: boolean; { -Werror: a warning counts as an error } | |
| 35 | + | |
| 36 | +function AddFile(const path: shortstring; buf: PChar; len: longint): longint; | |
| 37 | +function PathOf(f: longint): shortstring; | |
| 38 | +function LineOf(const p: TPos): longint; | |
| 39 | +procedure Error(const p: TPos; const msg: shortstring); | |
| 40 | +procedure Warning(const p: TPos; const msg: shortstring); | |
| 41 | +procedure Note(const p: TPos; const msg: shortstring); | |
| 42 | +procedure Help(const msg: shortstring); | |
| 43 | +procedure Fatal(const msg: shortstring); | |
| 44 | +procedure Finish; | |
| 45 | + | |
| 46 | +implementation | |
| 47 | + | |
| 48 | +uses BaseUnix, termio; | |
| 49 | + | |
| 50 | +type | |
| 51 | + TFile = record | |
| 52 | + path: shortstring; | |
| 53 | + buf: PChar; | |
| 54 | + len: longint; | |
| 55 | + end; | |
| 56 | + | |
| 57 | +var | |
| 58 | + files: ^TFile; { numFiles of them, in room for cap } | |
| 59 | + numFiles, cap: longint; | |
| 60 | + colour, colourKnown: boolean; | |
| 61 | + | |
| 62 | +function AddFile(const path: shortstring; buf: PChar; len: longint): longint; | |
| 63 | +begin | |
| 64 | + if numFiles = cap then begin | |
| 65 | + cap := 2 * cap + 8; | |
| 66 | + ReallocMem(files, cap * SizeOf(TFile)); | |
| 67 | + end; | |
| 68 | + files[numFiles].path := path; | |
| 69 | + files[numFiles].buf := buf; | |
| 70 | + files[numFiles].len := len; | |
| 71 | + AddFile := numFiles; | |
| 72 | + inc(numFiles); | |
| 73 | +end; | |
| 74 | + | |
| 75 | +function PathOf(f: longint): shortstring; | |
| 76 | +begin | |
| 77 | + PathOf := files[f].path; | |
| 78 | +end; | |
| 79 | + | |
| 80 | +{ Writing } | |
| 81 | + | |
| 82 | +const | |
| 83 | + Bold = #27'[1m'; Red = #27'[1;31m'; Yellow = #27'[1;33m'; | |
| 84 | + Cyan = #27'[1;36m'; Green = #27'[1;32m'; Blue = #27'[1;34m'; | |
| 85 | + Plain = #27'[0m'; | |
| 86 | + | |
| 87 | +{ Whether NO_COLOR is set, to anything } | |
| 88 | +function NoColour: boolean; | |
| 89 | +var e: PPChar; | |
| 90 | +begin | |
| 91 | + e := envp; | |
| 92 | + while (e^ <> nil) and (CompareByte(e^^, PChar('NO_COLOR=')^, 9) <> 0) do inc(e); | |
| 93 | + NoColour := e^ <> nil; | |
| 94 | +end; | |
| 95 | + | |
| 96 | +{ s in colour c, when colour is on } | |
| 97 | +function Paint(const c, s: shortstring): shortstring; | |
| 98 | +begin | |
| 99 | + if not colourKnown then begin | |
| 100 | + colour := (IsATTY(2) = 1) and not NoColour; | |
| 101 | + colourKnown := true; | |
| 102 | + end; | |
| 103 | + if colour then Paint := c + s + Plain else Paint := s; | |
| 104 | +end; | |
| 105 | + | |
| 106 | +function IntStr(n: longint): shortstring; | |
| 107 | +var s: shortstring; | |
| 108 | +begin | |
| 109 | + Str(n, s); | |
| 110 | + IntStr := s; | |
| 111 | +end; | |
| 112 | + | |
| 113 | +{ Positions } | |
| 114 | + | |
| 115 | +{ The line of p, and the byte where it starts. Lines are counted from | |
| 116 | + the start of the file, which is slow only next to compiling it. } | |
| 117 | +procedure Locate(const p: TPos; out line, start: longint); | |
| 118 | +var i: longint; buf: PChar; | |
| 119 | +begin | |
| 120 | + buf := files[p.f].buf; | |
| 121 | + line := 1; start := 0; | |
| 122 | + for i := 0 to p.a - 1 do | |
| 123 | + if buf[i] = #10 then begin inc(line); start := i + 1 end; | |
| 124 | +end; | |
| 125 | + | |
| 126 | +function LineOf(const p: TPos): longint; | |
| 127 | +var line, start: longint; | |
| 128 | +begin | |
| 129 | + Locate(p, line, start); | |
| 130 | + LineOf := line; | |
| 131 | +end; | |
| 132 | + | |
| 133 | +{ Characters from byte i to byte j of buf: every byte but a UTF-8 | |
| 134 | + continuation byte. } | |
| 135 | +function Chars(buf: PChar; i, j: longint): longint; | |
| 136 | +var n: longint; | |
| 137 | +begin | |
| 138 | + n := 0; | |
| 139 | + for i := i to j - 1 do | |
| 140 | + if (byte(buf[i]) and $C0) <> $80 then inc(n); | |
| 141 | + Chars := n; | |
| 142 | +end; | |
| 143 | + | |
| 144 | +{ "file:line:col: kind: msg", then the line with p underlined. A tab | |
| 145 | + before p is copied into the underline, so that the two line up | |
| 146 | + whatever the terminal makes of tabs. } | |
| 147 | +procedure Show(const p: TPos; const kind, kindColour, msg: shortstring); | |
| 148 | +var line, start, stop, i, n: longint; buf: PChar; num, gutter: shortstring; | |
| 149 | +begin | |
| 150 | + Locate(p, line, start); | |
| 151 | + buf := files[p.f].buf; | |
| 152 | + writeln(StdErr, Paint(Bold, files[p.f].path + ':' + IntStr(line) + ':' | |
| 153 | + + IntStr(Chars(buf, start, p.a) + 1) + ':'), ' ', Paint(kindColour, kind + ':'), | |
| 154 | + ' ', Paint(Bold, msg)); | |
| 155 | + stop := start; | |
| 156 | + while (stop < files[p.f].len) and (buf[stop] <> #10) do inc(stop); | |
| 157 | + num := IntStr(line); | |
| 158 | + while length(num) < 5 do num := ' ' + num; | |
| 159 | + gutter := Paint(Blue, num + ' |'); | |
| 160 | + write(StdErr, gutter, ' '); | |
| 161 | + for i := start to stop - 1 do write(StdErr, buf[i]); | |
| 162 | + writeln(StdErr); | |
| 163 | + write(StdErr, Paint(Blue, ' |'), ' '); | |
| 164 | + for i := start to p.a - 1 do | |
| 165 | + if buf[i] = #9 then write(StdErr, #9) | |
| 166 | + else if (byte(buf[i]) and $C0) <> $80 then write(StdErr, ' '); | |
| 167 | + if p.b < stop then n := Chars(buf, p.a, p.b) else n := Chars(buf, p.a, stop); | |
| 168 | + if n < 1 then n := 1; | |
| 169 | + writeln(StdErr, Paint(kindColour, StringOfChar('^', n))); | |
| 170 | +end; | |
| 171 | + | |
| 172 | +{ Messages } | |
| 173 | + | |
| 174 | +procedure Error(const p: TPos; const msg: shortstring); | |
| 175 | +begin | |
| 176 | + Show(p, 'error', Red, msg); | |
| 177 | + inc(errors); | |
| 178 | + if errors >= MaxErrors then begin | |
| 179 | + writeln(StdErr, 'raz: stopping after ', MaxErrors, ' errors'); | |
| 180 | + Halt(1); | |
| 181 | + end; | |
| 182 | +end; | |
| 183 | + | |
| 184 | +procedure Warning(const p: TPos; const msg: shortstring); | |
| 185 | +begin | |
| 186 | + if werror then Error(p, msg) | |
| 187 | + else begin | |
| 188 | + Show(p, 'warning', Yellow, msg); | |
| 189 | + inc(warnings); | |
| 190 | + end; | |
| 191 | +end; | |
| 192 | + | |
| 193 | +procedure Note(const p: TPos; const msg: shortstring); | |
| 194 | +begin | |
| 195 | + Show(p, 'note', Cyan, msg); | |
| 196 | +end; | |
| 197 | + | |
| 198 | +procedure Help(const msg: shortstring); | |
| 199 | +begin | |
| 200 | + writeln(StdErr, Paint(Green, 'help:'), ' ', msg); | |
| 201 | +end; | |
| 202 | + | |
| 203 | +{ An error with no place in the source, such as a file that can't be | |
| 204 | + read: it stops the compiler at once. } | |
| 205 | +procedure Fatal(const msg: shortstring); | |
| 206 | +begin | |
| 207 | + writeln(StdErr, 'raz: ', msg); | |
| 208 | + Halt(1); | |
| 209 | +end; | |
| 210 | + | |
| 211 | +function Count(n: longint; const what: shortstring): shortstring; | |
| 212 | +begin | |
| 213 | + if n = 1 then Count := '1 ' + what else Count := IntStr(n) + ' ' + what + 's'; | |
| 214 | +end; | |
| 215 | + | |
| 216 | +{ Says how many errors and warnings there were, and stops the compiler | |
| 217 | + if there were errors. } | |
| 218 | +procedure Finish; | |
| 219 | +begin | |
| 220 | + if (errors > 0) and (warnings > 0) then | |
| 221 | + writeln(StdErr, 'raz: ', Count(errors, 'error'), ', ', Count(warnings, 'warning')) | |
| 222 | + else if errors > 0 then writeln(StdErr, 'raz: ', Count(errors, 'error')) | |
| 223 | + else if warnings > 0 then writeln(StdErr, 'raz: ', Count(warnings, 'warning')); | |
| 224 | + if errors > 0 then Halt(1); | |
| 225 | +end; | |
| 226 | + | |
| 227 | +end. |
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | |
| 25 | 25 | interface |
| 26 | 26 | |
| 27 | -uses scan, x64, link; | |
| 27 | +uses scan, diag, x64, link; | |
| 28 | 28 | |
| 29 | 29 | var |
| 30 | 30 | needed: array[0..31] of longint; { the libraries to load, names interned } |
| @@ -27,7 +27,7 @@ | ||
| 27 | 27 | |
| 28 | 28 | interface |
| 29 | 29 | |
| 30 | -uses scan, sym, x64; | |
| 30 | +uses scan, diag, sym, x64; | |
| 31 | 31 | |
| 32 | 32 | type |
| 33 | 33 | TMode = (mConst, mVar, mReg, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | |
| 22 | 22 | interface |
| 23 | 23 | |
| 24 | -uses scan, x64; | |
| 24 | +uses scan, diag, x64; | |
| 25 | 25 | |
| 26 | 26 | const |
| 27 | 27 | Base = $400000; |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | |
| 22 | 22 | interface |
| 23 | 23 | |
| 24 | -uses scan, sym, x64, gen; | |
| 24 | +uses scan, diag, sym, x64, gen; | |
| 25 | 25 | |
| 26 | 26 | const |
| 27 | 27 | Target = 'linux-x86_64'; |
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | |
| 26 | 26 | {$mode objfpc}{$H-} |
| 27 | 27 | |
| 28 | -uses math, BaseUnix, syscall, scan, sym, x64, gen, parse, link, elf; | |
| 28 | +uses math, BaseUnix, syscall, scan, diag, sym, x64, gen, parse, link, elf; | |
| 29 | 29 | |
| 30 | 30 | const |
| 31 | 31 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| @@ -113,7 +113,9 @@ | ||
| 113 | 113 | end else if s = '-o' then begin |
| 114 | 114 | inc(i); |
| 115 | 115 | outPath := ParamStr(i); |
| 116 | - end else if s = '--version' then begin | |
| 116 | + end else if s = '-Werror' then | |
| 117 | + werror := true | |
| 118 | + else if s = '--version' then begin | |
| 117 | 119 | writeln('raz ', Version, ', report ', Report); |
| 118 | 120 | Halt(0); |
| 119 | 121 | end else if (s <> '') and (s[1] = '-') then |
| @@ -126,7 +128,7 @@ | ||
| 126 | 128 | inc(i); |
| 127 | 129 | end; |
| 128 | 130 | if path = '' then |
| 129 | - Fatal('usage: raz [-I dir]... [-o file] Main.raz, or raz run [-I dir]... Main.raz [arg]...'); | |
| 131 | + Fatal('usage: raz [-I dir]... [-o file] [-Werror] Main.raz, or raz run [options] Main.raz [arg]...'); | |
| 130 | 132 | if run and (outPath <> '') then Fatal('raz run takes no -o'); |
| 131 | 133 | dir := ParamStr(0); |
| 132 | 134 | i := length(dir); |
| @@ -140,6 +142,7 @@ | ||
| 140 | 142 | end; |
| 141 | 143 | |
| 142 | 144 | m := CompileMain(path); |
| 145 | + Finish; | |
| 143 | 146 | |
| 144 | 147 | Use(m); |
| 145 | 148 | rt := firstModule; |
| @@ -3,10 +3,10 @@ | ||
| 3 | 3 | Reads a raz source file and hands the parser one symbol at a time: |
| 4 | 4 | reserved words, identifiers, numbers, characters, strings and |
| 5 | 5 | operators. Identifiers are interned, so that the rest of the compiler |
| 6 | - compares names as small integers. The scanner also reports errors, | |
| 7 | - since it knows where in the file the compiler is. | |
| 6 | + compares names as small integers. The scanner also says where the | |
| 7 | + current symbol is, for messages. | |
| 8 | 8 | |
| 9 | - Depends on: BaseUnix, to read files. | |
| 9 | + Depends on: BaseUnix, to read files; diag, to report errors. | |
| 10 | 10 | Leaves out: recovery from errors. The first error stops the compiler. } |
| 11 | 11 | |
| 12 | 12 | unit scan; |
| @@ -15,6 +15,8 @@ | ||
| 15 | 15 | |
| 16 | 16 | interface |
| 17 | 17 | |
| 18 | +uses diag; | |
| 19 | + | |
| 18 | 20 | type |
| 19 | 21 | TSym = (sNull, sIdent, sInt, sReal, sChar, sString, |
| 20 | 22 | sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq, |
| @@ -43,6 +45,7 @@ | ||
| 43 | 45 | ival: int64; |
| 44 | 46 | rval: double; |
| 45 | 47 | id: longint; |
| 48 | + f: longint; { the file, as diag knows it } | |
| 46 | 49 | path: shortstring; |
| 47 | 50 | end; |
| 48 | 51 | |
| @@ -63,10 +66,9 @@ | ||
| 63 | 66 | function Open(const path: shortstring): boolean; |
| 64 | 67 | procedure Next; |
| 65 | 68 | procedure Mark(const msg: shortstring); |
| 66 | -procedure Fatal(const msg: shortstring); | |
| 67 | 69 | procedure SaveScan(var st: TScanState); |
| 68 | 70 | procedure RestoreScan(const st: TScanState); |
| 69 | -function Where: shortstring; | |
| 71 | +function TokPos: TPos; | |
| 70 | 72 | function CurPath: shortstring; |
| 71 | 73 | function CurLine: longint; |
| 72 | 74 | |
| @@ -161,24 +163,14 @@ | ||
| 161 | 163 | |
| 162 | 164 | { Errors } |
| 163 | 165 | |
| 164 | -function Column(pos: longint): longint; | |
| 165 | -var i, col: longint; | |
| 166 | +{ The current symbol } | |
| 167 | +function TokPos: TPos; | |
| 166 | 168 | begin |
| 167 | - { counted in characters: every byte but a UTF-8 continuation byte } | |
| 168 | - col := 1; | |
| 169 | - for i := cur.tokLineStart to pos - 1 do | |
| 170 | - if (byte(cur.buf[i]) and $C0) <> $80 then inc(col); | |
| 171 | - Column := col; | |
| 169 | + TokPos.f := cur.f; | |
| 170 | + TokPos.a := cur.tokPos; | |
| 171 | + TokPos.b := cur.pos; | |
| 172 | 172 | end; |
| 173 | 173 | |
| 174 | -function Where: shortstring; | |
| 175 | -var l, c: shortstring; | |
| 176 | -begin | |
| 177 | - Str(cur.tokLine, l); | |
| 178 | - Str(Column(cur.tokPos), c); | |
| 179 | - Where := cur.path + ':' + l + ':' + c; | |
| 180 | -end; | |
| 181 | - | |
| 182 | 174 | function CurPath: shortstring; |
| 183 | 175 | begin |
| 184 | 176 | CurPath := cur.path; |
| @@ -189,16 +181,10 @@ | ||
| 189 | 181 | CurLine := cur.tokLine; |
| 190 | 182 | end; |
| 191 | 183 | |
| 192 | -procedure Fatal(const msg: shortstring); | |
| 193 | -begin | |
| 194 | - writeln(StdErr, 'raz: ', msg); | |
| 195 | - Halt(1); | |
| 196 | -end; | |
| 197 | - | |
| 198 | 184 | procedure Mark(const msg: shortstring); |
| 199 | 185 | begin |
| 200 | - writeln(StdErr, Where, ': ', msg); | |
| 201 | - Halt(1); | |
| 186 | + Error(TokPos, msg); | |
| 187 | + Finish; | |
| 202 | 188 | end; |
| 203 | 189 | |
| 204 | 190 | { Files } |
| @@ -229,6 +215,7 @@ | ||
| 229 | 215 | cur.tokLine := 1; |
| 230 | 216 | cur.tokLineStart := 0; |
| 231 | 217 | cur.path := path; |
| 218 | + cur.f := AddFile(path, cur.buf, n); | |
| 232 | 219 | Open := true; |
| 233 | 220 | end; |
| 234 | 221 |
| @@ -64,8 +64,8 @@ | ||
| 64 | 64 | if $raz -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then |
| 65 | 65 | echo "FAIL $f: compiles" |
| 66 | 66 | fail=$((fail + 1)) |
| 67 | - elif ! grep -qF "$(cat test/err/$name.err)" $out/$name.cerr; then | |
| 68 | - echo "FAIL $f: says $(cat $out/$name.cerr)" | |
| 67 | + elif ! cmp -s test/err/$name.err $out/$name.cerr; then | |
| 68 | + echo "FAIL $f: says"; diff test/err/$name.err $out/$name.cerr | |
| 69 | 69 | fail=$((fail + 1)) |
| 70 | 70 | else |
| 71 | 71 | pass=$((pass + 1)) |
| @@ -1 +1,4 @@ | ||
| 1 | -can't use I32 as I64 | |
| 1 | +test/err/Assign.raz:5:11: error: can't use I32 as I64 | |
| 2 | + 5 | a := b; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -needs a procedure with the signature of hash | |
| 1 | +/home/luis/raz/lib/Map.raz:4:39: error: needs a procedure with the signature of hash | |
| 2 | + 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -can't use U64 as I64 | |
| 1 | +test/err/BigI64.raz:5:28: error: can't use U64 as I64 | |
| 2 | + 5 | i := 0FFFFFFFFFFFFFFFFH; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -break outside a loop | |
| 1 | +test/err/Break.raz:4:10: error: break outside a loop | |
| 2 | + 4 | break; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -Cycle imports itself | |
| 1 | +test/err/mods/CycleB.raz:2:13: error: Cycle imports itself | |
| 2 | + 2 | import Cycle; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -break outside a loop | |
| 1 | +test/err/DeferBreak.raz:5:20: error: break outside a loop | |
| 2 | + 5 | defer break; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -defer can't be deferred | |
| 1 | +test/err/DeferDefer.raz:4:17: error: defer can't be deferred | |
| 2 | + 4 | defer defer f; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -return can't leave a deferred statement | |
| 1 | +test/err/DeferReturn.raz:4:18: error: return can't leave a deferred statement | |
| 2 | + 4 | defer return 1; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -compare only with = and # | |
| 1 | +test/err/ErrOrder.raz:5:14: error: procedures and errors compare only with = and # | |
| 2 | + 5 | if A < B then end; | |
| 3 | + | ^^^^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -C takes integers, BOOL, reals, pointers and procedures | |
| 1 | +test/err/ExternRec.raz:5:20: error: C takes integers, BOOL, reals, pointers and procedures | |
| 2 | + 5 | extern proc f(r: R); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -an F32 goes to ... as F64(x) | |
| 1 | +test/err/F32Dots.raz:8:23: error: an F32 goes to ... as F64(x) | |
| 2 | + 8 | C.printf("%f\n", x); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -field extra isn't exported | |
| 1 | +test/err/Field.raz:6:7: error: field extra isn't exported | |
| 2 | + 6 | i.extra := 1; | |
| 3 | + | ^^^^^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -no arm for c | |
| 1 | +test/err/Match.raz:9:8: error: no arm for c | |
| 2 | + 9 | end; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -different types: F32 and F64 | |
| 1 | +test/err/Mixed.raz:6:15: error: different types: F32 and F64 | |
| 2 | + 6 | y := x + y; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -constant doesn't fit U64 | |
| 1 | +test/err/NegU64.raz:5:12: error: constant doesn't fit U64 | |
| 2 | + 5 | u := -1; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -needs a type for each type parameter | |
| 1 | +/home/luis/raz/lib/List.raz:2:13: error: needs a type for each type parameter | |
| 2 | + 2 | module List(T); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -Str isn't generic | |
| 1 | +/home/luis/raz/lib/Str.raz:3:11: error: Str isn't generic | |
| 2 | + 3 | module Str; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -a pointer to an array is indexed as p^[i] | |
| 1 | +test/err/PtrArray.raz:6:7: error: a pointer to an array is indexed as p^[i] | |
| 2 | + 6 | p[1] := 0; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1 @@ | ||
| 1 | -Bare says link static, but | |
| 1 | +raz: Bare says link static, but C uses C |
| @@ -1 +1 @@ | ||
| 1 | -Thread says link static, but | |
| 1 | +raz: Thread says link static, but C uses C |
| @@ -1 +1,4 @@ | ||
| 1 | -several results go only to a, b := ... | |
| 1 | +test/err/Tuple.raz:6:16: error: several results go only to a, b := ... | |
| 2 | + 6 | a := two() + 1; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -can't use List(I32).List as List(I64).List | |
| 1 | +test/err/TwoTypes.raz:6:11: error: can't use List(I32).List as List(I64).List | |
| 2 | + 6 | a := b; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1 @@ | ||
| 1 | -more isn't exported by | |
| 1 | +raz: test/err/mods/Extra.raz: more isn't exported by test/err/mods/Extra.linux.raz |
| @@ -1 +1 @@ | ||
| 1 | -f isn't exported as in | |
| 1 | +raz: test/err/mods/Iface.raz: f isn't exported as in test/err/mods/Iface.linux-x86_64.raz |
| @@ -1 +1 @@ | ||
| 1 | -R isn't exported as in | |
| 1 | +raz: test/err/mods/Rec.raz: R isn't exported as in test/err/mods/Rec.linux-x86_64.raz |
| @@ -1 +1,4 @@ | ||
| 1 | -Utf.raz:2:5: text outside strings, characters and comments must be ASCII | |
| 1 | +test/err/Utf.raz:2:5: error: text outside strings, characters and comments must be ASCII | |
| 2 | + 2 | var é: I64; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -... is only for extern procedures | |
| 1 | +test/err/Variadic.raz:3:16: error: ... is only for extern procedures | |
| 2 | + 3 | proc f(a: I64; ...); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |
| @@ -1 +1,4 @@ | ||
| 1 | -can't use ^VOLATILE I64 as ^I64 | |
| 1 | +test/err/Volatile.raz:6:11: error: can't use ^VOLATILE I64 as ^I64 | |
| 2 | + 6 | q := p; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | { Prints the symbols of a file, one per line: a test of scan.pas. } |
| 2 | 2 | program scantest; |
| 3 | 3 | {$mode objfpc}{$H-} |
| 4 | -uses scan; | |
| 4 | +uses diag, scan; | |
| 5 | 5 | var i: longint; |
| 6 | 6 | begin |
| 7 | 7 | if not Open(ParamStr(1)) then Fatal('no file'); |
| 8 | 8 | repeat |
| 9 | 9 | Next; |
| 10 | - write(Where, ' ', ord(tok)); | |
| 10 | + write(LineOf(TokPos), ':', TokPos.a, ' ', ord(tok)); | |
| 11 | 11 | case tok of |
| 12 | 12 | sIdent: write(' ', NameOf(id)); |
| 13 | 13 | sInt, sChar: write(' ', ival); |