From 4edfbcb04d6dff5959614de81388d9696aea8f0e Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Mon, 21 Feb 2022 14:03:58 -0500 Subject: [PATCH] Improve parse-set; do not swallow errors on unterminated collections --- src/ludus/parser.clj | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/ludus/parser.clj b/src/ludus/parser.clj index ab287cb..27c3066 100644 --- a/src/ludus/parser.clj +++ b/src/ludus/parser.clj @@ -156,7 +156,7 @@ (panic parser (str "Mismatched enclosure in tuple: " (::token/lexeme curr))) ::token/eof - (panic origin "Unterminated tuple" ::token/eof) + (panic (assoc origin ::errors (::errors parser)) "Unterminated tuple" ::token/eof) (let [parsed (parse-expr parser)] (recur parsed members (::ast parsed))))))) @@ -183,27 +183,37 @@ (panic parser (str "Mismatched enclosure in list: " (::token/lexeme curr))) ::token/eof - (panic origin "Unterminated list" ::token/eof) + (panic (assoc origin ::errors (::errors parser)) "Unterminated list" ::token/eof) (let [parsed (parse-expr parser)] (recur parsed members (::ast parsed))))))) -(defn- parse-set [parser] - (loop [parser (advance parser) +(defn- parse-set [origin] + (loop [ + parser (accept-many #{::token/newline ::token/comma} (advance origin)) members [] - current_member nil] + current_member nil + ] (let [curr (current parser)] - (case (::token/type curr) - ::token/rbrace (let [ms (add-member members current_member)] + (case (token-type parser) + ::token/rbrace (let [ms (add-member members current_member)] (assoc (advance parser) ::ast {::ast/type ::ast/set :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)] - (recur parsed members (::ast parsed))) - - )))) + (recur parsed members (::ast parsed))))))) (defn- parse-block [parser] (loop [parser (advance parser) @@ -386,14 +396,14 @@ (do (def pp pp/pprint) - (def source "[32, 23, 42]") + (def source "${32, (23, 42}") (def lexed (scanner/scan source)) (def tokens (:tokens lexed)) (def p (parser tokens)) (-> p (parse-script) - (::ast) + (::errors) (pp) ) )