Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Additional Rename/Identation Cleanup #1

Merged
2 commits merged into from

2 participants

@devn

Hey again,

I hope you don't mind but I figured I'd take a look into clarsec and do a bit of cleanup since haml-macro relies on fns defined in clarsec to work correctly.

I'm sure it's not perfect, and unfortunately the repo did not include any tests so I was unable to verify everything is working correctly, but I am fairly confident that all of the renaming should be consistent across libraries.

Cheers,
Devin

@mmikulicic
Owner

thank you again for the contribution, afaik haml-macro is the main user of clarsec for now, so your changes shouldn't break anything, and certainly will help the adoption of clarsec by the clojure community

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 5, 2011
  1. Updated indentation and renamed CamelCase to camel-case

    Devin Walters authored
  2. Addtl cleanup to work with haml-macro

    Devin Walters authored
This page is out of date. Refresh to see the latest.
View
10 project.clj
@@ -1,6 +1,6 @@
-(defproject clarsec "0.0.1-SNAPSHOT" :description "Parsec ported to clojure" :dependencies [
- [org.clojure/clojure "1.1.0"]
- [org.clojure/clojure-contrib "1.0-SNAPSHOT"]
- ]
+(defproject clarsec "0.0.1-SNAPSHOT"
+ :description "Parsec ported to clojure"
+ :dependencies [[org.clojure/clojure "1.1.0"]
+ [org.clojure/clojure-contrib "1.0-SNAPSHOT"]]
:dev-dependencies [[lein-clojars "0.5.0-SNAPSHOT"]]
- :main eu.dnetlib.dlms.parser)
+ :main eu.dnetlib.dlms.parser)
View
179 src/eu/dnetlib/clojure/clarsec.clj
@@ -3,13 +3,19 @@
(:use [eu.dnetlib.clojure.monad]))
-(defn consumed? [x] (= (x :type) :consumed))
-(defn failed? [x] (= (x :type) :failed))
+(defn consumed? [x]
+ (= (x :type) :consumed))
-(defn failed [] {:type :failed})
-(defn consumed [value rest] {:type :consumed
- :value value
- :rest rest})
+(defn failed? [x]
+ (= (x :type) :failed))
+
+(defn failed []
+ {:type :failed})
+
+(defn consumed [value rest]
+ {:type :consumed
+ :value value
+ :rest rest})
(defn failback [v f] (if (nil? v) f v))
@@ -22,71 +28,58 @@
(defmethod return 'Parser
[t x]
(make-monad t (fn p-return [strn] (consumed x strn))))
-
(defmethod bind 'Parser
[dm dfunc]
(let [m (force dm)
- func (force dfunc)]
+ func (force dfunc)]
(make-monad (monad-type m)
- (fn [strn]
- (let [parser (monad m)
- result (parser strn)]
- (if (consumed? result)
- ((force (monad (force (func (:value result))))) (:rest result))
- result
- )
- )
- ))))
-
-(defn result [v] (return 'Parser v))
+ (fn [strn]
+ (let [parser (monad m)
+ result (parser strn)]
+ (if (consumed? result)
+ ((force (monad (force (func (:value result))))) (:rest result))
+ result))))))
+(defn result [v]
+ (return 'Parser v))
(defn <|> [& parsers]
(make-monad 'Parser
- (fn opt-plus [strn]
- (failback
- (first
- (drop-while failed?
- (map #((monad (force %)) strn) parsers)))
- (failed)
- ))))
+ (fn opt-plus [strn]
+ (failback
+ (first (drop-while failed? (map #((monad (force %)) strn) parsers)))
+ (failed)))))
(defn >> [p1 p2]
(bind p1 (fn [_] p2)))
(def either <|>)
-; bind with a non monadic function
+;; Bind with a non monadic function
(defn >>== [p f]
(bind p #(result (f %))))
-;;
-(def anyToken
- (make-monad 'Parser
- (fn p-anyToken [strn]
- (if (= "" strn)
- (failed)
- (consumed (first strn)
- (. strn (substring 1))))
- )
- ))
-
-(def eof
+(def any-token
+ (make-monad 'Parser
+ (fn p-any-token [strn]
+ (if (= "" strn)
+ (failed)
+ (consumed (first strn)
+ (. strn (substring 1)))))))
+
+(def eof
(make-monad 'Parser
- (fn p-eof [strn]
- (if (= "" strn)
- (consumed "" "")
- (failed)
- ))))
+ (fn p-eof [strn]
+ (if (= "" strn)
+ (consumed "" "")
+ (failed)))))
-(def fail
- (make-monad 'Parser (fn p-fail [strn] (failed))))
+(def fail (make-monad 'Parser (fn p-fail [strn] (failed))))
(defn satisfy [pred]
- (let-bind [c anyToken]
- (if (pred c) (result c) fail)
- ))
+ (let-bind [c any-token]
+ (if (pred c) (result c) fail)))
(defn is-char [c]
(satisfy (partial = c)))
@@ -103,57 +96,54 @@
(defn string [strn]
(>>== (m-sequence (map is-char strn))
- #(apply str %)))
+ #(apply str %)))
(def many1)
(defn many [parser]
- (>>== (optional (many1 parser))
- #(if (nil? %) () %)))
+ (>>== (optional (many1 parser))
+ #(if (nil? %) () %)))
(defn many1 [parser]
- (let-bind
- [a parser
- as (many parser)]
- (result (concat [a] as))))
+ (let-bind [a parser
+ as (many parser)]
+ (result (concat [a] as))))
-(defn endByM [f p sep]
+(defn end-by-m [f p sep]
(f (let-bind [r p
- _ sep]
- (result r))))
+ _ sep]
+ (result r))))
-(defn endBy [p sep]
- (endByM many p sep))
+(defn end-by [p sep]
+ (end-by-m many p sep))
-(defn endBy1 [p sep]
- (endByM many1 p sep))
+(defn end-by-1 [p sep]
+ (end-by-m many1 p sep))
-(defn sepBy1 [p sep]
+(defn sep-by-1 [p sep]
(let-bind [x p
- xs (many (>> sep p))]
- (result (cons x xs))))
+ xs (many (>> sep p))]
+ (result (cons x xs))))
-(defn sepBy [p sep]
- (either (sepBy1 p sep) (result ())))
+(defn sep-by [p sep]
+ (either (sep-by-1 p sep) (result ())))
-(defn followedBy [p sep]
+(defn followed-by [p sep]
(let-bind [r p
- _ sep]
- (result r)))
-
+ _ sep]
+ (result r)))
(def letter
- (satisfy #(. Character isLetter %)))
+ (satisfy #(. Character isLetter %)))
(def digit
- (satisfy #(. Character isDigit %)))
+ (satisfy #(. Character isDigit %)))
(defn one-of [target-strn]
(let [str-chars (into #{} target-strn)]
(satisfy #(contains? str-chars %))))
-(def space
- (one-of " \r\n\t"))
+(def space (one-of " \r\n\t"))
(def spaces (many space))
@@ -166,46 +156,43 @@
(def semi (symb ";"))
(def comma (symb ","))
-; convert the result of a parse to a string, if its a list then concatenates the list
+;; Convert the result of a parse to a string, if it's a list then concatenates the list...
(defn stringify [p]
(bind p #(result (if (seq? %) (apply str %) (str %)))))
-(def baseIdentifier
+(def base-identifier
(let-bind [c letter
- cs (many (either letter digit))]
- (result (apply str (cons c cs)))))
+ cs (many (either letter digit))]
+ (result (apply str (cons c cs)))))
(def identifier
- (lexeme baseIdentifier))
+ (lexeme base-identifier))
(def natural
(lexeme (>>== (stringify (many1 digit))
- #(new Integer %))))
-
+ #(new Integer %))))
(defn between [open close p]
(let-bind [_ open
- x p
- _ close]
- (result x)))
+ x p
+ _ close]
+ (result x)))
(defn parens [p]
- (between (symb "(") (symb ")") p))
+ (between (symb "(") (symb ")") p))
(defn brackets [p]
- (between (symb "[") (symb "]") p))
-
+ (between (symb "[") (symb "]") p))
+
(defn braces [p]
- (between (symb "{") (symb "}") p))
+ (between (symb "{") (symb "}") p))
(def stringLiteral
(stringify (lexeme (between (is-char \") (is-char \") (many (not-char \"))))))
-(defn parse [parser input]
- ((monad (force parser)) input)
-)
-
+(defn parse [parser input]
+ ((monad (force parser)) input))
-;(defn -main []
-; (println (parse (>> (delay letter) (delay letter)) "ca.")))
+;;(defn -main []
+;; (println (parse (>> (delay letter) (delay letter)) "ca.")))
View
72 src/eu/dnetlib/clojure/monad.clj
@@ -1,32 +1,31 @@
-;-
-; Copyright 2008 (c) Meikel Brandmeyer.
-; All rights reserved.
-;
-; Permission is hereby granted, free of charge, to any person obtaining a copy
-; of this software and associated documentation files (the "Software"), to deal
-; in the Software without restriction, including without limitation the rights
-; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-; copies of the Software, and to permit persons to whom the Software is
-; furnished to do so, subject to the following conditions:
-;
-; The above copyright notice and this permission notice shall be included in
-; all copies or substantial portions of the Software.
-;
-; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-; THE SOFTWARE.
-
-; this is a slightly patched version which accepts "Delay" objects is monadic objects
-; this hack shouldn't be necessary but I was in a hurry
-; the correct solution is to perform the delay forcing within the monadic operators
-
-(clojure.core/ns eu.dnetlib.clojure.monad
- (:use
- [clojure.contrib.def :only (defstruct-)]))
+;;-
+;; Copyright 2008 (c) Meikel Brandmeyer.
+;; All rights reserved.
+;;
+;; Permission is hereby granted, free of charge, to any person obtaining a copy
+;; of this software and associated documentation files (the "Software"), to deal
+;; in the Software without restriction, including without limitation the rights
+;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+;; copies of the Software, and to permit persons to whom the Software is
+;; furnished to do so, subject to the following conditions:
+;;
+;; The above copyright notice and this permission notice shall be included in
+;; all copies or substantial portions of the Software.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
+;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
+;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+;; THE SOFTWARE.
+
+;; this is a slightly patched version which accepts "Delay" objects is monadic objects
+;; this hack shouldn't be necessary but I was in a hurry
+;; the correct solution is to perform the delay forcing within the monadic operators
+
+(ns eu.dnetlib.clojure.monad
+ (:use [clojure.contrib.def :only (defstruct-)]))
(declare Monad MZero MRunnable)
@@ -51,8 +50,7 @@
(defmulti
#^{:arglists '([monad-type monad-argument])
- :doc
- "Return a monad of the given type with the given argument."}
+ :doc "Return a monad of the given type with the given argument."}
return
(fn [t _] t))
@@ -61,8 +59,7 @@
(make-monad t m))
(defmulti
- #^{:doc
- "bind makes the value of the given monad available to a function.
+ #^{:doc "bind makes the value of the given monad available to a function.
The function may act on the value, but it must return another monad.
Although this cannot be enforced in Clojure."}
bind (fn [m _] (monad-type (force m))))
@@ -71,9 +68,7 @@
(defmethod bind `Monad [m f] (f (monad m)))
(defmulti
- #^{:doc
- "If the first argument is not a `MZero, return it. Otherwise return
- the second value."}
+ #^{:doc "If the first argument is not a `MZero, return it. Otherwise return the second value."}
mplus
(fn [m1 m2] #(vec (map monad-type [%1 %2]))))
@@ -99,7 +94,7 @@
(let [f (fn [ms m]
(let-bind [result m
results ms]
- (return (monad-type m) (conj results result))))]
+ (return (monad-type m) (conj results result))))]
(reduce f (return (-> monads first monad-type) nil) (reverse monads))))
(defmacro lift-into
@@ -111,8 +106,7 @@
(defmulti
#^{:arglists '([monad & args])
- :doc
- "Applies the value of the given monad to the arguments and returns
+ :doc "Applies the value of the given monad to the arguments and returns
the result. Not all monad types are `Runnable."}
run
(fn [m & _] (monad-type m)))
Something went wrong with that request. Please try again.