From fc966ca0c17208b3395bed52445a8cd0581b6d02 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 22:29:06 +0000 Subject: [PATCH] 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. --- diff --git a/conform/err/ScanRawIndent.raz b/conform/err/ScanRawIndent.raz new file mode 100644 index 0000000..de502fd --- /dev/null +++ b/conform/err/ScanRawIndent.raz @@ -0,0 +1,10 @@ +module ScanRawIndent; +import Out; +proc main*; +begin + Out.str(` + a + b + `); +end; +end ScanRawIndent. diff --git a/conform/err/ScanRawOpen.raz b/conform/err/ScanRawOpen.raz new file mode 100644 index 0000000..783be2b --- /dev/null +++ b/conform/err/ScanRawOpen.raz @@ -0,0 +1,4 @@ +module ScanRawOpen; +const s = `abc; + +end ScanRawOpen. diff --git a/conform/run/RawStrings.expect b/conform/run/RawStrings.expect new file mode 100644 index 0000000..940daf0 --- /dev/null +++ b/conform/run/RawStrings.expect @@ -0,0 +1,27 @@ +14 |C:\tmp\"a b"\n| +45 |usage: tool [-v] file + -v say what is done +| +10 |back`quote| +3 |two| +0 || +7 |one +two| +22 |first + second + +third +| +6 |x + y +| +13 | a + b + | +7 | kept +| +0 || +5 |cr +lf| +equal +195 diff --git a/conform/run/RawStrings.raz b/conform/run/RawStrings.raz new file mode 100644 index 0000000..9621203 --- /dev/null +++ b/conform/run/RawStrings.raz @@ -0,0 +1,61 @@ +(* Raw strings: no escapes, over lines, indented with the code *) +module RawStrings; +import Out; + +const + Path = `C:\tmp\"a b"\n`; + Usage = ` +usage: tool [-v] file + -v say what is done +`; + Joined = `back` + "`" + `quote`; + Names: [2][]U8 = [`one`, `two`]; + +proc show(s: []U8); +begin + Out.int(LEN(s)); Out.str(" |"); Out.str(s); Out.str("|\n"); +end; + +proc main*; +var b: [8]U8; +begin + show(Path); + show(Usage); + show(Joined); + show(Names[1]); + show(``); + show(`one +two`); + (* indented: the closing line's blanks come off every line, and a + line of blanks alone is empty *) + show(` + first + second + + third + `); + (* a tab as the indentation *) + show(` + x + y + `); + (* not indented: something follows the opening backquote *) + show(` a + b + `); + (* the closing backquote at the margin takes nothing off *) + show(` + kept +`); + (* empty, indented *) + show(` + `); + (* carriage returns are left out *) + show(`cr +lf`); + if `abc` = "abc" then Out.str("equal\n"); end; + b := `héllo`; + Out.int(I64(b[1])); Out.ln; +end; + +end RawStrings. diff --git a/doc/compiler.md b/doc/compiler.md index 0ad5e88..864b372 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -14,7 +14,7 @@ | File | Lines | Code | What | |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions | +| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions | | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | @@ -902,7 +902,12 @@ Read `next` first: it skips white space and comments (`comment`, which counts nesting), then dispatches on the first character. `number` handles decimal, `0FFH`-style hexadecimal and reals; `litChar` handles -one character of a literal, escapes included. `readReal` turns a real's +one character of a literal, escapes included. `rawString` reads a raw +string, between backquotes, which has no escapes: first to the closing +backquote, counting lines and checking UTF-8 as a comment does, to find +the blanks before it; then line by line into `sbuf`, taking those +blanks off the start of each line if the string is indented (the +report's section 2), and leaving carriage returns out. `readReal` turns a real's digits into the nearest `F64` exactly, as a fraction of two big integers (`Big`), since a library's reader can be one bit off. diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 2df17d8..a9bd8b6 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -1,13 +1,14 @@ ;;; raz-mode.el --- a major mode for raz -*- lexical-binding: t -*- ;; Colours raz's reserved words, basic types, built-in procedures, -;; numbers, characters, strings and nested comments, and indents by +;; numbers, characters, strings, raw strings over lines and nested +;; comments, and indents by ;; `raz-indent-offset': a block's statements one level in from the ;; word that opens it; end, until, else and elsif at that word's ;; level; match arms at the level of match; procedures, sections ;; (const, type, var) and module bodies at the left margin. The text -;; of a comment, and a line that goes on with the expression of the -;; line before, keep the indent they have. +;; of a comment or of a raw string, and a line that goes on with the +;; expression of the line before, keep the indent they have. ;; ;; (add-to-list 'load-path "/path/to/raz/editor/emacs") ;; (require 'raz-mode) @@ -43,6 +44,7 @@ (modify-syntax-entry ?\" "\"" st) (modify-syntax-entry ?\' "\"" st) (modify-syntax-entry ?\\ "\\" st) + (modify-syntax-entry ?` "." st) (modify-syntax-entry ?_ "_" st) (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~)) (modify-syntax-entry c "." st)) @@ -58,7 +60,28 @@ ("\\_<[0-9][0-9A-F]*H\\_>\\|\\_<[0-9]+\\(?:\\.[0-9]+\\(?:E[-+]?[0-9]+\\)?\\)?\\_>" . font-lock-constant-face)) "What `raz-mode' colours, besides strings and comments.") + +;;; Raw strings +(defun raz--raw-string () + "Make the backquote just matched open a raw string, if it isn't in a +comment or string: it and the closing one string fences, and the +backslashes between them nothing special, since a raw string has no +escapes." + (let ((open (match-beginning 0))) + (unless (nth 8 (save-excursion (syntax-ppss open))) + (put-text-property open (1+ open) 'syntax-table (string-to-syntax "|")) + (when (search-forward "`" nil t) + (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax "|")) + (save-excursion + (let ((close (1- (point)))) + (goto-char open) + (while (search-forward "\\" close t) + (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax "."))))))))) + +(defconst raz--syntax-propertize + (syntax-propertize-rules ("`" (0 (ignore (raz--raw-string)))))) + ;;; Indenting (defconst raz--decl @@ -85,6 +108,14 @@ (back-to-indentation) (and (nth 4 (syntax-ppss)) (not (looking-at "(\\*"))))) + +(defun raz--in-raw (n) + "Whether line N starts inside a raw string, which only a raw string +can, since it alone runs over lines." + (save-excursion + (goto-char (point-min)) + (forward-line (1- n)) + (eq (nth 3 (syntax-ppss)) t))) (defun raz--prev (n) "The last line before N that isn't blank, or 0." @@ -98,11 +129,15 @@ (let ((line (raz--line n))) (when (raz--in-comment n) (setq line (if (string-match "\\*)" line) (substring line (match-end 0)) ""))) + ;; and inside a raw string, after the string + (when (raz--in-raw n) + (setq line (if (string-match "`" line) (concat "0" (substring line (match-end 0))) ""))) ;; one pattern, so that whichever comes first is taken (setq line (replace-regexp-in-string - "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|(\\*.*?\\*)" + "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|`[^`]*`\\|(\\*.*?\\*)" (lambda (m) (if (string-prefix-p "(" m) "" "0")) line t t)) (setq line (replace-regexp-in-string "(\\*.*\\'" "" line t t)) + (setq line (replace-regexp-in-string "`.*\\'" "0" line t t)) (string-trim line))) (defun raz--count (re s) @@ -124,7 +159,8 @@ (let ((p (raz--prev l))) (if (or (= p 0) (and (not (raz--continues p)) - (not (string-match-p raz--leading (raz--line l))))) + (not (string-match-p raz--leading (raz--line l))) + (not (raz--in-raw l)))) (setq done t) (setq l p)))) l)) @@ -166,7 +202,7 @@ (cur (raz--line n)) (prev (raz--prev n))) (catch 'done - (when (raz--in-comment n) (throw 'done nil)) + (when (or (raz--in-comment n) (raz--in-raw n)) (throw 'done nil)) (when (and (string-match-p raz--decl cur) (not (string-match-p "^[ \t]*proc[ \t]*(" cur))) (throw 'done 0)) ;; a comment before a declaration goes with it, at the margin @@ -216,6 +252,7 @@ "Major mode for editing raz." :syntax-table raz-mode-syntax-table (setq-local font-lock-defaults '(raz-font-lock-keywords nil nil)) + (setq-local syntax-propertize-function raz--syntax-propertize) (setq-local comment-start "(* ") (setq-local comment-end " *)") (setq-local comment-start-skip "(\\*+[ \t]*") diff --git a/editor/vim/indent/raz.vim b/editor/vim/indent/raz.vim index ccb433a..7e6b68c 100644 --- a/editor/vim/indent/raz.vim +++ b/editor/vim/indent/raz.vim @@ -2,8 +2,8 @@ " level in from the word that opens it; end, until, else and elsif at " that word's level; match arms at the level of match; procedures, " sections (const, type, var) and module bodies at the left margin. -" The text of a comment, and a line that goes on with the expression -" of the line before, keep the indent they have. +" The text of a comment or of a raw string, and a line that goes on with +" the expression of the line before, keep the indent they have. if exists("b:did_indent") finish @@ -31,12 +31,17 @@ if s:InComment(a:lnum) let line = line =~ '\*)' ? substitute(line, '^.\{-}\*)', '', '') : '' endif + " and inside a raw string, after the string + if s:InRaw(a:lnum) + let line = line =~ '`' ? substitute(line, '^[^`]*`', '0', '') : '' + endif " one pattern, so that whichever comes first is taken: a " in a " character, a ' in a string; strings become 0, comments go let line = substitute(line, - \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|(\*.\{-}\*)', + \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|`[^`]*`\|(\*.\{-}\*)', \ '\=submatch(0)[0] == "(" ? "" : "0"', 'g') let line = substitute(line, '(\*.*$', '', '') + let line = substitute(line, '`.*$', '0', '') return substitute(line, '^\s*\(.\{-}\)\s*$', '\1', '') endfunction @@ -56,7 +61,7 @@ let l = a:lnum while 1 let p = prevnonblank(l - 1) - if p == 0 || !s:Continues(p) && getline(l) !~# s:leading + if p == 0 || !s:Continues(p) && getline(l) !~# s:leading && !s:InRaw(l) return l endif let l = p @@ -96,9 +101,20 @@ \ && getline(a:lnum) !~ '^\s*(\*' endfunction +" Whether the line starts inside a raw string, which only a raw string +" can, since it alone runs over lines; a backquote first on the line +" closes one if what follows it isn't in a string. +function! s:IsRaw(lnum, col) + return synIDattr(synID(a:lnum, a:col, 1), "name") ==# 'razRawString' +endfunction + +function! s:InRaw(lnum) + return s:IsRaw(a:lnum, 1) && (getline(a:lnum) !~ '^`' || !s:IsRaw(a:lnum, 2)) +endfunction + function! GetRazIndent(lnum) let cur = getline(a:lnum) - if s:InComment(a:lnum) + if s:InComment(a:lnum) || s:InRaw(a:lnum) return -1 endif if cur =~# s:decl && cur !~# '^\s*proc\s*(' diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index cc39dda..fa23d20 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -1,5 +1,6 @@ " Vim syntax file for raz: reserved words, basic types, built-in -" procedures, numbers, characters, strings and nested comments. +" procedures, numbers, characters, strings, raw strings over lines and +" nested comments. if exists("b:current_syntax") finish @@ -25,6 +26,7 @@ syntax match razEscape contained "\\\([ntr0\\'\"]\|x\x\x\|u{\x\+}\)" syntax region razString start=+"+ skip=+\\.+ end=+"+ contains=razEscape oneline syntax region razChar start=+'+ skip=+\\.+ end=+'+ contains=razEscape oneline +syntax region razRawString start=+`+ end=+`+ syntax region razComment start="(\*" end="\*)" contains=razComment,@Spell syntax sync fromstart @@ -35,6 +37,7 @@ highlight default link razConstant Constant highlight default link razNumber Number highlight default link razString String +highlight default link razRawString String highlight default link razChar Character highlight default link razEscape SpecialChar highlight default link razComment Comment diff --git a/src/Scan.raz b/src/Scan.raz index 3172017..f53dcd6 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -907,6 +907,68 @@ end; end; +(* A raw string, between backquotes (report, section 2): its bytes as + they stand, with no escapes, over as many lines as it runs; carriage + returns are left out. It is indented if the opening backquote ends + its line and the closing one has only blanks before it: the lines + between are its text, each without those blanks at its start. So + + Out.str(` + a + b + `); + + is "a\n b\n". The text is read twice: to the closing backquote, + for its blanks, then line by line into sbuf. *) +proc rawString; +var p, close, last, q, i, j: INT; c: U8; indented: BOOL; +begin + INC(cur.pos); + p := cur.pos; + while cur.buf[cur.pos] # '`' do + c := cur.buf[cur.pos]; + if c = 0 then + if cur.pos >= cur.len then markBytes(cur.tokPos, cur.tokPos + 1, "string not closed"); end; + markBytes(cur.pos, cur.pos + 1, "NUL byte in the text"); + elsif c >= 80H then + _ := utf8(cur.pos); + else + if c = '\n' then INC(cur.line); cur.lineStart := cur.pos + 1; end; + INC(cur.pos); + end; + end; + close := cur.pos; + q := close; (* where the blanks before the closing backquote start *) + while (q > p) and ((cur.buf[q - 1] = ' ') or (cur.buf[q - 1] = '\t')) do DEC(q); end; + i := p; + if cur.buf[i] = '\r' then INC(i); end; + indented := (cur.buf[i] = '\n') and (q > i) and (cur.buf[q - 1] = '\n'); + if indented then INC(i); last := q; else i := p; last := close; end; + slen := 0; + while i < last do + (* i is where a line starts *) + if indented then + j := i; + while (cur.buf[j] = ' ') or (cur.buf[j] = '\t') do INC(j); end; + if (cur.buf[j] = '\n') or (cur.buf[j] = '\r') then + i := j; (* only blanks: an empty line *) + elsif (j - i < close - q) or (cur.buf[i..i + close - q] # cur.buf[q..close]) then + markBytes(i, j + 1, "this line doesn't start with the blanks before the closing backquote"); + else + i := i + close - q; + end; + end; + repeat + c := cur.buf[i]; + INC(i); + if c # '\r' then put(INT(c)); end; + until (c = '\n') or (i >= last); + end; + cur.pos := close + 1; + sbuf[slen] := 0; + tok := sString; +end; + (* Reads the next symbol *) proc next*; var c: U8; start, h, v, i, p: INT; buf: ^U8; @@ -989,6 +1051,7 @@ INC(cur.pos); sbuf[slen] := 0; tok := sString; + | '`': rawString; | '=': if cur.buf[cur.pos + 1] = '=' then slip(2, sEql, "comparing is written =, not =="); diff --git a/src/check.sh b/src/check.sh index f299f7a..603dd37 100755 --- a/src/check.sh +++ b/src/check.sh @@ -5,6 +5,13 @@ # side is built by fpc from seed/. set -e b=build/src +# The seed predates raw strings and match on a string (report +# 2026-10.25): the files that use them are the compiler's alone. +newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz" +newer() { + case " $newer " in *" $1 "*) return 0 ;; esac + return 1 +} mkdir -p $b fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/scandump.seed src/test/ScanDump.pas >/dev/null ./raz -Werror -I src -o $b/scandump.src src/test/ScanDump.raz @@ -17,6 +24,7 @@ for f in $b/lex/*.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz src/test/*.raz test/run/*.raz test/err/*.raz \ conform/*/*.raz $b/reals.raz; do [ -f "$f" ] || continue + newer $f && continue s1=0; s2=0 $b/scandump.seed $f >$b/seed.out 2>$b/seed.err || s1=$? $b/scandump.src $f >$b/src.out 2>$b/src.err || s2=$? @@ -116,7 +124,7 @@ files= for f in $c/bench/Bench.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz \ conform/run/*.raz test/err/*.raz conform/err/*.raz; do - files="$files $f" + newer $f || files="$files $f" done for side in seed src; do rm -rf $c/$side diff --git a/test/msg/ScanRawIndent.err b/test/msg/ScanRawIndent.err new file mode 100644 index 0000000..a0d2cd2 --- /dev/null +++ b/test/msg/ScanRawIndent.err @@ -0,0 +1,4 @@ +conform/err/ScanRawIndent.raz:7:1: error: this line doesn't start with the blanks before the closing backquote + 7 | b + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ScanRawOpen.err b/test/msg/ScanRawOpen.err new file mode 100644 index 0000000..c627abb --- /dev/null +++ b/test/msg/ScanRawOpen.err @@ -0,0 +1,4 @@ +conform/err/ScanRawOpen.raz:2:11: error: string not closed + 2 | const s = `abc; + | ^ +raz: 1 error diff --git a/test/pretty/RawString.expect b/test/pretty/RawString.expect new file mode 100644 index 0000000..6a1641f --- /dev/null +++ b/test/pretty/RawString.expect @@ -0,0 +1,11 @@ +module RawString; +import Out; +proc main*; +begin + Out.str(` + a + b + `); + Out.str(`x` + "y"); +end; +end RawString. diff --git a/test/pretty/RawString.raz b/test/pretty/RawString.raz new file mode 100644 index 0000000..f4313b5 --- /dev/null +++ b/test/pretty/RawString.raz @@ -0,0 +1,11 @@ +module RawString; +import Out; +proc main*; +begin + Out.str(` + a + b + `); + Out.str(`x`+"y"); +end; +end RawString. diff --git a/tools/Pretty.raz b/tools/Pretty.raz index 14112eb..4266789 100644 --- a/tools/Pretty.raz +++ b/tools/Pretty.raz @@ -14,7 +14,8 @@ module, import, proc, the sections and begin at the margin, with the comment just before them. A line that goes on with the expression of the line before keeps its indent, moved as far as the statement's - first line moved; so does the text of a comment. + first line moved; so does the text of a comment. A raw string stays + as written, every byte, since its lines are its value. Within a line, a binary operator has one space on each side, a unary minus or ~ none after it, and , and ; none before them and one after. A @@ -224,13 +225,18 @@ end; (* The current symbol as written; each line of a comment after its - first is moved as far as the line the comment starts on. *) + first is moved as far as the line the comment starts on, and a raw + string's lines are left as they are. *) proc write; var i, k: INT; begin i := sc.start; while i < sc.stop do - if src[i] = '\n' then + if (src[i] = '\n') and (sc.kind = Scan.literal) then + Buf.add(ADR(out), '\n'); + lineAt := out.len; + INC(i); + elsif src[i] = '\n' then newLine; k := i + 1; while (k < sc.stop) and (src[k] = ' ') do INC(k); end; diff --git a/tools/Scan.raz b/tools/Scan.raz index cad5946..c05aaf3 100644 --- a/tools/Scan.raz +++ b/tools/Scan.raz @@ -2,8 +2,8 @@ compiler's scanner it keeps comments, as symbols of their own, and says where each symbol is in the text and whether a line break comes before it. It doesn't check what it reads: a malformed number is - read as far as it goes, and a string, character or comment left open - ends with its line or the text. *) + read as far as it goes, and a string or character left open ends + with its line, a raw string or comment with the text. *) module Scan; type @@ -98,14 +98,16 @@ return p; end; -(* Past the string or character that starts at p, or to its line's end. *) +(* Past the string or character that starts at p, or to its line's end; + a raw string, between backquotes, has no escapes and may run over + lines. *) proc pastLiteral(src: []U8; p: INT): INT; var q: U8; begin q := src[p]; INC(p); - while (p < LEN(src)) and (src[p] # q) and (src[p] # '\n') do - if (src[p] = '\\') and (p + 1 < LEN(src)) then INC(p); end; + while (p < LEN(src)) and (src[p] # q) and ((src[p] # '\n') or (q = '`')) do + if (src[p] = '\\') and (q # '`') and (p + 1 < LEN(src)) then INC(p); end; INC(p); end; if (p < LEN(src)) and (src[p] = q) then INC(p); end; @@ -141,7 +143,7 @@ elsif isWord(c) then s^.kind := word; while (p < LEN(src)) and isWord(src[p]) do INC(p); end; - elsif (c = '"') or (c = '\'') then + elsif (c = '"') or (c = '\'') or (c = '`') then s^.kind := literal; p := pastLiteral(src, p); else s^.kind := symbol;