Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created June 1, 2013 23:26
Show Gist options
  • Select an option

  • Save ericnormand/5692047 to your computer and use it in GitHub Desktop.

Select an option

Save ericnormand/5692047 to your computer and use it in GitHub Desktop.
A slight reworking of the docstring for mkscope plus a new, similar function for creating lexical scopes.
(defn mkscope
"Create a rule which contains the scope of the given rule. Bindings
made in rule do not escape this rule's scope.
<code>mkscope</code> creates a scope protector around a rule so that
bindings that the given rule creates do not leak into the current
scope. This function should be used around your own rules.
Example:
;; a rule that binds to as, but is protected by a scope
(def as (mkscope (mkbind (mk1om (mklit \\a)) :as)))
;; a rule that calls as
(def xab (mkseq (mkbind (mk1om (mklit \\x)) :as) ;; bind to :as
as ;; existing binding to :as
;; is not available
(mk1om (mklit \\b))))"
[rule]
(fn [input bindings context memo]
(let [r (rule input {} context memo)]
(if (success? r)
(succeed (:r r) (:s r) (:i r) bindings (:m r))
r))))
(defn mklex
"Create a rule which contains the scope of the given rule. The
bindings at the point the rule is called are available, but no new
bindings will escape the scope.
<code>mklex</code> creates a scope protector around a rule so that
bindings that the given rule creates do not leak into the current
scope. This function should be used around other rules that do local
bindings.
Example:
;; a rule that binds but does not protect
(def as (mkbind (mk1om (mklit \\a)) :as))
;; a rule that calls as
(def xab (mkseq (mkbind (mk1om (mklit \\x)) :as) ;; bind to :as
(mklex as) ;; :as is available to as,
;; but as cannot rebind
(mk1om (mklit \\b))))"
[rule]
(fn [input bindings context memo]
(let [r (rule input bindings context memo)]
(if (success? r)
(succeed (:r r) (:s r) (:i r) bindings (:m r))
r))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment