From 942f55fb39e304a4333dc78ff8bc6a7cd9d681c7 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Wed, 8 May 2024 15:56:59 -0400 Subject: [PATCH] fix panic off-by-one-error --- janet/recursive.janet | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/janet/recursive.janet b/janet/recursive.janet index 773e614..4a40d66 100644 --- a/janet/recursive.janet +++ b/janet/recursive.janet @@ -328,9 +328,12 @@ (array/push (ast :data) (capture nonbinding parser)) 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] - (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) (while (terminates? parser) (advance parser))) @@ -511,14 +514,16 @@ (import ./scanner :as s) (do #(comment -(def source "when { -foo -> bar +(def source `when { + foo -> bar quux + bar -> baz } -") +`) (def scanned (s/scan source)) (def a-parser (new-parser scanned)) (def parsed (whenn a-parser)) (-> parsed) +# (map (fn [t] (t :type)) (scanned :tokens)) )