Permalink
Browse files

rule-match added; rule-matcher deprecated

  • Loading branch information...
1 parent 36aa558 commit bd0f7a9e23664dc7cabe14a50e9e13fca2d99ce9 joshua-choi committed Jun 11, 2009
Showing with 25 additions and 17 deletions.
  1. +17 −7 src/name/choi/joshua/fnparse.clj
  2. +8 −10 test/test_parse.clj
@@ -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))
View
@@ -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")})
@@ -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.