Parse splats in hashmaps *correctly*

This commit is contained in:
Scott Richmond 2022-05-20 18:46:24 -04:00
parent ea5d9fa948
commit f70aa0f4ce

View File

@ -272,7 +272,7 @@
(defn- parse-hash [origin]
(loop [parser (accept-many #{::token/newline ::token/comma} (advance origin))
members {}
members []
current_member nil]
(let [curr (current parser)]
(case (token-type parser)
@ -294,13 +294,17 @@
(panic (assoc origin ::errors (::errors parser)) "Unterminated hashmap" ::token/eof)
::token/word
(if (not current_member) (let [parsed (parse-word parser) word (get-in parsed [::ast :word])]
(recur parsed members {(keyword word) (::ast parsed)}))
(if (not current_member)
(let [parsed (parse-word parser)
word (get-in parsed [::ast :word])]
(recur parsed members [(keyword word) (::ast parsed)]))
(panic parser "Hashmap entries must be single words or keyword+expression pairs." #{::token/rbrace}))
::token/keyword
(if (not current_member) (let [kw (parse-atom parser) expr (parse-expr kw #{::token/comma ::token/newline ::token/rbrace})]
(recur expr members {(:value (::ast kw)) (::ast expr)}))
(if (not current_member)
(let [kw (parse-atom parser)
expr (parse-expr kw #{::token/comma ::token/newline ::token/rbrace})]
(recur expr members [(:value (::ast kw)) (::ast expr)]))
(panic parser "Hashmap entries must be single words or keyword+expression pairs." #{::token/rbrace}))
::token/splat
@ -1042,10 +1046,9 @@
(parser)
(parse-script)))
(comment
(do
(def pp pp/pprint)
(def source "${a, b, ...c, d, ...e}
#{:a b,...d, :e f, ...g}
(def source "let foo = #{:a 1,...b}
")
(def lexed (scanner/scan source))