From b1f5815f57cab990612222274b665aa20363bf8e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:21:36 +0100 Subject: [PATCH] Notes and help: declared here, did you mean, slips from other languages Objects keep where their names are declared (TObj.pos), for notes: "the first one is here" for a name declared twice, "the signature is here" for a wrong count of args, a fwd mismatch and a generic's proc, "declared here, without a *" for what isn't exported. main's checks point at main. Notes and help show only with a shown error; quiet moves to diag, and scan's Fail does the recovery jump. Did you mean: sym's Closest, by edit distance ignoring case, over the scopes in reach, a module's exports, a record's fields. A name not declared that is a module's file gets "import X"; elif gets elsif. Slips: scan's Slip reports !=, !, ==, &&, || and reads on as the right symbol, not while re-skipping after an error; help for x = 1 as a statement and { for then/do. Tests for each in test/err. Code: 5,545 -> 5,664 lines. --- diff --git a/CLAUDE.md b/CLAUDE.md index 080456d..0321279 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -57,7 +57,8 @@ ravite (vim's). What they needed of the system went into `Sys` (report 2026-10.2, approved): the terminal, files, directories and the environment. `tools/Scan.raz` is theirs to use, as a tool, not part of -the library. New work starts only on the owner's word. Keep +the library. Stage 4, diagnostics (steps 19-27), is under way: +steps 19-24 are done. New work starts only on the owner's word. Keep `doc/compiler.md`'s table of lines and its routine names true when the compiler changes. Left for later at the owner's word: a rewrite of var parameters. diff --git a/PLAN.md b/PLAN.md index e496057..3ab11b7 100644 --- a/PLAN.md +++ b/PLAN.md @@ -249,7 +249,7 @@ (`scan.pas`, `parse.pas` in two halves, `gen.pas`, the rest), against a list of messages and their tests the main model writes first; *main* reviews the wording as a whole. -- [ ] 24. Notes and help. Objects remember where they are declared: +- [x] 24. Notes and help. Objects remember where they are declared: "declared here", "the first one is here", "the signature is here". The checks on `main` in `raz.pas` point at it, so that every error has a place (the fuzzer finds them). Did you mean: the closest name by edit distance in the diff --git a/doc/compiler.md b/doc/compiler.md index fd06608..292508f 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -8,15 +8,15 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/diag.pas` | 260 | 187 | Messages: positions, source excerpts, colour, counts | -| `src/scan.pas` | 659 | 529 | Symbols, literals, comments, positions | -| `src/sym.pas` | 526 | 437 | Types, objects, scopes, layout | +| `src/diag.pas` | 267 | 191 | Messages: positions, source excerpts, colour, counts | +| `src/scan.pas` | 687 | 553 | Symbols, literals, comments, positions | +| `src/sym.pas` | 578 | 479 | Types, objects, scopes, layout | | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | -| `src/gen.pas` | 1,512 | 1,318 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,094 | 1,908 | Declarations, statements, expressions, built-ins, recovery | +| `src/gen.pas` | 1,515 | 1,319 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,151 | 1,955 | Declarations, statements, expressions, built-ins, recovery | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 219 | 172 | The command line, the entry points | +| `src/raz.pas` | 220 | 173 | The command line, the entry points | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -111,7 +111,10 @@ Every message the compiler prints is made here. A position, `TPos`, is a file and a stretch of bytes in it; `Error`, `Warning` and `Note` show it as `file:line:column`, then the line of source with the stretch -underlined, and `Help` adds a line of advice. The line and column are +underlined, and `Help` adds a line of advice. Notes and help are shown +only with an error that was: not one at the place of the last, and +not one under `quiet`, which the parser sets when an error follows +from another (see parse.pas). The line and column are worked out only when a message is printed (`Locate`, `Chars`), so positions cost nothing while compiling. `AddFile` keeps every source file the scanner opens, so a note can show a line of any of them. @@ -146,13 +149,16 @@ through `Mark`, at the current symbol, `MarkAt`, at a position, or `MarkHelp`, which adds a line of help; each reports the error (`diag.Error`) and jumps to `recovery`, the parser's place to go on -from (see parse.pas). A message names what it found: `Found` describes +from (see parse.pas), through `Fail`. An error with notes is reported +with `Error` and `Note`, then `Fail`. A message names what it found: `Found` describes the current symbol and `SymText` spells an operator or reserved word, so that the parser's `Expect` and `Expected` say `expected ";", found "end"`. Errors in the text itself stop the compiler (`MarkBytes`, `LexError`): past a string left open, the -symbols can't be trusted. There is no -recovery: the first error ends the compilation. `SaveScan` and `RestoreScan` keep +symbols can't be trusted. A slip from another language, such as `!=` +for `#`, is reported by `Slip`, which reads on as if the right symbol +were written; not while the parser is `skipping` what it read once +before an error. `SaveScan` and `RestoreScan` keep the scanner's state while an imported module is compiled. ## sym.pas: types, objects and scopes @@ -168,7 +174,8 @@ the C rules. `obj` is the declaration that named the type, if any. `TObj` is anything a name can stand for: a constant, type, variable, -procedure, module, field, variant case or built-in (`cls`). Where it +procedure, module, field, variant case or built-in (`cls`), and `pos` +is where its name is declared, for notes. Where it lives is decided here too: a module variable's chunk, a local's frame offset in `val`, a procedure's chunk. @@ -177,7 +184,9 @@ sit in one open-addressed hash table, keyed by scope and name: `Find` looks in one scope, `Lookup` in each open scope from the innermost out. A lookup costs the same however big a program grows, which matters at -a million lines a second. +a million lines a second. After an error, `Closest` looks through the +whole table for the name in reach spelled most like one not found +(`Distance`), for "did you mean". `Same` and `Alike` decide whether two types are the same: one declaration for named types, built alike otherwise. `Fingerprint` @@ -329,9 +338,11 @@ blocks. A symbol at the start of a line ends the skip: by the usual layout it starts the next declaration, so the statement gives up its procedure (`GiveUp`), and `SkipProc` reads to the procedure's -`end Name;`. A name not declared is declared where it is used, with -the type `tPoison` (`Poison`), as are the names of a declaration that -had an error; a construct that uses poison sets `quiet`, and its errors +`end Name;`. A name not declared is reported by `NotDeclared`, with +what it may have meant: a module to import (`ModuleFile`) or a name +spelled alike. It is then declared where it is used, with the type +`tPoison` (`Poison`), as are the names of a declaration that had an +error; a construct that uses poison sets `quiet`, and its errors aren't reported, since they follow from one that was. Nothing breaks for the half-made code: once there is an error, no executable is written. diff --git a/src/diag.pas b/src/diag.pas index a337177..871a56a 100644 --- a/src/diag.pas +++ b/src/diag.pas @@ -32,6 +32,9 @@ var errors, warnings: longint; werror: boolean; { -Werror: a warning counts as an error } + { The construct being read uses a name already reported as not + declared: its errors follow from that one, and aren't shown. } + quiet: boolean; function AddFile(const path: shortstring; buf: PChar; len: longint): longint; function PathOf(f: longint): shortstring; @@ -63,6 +66,7 @@ colour, colourKnown: boolean; cwd: shortstring; { the working directory and a /, or '' if unknown } lastError: TPos; { where the last error was } + shown: boolean; { whether it was shown: its notes and help are only then } { Paths are shown from the working directory where they are under it, so messages read the same wherever the compiler or its library is. } @@ -204,7 +208,8 @@ begin { a second error at one place follows from the first: when a symbol ends a construct with an error, the one around it may stop there too } - if (errors > 0) and (p.f = lastError.f) and (p.a = lastError.a) then exit; + shown := not quiet and not ((errors > 0) and (p.f = lastError.f) and (p.a = lastError.a)); + if not shown then exit; lastError := p; Show(p, 'error', Red, msg); inc(errors); @@ -219,18 +224,20 @@ if werror then Error(p, msg) else begin Show(p, 'warning', Yellow, msg); + shown := true; inc(warnings); end; end; +{ Notes and help follow an error or a warning, and are shown with it } procedure Note(const p: TPos; const msg: shortstring); begin - Show(p, 'note', Cyan, msg); + if shown then Show(p, 'note', Cyan, msg); end; procedure Help(const msg: shortstring); begin - writeln(StdErr, Paint(Green, 'help:'), ' ', msg); + if shown then writeln(StdErr, Paint(Green, 'help:'), ' ', msg); end; { An error with no place in the source, such as a file that can't be diff --git a/src/gen.pas b/src/gen.pas index cc6a099..23db77d 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -1115,6 +1115,15 @@ c.n := 0; end; +{ Too many arguments or too few, at p } +procedure WrongCount(var c: TCall; const p: TPos; const what: shortstring); +begin + if c.obj = nil then MarkAt(p, what); + Error(p, what + ': ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); + if c.obj^.module <> nil then Note(c.obj^.pos, 'the signature is here'); + Fail; +end; + procedure PassArg(var c: TCall; var a: TItem); var p: PObj; s: int64; begin @@ -1124,10 +1133,7 @@ { past the parameters of a C procedure that ends in ...: a string goes as a C string, an integer constant as an I64, a real constant as an F64; C takes no F32 there } - if not c.sig^.variadic then begin - if c.obj = nil then MarkAt(a.pos, 'too many arguments'); - MarkAt(a.pos, 'too many arguments: ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); - end; + if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); if a.mode = mStr then Coerce(a, tPtrU8) else if a.typ^.form = fConst then Coerce(a, tI64) else if a.typ = tRConst then Coerce(a, tReal) @@ -1166,10 +1172,7 @@ res: PType; i, ni, nf, k, pad: longint; ret: int64; onStack: array[0..63] of boolean; begin - if c.param <> nil then begin - if c.obj = nil then Mark('too few arguments'); - Mark('too few arguments: ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); - end; + if c.param <> nil then WrongCount(c, TokPos, 'too few arguments'); res := c.sig^.base; ret := 0; ni := 0; nf := 0; diff --git a/src/parse.pas b/src/parse.pas index ba86129..69ba46f 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -85,6 +85,8 @@ procedure Expect(s: TSym); begin + if (tok = sLbrace) and (s in [sThen, sDo]) then + MarkHelp(MissingPos, 'expected ' + SymText(s) + ', found ' + Found, 'a block is written ' + SymText(s) + ' ... end'); if tok <> s then Expected(SymText(s)); Next; end; @@ -94,10 +96,12 @@ Name := Intern(@s[1], length(s)); end; -procedure IdentDef(var n: longint; var exp: boolean); +{ A name being declared, and whether it is exported; at is the name } +procedure IdentDef(var n: longint; var exp: boolean; var at: TPos); begin if tok <> sIdent then Expected('a name'); n := id; + at := TokPos; Next; exp := false; if tok = sTimes then begin @@ -162,6 +166,7 @@ begin recovery := r.outer; quiet := r.quiet; + skipping := false; end; { After an error: the state as it was at the start of r's construct, @@ -169,6 +174,7 @@ procedure Restore(var r: TRecover); begin RestoreScan(r.scan); + skipping := true; CutScopes(r.scopes); depth := r.depth; numDefers := r.numDefers; curLoop := r.loop; curResult := r.result; deferring := r.deferring; directDefer := r.directDefer; @@ -247,6 +253,25 @@ ConstExpr := ConstValue(x); end; +{ Help after a name that isn't found: o, the closest name in reach } +procedure DidYouMean(o: PObj); +begin + if o <> nil then Help('did you mean ' + NameOf(o^.name) + '?'); +end; + +{ Where module n is found, if it is: dir and the file in it } +function ModuleFile(n: longint; var dir, f: shortstring): boolean; forward; + +{ Reports name n at p as not declared, with what it may have meant } +procedure NotDeclared(const p: TPos; n: longint); +var dir, f: shortstring; +begin + Error(p, NameOf(n) + ' isn''t declared'); + if ModuleFile(n, dir, f) then Help(NameOf(n) + ' is a module: import ' + NameOf(n)) + else if (NameOf(n) = 'elif') or (NameOf(n) = 'elseif') then Help('raz writes elsif') + else DidYouMean(Closest(n, -1)); +end; + function QualIdent: PObj; var o: PObj; m: PModule; begin @@ -255,7 +280,8 @@ if o = nil then begin Poison(id); quiet := false; - Mark(NameOf(id) + ' isn''t declared'); + NotDeclared(TokPos, id); + Fail; end; if o^.typ = tPoison then quiet := true; Next; @@ -264,12 +290,35 @@ if tok <> sIdent then Expected('a name'); m := o^.module; o := Find(m^.scope, id); - if (o = nil) or not o^.exported then Mark(NameOf(id) + ' isn''t exported by ' + NameOf(m^.name)); + if (o = nil) or not o^.exported then begin + Error(TokPos, NameOf(id) + ' isn''t exported by ' + NameOf(m^.name)); + if o <> nil then Note(o^.pos, NameOf(id) + ' is declared here, without a *') + else DidYouMean(Closest(id, m^.scope)); + Fail; + end; Next; end; QualIdent := o; end; +{ The field of record type t named by the current symbol } +function FieldOf(t: PType): PObj; +var f: PObj; +begin + f := Find(t^.scope, id); + if f = nil then begin + Error(TokPos, TypeName(t) + ' has no field ' + NameOf(id)); + DidYouMean(Closest(id, t^.scope)); + Fail; + end; + if not f^.exported and (t^.module <> curModule) then begin + Error(TokPos, 'field ' + NameOf(id) + ' of ' + TypeName(t) + ' isn''t exported'); + Note(f^.pos, NameOf(id) + ' is declared here, without a *'); + Fail; + end; + FieldOf := f; +end; + function NewPtr(base: PType; vol: boolean): PType; var t: PType; begin @@ -284,7 +333,8 @@ function RecordType: PType; var t, ft: PType; f, last: PObj; names: array[0..63] of longint; - exps: array[0..63] of boolean; n, i: longint; off, a, maxA: int64; + exps: array[0..63] of boolean; poss: array[0..63] of TPos; + n, i: longint; off, a, maxA: int64; begin t := NewType(fRecord, 0, 1); if tok = sPacked then begin t^.packd := true; Next end; @@ -295,7 +345,7 @@ n := 0; repeat if n > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); - IdentDef(names[n], exps[n]); + IdentDef(names[n], exps[n], poss[n]); inc(n); if tok <> sComma then break; Next; @@ -307,6 +357,7 @@ off := AlignUp(off, a); f := NewObj(oField, names[i], ft); f^.exported := exps[i]; + f^.pos := poss[i]; f^.val := off; Insert(f, t^.scope); if last = nil then t^.fields := f else last^.next := f; @@ -778,10 +829,7 @@ Next; if (x.mode <> mVar) or (x.typ^.form <> fRecord) then MarkAt(x.pos, 'expected a record, found ' + What(x)); if tok <> sIdent then Expected('a field'); - f := Find(x.typ^.scope, id); - if f = nil then Mark(TypeName(x.typ) + ' has no field ' + NameOf(id)); - if not f^.exported and (x.typ^.module <> curModule) then - Mark('field ' + NameOf(id) + ' of ' + TypeName(x.typ) + ' isn''t exported'); + f := FieldOf(x.typ); Next; Field(x, f); called := false; @@ -885,10 +933,7 @@ if tok <> sRbrace then repeat if tok <> sIdent then Expected('a field'); - f := Find(t^.scope, id); - if f = nil then Mark(TypeName(t) + ' has no field ' + NameOf(id)); - if not f^.exported and (t^.module <> curModule) then - Mark('field ' + NameOf(id) + ' of ' + TypeName(t) + ' isn''t exported'); + f := FieldOf(t); for i := 0 to n - 1 do if seen[i] = id then Mark('field ' + NameOf(id) + ' is given twice'); if n <= High(seen) then begin seen[n] := id; inc(n) end; Next; @@ -1131,7 +1176,7 @@ Next; if tok <> sIdent then Expected('a name'); o := Lookup(id); - if o = nil then Mark(NameOf(id) + ' isn''t declared'); + if o = nil then begin NotDeclared(TokPos, id); Fail end; if (o^.cls <> oVar) or not (o^.typ^.form in [fInt, fEnum]) then if o^.cls = oVar then Mark('a for loop needs an integer or enumeration variable, found ' + TypeName(o^.typ)) @@ -1346,7 +1391,9 @@ if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin BeginCall(c, x); EndCall(c, x); - end else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); + end else if tok = sEql then + MarkHelp(x.pos, Subject(x, first) + ' isn''t a statement', 'assignment is written :=') + else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); end; end; sIf: IfStatement; @@ -1518,18 +1565,21 @@ begin line := CurLine; Next; - at := TokPos; - IdentDef(n, exp); - at := SpanFrom(at); + IdentDef(n, exp, at); sig := Signature; Expect(sSemicolon); p := Find(TopScope, n); if (p <> nil) and (p^.cls = oProc) and p^.fwd then begin - if not Same(p^.typ, sig) then MarkAt(at, NameOf(n) + ' doesn''t have the signature it was declared forward with'); + if not Same(p^.typ, sig) then begin + Error(at, NameOf(n) + ' doesn''t have the signature it was declared forward with'); + Note(p^.pos, 'the signature is here'); + Fail; + end; if tok = sForward then Mark(NameOf(n) + ' is declared forward twice'); p^.exported := p^.exported or exp; end else begin p := NewObj(oProc, n, sig); + p^.pos := at; p^.exported := exp; if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq @@ -1564,10 +1614,8 @@ curModule^.usesC := true; if tok = sProc then begin Next; - at := TokPos; - IdentDef(n, exp); + IdentDef(n, exp, at); WillDeclare(n); - at := SpanFrom(at); inExtern := true; t := Signature; inExtern := false; @@ -1582,7 +1630,7 @@ end else begin if tok <> sVar then Expected('"proc" or "var"'); Next; - IdentDef(n, exp); + IdentDef(n, exp, at); WillDeclare(n); Expect(sColon); declExported := exp; @@ -1590,6 +1638,7 @@ declExported := false; o := NewObj(oVar, n, t); end; + o^.pos := at; o^.exported := exp; o^.ext := true; o^.chunk := ExternChunk(n); @@ -1597,9 +1646,9 @@ end; procedure ConstDecl(top: boolean); -var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start: TPos; +var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos; begin - IdentDef(n, exp); + IdentDef(n, exp, at); WillDeclare(n); t := nil; if tok = sColon then begin @@ -1611,6 +1660,7 @@ Expect(sEql); Expression(x, nil); o := NewObj(oConst, n, x.typ); + o^.pos := at; o^.exported := exp and top; if t = tError then begin o^.typ := tError; @@ -1622,15 +1672,16 @@ end; procedure TypeDecl(top: boolean); -var n: longint; exp: boolean; o: PObj; t: PType; +var n: longint; exp: boolean; o: PObj; t: PType; at: TPos; begin - IdentDef(n, exp); + IdentDef(n, exp, at); WillDeclare(n); Expect(sEql); declExported := exp and top; inTypes := true; t := ParseType; o := NewObj(oType, n, t); + o^.pos := at; o^.exported := exp and top; if t^.obj = nil then t^.obj := o; Declare(o); @@ -1641,11 +1692,12 @@ var i, k: longint; o: PObj; t: PType; names: array[0..63] of longint; exps: array[0..63] of boolean; + poss: array[0..63] of TPos; begin k := 0; repeat if k > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); - IdentDef(names[k], exps[k]); + IdentDef(names[k], exps[k], poss[k]); WillDeclare(names[k]); inc(k); if tok <> sComma then break; @@ -1657,6 +1709,7 @@ declExported := false; for i := 0 to k - 1 do begin o := NewObj(oVar, names[i], t); + o^.pos := poss[i]; o^.exported := exps[i] and top; o^.isVar := false; if top then begin @@ -1741,7 +1794,7 @@ for i := 0 to numFwd - 1 do begin o := Lookup(fwdTypes[i]^.fwd); if (o = nil) or (o^.cls <> oType) then begin - Error(fwdPos[i], NameOf(fwdTypes[i]^.fwd) + ' isn''t declared'); + NotDeclared(fwdPos[i], fwdTypes[i]^.fwd); fwdTypes[i]^.base := tPoison; end else fwdTypes[i]^.base := o^.typ; fwdTypes[i]^.fwd := -1; @@ -1830,7 +1883,7 @@ if a = nil then MarkAt(m^.args.pos[i], NameOf(m^.name) + ' needs a procedure for ' + NameOf(o^.name)); if not Same(a^.typ, sig) then begin Error(m^.args.pos[i], NameOf(a^.name) + ' doesn''t have the signature of ' + NameOf(o^.name)); - Note(at, NameOf(o^.name) + ' is declared here'); + Note(at, 'the signature is here'); Finish; end; o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; @@ -2029,29 +2082,33 @@ CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); end; -function ImportModule(name: longint; const args: TArgs): PModule; +function ModuleFile(n: longint; var dir, f: shortstring): boolean; const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); -var m: PModule; i, j: longint; dir, f: shortstring; +var i, j: longint; begin - m := FindModule(name, args); - if m <> nil then exit(m); - for i := 0 to numPending - 1 do - if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); for i := -1 to numInc do begin if i = -1 then dir := mainDir else if i = numInc then dir := libDir else dir := incDirs[i]; for j := 0 to 3 do begin - f := NameOf(name) + Suffixes[j] + '.raz'; - if Exists(dir + '/' + f) then begin - m := CompileFile(dir + '/' + f, name, args, false); - CheckSiblings(dir, f, name, m, args); - exit(m); - end; + f := NameOf(n) + Suffixes[j] + '.raz'; + if Exists(dir + '/' + f) then exit(true); end; end; - MarkAt(args.at, 'can''t find module ' + NameOf(name)); - ImportModule := nil; + ModuleFile := false; +end; + +function ImportModule(name: longint; const args: TArgs): PModule; +var m: PModule; i: longint; dir, f: shortstring; +begin + m := FindModule(name, args); + if m <> nil then exit(m); + for i := 0 to numPending - 1 do + if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); + if not ModuleFile(name, dir, f) then MarkAt(args.at, 'can''t find module ' + NameOf(name)); + m := CompileFile(dir + '/' + f, name, args, false); + CheckSiblings(dir, f, name, m, args); + ImportModule := m; end; function CompileMain(const path: shortstring): PModule; diff --git a/src/raz.pas b/src/raz.pas index ab738ed..5355a71 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -152,9 +152,10 @@ main := Find(m^.scope, Intern('main', 4)); hasResult := false; if main <> nil then begin - if (main^.cls <> oProc) or not main^.exported then Fatal('main must be an exported procedure'); - if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then - Fatal('main must be proc main* or proc main*: I64'); + if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') + else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then + Error(main^.pos, 'main must be proc main* or proc main*: I64'); + Finish; hasResult := main^.typ^.base <> nil; end; diff --git a/src/scan.pas b/src/scan.pas index 71c8ea7..1b2425e 100644 --- a/src/scan.pas +++ b/src/scan.pas @@ -71,6 +71,7 @@ procedure Mark(const msg: shortstring); procedure MarkAt(const p: TPos; const msg: shortstring); procedure MarkHelp(const p: TPos; const msg, hint: shortstring); +procedure Fail; function AtLineStart: boolean; function SymText(s: TSym): shortstring; function Found: shortstring; @@ -79,9 +80,9 @@ { Where the parser goes on after an error: the innermost statement or declaration being read. nil stops the compiler at the first error. } recovery: ^jmp_buf; - { The construct being read uses a name already reported as not - declared: its errors follow from that one, and aren't reported. } - quiet: boolean; + { The parser is skipping what it has read once already, after an + error, so slips in it aren't reported again. } + skipping: boolean; procedure SaveScan(var st: TScanState); procedure RestoreScan(const st: TScanState); function TokPos: TPos; @@ -237,21 +238,26 @@ MarkAt(TokPos, msg); end; -{ An error in the parser's construct at p: reported, then the parser - goes on from the recovery point. } -procedure MarkAt(const p: TPos; const msg: shortstring); +{ After an error in the parser's construct, reported with its notes + and help: the parser goes on from the recovery point. } +procedure Fail; begin - if not quiet then Error(p, msg); if recovery = nil then Finish; longjmp(recovery^, 1); +end; + +procedure MarkAt(const p: TPos; const msg: shortstring); +begin + Error(p, msg); + Fail; end; { MarkAt, with a line of help after the message. } procedure MarkHelp(const p: TPos; const msg, hint: shortstring); begin - if not quiet then begin Error(p, msg); Help(hint) end; - if recovery = nil then Finish; - longjmp(recovery^, 1); + Error(p, msg); + Help(hint); + Fail; end; { An error in the text at bytes a to b of the file being read, which @@ -260,6 +266,7 @@ var p: TPos; begin p.f := cur.f; p.a := a; p.b := b; + quiet := false; Error(p, msg); Finish; end; @@ -269,6 +276,17 @@ MarkBytes(cur.tokPos, cur.pos, msg); end; +{ The n characters at the scanner are how another language writes t: + an error, but reading goes on as if t were written. } +procedure Slip(n: longint; t: TSym; const msg: shortstring); +var p: TPos; +begin + p.f := cur.f; p.a := cur.pos; p.b := cur.pos + n; + if not skipping then Error(p, msg); + inc(cur.pos, n); + tok := t; +end; + { How an operator or reserved word is written, quoted, for messages. } function SymText(s: TSym): shortstring; begin @@ -597,7 +615,12 @@ '-': begin inc(cur.pos); tok := sMinus end; '*': begin inc(cur.pos); tok := sTimes end; '/': begin inc(cur.pos); tok := sSlash end; - '=': begin inc(cur.pos); tok := sEql end; + '=': + if cur.buf[cur.pos + 1] = '=' then Slip(2, sEql, 'comparing is written =, not ==') + else begin inc(cur.pos); tok := sEql end; + '&': + if cur.buf[cur.pos + 1] = '&' then Slip(2, sAnd, '"and" is written and, not &&') + else MarkBytes(cur.pos, cur.pos + 1, 'unexpected character "&"'); '#': begin inc(cur.pos); tok := sNeq end; '<': begin inc(cur.pos); @@ -631,7 +654,12 @@ '{': begin inc(cur.pos); tok := sLbrace end; '}': begin inc(cur.pos); tok := sRbrace end; '^': begin inc(cur.pos); tok := sArrow end; - '|': begin inc(cur.pos); tok := sBar end; + '|': + if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||') + else begin inc(cur.pos); tok := sBar end; + '!': + if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=') + else Slip(1, sNot, '"not" is written not, not !'); #0: begin if cur.pos < cur.len then MarkBytes(cur.pos, cur.pos + 1, 'NUL byte in the text'); tok := sEof; diff --git a/src/sym.pas b/src/sym.pas index b60ac2f..a939a6d 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -58,6 +58,7 @@ typ: PType; next: PObj; { the next in its list: scope, fields, parameters } scope: longint; { where it is declared } + pos: TPos; { where its name is declared, for messages } val: int64; { oConst: value; oBuiltin: which; oCase: tag; oField: offset; oVar: frame offset } chunk: longint; { global oVar, oProc, string oConst: its chunk } @@ -140,6 +141,7 @@ procedure Insert(obj: PObj; s: longint); function Find(s, name: longint): PObj; function Lookup(name: longint): PObj; +function Closest(name, s: longint): PObj; function AlignUp(x, a: int64): int64; function Same(a, b: PType): boolean; @@ -151,6 +153,8 @@ function Fingerprint(o: PObj): qword; implementation + +uses math; type PObjTab = ^TObjTab; @@ -186,6 +190,7 @@ o^.typ := typ; o^.chunk := -1; o^.module := curModule; + o^.pos := TokPos; NewObj := o; end; @@ -270,8 +275,11 @@ i := Slot(s, obj^.name); o := hashTab^[i]; while o <> nil do begin - if (o^.scope = s) and (o^.name = obj^.name) then - Mark(NameOf(obj^.name) + ' is declared twice'); + if (o^.scope = s) and (o^.name = obj^.name) then begin + Error(obj^.pos, NameOf(obj^.name) + ' is declared twice'); + Note(o^.pos, 'the first one is here'); + Fail; + end; i := (i + 1) and hashMask; o := hashTab^[i]; end; @@ -302,6 +310,50 @@ if o <> nil then exit(o); end; Lookup := nil; +end; + +{ How far apart two names are: the edits, a character put in, taken out, + changed or swapped with the next, that make one the other, ignoring + case. } +function Distance(const a, b: shortstring): longint; +var d: array[0..MaxIdent + 1, 0..MaxIdent + 1] of longint; i, j, c: longint; +begin + for i := 0 to length(a) do d[i, 0] := i; + for j := 0 to length(b) do d[0, j] := j; + for i := 1 to length(a) do + for j := 1 to length(b) do begin + c := ord(UpCase(a[i]) <> UpCase(b[j])); + d[i, j] := Min(Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + c); + if (i > 1) and (j > 1) and (UpCase(a[i]) = UpCase(b[j - 1])) + and (UpCase(a[i - 1]) = UpCase(b[j])) then + d[i, j] := Min(d[i, j], d[i - 2, j - 2] + 1); + end; + Distance := d[length(a), length(b)]; +end; + +{ The name spelled most like name, for "did you mean": in scope s, if + it can be reached from here, or in the scopes in reach if s < 0; nil + if none is within an edit for every three characters. Only after an + error, so it looks at every object. } +function Closest(name, s: longint): PObj; +var i, j, d, best: longint; o: PObj; n: shortstring; reach: boolean; +begin + n := NameOf(name); + best := length(n) div 3 + 1; + Closest := nil; + for i := 0 to hashMask do begin + o := hashTab^[i]; + if (o = nil) or (o^.name = name) or (o^.name < 0) or (o^.typ = tPoison) then continue; + if s >= 0 then reach := (o^.scope = s) and (o^.exported or (o^.module = curModule)) + else begin + reach := false; + for j := 0 to depth - 1 do reach := reach or (o^.scope = scopes[j]); + end; + if reach then begin + d := Distance(n, NameOf(o^.name)); + if d < best then begin best := d; Closest := o end; + end; + end; end; { Types } diff --git a/test/err/AtMany.err b/test/err/AtMany.err index 53d73d4..aaedd42 100644 --- a/test/err/AtMany.err +++ b/test/err/AtMany.err @@ -1,4 +1,7 @@ test/err/AtMany.raz:7:16: error: too many arguments: Id takes 1 7 | x := Id(1, 2 * 3); | ^^^^^ +test/err/AtMany.raz:2:6: note: the signature is here + 2 | proc Id(a: I64): I64; + | ^^ raz: 1 error diff --git a/test/err/BadProc.err b/test/err/BadProc.err index 238d88e..9eb90a3 100644 --- a/test/err/BadProc.err +++ b/test/err/BadProc.err @@ -1,7 +1,7 @@ test/err/BadProc.raz:2:33: error: cmp doesn't have the signature of hash 2 | import Str, M := Map([]U8, I64, Str.cmp, Str.eq); | ^^^^^^^ -lib/Map.raz:4:18: note: hash is declared here +lib/Map.raz:4:18: note: the signature is here 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); | ^^^^ raz: 1 error diff --git a/test/err/DeclLitNotExported.err b/test/err/DeclLitNotExported.err index a1038c0..cdbaade 100644 --- a/test/err/DeclLitNotExported.err +++ b/test/err/DeclLitNotExported.err @@ -1,4 +1,7 @@ test/err/DeclLitNotExported.raz:6:11: error: field extra of Plat.Info isn't exported 6 | i := {extra: 1}; | ^^^^^ +test/run/mods/Plat.linux-x86_64.raz:3:33: note: extra is declared here, without a * + 3 | type Info* = record kind*: I64; extra: I64; end; + | ^^^^^ raz: 1 error diff --git a/test/err/DeclNotExported.err b/test/err/DeclNotExported.err index 18d5597..ca471ae 100644 --- a/test/err/DeclNotExported.err +++ b/test/err/DeclNotExported.err @@ -1,4 +1,7 @@ test/err/DeclNotExported.raz:6:21: error: hidden isn't exported by DeclHidden 6 | n := DeclHidden.hidden; | ^^^^^^ +test/err/mods/DeclHidden.raz:3:7: note: hidden is declared here, without a * + 3 | const hidden = 2; + | ^^^^^^ raz: 1 error diff --git a/test/err/Field.err b/test/err/Field.err index c08659b..9b154fa 100644 --- a/test/err/Field.err +++ b/test/err/Field.err @@ -1,4 +1,7 @@ test/err/Field.raz:6:7: error: field extra of Plat.Info isn't exported 6 | i.extra := 1; | ^^^^^ +test/run/mods/Plat.linux-x86_64.raz:3:33: note: extra is declared here, without a * + 3 | type Info* = record kind*: I64; extra: I64; end; + | ^^^^^ raz: 1 error diff --git a/test/err/GenTooFew.err b/test/err/GenTooFew.err index fe013d9..e42ea12 100644 --- a/test/err/GenTooFew.err +++ b/test/err/GenTooFew.err @@ -1,4 +1,7 @@ test/err/GenTooFew.raz:6:14: error: too few arguments: Id takes 1 6 | x := Id(); | ^ +test/err/GenTooFew.raz:2:6: note: the signature is here + 2 | proc Id(a: I64): I64; begin return a; end; + | ^^ raz: 1 error diff --git a/test/err/NoteBuiltin.err b/test/err/NoteBuiltin.err new file mode 100644 index 0000000..ab48a47 --- /dev/null +++ b/test/err/NoteBuiltin.err @@ -0,0 +1,5 @@ +test/err/NoteBuiltin.raz:5:10: error: len isn't declared + 5 | x := len(y); + | ^^^ +help: did you mean LEN? +raz: 1 error diff --git a/test/err/NoteBuiltin.raz b/test/err/NoteBuiltin.raz new file mode 100644 index 0000000..37a5714 --- /dev/null +++ b/test/err/NoteBuiltin.raz @@ -0,0 +1,7 @@ +module NoteBuiltin; +proc main*; +var x, y: I64; +begin + x := len(y); +end; +end NoteBuiltin. diff --git a/test/err/NoteExport.err b/test/err/NoteExport.err new file mode 100644 index 0000000..8d3d9e7 --- /dev/null +++ b/test/err/NoteExport.err @@ -0,0 +1,5 @@ +test/err/NoteExport.raz:6:9: error: Int isn't exported by Out + 6 | Out.Int(x); + | ^^^ +help: did you mean int? +raz: 1 error diff --git a/test/err/NoteExport.raz b/test/err/NoteExport.raz new file mode 100644 index 0000000..81e331a --- /dev/null +++ b/test/err/NoteExport.raz @@ -0,0 +1,8 @@ +module NoteExport; +import Out; +proc main*; +var x, y: I64; +begin + Out.Int(x); +end; +end NoteExport. diff --git a/test/err/NoteField.err b/test/err/NoteField.err new file mode 100644 index 0000000..baad96f --- /dev/null +++ b/test/err/NoteField.err @@ -0,0 +1,5 @@ +test/err/NoteField.raz:6:7: error: P has no field cuont + 6 | p.cuont := 1; + | ^^^^^ +help: did you mean count? +raz: 1 error diff --git a/test/err/NoteField.raz b/test/err/NoteField.raz new file mode 100644 index 0000000..c35bc13 --- /dev/null +++ b/test/err/NoteField.raz @@ -0,0 +1,8 @@ +module NoteField; +type P = record count: I64; end; +proc main*; +var p: P; +begin + p.cuont := 1; +end; +end NoteField. diff --git a/test/err/NoteImport.err b/test/err/NoteImport.err new file mode 100644 index 0000000..2b26aef --- /dev/null +++ b/test/err/NoteImport.err @@ -0,0 +1,5 @@ +test/err/NoteImport.raz:5:5: error: Fmt isn't declared + 5 | Fmt.print(x); + | ^^^ +help: Fmt is a module: import Fmt +raz: 1 error diff --git a/test/err/NoteImport.raz b/test/err/NoteImport.raz new file mode 100644 index 0000000..9a6373d --- /dev/null +++ b/test/err/NoteImport.raz @@ -0,0 +1,7 @@ +module NoteImport; +proc main*; +var x, y: I64; +begin + Fmt.print(x); +end; +end NoteImport. diff --git a/test/err/NoteLocal.err b/test/err/NoteLocal.err new file mode 100644 index 0000000..3aee8c0 --- /dev/null +++ b/test/err/NoteLocal.err @@ -0,0 +1,5 @@ +test/err/NoteLocal.raz:6:5: error: cuont isn't declared + 6 | cuont := 1; + | ^^^^^ +help: did you mean count? +raz: 1 error diff --git a/test/err/NoteLocal.raz b/test/err/NoteLocal.raz new file mode 100644 index 0000000..2bd3ddd --- /dev/null +++ b/test/err/NoteLocal.raz @@ -0,0 +1,8 @@ +module NoteLocal; +proc main*; +var x, y, count: I64; +begin + + cuont := 1; +end; +end NoteLocal. diff --git a/test/err/ScanMain.err b/test/err/ScanMain.err index 1f4c345..353e780 100644 --- a/test/err/ScanMain.err +++ b/test/err/ScanMain.err @@ -1 +1,4 @@ -raz: main must be proc main* or proc main*: I64 +test/err/ScanMain.raz:2:6: error: main must be proc main* or proc main*: I64 + 2 | proc main*(n: I64); + | ^^^^ +raz: 1 error diff --git a/test/err/ScanTwice.err b/test/err/ScanTwice.err index 868d018..a014a4d 100644 --- a/test/err/ScanTwice.err +++ b/test/err/ScanTwice.err @@ -1,4 +1,7 @@ -test/err/ScanTwice.raz:4:11: error: x is declared twice +test/err/ScanTwice.raz:4:5: error: x is declared twice 4 | x: I64; - | ^ + | ^ +test/err/ScanTwice.raz:3:5: note: the first one is here + 3 | var x: I64; + | ^ raz: 1 error diff --git a/test/err/SlipAnd.err b/test/err/SlipAnd.err new file mode 100644 index 0000000..b10122b --- /dev/null +++ b/test/err/SlipAnd.err @@ -0,0 +1,4 @@ +test/err/SlipAnd.raz:5:16: error: "and" is written and, not && + 5 | if (x = 1) && (y = 2) then x := 2; end; + | ^^ +raz: 1 error diff --git a/test/err/SlipAnd.raz b/test/err/SlipAnd.raz new file mode 100644 index 0000000..e0dcbf1 --- /dev/null +++ b/test/err/SlipAnd.raz @@ -0,0 +1,7 @@ +module SlipAnd; +proc main*; +var x, y: I64; +begin + if (x = 1) && (y = 2) then x := 2; end; +end; +end SlipAnd. diff --git a/test/err/SlipAssign.err b/test/err/SlipAssign.err new file mode 100644 index 0000000..b5db1d3 --- /dev/null +++ b/test/err/SlipAssign.err @@ -0,0 +1,5 @@ +test/err/SlipAssign.raz:5:5: error: x isn't a statement + 5 | x = 1; + | ^ +help: assignment is written := +raz: 1 error diff --git a/test/err/SlipAssign.raz b/test/err/SlipAssign.raz new file mode 100644 index 0000000..7fd552b --- /dev/null +++ b/test/err/SlipAssign.raz @@ -0,0 +1,7 @@ +module SlipAssign; +proc main*; +var x, y: I64; +begin + x = 1; +end; +end SlipAssign. diff --git a/test/err/SlipBrace.err b/test/err/SlipBrace.err new file mode 100644 index 0000000..f756de0 --- /dev/null +++ b/test/err/SlipBrace.err @@ -0,0 +1,5 @@ +test/err/SlipBrace.raz:5:14: error: expected "then", found "{" + 5 | if x = 1 { x := 2 } + | ^ +help: a block is written "then" ... end +raz: 1 error diff --git a/test/err/SlipBrace.raz b/test/err/SlipBrace.raz new file mode 100644 index 0000000..7f7f351 --- /dev/null +++ b/test/err/SlipBrace.raz @@ -0,0 +1,7 @@ +module SlipBrace; +proc main*; +var x, y: I64; +begin + if x = 1 { x := 2 } +end; +end SlipBrace. diff --git a/test/err/SlipDo.err b/test/err/SlipDo.err new file mode 100644 index 0000000..6c38943 --- /dev/null +++ b/test/err/SlipDo.err @@ -0,0 +1,5 @@ +test/err/SlipDo.raz:5:17: error: expected "do", found "{" + 5 | while x < 3 { x := x + 1 } + | ^ +help: a block is written "do" ... end +raz: 1 error diff --git a/test/err/SlipDo.raz b/test/err/SlipDo.raz new file mode 100644 index 0000000..56f014b --- /dev/null +++ b/test/err/SlipDo.raz @@ -0,0 +1,7 @@ +module SlipDo; +proc main*; +var x, y: I64; +begin + while x < 3 { x := x + 1 } +end; +end SlipDo. diff --git a/test/err/SlipElif.err b/test/err/SlipElif.err new file mode 100644 index 0000000..2fb6008 --- /dev/null +++ b/test/err/SlipElif.err @@ -0,0 +1,5 @@ +test/err/SlipElif.raz:5:27: error: elif isn't declared + 5 | if x = 1 then x := 2; elif x = 3 then x := 4; end; + | ^^^^ +help: raz writes elsif +raz: 1 error diff --git a/test/err/SlipElif.raz b/test/err/SlipElif.raz new file mode 100644 index 0000000..e7ad101 --- /dev/null +++ b/test/err/SlipElif.raz @@ -0,0 +1,7 @@ +module SlipElif; +proc main*; +var x, y: I64; +begin + if x = 1 then x := 2; elif x = 3 then x := 4; end; +end; +end SlipElif. diff --git a/test/err/SlipEql.err b/test/err/SlipEql.err new file mode 100644 index 0000000..3e2b7a0 --- /dev/null +++ b/test/err/SlipEql.err @@ -0,0 +1,4 @@ +test/err/SlipEql.raz:5:10: error: comparing is written =, not == + 5 | if x == 1 then x := 2; end; + | ^^ +raz: 1 error diff --git a/test/err/SlipEql.raz b/test/err/SlipEql.raz new file mode 100644 index 0000000..1b27239 --- /dev/null +++ b/test/err/SlipEql.raz @@ -0,0 +1,7 @@ +module SlipEql; +proc main*; +var x, y: I64; +begin + if x == 1 then x := 2; end; +end; +end SlipEql. diff --git a/test/err/SlipMany.err b/test/err/SlipMany.err new file mode 100644 index 0000000..591b0e3 --- /dev/null +++ b/test/err/SlipMany.err @@ -0,0 +1,10 @@ +test/err/SlipMany.raz:5:10: error: "not equal" is written #, not != + 5 | if x != 1 && z then x := y; end; + | ^^ +test/err/SlipMany.raz:5:15: error: "and" is written and, not && + 5 | if x != 1 && z then x := y; end; + | ^^ +test/err/SlipMany.raz:5:18: error: z isn't declared + 5 | if x != 1 && z then x := y; end; + | ^ +raz: 3 errors diff --git a/test/err/SlipMany.raz b/test/err/SlipMany.raz new file mode 100644 index 0000000..e6363f0 --- /dev/null +++ b/test/err/SlipMany.raz @@ -0,0 +1,7 @@ +module SlipMany; +proc main*; +var x, y: I64; +begin + if x != 1 && z then x := y; end; +end; +end SlipMany. diff --git a/test/err/SlipNeq.err b/test/err/SlipNeq.err new file mode 100644 index 0000000..5ce9b3a --- /dev/null +++ b/test/err/SlipNeq.err @@ -0,0 +1,4 @@ +test/err/SlipNeq.raz:5:10: error: "not equal" is written #, not != + 5 | if x != 1 then x := 2; end; + | ^^ +raz: 1 error diff --git a/test/err/SlipNeq.raz b/test/err/SlipNeq.raz new file mode 100644 index 0000000..3af8ca3 --- /dev/null +++ b/test/err/SlipNeq.raz @@ -0,0 +1,7 @@ +module SlipNeq; +proc main*; +var x, y: I64; +begin + if x != 1 then x := 2; end; +end; +end SlipNeq. diff --git a/test/err/SlipNot.err b/test/err/SlipNot.err new file mode 100644 index 0000000..bdfb059 --- /dev/null +++ b/test/err/SlipNot.err @@ -0,0 +1,4 @@ +test/err/SlipNot.raz:5:8: error: "not" is written not, not ! + 5 | if ! (x = 1) then x := 2; end; + | ^ +raz: 1 error diff --git a/test/err/SlipNot.raz b/test/err/SlipNot.raz new file mode 100644 index 0000000..2147db5 --- /dev/null +++ b/test/err/SlipNot.raz @@ -0,0 +1,7 @@ +module SlipNot; +proc main*; +var x, y: I64; +begin + if ! (x = 1) then x := 2; end; +end; +end SlipNot. diff --git a/test/err/SlipOr.err b/test/err/SlipOr.err new file mode 100644 index 0000000..01eda37 --- /dev/null +++ b/test/err/SlipOr.err @@ -0,0 +1,4 @@ +test/err/SlipOr.raz:5:16: error: "or" is written or, not || + 5 | if (x = 1) || (y = 2) then x := 2; end; + | ^^ +raz: 1 error diff --git a/test/err/SlipOr.raz b/test/err/SlipOr.raz new file mode 100644 index 0000000..c6c4af2 --- /dev/null +++ b/test/err/SlipOr.raz @@ -0,0 +1,7 @@ +module SlipOr; +proc main*; +var x, y: I64; +begin + if (x = 1) || (y = 2) then x := 2; end; +end; +end SlipOr. diff --git a/test/err/StmtSigForward.err b/test/err/StmtSigForward.err index f4f7eb4..be4b3a5 100644 --- a/test/err/StmtSigForward.err +++ b/test/err/StmtSigForward.err @@ -1,6 +1,9 @@ test/err/StmtSigForward.raz:3:6: error: f doesn't have the signature it was declared forward with 3 | proc f(a: BOOL); begin end; | ^ +test/err/StmtSigForward.raz:2:6: note: the signature is here + 2 | proc f(a: I64); forward; + | ^ test/err/StmtSigForward.raz:4:19: error: f is declared forward but never defined 4 | end StmtSigForward. | ^