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

Commit

Permalink
after a lot of work, added the parse function and test methods to Hound
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-choi committed Jan 14, 2010
1 parent df1ad44 commit 1f33c2e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/name/choi/joshua/fnparse/clojure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@
ws?
(alt list-r vector-r map-r dispatched-form string-r syntax-quoted-form unquote-spliced-form unquoted-form division-symbol character-form keyword-r peculiar-symbol symbol-r decimal-number))))

(use 'clojure.test 'name.choi.joshua.fnparse.hound.test)

(is (full-match? "55" decimal-number 55 2))
; (-> "#^{} #{[a b;Comment\nc]}" make-state form prn)
; (-> "#_#_'a'b'c" make-state form prn)
(-> "#^:monster #{a b c d}" make-state form prn)
; (-> "#^:monster #{a b c d}" (parse form vector nil) prn)
; (-> "aa\" 2\"]" make-state form println)
; (-> "\"a\\tb\"" make-state form prn)
; (-> "\\t\"" make-state escape-sequence prn)
Expand Down
13 changes: 10 additions & 3 deletions src/name/choi/joshua/fnparse/hound.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns name.choi.joshua.fnparse.hound
[:use clojure.contrib.seq-utils clojure.contrib.def clojure.test
clojure.set clojure.contrib.monads clojure.template]
[:import [clojure.lang Sequential IPersistentMap IPersistentVector Var]])
(:use clojure.contrib.seq-utils clojure.contrib.def clojure.test
clojure.set clojure.contrib.monads clojure.template)
(:import [clojure.lang Sequential IPersistentMap IPersistentVector Var]))

(set! *warn-on-reflection* true)

Expand Down Expand Up @@ -51,6 +51,13 @@
(defn failure? [result]
(isa? (type result) ::Failure))

(defn parse
[input rule success-fn failure-fn]
(let [result (-> input make-state rule :result force)]
(if (failure? result)
(failure-fn (:expectation result))
(success-fn (:product result) (-> result :state :remainder)))))

(letfn [(reply-expected-rules [reply]
(-> reply :result :expectation :expected-rules))]
(defn merge-replies [mergee merger]
Expand Down
38 changes: 38 additions & 0 deletions src/name/choi/joshua/fnparse/hound/test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns name.choi.joshua.fnparse.hound.test
(:use name.choi.joshua.fnparse.hound clojure.test)
(:require [clojure.contrib.str-utils2 :as str]))

(defmethod assert-expr 'partial-match?
[msg [_ input rule product consumed-tokens-num]]
(let [input-count (count input)]
`(letfn [(report-this#
([kind# format-template# expected-arg# actual-arg# & other-args#]
(report {:type kind#, :message ~msg,
:expected (apply format format-template#
expected-arg# other-args#),
:actual (apply format format-template#
actual-arg# other-args#)}))
([kind#] (report {:type kind#, :message ~msg})))]
(parse ~input ~rule
(fn [actual-product# actual-remainder#]
(let [actual-consumed-tokens-num#
(- ~input-count (count actual-remainder#))]
(if (not= actual-consumed-tokens-num# ~consumed-tokens-num)
(report-this# :fail "%s tokens consumed"
~consumed-tokens-num actual-consumed-tokens-num#)
(if (not= actual-product# ~product)
(report-this# :fail "a product of %s"
~product actual-product#)
(report-this# :pass)))))
(fn [expectation#]
(let [unexpected-token# (:unexpected-token expectation#)]
(report-this# :fail "%s at position %s"
(str/join " or " (:expected-rules expectation#))
(str "a token " unexpected-token# " (of the type "
(pr-str (type unexpected-token#)) ")")
(:position expectation#))))))))

(defmethod assert-expr 'full-match?
[msg [_ input rule product]]
(assert-expr msg
(list 'partial-match? input rule product (count input))))

0 comments on commit 1f33c2e

Please sign in to comment.