fix string escapes

This commit is contained in:
Scott Richmond 2024-06-14 15:25:05 -04:00
parent c61981fa16
commit 9584a21521
2 changed files with 18 additions and 11 deletions

View File

@ -49,10 +49,10 @@
(set (out :draw) (post :draw)) (set (out :draw) (post :draw))
(-> out j/encode string)) (-> out j/encode string))
(comment # (comment
# (do (do
(def source ` (def source `
words ("foo bar") "foo \bbar"
`) `)
(def out (-> source (def out (-> source
ludus ludus

View File

@ -194,6 +194,15 @@
:else (add-error scanner (string "Unexpected " curr " after number " num "."))))) :else (add-error scanner (string "Unexpected " curr " after number " num ".")))))
(recur scanner (buffer char) false)) (recur scanner (buffer char) false))
(def escape {
"\"" "\""
"n" "\n"
"{" "{"
"t" "\t"
"r" "\r"
"\\" "\\"
})
(defn- add-string (defn- add-string
[scanner] [scanner]
(defn recur [scanner buff interpolate?] (defn recur [scanner buff interpolate?]
@ -204,12 +213,10 @@
"\n" (recur (update (advance scanner) :line inc) (buffer/push buff char) interpolate?) "\n" (recur (update (advance scanner) :line inc) (buffer/push buff char) interpolate?)
"\"" (add-token (advance scanner) (if interpolate? :interpolated :string) (string buff)) "\"" (add-token (advance scanner) (if interpolate? :interpolated :string) (string buff))
"\\" (let [next (next-char scanner)] "\\" (let [next (next-char scanner)]
(if (= next "{") (recur
(do (advance (advance scanner))
(buffer/push buff char) (buffer/push buff (get escape next next))
(buffer/push buff next) interpolate?))
(recur (advance (advance scanner)) buff interpolate?))
(recur (advance scanner) (buffer/push buff char) interpolate?)))
(if (at-end? scanner) (if (at-end? scanner)
(add-error scanner "Unterminated string.") (add-error scanner "Unterminated string.")
(recur (advance scanner) (buffer/push buff char) interpolate?))))) (recur (advance scanner) (buffer/push buff char) interpolate?)))))
@ -341,8 +348,8 @@
(recur (-> scanner (scan-token) (next-token))))) (recur (-> scanner (scan-token) (next-token)))))
(recur (new-scanner source input))) (recur (new-scanner source input)))
# (comment (comment
(do # (do
(def source "/iii") (def source "/iii")
(scan source) (scan source)
) )