fix panic off-by-one-error

This commit is contained in:
Scott Richmond 2024-05-08 15:56:59 -04:00
parent 0eb212dd45
commit 942f55fb39

View File

@ -328,9 +328,12 @@
(array/push (ast :data) (capture nonbinding parser)) (array/push (ast :data) (capture nonbinding parser))
ast) ast)
### XXX: We've got an off-by-one error here
# We're expecting a terminator, we panic until we get to a terminator, and we then return back to something that expects a terminator, and now we've started parsing again *at the terminator*
(defn- terminator [parser] (defn- terminator [parser]
(if-not (terminates? parser) (panic parser "expected terminator")) (if-not (terminates? parser)
# this line panics, captures the panic, advances the parser, and re-throws the error
(try (panic parser "expected terminator") ([e] (advance parser) (error e))))
(advance parser) (advance parser)
(while (terminates? parser) (advance parser))) (while (terminates? parser) (advance parser)))
@ -511,14 +514,16 @@
(import ./scanner :as s) (import ./scanner :as s)
(do (do
#(comment #(comment
(def source "when { (def source `when {
foo -> bar foo -> bar quux
bar -> baz
} }
") `)
(def scanned (s/scan source)) (def scanned (s/scan source))
(def a-parser (new-parser scanned)) (def a-parser (new-parser scanned))
(def parsed (whenn a-parser)) (def parsed (whenn a-parser))
(-> parsed) (-> parsed)
# (map (fn [t] (t :type)) (scanned :tokens))
) )