From b17e69bd2ee9c3f06c47621b5505596fc90e8299 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 09:00:33 +0000 Subject: [PATCH] tools/Scan.raz takes the scanner by var, or const to read it Scan.init and next move the scanner and take var s: Scanner; text and is only read it and take const. Doc, Mutate and Pretty drop their ADRs; Pretty's endsOperand takes its scanner const. rmacs and ravite, which use Scan as a tool, mend theirs when they move their pins, in the same release as the library's, so that they break once. --- diff --git a/tools/Doc.raz b/tools/Doc.raz index 8ac1e7c..3cbdffa 100644 --- a/tools/Doc.raz +++ b/tools/Doc.raz @@ -38,7 +38,7 @@ docStart := -1; newLine := false; repeat - Scan.next(ADR(sc)); + Scan.next(sc); newLine := newLine or sc.lineBreak; if (sc.kind = Scan.comment) and newLine then docStart := sc.start; docEnd := sc.stop; @@ -76,7 +76,7 @@ proc text: []U8; begin - return Scan.text(ADR(sc)); + return Scan.text(sc); end; proc is(s: []U8): BOOL; @@ -114,7 +114,7 @@ while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end; t := sc; t.pos := stop; - Scan.next(ADR(t)); + Scan.next(t); if (t.kind = Scan.comment) and (t.start = stop) then return t.stop; end; return stop; end; @@ -307,7 +307,7 @@ Buf.init(b, Heap.heap); if not Files.read(path, b) then Rt.stop("Doc: can't read the file"); end; src := Buf.bytes(b); - Scan.init(ADR(sc), src); + Scan.init(sc, src); next; while (sc.kind # Scan.eof) and not is("module") do next; end; if sc.kind = Scan.eof then Rt.stop("Doc: no module"); end; diff --git a/tools/Mutate.raz b/tools/Mutate.raz index dcaab3d..2de92f3 100644 --- a/tools/Mutate.raz +++ b/tools/Mutate.raz @@ -36,12 +36,12 @@ proc count(): INT; var sc: Scan.Scanner; n: INT; begin - Scan.init(ADR(sc), src); - Scan.next(ADR(sc)); + Scan.init(sc, src); + Scan.next(sc); n := 0; while sc.kind # Scan.eof do if sc.kind # Scan.comment then INC(n); end; - Scan.next(ADR(sc)); + Scan.next(sc); end; return n; end; @@ -50,9 +50,9 @@ proc symbol(k: INT): Scan.Scanner; var sc: Scan.Scanner; begin - Scan.init(ADR(sc), src); + Scan.init(sc, src); repeat - Scan.next(ADR(sc)); + Scan.next(sc); if sc.kind # Scan.comment then DEC(k); end; until k < 0; return sc; @@ -77,17 +77,17 @@ if op = 0 then Out.str(src[a.stop..b.start]); elsif op = 1 then - Out.str(Scan.text(ADR(a))); + Out.str(Scan.text(a)); Out.str(" "); - Out.str(Scan.text(ADR(a))); + Out.str(Scan.text(a)); Out.str(src[a.stop..b.start]); elsif op = 2 then - Out.str(Scan.text(ADR(b))); + Out.str(Scan.text(b)); Out.str(src[a.stop..b.start]); - Out.str(Scan.text(ADR(a))); + Out.str(Scan.text(a)); b.start := b.stop; (* b has been written, as the first *) else - Out.str(Scan.text(ADR(c))); + Out.str(Scan.text(c)); Out.str(src[a.stop..b.start]); end; Out.str(src[b.start..LEN(src)]); diff --git a/tools/Pretty.raz b/tools/Pretty.raz index ba0e647..9302901 100644 --- a/tools/Pretty.raz +++ b/tools/Pretty.raz @@ -94,10 +94,10 @@ end; (* Whether s ends an operand, so that a - after it subtracts. *) -proc endsOperand(s: ^Scan.Scanner): BOOL; +proc endsOperand(const s: Scan.Scanner): BOOL; begin - if s^.kind = Scan.word then return not Scan.reserved(Scan.text(s)); end; - return (s^.kind = Scan.number) or (s^.kind = Scan.literal) + if s.kind = Scan.word then return not Scan.reserved(Scan.text(s)); end; + return (s.kind = Scan.number) or (s.kind = Scan.literal) or Scan.is(s, ")") or Scan.is(s, "]") or Scan.is(s, "}") or Scan.is(s, "^"); end; @@ -106,7 +106,7 @@ var s: Scan.Scanner; begin s := sc; - repeat Scan.next(ADR(s)); until s.kind # Scan.comment; + repeat Scan.next(s); until s.kind # Scan.comment; return s; end; @@ -115,18 +115,18 @@ proc binary: BOOL; var n: Scan.Scanner; begin - if sc.kind = Scan.word then return Scan.among(Scan.text(ADR(sc)), binaryWords); end; - if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(ADR(sc)), binarySymbols) then + if sc.kind = Scan.word then return Scan.among(Scan.text(sc), binaryWords); end; + if (sc.kind # Scan.symbol) or not Scan.among(Scan.text(sc), binarySymbols) then return false; end; (* a | after an operand is or; one after anything else starts an arm *) - if Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+") or Scan.is(ADR(sc), "|") then - return endsOperand(ADR(prev)); + if Scan.is(sc, "-") or Scan.is(sc, "+") or Scan.is(sc, "|") then + return endsOperand(prev); end; - if Scan.is(ADR(sc), "*") and (prev.kind = Scan.word) then + if Scan.is(sc, "*") and (prev.kind = Scan.word) then n := following(); - if Scan.among(Scan.text(ADR(n)), "; , : = )") then return false; end; - if Scan.is(ADR(n), "(") and Scan.is(ADR(prev2), "proc") then return false; end; + if Scan.among(Scan.text(n), "; , : = )") then return false; end; + if Scan.is(n, "(") and Scan.is(prev2, "proc") then return false; end; end; return true; end; @@ -136,10 +136,10 @@ proc continues: BOOL; var t: []U8; begin - if (brackets > 0) or prevBinary or Scan.is(ADR(prev), ",") then return true; end; - t := Scan.text(ADR(sc)); + if (brackets > 0) or prevBinary or Scan.is(prev, ",") then return true; end; + t := Scan.text(sc); if sc.kind = Scan.word then return Scan.among(t, binaryWords) or Scan.among(t, "then do of"); end; - if Scan.is(ADR(sc), "|") then return endsOperand(ADR(prev)); end; + if Scan.is(sc, "|") then return endsOperand(prev); end; return (sc.kind = Scan.symbol) and Scan.among(t, binarySymbols); end; @@ -147,10 +147,10 @@ procedure type. *) proc declaration(s: Scan.Scanner): BOOL; begin - if (s.kind # Scan.word) or not Scan.among(Scan.text(ADR(s)), declarations) then return false; end; - if not Scan.is(ADR(s), "proc") then return true; end; - Scan.next(ADR(s)); - return not Scan.is(ADR(s), "("); + if (s.kind # Scan.word) or not Scan.among(Scan.text(s), declarations) then return false; end; + if not Scan.is(s, "proc") then return true; end; + Scan.next(s); + return not Scan.is(s, "("); end; (* The indent of the block's own lines, or of its end. *) @@ -182,10 +182,10 @@ end; if declaration(sc) then depth := 0; - inSection := Scan.among(Scan.text(ADR(sc)), "const type var"); + inSection := Scan.among(Scan.text(sc), "const type var"); return 0; end; - if Scan.among(Scan.text(ADR(sc)), "end until else elsif |") then + if Scan.among(Scan.text(sc), "end until else elsif |") then return outer(); end; return inner(); @@ -216,8 +216,8 @@ spaces(n); elsif afterComment then Buf.str(out, src[last..sc.start]); - elsif Scan.is(ADR(sc), ",") or Scan.is(ADR(sc), ";") then - elsif Scan.is(ADR(prev), ",") or Scan.is(ADR(prev), ";") or binaryNow or prevBinary then + elsif Scan.is(sc, ",") or Scan.is(sc, ";") then + elsif Scan.is(prev, ",") or Scan.is(prev, ";") or binaryNow or prevBinary then if not prevUnary then spaces(1); end; elsif not prevUnary then Buf.str(out, src[last..sc.start]); @@ -254,7 +254,7 @@ proc track(binaryNow: BOOL); var t: []U8; begin - t := Scan.text(ADR(sc)); + t := Scan.text(sc); if sc.kind = Scan.word then if Scan.among(t, openers) then if depth = LEN(bases) then Rt.stop("Pretty: blocks nested too deep"); end; @@ -268,8 +268,8 @@ elsif Scan.among(t, ") ] }") and (brackets > 0) then DEC(brackets); end; end; - prevUnary := (Scan.is(ADR(sc), "-") or Scan.is(ADR(sc), "+")) and not binaryNow - or Scan.is(ADR(sc), "~"); + prevUnary := (Scan.is(sc, "-") or Scan.is(sc, "+")) and not binaryNow + or Scan.is(sc, "~"); prevBinary := binaryNow; prev2 := prev; prev := sc; @@ -296,14 +296,14 @@ inSection := false; stmtIndent := 0; stmtDelta := 0; - Scan.init(ADR(sc), src); + Scan.init(sc, src); prev := sc; prev2 := sc; prevBinary := false; prevUnary := false; afterComment := false; last := 0; - Scan.next(ADR(sc)); + Scan.next(sc); while sc.kind # Scan.eof do b := (sc.kind # Scan.comment) and binary(); if sc.lineBreak then @@ -316,7 +316,7 @@ afterComment := sc.kind = Scan.comment; if not afterComment then track(b); end; last := sc.stop; - Scan.next(ADR(sc)); + Scan.next(sc); end; lineBreaks(last, LEN(src)); trim; diff --git a/tools/Scan.raz b/tools/Scan.raz index c97eb0a..eb2b057 100644 --- a/tools/Scan.raz +++ b/tools/Scan.raz @@ -60,9 +60,9 @@ end; (* The scanner at the start of src; next gives the first symbol. *) -proc init*(s: ^Scanner; src: []U8); +proc init*(var s: Scanner; src: []U8); begin - s^ := {src: src, kind: eof}; + s := {src: src, kind: eof}; end; (* Past the comment that starts at p, its nested comments with it. *) @@ -121,32 +121,32 @@ end; (* On to the next symbol. *) -proc next*(s: ^Scanner); +proc next*(var s: Scanner); var src: []U8; p: INT; c: U8; begin - src := s^.src; - p := s^.pos; - s^.lineBreak := p = 0; + src := s.src; + p := s.pos; + s.lineBreak := p = 0; while (p < LEN(src)) and isSpace(src[p]) do - if src[p] = '\n' then s^.lineBreak := true; end; + if src[p] = '\n' then s.lineBreak := true; end; INC(p); end; - s^.start := p; + s.start := p; if p >= LEN(src) then - s^.kind := eof; + s.kind := eof; else c := src[p]; if at(src, p, "(*") then - s^.kind := comment; p := pastComment(src, p); + s.kind := comment; p := pastComment(src, p); elsif isDigit(c) then - s^.kind := number; p := pastNumber(src, p); + s.kind := number; p := pastNumber(src, p); elsif isWord(c) then - s^.kind := word; + s.kind := word; while (p < LEN(src)) and isWord(src[p]) do INC(p); end; elsif (c = '"') or (c = '\'') or (c = '`') then - s^.kind := literal; p := pastLiteral(src, p); + s.kind := literal; p := pastLiteral(src, p); else - s^.kind := symbol; + s.kind := symbol; if at(src, p, "...") then INC(p, 3); elsif at(src, p, ":=") or at(src, p, "<=") or at(src, p, ">=") or at(src, p, "..") then INC(p, 2); @@ -155,17 +155,17 @@ end; end; end; - s^.stop := p; - s^.pos := p; + s.stop := p; + s.pos := p; end; -proc text*(s: ^Scanner): []U8; +proc text*(const s: Scanner): []U8; begin - return s^.src[s^.start..s^.stop]; + return s.src[s.start..s.stop]; end; (* Whether the current symbol is t. *) -proc is*(s: ^Scanner; t: []U8): BOOL; +proc is*(const s: Scanner; t: []U8): BOOL; begin return text(s) = t; end;