Permalink
Browse files

Remove remaining use of parser monad

  • Loading branch information...
1 parent ff995ed commit dc33ed6e4aac73cc346bc28ef170cf435b0e3fba @hlship committed Dec 5, 2011
Showing with 18 additions and 91 deletions.
  1. +1 −2 project.clj
  2. +8 −14 src/cascade/internal/parse_functions.clj
  3. +9 −75 src/cascade/internal/parser.clj
View
@@ -3,8 +3,7 @@
:url "http://github.com/hlship/cascade"
:warn-on-reflection true
:dependencies [[org.clojure/clojure "1.3.0"]
- [org.clojure/algo.monads "0.1.0"]
- [clj-json "0.4.3"]
+ [clj-json "0.4.3"]
[compojure "0.6.5"]]
:dev-dependencies [[ring/ring-jetty-adapter "0.3.11"]
[hiccup "0.3.7"]
@@ -16,24 +16,18 @@
"A form parser used when building functions, to emulate (defn)'s ability to add documentation
and other meta-data"
(:use
- (clojure.algo monads)
cascade.internal.parser))
-(def fn-def-parser
- (domonad parser-m
- [fn-name match-symbol
- doc-string (optional match-string)
- fn-meta-data (optional match-map)
- parameters match-vector
- fn-forms (one-or-more any-form)]
- (let [doc-meta (and doc-string {:doc doc-string})
- full-meta (merge (meta fn-name) fn-meta-data doc-meta)
- symbol-with-meta (with-meta fn-name full-meta)]
- [symbol-with-meta parameters fn-forms])))
-
(defn parse-function-def
"Parses a flexible set of forms consisting of an optional documention string, an optional meta-data map, a
required vector of parameters, and a series of additional forms. Returns a vector of the name (with additional meta
data from the documentation string provided meta-data map), the parameters vector, and a seq of the additional forms."
[fn-def-forms]
- (run-parse fn-def-parser fn-def-forms "function definition"))
+ (let [[fn-name remaining] (must-consume symbol? fn-def-forms "symbol to define function name")
+ [doc-string remaining] (maybe-consume string? remaining)
+ [fn-meta-data remaining] (maybe-consume map? remaining)
+ [parameters fn-forms] (must-consume vector? remaining "vector of function parameters")]
+ (let [doc-meta (and doc-string {:doc doc-string})
+ full-meta (merge (meta fn-name) fn-meta-data doc-meta)
+ symbol-with-meta (with-meta fn-name full-meta)]
+ [symbol-with-meta parameters fn-forms])))
@@ -13,89 +13,23 @@
(ns
cascade.internal.parser
- "Parser monad used to parse Clojure forms into higher-level structures"
+ "Parser utiliuties Clojure forms into higher-level structures"
(:use
- (clojure.algo monads)
cascade.fail))
-
(defn maybe-consume
"Evaluates a predicate against the first form. Returns a vector of two values. When the predicate matches, the return value is the first form and the rest of the forms.
-When the predicate does not match, the return value is nil and the forms."
+when the predicate does not match, the return value is nil and the forms."
[predfn forms]
(let [first-form (first forms)]
(if (predfn first-form)
[first-form (rest forms)]
[nil forms])))
-(def parser-m (state-t maybe-m))
-
-(defn any-form
- "Fundamental parser action; returns [first, rest] if forms is not empty, nil otherse."
- [forms]
- (if (empty? forms)
- nil
- ; "Consume" the form
- (list (first forms) (rest forms))))
-
-(with-monad parser-m
- (defn optional
- [parser]
- (m-plus parser (m-result nil)))
-
- (declare one-or-more)
-
- (defn none-or-more [parser]
- (optional (one-or-more parser)))
-
- (defn one-or-more [parser]
- (domonad [a parser
- as (none-or-more parser)]
- (cons a as)))
-
- (def match-first m-plus)
-
- (defn form-test
- "Parser factory using a predicate. When a form matches the predicate, it becomes the new result."
- [pred]
- (domonad
- [form any-form :when (pred form)]
- ; And return the matched form
- form))
-
- (def match-keyword
- (form-test keyword?))
-
- (def match-string
- (form-test string?))
-
- (def match-map
- (form-test map?))
-
- (def match-vector
- (form-test vector?))
-
- (def match-list
- (form-test list?))
-
- (def match-symbol
- (form-test symbol?))
-
- (def match-form
- (match-first match-list match-symbol))
-) ; with-monad parser-m
-
-(defn run-parse
- "Executes a parse given a parser function and a set of forms to parse. The construct-name is used for reporting errors:
- either a nil monadic result, or an incomplete parse (that leaves some forms unparsed)."
- [parser forms construct-name]
- (let [monadic-result (parser forms)]
- (when (nil? monadic-result)
- (fail "Parse of %s completed with no result." construct-name))
- (let [[result remaining-forms] monadic-result]
- (when-not (empty? remaining-forms)
- (fail "Incomplete parse of %s, %s forms remain, starting with %s."
- forms
- (count remaining-forms)
- (first remaining-forms)))
- result)))
+(defn must-consume
+ "Like maybe-consume, but the first form must pass the predicate function, or a failure occurs."
+ [predfn forms description]
+ (let [first-form (first forms)]
+ (if (predfn first-form)
+ [first-form (rest forms)]
+ (fail "Unexpected input form when expecting %s: %s" description (str first-form)))))

0 comments on commit dc33ed6

Please sign in to comment.