lua: decimal string escapes \\ddd + control escapes (\\a/\\b/\\f/\\v) +2 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 21:20:38 +00:00
parent fc2baee9c7
commit 9435fab790
5 changed files with 100 additions and 40 deletions

View File

@@ -1,3 +1,19 @@
(define __ascii-tok " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
(define lua-byte-to-char
(fn (n)
(cond
((= n 0) "\0")
((= n 7) "\a")
((= n 8) "\b")
((= n 9) "\t")
((= n 10) "\n")
((= n 11) "\v")
((= n 12) "\f")
((= n 13) "\r")
((and (>= n 32) (<= n 126)) (char-at __ascii-tok (- n 32)))
(else "?"))))
(define lua-make-token (fn (type value pos) {:pos pos :value value :type type}))
(define lua-digit? (fn (c) (and (not (= c nil)) (>= c "0") (<= c "9"))))
@@ -228,6 +244,35 @@
(begin (advance! 1) (read-decimal-digits!)))
(read-exp-part!)
(parse-number (slice src start pos)))))))
(define
lua-char-one-tok
(fn (n)
(cond
((= n 7) (str (list n)))
((= n 8) (str (list n)))
((= n 11) (str (list n)))
((= n 12) (str (list n)))
(else (str (list n))))))
(define
read-decimal-escape!
(fn (chars)
(let ((d0 (cur)))
(begin
(advance! 1)
(let ((n (- (char-code d0) (char-code "0"))))
(begin
(when
(and (< pos src-len) (lua-digit? (cur)))
(begin
(set! n (+ (* n 10) (- (char-code (cur)) (char-code "0"))))
(advance! 1)
(when
(and (< pos src-len) (lua-digit? (cur))
(<= (+ (* n 10) (- (char-code (cur)) (char-code "0"))) 255))
(begin
(set! n (+ (* n 10) (- (char-code (cur)) (char-code "0"))))
(advance! 1)))))
(append! chars (lua-byte-to-char n))))))))
(define
read-string
(fn
@@ -251,14 +296,18 @@
((ch (cur)))
(begin
(cond
((= ch "n") (append! chars "\n"))
((= ch "t") (append! chars "\t"))
((= ch "r") (append! chars "\r"))
((= ch "\\") (append! chars "\\"))
((= ch "'") (append! chars "'"))
((= ch "\"") (append! chars "\""))
(else (append! chars ch)))
(advance! 1))))
((= ch "n") (begin (append! chars "\n") (advance! 1)))
((= ch "t") (begin (append! chars "\t") (advance! 1)))
((= ch "r") (begin (append! chars "\r") (advance! 1)))
((= ch "a") (begin (append! chars (lua-char-one-tok 7)) (advance! 1)))
((= ch "b") (begin (append! chars (lua-char-one-tok 8)) (advance! 1)))
((= ch "f") (begin (append! chars (lua-char-one-tok 12)) (advance! 1)))
((= ch "v") (begin (append! chars (lua-char-one-tok 11)) (advance! 1)))
((= ch "\\") (begin (append! chars "\\") (advance! 1)))
((= ch "'") (begin (append! chars "'") (advance! 1)))
((= ch "\"") (begin (append! chars "\"") (advance! 1)))
((lua-digit? ch) (read-decimal-escape! chars))
(else (begin (append! chars ch) (advance! 1)))))))
(loop)))
((= (cur) quote-char) (advance! 1))
(else