Better error handling, in script & tuple

This commit is contained in:
Scott Richmond 2022-02-21 13:43:30 -05:00
parent 9414109312
commit e6b204a54a

View File

@ -60,13 +60,16 @@
([parser message] (panic parser message sync-on)) ([parser message] (panic parser message sync-on))
([parser message sync-on] ([parser message sync-on]
(println "PANIC!!! in the parser") (println "PANIC!!! in the parser")
(let [origin (current parser)] (let [
sync-on (if (set? sync-on) sync-on #{sync-on})
origin (current parser)
]
(loop [parser (advance parser)] (loop [parser (advance parser)]
(let [ (let [
curr (current parser) curr (current parser)
type (::token/type curr) type (::token/type curr)
] ]
(if (or (= ::token/eof type) (contains? sync-on type)) (if (or (at-end? parser) (contains? sync-on type))
(sync parser message origin curr) (sync parser message origin curr)
(recur (advance parser)))))))) (recur (advance parser))))))))
@ -130,9 +133,9 @@
members members
(conj members member))) (conj members member)))
(defn- parse-tuple [parser] (defn- parse-tuple [origin]
(loop [ (loop [
parser (accept-many #{::token/newline ::token/comma} (advance parser)) parser (accept-many #{::token/newline ::token/comma} (advance origin))
members [] members []
current_member nil current_member nil
] ]
@ -154,10 +157,12 @@
(::token/rbrace ::token/rbracket) (::token/rbrace ::token/rbracket)
(panic parser (str "Mismatched enclosure in tuple: " (::token/lexeme curr))) (panic parser (str "Mismatched enclosure in tuple: " (::token/lexeme curr)))
::token/eof
(panic origin "Unterminated tuple" ::token/eof)
(let [parsed (parse-expr parser)] (let [parsed (parse-expr parser)]
(recur parsed members (::ast parsed)) (recur parsed members (::ast parsed))
) )
) )
) )
) )
@ -223,12 +228,9 @@
(loop [parser parser (loop [parser parser
exprs [] exprs []
current_expr nil] current_expr nil]
(comment (println "*** Parsing script") (if (at-end? parser)
(print "Exprs: ") (assoc parser ::ast
(pp/pprint exprs) {::ast/type ::ast/script :exprs (add-member exprs current_expr)})
(print "Current expr: ")
(pp/pprint current_expr)
(println "Current token type " (::token/type (current parser))))
(case (::token/type (current parser)) (case (::token/type (current parser))
::token/eof (assoc parser ::ast ::token/eof (assoc parser ::ast
{::ast/type ::ast/script :exprs (add-member exprs current_expr)}) {::ast/type ::ast/script :exprs (add-member exprs current_expr)})
@ -246,7 +248,7 @@
(recur parsed exprs (::ast parsed)) (recur parsed exprs (::ast parsed))
)) ))
))) ))))
(defn- parse-synthetic [parser] (defn- parse-synthetic [parser]
(loop [parser parser (loop [parser parser
@ -367,21 +369,13 @@
::token/if (parse-if parser) ::token/if (parse-if parser)
::token/error (panic parser (:message token) ::token/error (panic parser (:message token))
#{
::token/newline
::token/semicolon
::token/comma
::token/rparen
::token/rbracket
::token/rbrace
})
(::token/rparen ::token/rbrace ::token/rbracket) (::token/rparen ::token/rbrace ::token/rbracket)
(panic parser (str "Unbalanced enclosure: " (::token/lexeme token)) expr-sync) (panic parser (str "Unbalanced enclosure: " (::token/lexeme token)))
(::token/semicolon ::token/comma) (::token/semicolon ::token/comma)
(panic parser (str "Unexpected delimiter: " (::token/lexeme token)) expr-sync) (panic parser (str "Unexpected delimiter: " (::token/lexeme token)))
(panic parser "Expected expression" expr-sync) (panic parser "Expected expression" expr-sync)
@ -389,7 +383,8 @@
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "(1, 2, (},; 4, 123^)") (def source "let foo = (32, 23
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))