Scan strings

This commit is contained in:
Scott Richmond 2022-01-22 18:07:04 -05:00
parent 1c60dce605
commit eb5f5233b6
2 changed files with 45 additions and 24 deletions

View File

@ -26,6 +26,7 @@
"ns" "ns"
"recur" "recur"
"repeat" "repeat"
;; below here, possible
"when" "when"
}) })
@ -91,10 +92,16 @@
([scanner token-type] ([scanner token-type]
(add-token scanner token-type nil)) (add-token scanner token-type nil))
([scanner token-type literal] ([scanner token-type literal]
(update scanner ::tokens conj (token/token token-type (current-lexeme scanner) literal (::line scanner))))) (update scanner ::tokens conj
(token/token
token-type
(current-lexeme scanner)
literal
(::line scanner)
(::start scanner)))))
(defn- add-error [scanner msg] (defn- add-error [scanner msg]
(update scanner ::errors conj {:msg msg :line (::line scanner)})) (update scanner ::errors conj {:msg msg :line (::line scanner) :start (::start scanner)}))
(defn- scan-keyword (defn- scan-keyword
([scanner] (scan-keyword scanner scanner)) ([scanner] (scan-keyword scanner scanner))
@ -114,11 +121,18 @@
(if (nonzero-digit? current) (if (nonzero-digit? current)
(loop [current current])))) (loop [current current]))))
;; I am working here--trying to figure out how to add a string token
(defn- add-string (defn- add-string
([scanner] (add-string scanner "") [scanner]
([scanner string] (loop [scanner scanner
(let [char (current-char scanner)])))) string ""]
(let [char (current-char scanner)]
(case char
\newline (add-error scanner "Unterminated string.")
\" (add-token (advance scanner) ::token/string string)
\\ (recur (advance (advance scanner)) (str string (next-char scanner)))
(if (at-end? scanner)
(add-error scanner "Unterminated string.")
(recur (advance scanner) (str string char)))))))
(defn- add-word [scanner]) (defn- add-word [scanner])
@ -211,6 +225,12 @@
(recur (-> scanner (scan-token) (next-token)))))) (recur (-> scanner (scan-token) (next-token))))))
(let [source "|)"] (let [source "\"foo\\\nbar\"\n)"]
(scan source)) (scan source))
;; string scanning is (I think) working
;; line counting is not working
;; do I just save a location and then calculate line numbers if an error happens?
;; next up: numbers!

View File

@ -3,11 +3,12 @@
(defn token (defn token
([type text] ([type text]
(token type text nil 1)) (token type text nil 1))
([type text literal line] ([type text literal line start]
{::type type {::type type
::lexeme text ::lexeme text
::literal literal ::literal literal
::line line})) ::line line
::start start}))
(defn from-keyword [kw] (defn from-keyword [kw]
(keyword "ludus.token" kw)) (keyword "ludus.token" kw))