Skip to content
This repository has been archived by the owner on Feb 3, 2018. It is now read-only.

Commit

Permalink
rule-match added; rule-matcher deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-choi committed Jun 11, 2009
1 parent 36aa558 commit bd0f7a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
24 changes: 17 additions & 7 deletions src/name/choi/joshua/fnparse.clj
Expand Up @@ -400,17 +400,27 @@
(complex [subproduct subrule, subremainder get-remainder, :when (validator subremainder)]
subproduct))

(defn rule-matcher
(defn rule-match
"Creates a function that tries to completely match the given rule to the given state, with
no remainder left.
- If (rule given-state) fails, then (failure-fn given-state) is called.
- If the remainder of (rule given-state) is not empty, then
(incomplete-fn given-state new-state-after-rule) is called.
- If the new remainder is empty, then the product of the rule is returned."
[rule failure-fn incomplete-fn state]
(if-let [[product new-state] (rule state)]
(if (empty? (*remainder-accessor* new-state))
product
(incomplete-fn state new-state))
(failure-fn state)))

(defn rule-matcher
"DEPRECATED: Use rule-match instead.
Creates a function that tries to completely match the given rule to the given state, with
no remainder left.
- If (rule given-state) fails, then (failure-fn given-state) is called.
- If the remainder of (rule given-state) is not empty, then
(incomplete-fn given-state new-state-after-rule) is called.
- If the new remainder is empty, then the product of the rule is returned."
[rule failure-fn incomplete-fn]
(fn [state]
(if-let [[product new-state] (rule state)]
(if (empty? (*remainder-accessor* new-state))
product
(incomplete-fn state new-state))
(failure-fn state))))
(partial rule-match rule failure-fn incomplete-fn))
18 changes: 8 additions & 10 deletions test/test_parse.clj
Expand Up @@ -7,8 +7,8 @@
(def make-state (partial struct state-s))
(deferror parse-error [] []
{:msg "WHEEE", :unhandled (throw-msg IllegalArgumentException)})
(deferror weird-error [] [n]
{:msg (str "BOOM " n), :unhandled (throw-msg Exception)})
(deferror weird-error [] []
{:msg "BOOM", :unhandled (throw-msg Exception)})

(deftest emptiness
(is (= (p/emptiness {:remainder (list "A" "B" "C")})
Expand Down Expand Up @@ -288,13 +288,11 @@
(binding [p/*remainder-accessor* (accessor state-s :remainder)]
(is (= ((p/lit \a) (make-state "abc")) [\a (make-state (seq "bc"))]))))

(deftest rule-matcher
(let [rule (p/errorpoint (p/lit "A") (raise weird-error 55))
matcher1 (p/rule-matcher rule identity vector)
matcher2 (p/rule-matcher rule identity identity vector)]
(is (= (matcher1 (make-state ["A"])) "A"))
(is (= (matcher1 (make-state ["B"])) (make-state ["B"])))
(is (= (matcher1 (make-state ["A" "B"])) [(make-state ["A" "B"]) (make-state ["B"])]))
(is (= (matcher1 (make-state ["B"])) 55))))
(deftest rule-match
(let [rule (p/lit "A")
matcher (partial p/rule-match rule identity vector)]
(is (= (matcher (make-state ["A"])) "A"))
(is (= (matcher (make-state ["B"])) (make-state ["B"])))
(is (= (matcher (make-state ["A" "B"])) [(make-state ["A" "B"]) (make-state ["B"])]))))

(time (run-tests))

0 comments on commit bd0f7a9

Please sign in to comment.