raz / commit fc966ca0

commit
fc966ca0c17208b3395bed52445a8cd0581b6d02
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
639f4eaa
Raw strings, indented with the code

Scan.rawString reads a string between backquotes, with no escapes,
over lines; one whose opening backquote ends its line and whose
closing one has only blanks before it loses those blanks from every
line (report 2026-10.25, section 2). tools/Scan reads them, Pretty
leaves them as written, and vim and emacs colour them and keep their
lines' indents. The seed predates them: src/check.sh leaves out the
files that use them.

patch · log

16 files changed, +296 -24

conform/err/ScanRawIndent.raz (history) added +10 -0

@@ -0,0 +1,10 @@
1+module ScanRawIndent;
2+import Out;
3+proc main*;
4+begin
5+ Out.str(`
6+ a
7+ b
8+ `);
9+end;
10+end ScanRawIndent.

conform/err/ScanRawOpen.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module ScanRawOpen;
2+const s = `abc;
3+
4+end ScanRawOpen.

conform/run/RawStrings.expect (history) added +27 -0

@@ -0,0 +1,27 @@
1+14 |C:\tmp\"a b"\n|
2+45 |usage: tool [-v] file
3+ -v say what is done
4+|
5+10 |back`quote|
6+3 |two|
7+0 ||
8+7 |one
9+two|
10+22 |first
11+ second
12+
13+third
14+|
15+6 |x
16+ y
17+|
18+13 | a
19+ b
20+ |
21+7 | kept
22+|
23+0 ||
24+5 |cr
25+lf|
26+equal
27+195

conform/run/RawStrings.raz (history) added +61 -0

@@ -0,0 +1,61 @@
1+(* Raw strings: no escapes, over lines, indented with the code *)
2+module RawStrings;
3+import Out;
4+
5+const
6+ Path = `C:\tmp\"a b"\n`;
7+ Usage = `
8+usage: tool [-v] file
9+ -v say what is done
10+`;
11+ Joined = `back` + "`" + `quote`;
12+ Names: [2][]U8 = [`one`, `two`];
13+
14+proc show(s: []U8);
15+begin
16+ Out.int(LEN(s)); Out.str(" |"); Out.str(s); Out.str("|\n");
17+end;
18+
19+proc main*;
20+var b: [8]U8;
21+begin
22+ show(Path);
23+ show(Usage);
24+ show(Joined);
25+ show(Names[1]);
26+ show(``);
27+ show(`one
28+two`);
29+ (* indented: the closing line's blanks come off every line, and a
30+ line of blanks alone is empty *)
31+ show(`
32+ first
33+ second
34+
35+ third
36+ `);
37+ (* a tab as the indentation *)
38+ show(`
39+ x
40+ y
41+ `);
42+ (* not indented: something follows the opening backquote *)
43+ show(` a
44+ b
45+ `);
46+ (* the closing backquote at the margin takes nothing off *)
47+ show(`
48+ kept
49+`);
50+ (* empty, indented *)
51+ show(`
52+ `);
53+ (* carriage returns are left out *)
54+ show(`cr
55+lf`);
56+ if `abc` = "abc" then Out.str("equal\n"); end;
57+ b := `héllo`;
58+ Out.int(I64(b[1])); Out.ln;
59+end;
60+
61+end RawStrings.

doc/compiler.md (history) modified +7 -2

@@ -14,7 +14,7 @@
1414 | File | Lines | Code | What |
1515 |---|---|---|---|
1616 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
17-| `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions |
17+| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions |
1818 | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
@@ -902,7 +902,12 @@
902902 Read `next` first: it skips white space and comments (`comment`, which
903903 counts nesting), then dispatches on the first character. `number`
904904 handles decimal, `0FFH`-style hexadecimal and reals; `litChar` handles
905-one character of a literal, escapes included. `readReal` turns a real's
905+one character of a literal, escapes included. `rawString` reads a raw
906+string, between backquotes, which has no escapes: first to the closing
907+backquote, counting lines and checking UTF-8 as a comment does, to find
908+the blanks before it; then line by line into `sbuf`, taking those
909+blanks off the start of each line if the string is indented (the
910+report's section 2), and leaving carriage returns out. `readReal` turns a real's
906911 digits into the nearest `F64` exactly, as a fraction of two big integers
907912 (`Big`), since a library's reader can be one bit off.
908913

editor/emacs/raz-mode.el (history) modified +43 -6

@@ -1,13 +1,14 @@
11 ;;; raz-mode.el --- a major mode for raz -*- lexical-binding: t -*-
22
33 ;; Colours raz's reserved words, basic types, built-in procedures,
4-;; numbers, characters, strings and nested comments, and indents by
4+;; numbers, characters, strings, raw strings over lines and nested
5+;; comments, and indents by
56 ;; `raz-indent-offset': a block's statements one level in from the
67 ;; word that opens it; end, until, else and elsif at that word's
78 ;; level; match arms at the level of match; procedures, sections
89 ;; (const, type, var) and module bodies at the left margin. The text
9-;; of a comment, and a line that goes on with the expression of the
10-;; line before, keep the indent they have.
10+;; of a comment or of a raw string, and a line that goes on with the
11+;; expression of the line before, keep the indent they have.
1112 ;;
1213 ;; (add-to-list 'load-path "/path/to/raz/editor/emacs")
1314 ;; (require 'raz-mode)
@@ -43,6 +44,7 @@
4344 (modify-syntax-entry ?\" "\"" st)
4445 (modify-syntax-entry ?\' "\"" st)
4546 (modify-syntax-entry ?\\ "\\" st)
47+ (modify-syntax-entry ?` "." st)
4648 (modify-syntax-entry ?_ "_" st)
4749 (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~))
4850 (modify-syntax-entry c "." st))
@@ -58,7 +60,28 @@
5860 ("\\_<[0-9][0-9A-F]*H\\_>\\|\\_<[0-9]+\\(?:\\.[0-9]+\\(?:E[-+]?[0-9]+\\)?\\)?\\_>"
5961 . font-lock-constant-face))
6062 "What `raz-mode' colours, besides strings and comments.")
63+
64+;;; Raw strings
6165
66+(defun raz--raw-string ()
67+ "Make the backquote just matched open a raw string, if it isn't in a
68+comment or string: it and the closing one string fences, and the
69+backslashes between them nothing special, since a raw string has no
70+escapes."
71+ (let ((open (match-beginning 0)))
72+ (unless (nth 8 (save-excursion (syntax-ppss open)))
73+ (put-text-property open (1+ open) 'syntax-table (string-to-syntax "|"))
74+ (when (search-forward "`" nil t)
75+ (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax "|"))
76+ (save-excursion
77+ (let ((close (1- (point))))
78+ (goto-char open)
79+ (while (search-forward "\\" close t)
80+ (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax ".")))))))))
81+
82+(defconst raz--syntax-propertize
83+ (syntax-propertize-rules ("`" (0 (ignore (raz--raw-string))))))
84+
6285 ;;; Indenting
6386
6487 (defconst raz--decl
@@ -85,6 +108,14 @@
85108 (back-to-indentation)
86109 (and (nth 4 (syntax-ppss))
87110 (not (looking-at "(\\*")))))
111+
112+(defun raz--in-raw (n)
113+ "Whether line N starts inside a raw string, which only a raw string
114+can, since it alone runs over lines."
115+ (save-excursion
116+ (goto-char (point-min))
117+ (forward-line (1- n))
118+ (eq (nth 3 (syntax-ppss)) t)))
88119
89120 (defun raz--prev (n)
90121 "The last line before N that isn't blank, or 0."
@@ -98,11 +129,15 @@
98129 (let ((line (raz--line n)))
99130 (when (raz--in-comment n)
100131 (setq line (if (string-match "\\*)" line) (substring line (match-end 0)) "")))
132+ ;; and inside a raw string, after the string
133+ (when (raz--in-raw n)
134+ (setq line (if (string-match "`" line) (concat "0" (substring line (match-end 0))) "")))
101135 ;; one pattern, so that whichever comes first is taken
102136 (setq line (replace-regexp-in-string
103- "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|(\\*.*?\\*)"
137+ "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|`[^`]*`\\|(\\*.*?\\*)"
104138 (lambda (m) (if (string-prefix-p "(" m) "" "0")) line t t))
105139 (setq line (replace-regexp-in-string "(\\*.*\\'" "" line t t))
140+ (setq line (replace-regexp-in-string "`.*\\'" "0" line t t))
106141 (string-trim line)))
107142
108143 (defun raz--count (re s)
@@ -124,7 +159,8 @@
124159 (let ((p (raz--prev l)))
125160 (if (or (= p 0)
126161 (and (not (raz--continues p))
127- (not (string-match-p raz--leading (raz--line l)))))
162+ (not (string-match-p raz--leading (raz--line l)))
163+ (not (raz--in-raw l))))
128164 (setq done t)
129165 (setq l p))))
130166 l))
@@ -166,7 +202,7 @@
166202 (cur (raz--line n))
167203 (prev (raz--prev n)))
168204 (catch 'done
169- (when (raz--in-comment n) (throw 'done nil))
205+ (when (or (raz--in-comment n) (raz--in-raw n)) (throw 'done nil))
170206 (when (and (string-match-p raz--decl cur) (not (string-match-p "^[ \t]*proc[ \t]*(" cur)))
171207 (throw 'done 0))
172208 ;; a comment before a declaration goes with it, at the margin
@@ -216,6 +252,7 @@
216252 "Major mode for editing raz."
217253 :syntax-table raz-mode-syntax-table
218254 (setq-local font-lock-defaults '(raz-font-lock-keywords nil nil))
255+ (setq-local syntax-propertize-function raz--syntax-propertize)
219256 (setq-local comment-start "(* ")
220257 (setq-local comment-end " *)")
221258 (setq-local comment-start-skip "(\\*+[ \t]*")

editor/vim/indent/raz.vim (history) modified +21 -5

@@ -2,8 +2,8 @@
22 " level in from the word that opens it; end, until, else and elsif at
33 " that word's level; match arms at the level of match; procedures,
44 " sections (const, type, var) and module bodies at the left margin.
5-" The text of a comment, and a line that goes on with the expression
6-" of the line before, keep the indent they have.
5+" The text of a comment or of a raw string, and a line that goes on with
6+" the expression of the line before, keep the indent they have.
77
88 if exists("b:did_indent")
99 finish
@@ -31,12 +31,17 @@
3131 if s:InComment(a:lnum)
3232 let line = line =~ '\*)' ? substitute(line, '^.\{-}\*)', '', '') : ''
3333 endif
34+ " and inside a raw string, after the string
35+ if s:InRaw(a:lnum)
36+ let line = line =~ '`' ? substitute(line, '^[^`]*`', '0', '') : ''
37+ endif
3438 " one pattern, so that whichever comes first is taken: a " in a
3539 " character, a ' in a string; strings become 0, comments go
3640 let line = substitute(line,
37- \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|(\*.\{-}\*)',
41+ \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|`[^`]*`\|(\*.\{-}\*)',
3842 \ '\=submatch(0)[0] == "(" ? "" : "0"', 'g')
3943 let line = substitute(line, '(\*.*$', '', '')
44+ let line = substitute(line, '`.*$', '0', '')
4045 return substitute(line, '^\s*\(.\{-}\)\s*$', '\1', '')
4146 endfunction
4247
@@ -56,7 +61,7 @@
5661 let l = a:lnum
5762 while 1
5863 let p = prevnonblank(l - 1)
59- if p == 0 || !s:Continues(p) && getline(l) !~# s:leading
64+ if p == 0 || !s:Continues(p) && getline(l) !~# s:leading && !s:InRaw(l)
6065 return l
6166 endif
6267 let l = p
@@ -96,9 +101,20 @@
96101 \ && getline(a:lnum) !~ '^\s*(\*'
97102 endfunction
98103
104+" Whether the line starts inside a raw string, which only a raw string
105+" can, since it alone runs over lines; a backquote first on the line
106+" closes one if what follows it isn't in a string.
107+function! s:IsRaw(lnum, col)
108+ return synIDattr(synID(a:lnum, a:col, 1), "name") ==# 'razRawString'
109+endfunction
110+
111+function! s:InRaw(lnum)
112+ return s:IsRaw(a:lnum, 1) && (getline(a:lnum) !~ '^`' || !s:IsRaw(a:lnum, 2))
113+endfunction
114+
99115 function! GetRazIndent(lnum)
100116 let cur = getline(a:lnum)
101- if s:InComment(a:lnum)
117+ if s:InComment(a:lnum) || s:InRaw(a:lnum)
102118 return -1
103119 endif
104120 if cur =~# s:decl && cur !~# '^\s*proc\s*('

editor/vim/syntax/raz.vim (history) modified +4 -1

@@ -1,5 +1,6 @@
11 " Vim syntax file for raz: reserved words, basic types, built-in
2-" procedures, numbers, characters, strings and nested comments.
2+" procedures, numbers, characters, strings, raw strings over lines and
3+" nested comments.
34
45 if exists("b:current_syntax")
56 finish
@@ -25,6 +26,7 @@
2526 syntax match razEscape contained "\\\([ntr0\\'\"]\|x\x\x\|u{\x\+}\)"
2627 syntax region razString start=+"+ skip=+\\.+ end=+"+ contains=razEscape oneline
2728 syntax region razChar start=+'+ skip=+\\.+ end=+'+ contains=razEscape oneline
29+syntax region razRawString start=+`+ end=+`+
2830
2931 syntax region razComment start="(\*" end="\*)" contains=razComment,@Spell
3032 syntax sync fromstart
@@ -35,6 +37,7 @@
3537 highlight default link razConstant Constant
3638 highlight default link razNumber Number
3739 highlight default link razString String
40+highlight default link razRawString String
3841 highlight default link razChar Character
3942 highlight default link razEscape SpecialChar
4043 highlight default link razComment Comment

src/Scan.raz (history) modified +63 -0

@@ -907,6 +907,68 @@
907907 end;
908908 end;
909909
910+(* A raw string, between backquotes (report, section 2): its bytes as
911+ they stand, with no escapes, over as many lines as it runs; carriage
912+ returns are left out. It is indented if the opening backquote ends
913+ its line and the closing one has only blanks before it: the lines
914+ between are its text, each without those blanks at its start. So
915+
916+ Out.str(`
917+ a
918+ b
919+ `);
920+
921+ is "a\n b\n". The text is read twice: to the closing backquote,
922+ for its blanks, then line by line into sbuf. *)
923+proc rawString;
924+var p, close, last, q, i, j: INT; c: U8; indented: BOOL;
925+begin
926+ INC(cur.pos);
927+ p := cur.pos;
928+ while cur.buf[cur.pos] # '`' do
929+ c := cur.buf[cur.pos];
930+ if c = 0 then
931+ if cur.pos >= cur.len then markBytes(cur.tokPos, cur.tokPos + 1, "string not closed"); end;
932+ markBytes(cur.pos, cur.pos + 1, "NUL byte in the text");
933+ elsif c >= 80H then
934+ _ := utf8(cur.pos);
935+ else
936+ if c = '\n' then INC(cur.line); cur.lineStart := cur.pos + 1; end;
937+ INC(cur.pos);
938+ end;
939+ end;
940+ close := cur.pos;
941+ q := close; (* where the blanks before the closing backquote start *)
942+ while (q > p) and ((cur.buf[q - 1] = ' ') or (cur.buf[q - 1] = '\t')) do DEC(q); end;
943+ i := p;
944+ if cur.buf[i] = '\r' then INC(i); end;
945+ indented := (cur.buf[i] = '\n') and (q > i) and (cur.buf[q - 1] = '\n');
946+ if indented then INC(i); last := q; else i := p; last := close; end;
947+ slen := 0;
948+ while i < last do
949+ (* i is where a line starts *)
950+ if indented then
951+ j := i;
952+ while (cur.buf[j] = ' ') or (cur.buf[j] = '\t') do INC(j); end;
953+ if (cur.buf[j] = '\n') or (cur.buf[j] = '\r') then
954+ i := j; (* only blanks: an empty line *)
955+ elsif (j - i < close - q) or (cur.buf[i..i + close - q] # cur.buf[q..close]) then
956+ markBytes(i, j + 1, "this line doesn't start with the blanks before the closing backquote");
957+ else
958+ i := i + close - q;
959+ end;
960+ end;
961+ repeat
962+ c := cur.buf[i];
963+ INC(i);
964+ if c # '\r' then put(INT(c)); end;
965+ until (c = '\n') or (i >= last);
966+ end;
967+ cur.pos := close + 1;
968+ sbuf[slen] := 0;
969+ tok := sString;
970+end;
971+
910972 (* Reads the next symbol *)
911973 proc next*;
912974 var c: U8; start, h, v, i, p: INT; buf: ^U8;
@@ -989,6 +1051,7 @@
9891051 INC(cur.pos);
9901052 sbuf[slen] := 0;
9911053 tok := sString;
1054+ | '`': rawString;
9921055 | '=':
9931056 if cur.buf[cur.pos + 1] = '=' then
9941057 slip(2, sEql, "comparing is written =, not ==");

src/check.sh modified +9 -1

@@ -5,6 +5,13 @@
55 # side is built by fpc from seed/.
66 set -e
77 b=build/src
8+# The seed predates raw strings and match on a string (report
9+# 2026-10.25): the files that use them are the compiler's alone.
10+newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz"
11+newer() {
12+ case " $newer " in *" $1 "*) return 0 ;; esac
13+ return 1
14+}
815 mkdir -p $b
916 fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/scandump.seed src/test/ScanDump.pas >/dev/null
1017 ./raz -Werror -I src -o $b/scandump.src src/test/ScanDump.raz
@@ -17,6 +24,7 @@
1724 for f in $b/lex/*.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz src/test/*.raz test/run/*.raz test/err/*.raz \
1825 conform/*/*.raz $b/reals.raz; do
1926 [ -f "$f" ] || continue
27+ newer $f && continue
2028 s1=0; s2=0
2129 $b/scandump.seed $f >$b/seed.out 2>$b/seed.err || s1=$?
2230 $b/scandump.src $f >$b/src.out 2>$b/src.err || s2=$?
@@ -116,7 +124,7 @@
116124 files=
117125 for f in $c/bench/Bench.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz \
118126 conform/run/*.raz test/err/*.raz conform/err/*.raz; do
119- files="$files $f"
127+ newer $f || files="$files $f"
120128 done
121129 for side in seed src; do
122130 rm -rf $c/$side

test/msg/ScanRawIndent.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/ScanRawIndent.raz:7:1: error: this line doesn't start with the blanks before the closing backquote
2+ 7 | b
3+ | ^^^^^^^
4+raz: 1 error

test/msg/ScanRawOpen.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/ScanRawOpen.raz:2:11: error: string not closed
2+ 2 | const s = `abc;
3+ | ^
4+raz: 1 error

test/pretty/RawString.expect (history) added +11 -0

@@ -0,0 +1,11 @@
1+module RawString;
2+import Out;
3+proc main*;
4+begin
5+ Out.str(`
6+ a
7+ b
8+ `);
9+ Out.str(`x` + "y");
10+end;
11+end RawString.

test/pretty/RawString.raz (history) added +11 -0

@@ -0,0 +1,11 @@
1+module RawString;
2+import Out;
3+proc main*;
4+begin
5+ Out.str(`
6+ a
7+ b
8+ `);
9+ Out.str(`x`+"y");
10+end;
11+end RawString.

tools/Pretty.raz (history) modified +9 -3

@@ -14,7 +14,8 @@
1414 module, import, proc, the sections and begin at the margin, with the
1515 comment just before them. A line that goes on with the expression of
1616 the line before keeps its indent, moved as far as the statement's
17- first line moved; so does the text of a comment.
17+ first line moved; so does the text of a comment. A raw string stays
18+ as written, every byte, since its lines are its value.
1819
1920 Within a line, a binary operator has one space on each side, a unary
2021 minus or ~ none after it, and , and ; none before them and one after. A
@@ -224,13 +225,18 @@
224225 end;
225226
226227 (* The current symbol as written; each line of a comment after its
227- first is moved as far as the line the comment starts on. *)
228+ first is moved as far as the line the comment starts on, and a raw
229+ string's lines are left as they are. *)
228230 proc write;
229231 var i, k: INT;
230232 begin
231233 i := sc.start;
232234 while i < sc.stop do
233- if src[i] = '\n' then
235+ if (src[i] = '\n') and (sc.kind = Scan.literal) then
236+ Buf.add(ADR(out), '\n');
237+ lineAt := out.len;
238+ INC(i);
239+ elsif src[i] = '\n' then
234240 newLine;
235241 k := i + 1;
236242 while (k < sc.stop) and (src[k] = ' ') do INC(k); end;

tools/Scan.raz (history) modified +8 -6

@@ -2,8 +2,8 @@
22 compiler's scanner it keeps comments, as symbols of their own, and
33 says where each symbol is in the text and whether a line break comes
44 before it. It doesn't check what it reads: a malformed number is
5- read as far as it goes, and a string, character or comment left open
6- ends with its line or the text. *)
5+ read as far as it goes, and a string or character left open ends
6+ with its line, a raw string or comment with the text. *)
77 module Scan;
88
99 type
@@ -98,14 +98,16 @@
9898 return p;
9999 end;
100100
101-(* Past the string or character that starts at p, or to its line's end. *)
101+(* Past the string or character that starts at p, or to its line's end;
102+ a raw string, between backquotes, has no escapes and may run over
103+ lines. *)
102104 proc pastLiteral(src: []U8; p: INT): INT;
103105 var q: U8;
104106 begin
105107 q := src[p];
106108 INC(p);
107- while (p < LEN(src)) and (src[p] # q) and (src[p] # '\n') do
108- if (src[p] = '\\') and (p + 1 < LEN(src)) then INC(p); end;
109+ while (p < LEN(src)) and (src[p] # q) and ((src[p] # '\n') or (q = '`')) do
110+ if (src[p] = '\\') and (q # '`') and (p + 1 < LEN(src)) then INC(p); end;
109111 INC(p);
110112 end;
111113 if (p < LEN(src)) and (src[p] = q) then INC(p); end;
@@ -141,7 +143,7 @@
141143 elsif isWord(c) then
142144 s^.kind := word;
143145 while (p < LEN(src)) and isWord(src[p]) do INC(p); end;
144- elsif (c = '"') or (c = '\'') then
146+ elsif (c = '"') or (c = '\'') or (c = '`') then
145147 s^.kind := literal; p := pastLiteral(src, p);
146148 else
147149 s^.kind := symbol;