From de30e5e87ce0f14899f516af5b2c823990957ed5 Mon Sep 17 00:00:00 2001 From: Dominic Monroe Date: Sat, 27 May 2017 15:52:04 +0100 Subject: [PATCH] Add bindings for match context This binds 2 vars during the evaluation of targets. `mach.core/*target-ctx*` this is bound to a string that contains the whole string that matched this target e.g. a glob fo* would match "foo.xml", this var would contain "foo.xml" `mach.core/*matcher-ctx*` this is bound to the object that was used to match this target. e.g. for `#mach/glob "fo*"` the underlying Glob object will be bound to this var. --- src/mach/core.cljs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/mach/core.cljs b/src/mach/core.cljs index a928996..3be2dab 100755 --- a/src/mach/core.cljs +++ b/src/mach/core.cljs @@ -278,13 +278,25 @@ [expr target] (postwalk (fn [x] (or (and (symbol? x) (get target x)) x)) expr)) +(def ^:dynamic *target-ctx* nil) +(def ^:dynamic *matcher-ctx* nil) + +(defn- with-target-ctx-bindings + [expr target] + (list 'binding (into [] cat (-> target + (select-keys [:mach/_target-ctx :mach/_matcher-ctx]) + (rename-keys {:mach/_target-ctx 'mach.core/*target-ctx* + :mach/_matcher-ctx 'mach.core/*matcher-ctx*}))) + expr)) + (defn- eval-rule "Evals Mach rule and returns the result if successful, throws an error if not." [code target machfile] (let [code (-> code (resolve-symbols target) - (with-prop-bindings machfile))] + (with-prop-bindings machfile) + (with-target-ctx-bindings target))] ;; Eval the code (let [{:keys [value error]} (cljs/eval repl/st code identity)] (when error