Start work on if errors

This commit is contained in:
Scott Richmond 2022-03-13 19:43:57 -04:00
parent a05feea32a
commit 83392e3ac4

View File

@ -61,7 +61,7 @@
(defn- panic (defn- panic
([parser message] (panic parser message sync-on)) ([parser message] (panic parser message sync-on))
([parser message sync-on] ([parser message sync-on]
(println (str "PANIC!!! in the parser" message)) (println (str "PANIC!!! in the parser: " message))
(let [ (let [
sync-on (conj (if (set? sync-on) sync-on #{sync-on}) ::token/eof) sync-on (conj (if (set? sync-on) sync-on #{sync-on}) ::token/eof)
origin (current parser) origin (current parser)
@ -360,11 +360,11 @@
;; TODO: Fix failure case here ;; TODO: Fix failure case here
(defn- parse-if [parser] (defn- parse-if [parser]
(let [ (let [
if-expr (parse-expr (advance parser)) if-expr (parse-expr (advance parser) #{::token/then ::token/newline})
then (expect ::token/then "Expected then" (accept ::token/newline if-expr)) then (expect ::token/then "Expected then" (accept ::token/newline if-expr))
then-expr (parse-expr then) then-expr (parse-expr then)
else (expect ::token/else "Epected else" (accept ::token/newline then-expr)) else (expect ::token/else "Epected else" (accept ::token/newline then-expr))
else-expr (parse-expr else) else-expr (parse-expr else #{::token/else ::token/newline})
results (map #(get-in % [::ast ::ast/type]) [if-expr then then-expr else else-expr]) results (map #(get-in % [::ast ::ast/type]) [if-expr then then-expr else else-expr])
] ]
(if (some #(= ::ast/poison %) results) (if (some #(= ::ast/poison %) results)
@ -377,55 +377,58 @@
})) }))
)) ))
(defn- parse-expr [parser] (defn- parse-expr
(let [token (current parser)] ([parser] (parse-expr parser sync-on))
(case (::token/type token) ([parser sync-on] (let [token (current parser)]
(case (::token/type token)
(::token/number ::token/string)
(parse-atom parser) (::token/number ::token/string)
(parse-atom parser)
::token/keyword (let [next (peek parser)
type (::token/type next)] ::token/keyword (let [next (peek parser)
(if (= type ::token/lparen) type (::token/type next)]
(parse-synthetic parser) (if (= type ::token/lparen)
(parse-atom parser))) (parse-synthetic parser)
(parse-atom parser)))
::token/word (let [next (peek parser)
type (::token/type next)] ::token/word (let [next (peek parser)
(case type type (::token/type next)]
(::token/lparen ::token/keyword) (parse-synthetic parser) (case type
(parse-word parser))) (::token/lparen ::token/keyword) (parse-synthetic parser)
(parse-word parser)))
(::token/nil ::token/true ::token/false)
(parse-atomic-word parser) (::token/nil ::token/true ::token/false)
(parse-atomic-word parser)
::token/lparen (parse-tuple parser)
::token/lparen (parse-tuple parser)
::token/lbracket (parse-list parser)
::token/lbracket (parse-list parser)
::token/startset (parse-set parser)
::token/startset (parse-set parser)
::token/lbrace (parse-block parser)
::token/lbrace (parse-block parser)
::token/let (parse-let* parser)
::token/let (parse-let* parser)
::token/if (parse-if parser)
::token/if (parse-if parser)
::token/error (panic parser (:message token))
::token/error (panic parser (:message token))
(::token/rparen ::token/rbrace ::token/rbracket)
(panic parser (str "Unbalanced enclosure: " (::token/lexeme token))) (::token/rparen ::token/rbrace ::token/rbracket)
(panic parser (str "Unbalanced enclosure: " (::token/lexeme token)))
(::token/semicolon ::token/comma)
(panic parser (str "Unexpected delimiter: " (::token/lexeme token))) (::token/semicolon ::token/comma)
(panic parser (str "Unexpected delimiter: " (::token/lexeme token)))
(panic parser "Expected expression")
(panic parser "Expected expression" sync-on)
)))
))))
(do (do
(def pp pp/pprint) (def pp pp/pprint)
(def source "if *else then bar else baz") (def source "if
then bar
else baz")
(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))
@ -437,7 +440,7 @@
(println "*** *** NEW PARSE *** ***") (println "*** *** NEW PARSE *** ***")
(-> p (-> p
(parse-script) (parse-if)
(::ast) (::ast)
(pp) (pp)
) )