From 80bd0d6a35d451fa8dd4c400b2243ec8aa6067f2 Mon Sep 17 00:00:00 2001 From: janos erdos Date: Tue, 14 Nov 2023 19:18:57 +0100 Subject: [PATCH] small refactor in ast building step --- src/stencil/cleanup.clj | 61 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/stencil/cleanup.clj b/src/stencil/cleanup.clj index 43d8626e..59a1172c 100644 --- a/src/stencil/cleanup.clj +++ b/src/stencil/cleanup.clj @@ -12,37 +12,36 @@ (set! *warn-on-reflection* true) -(defn- tokens->ast-step [[queue & ss0 :as stack] token] - (case (:cmd token) - (:if :for) (conj (mod-stack-top-conj stack token) []) - - :else - (if (empty? ss0) - (throw (parsing-exception (str open-tag "else" close-tag) - "Unexpected {%else%} tag, it must come right after a condition!")) - (conj (mod-stack-top-last ss0 update ::blocks (fnil conj []) {::children queue}) [])) - - :else-if - (if (empty? ss0) - (throw (parsing-exception (str open-tag "else if" close-tag) - "Unexpected {%else if%} tag, it must come right after a condition!")) - (-> ss0 - (mod-stack-top-last update ::blocks (fnil conj []) {::children queue}) - (conj [(assoc token :cmd :if :r true)]) - (conj []))) - - :end - (if (empty? ss0) - (throw (parsing-exception (str open-tag "end" close-tag) - "Too many {%end%} tags!")) - (loop [[queue & ss0] stack] - (let [new-stack (mod-stack-top-last ss0 update ::blocks conj {::children queue})] - (if (:r (peek (first new-stack))) - (recur (mod-stack-top-last new-stack dissoc :r)) - new-stack)))) - - (:cmd/echo nil :cmd/include) - (mod-stack-top-conj stack token))) +(defmulti tokens->ast-step (fn [stack token] (:cmd token))) + +(defmethod tokens->ast-step :if [stack token] (conj (mod-stack-top-conj stack token) [])) +(defmethod tokens->ast-step :for [stack token] (conj (mod-stack-top-conj stack token) [])) +(defmethod tokens->ast-step :cmd/echo [stack token] (mod-stack-top-conj stack token)) +(defmethod tokens->ast-step nil [stack token] (mod-stack-top-conj stack token)) +(defmethod tokens->ast-step :cmd/include [stack token] (mod-stack-top-conj stack token)) + +(defmethod tokens->ast-step :else [[queue & rest-stack] _] + (-> (not-empty rest-stack) + (or (throw (parsing-exception (str open-tag "else" close-tag) + "Unexpected {%else%} tag, it must come right after a condition!"))) + (mod-stack-top-last update ::blocks (fnil conj []) {::children queue}) + (conj []))) + +(defmethod tokens->ast-step :else-if [[queue & rest-stack] token] + (-> (not-empty rest-stack) + (or (throw (parsing-exception (str open-tag "else if" close-tag) + "Unexpected {%else if%} tag, it must come right after a condition!"))) + (mod-stack-top-last update ::blocks (fnil conj []) {::children queue}) + (conj [(assoc token :cmd :if :r true)] []))) + +(defmethod tokens->ast-step :end [stack _] + (if (empty? (next stack)) + (throw (parsing-exception (str open-tag "end" close-tag) "Too many {%end%} tags!")) + (loop [[queue & ss0] stack] + (let [new-stack (mod-stack-top-last ss0 update ::blocks conj {::children queue})] + (if (:r (peek (first new-stack))) + (recur (mod-stack-top-last new-stack dissoc :r)) + new-stack))))) (defn tokens->ast "Flat token listabol nested AST-t csinal (listak listai)"