fix escaping brace bug, which was fixing next-char bug; also clean some stuff up

This commit is contained in:
Scott Richmond 2024-05-09 16:35:22 -04:00
parent c5d04ddd66
commit 3f16e45204

View File

@ -71,7 +71,7 @@
length (length source)] length (length source)]
(if (>= next length) (if (>= next length)
nil nil
(string/from-bytes (get source current))))) (string/from-bytes (get source next)))))
(defn- current-lexeme (defn- current-lexeme
[scanner] [scanner]
@ -126,7 +126,6 @@
"&" true "&" true
"," true "," true
">" true ">" true
# nil was in here; I don't want to be passing in literal nil to this function anyway, and "nil" is also not it...
"\"" true}) "\"" true})
(defn- terminates? [c] (defn- terminates? [c]
@ -171,9 +170,7 @@
(defn- read-literal [lit] (-> lit parse-all first)) (defn- read-literal [lit] (-> lit parse-all first))
## TODO: improve number parsing? ### TODO: consider whether Janet's number rules are right for Ludus
## Currently this uses Clojure's number formatting rules (since we use the EDN reader)
## These rules are here: https://cljs.github.io/api/syntax/number
(defn- add-number [char scanner] (defn- add-number [char scanner]
(defn recur [scanner num float?] (defn recur [scanner num float?]
(let [curr (current-char scanner)] (let [curr (current-char scanner)]
@ -192,19 +189,21 @@
(print "Adding string") (print "Adding string")
(defn recur [scanner buff interpolate?] (defn recur [scanner buff interpolate?]
(let [char (current-char scanner)] (let [char (current-char scanner)]
(print "scanning current char: " char)
(case char (case char
"{" (recur (advance scanner) (buffer/push buff char) true) "{" (recur (advance scanner) (buffer/push buff char) true)
# allow multiline strings # allow multiline strings
"\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))
### FIXME: Actually add the escaped character to the string; "\\" (let [next (next-char scanner)]
### The only weird escapy-thing is actually the lbrace (print "found slash")
### So only do anything fancy if the next char is "{" (print "next char: " next)
"\\" (let [next (next-char scanner) (if (= next "{")
scanner (if (= next "\n") (do
(update scanner :line inc) (print "escaped brace")
scanner)] (buffer/push buff next)
(recur (advance (advance scanner)) (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?)))))