Improve parse-set; do not swallow errors on unterminated collections

This commit is contained in:
Scott Richmond 2022-02-21 14:03:58 -05:00
parent d23a561eda
commit 4edfbcb04d

View File

@ -156,7 +156,7 @@
(panic parser (str "Mismatched enclosure in tuple: " (::token/lexeme curr))) (panic parser (str "Mismatched enclosure in tuple: " (::token/lexeme curr)))
::token/eof ::token/eof
(panic origin "Unterminated tuple" ::token/eof) (panic (assoc origin ::errors (::errors parser)) "Unterminated tuple" ::token/eof)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(recur parsed members (::ast parsed))))))) (recur parsed members (::ast parsed)))))))
@ -183,27 +183,37 @@
(panic parser (str "Mismatched enclosure in list: " (::token/lexeme curr))) (panic parser (str "Mismatched enclosure in list: " (::token/lexeme curr)))
::token/eof ::token/eof
(panic origin "Unterminated list" ::token/eof) (panic (assoc origin ::errors (::errors parser)) "Unterminated list" ::token/eof)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(recur parsed members (::ast parsed))))))) (recur parsed members (::ast parsed)))))))
(defn- parse-set [parser] (defn- parse-set [origin]
(loop [parser (advance parser) (loop [
parser (accept-many #{::token/newline ::token/comma} (advance origin))
members [] members []
current_member nil] current_member nil
]
(let [curr (current parser)] (let [curr (current parser)]
(case (::token/type curr) (case (token-type parser)
::token/rbrace (let [ms (add-member members current_member)] ::token/rbrace (let [ms (add-member members current_member)]
(assoc (advance parser) ::ast (assoc (advance parser) ::ast
{::ast/type ::ast/set {::ast/type ::ast/set
:members ms})) :members ms}))
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(::token/comma ::token/newline)
(recur
(accept-many #{::token/comma ::token/newline} parser)
(add-member members current_member) nil)
(::token/rbracket ::token/rparen)
(panic parser (str "Mismatched enclosure in set: " (::token/lexeme curr)))
::token/eof
(panic (assoc origin ::errors (::errors parser)) "Unterminated set" ::token/eof)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(recur parsed members (::ast parsed))) (recur parsed members (::ast parsed)))))))
))))
(defn- parse-block [parser] (defn- parse-block [parser]
(loop [parser (advance parser) (loop [parser (advance parser)
@ -386,14 +396,14 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "[32, 23, 42]") (def source "${32, (23, 42}")
(def lexed (scanner/scan source)) (def lexed (scanner/scan source))
(def tokens (:tokens lexed)) (def tokens (:tokens lexed))
(def p (parser tokens)) (def p (parser tokens))
(-> p (-> p
(parse-script) (parse-script)
(::ast) (::errors)
(pp) (pp)
) )
) )