Improve parse-list (based on parse-tuple)

This commit is contained in:
Scott Richmond 2022-02-21 13:56:46 -05:00
parent 61b25ca22d
commit d23a561eda

View File

@ -161,22 +161,32 @@
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(recur parsed members (::ast parsed))))))) (recur parsed members (::ast parsed)))))))
(defn- parse-list [parser] (defn- parse-list [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/rbracket (let [ms (add-member members current_member)] ::token/rbracket (let [ms (add-member members current_member)]
(assoc (advance parser) ::ast (assoc (advance parser) ::ast
{::ast/type ::ast/list {::ast/type ::ast/list
: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/rbrace ::token/rparen)
(panic parser (str "Mismatched enclosure in list: " (::token/lexeme curr)))
::token/eof
(panic origin "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 [parser]
(loop [parser (advance parser) (loop [parser (advance parser)
@ -376,15 +386,14 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "let foo = (32, 23 (def source "[32, 23, 42]")
let baz = foo () :bar 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)
(::errors) (::ast)
(pp) (pp)
) )
) )