Permalink
Browse files

alternative

  • Loading branch information...
1 parent 901db7c commit 12be90f997926b5a9687972cf006c18f25aa39f6 @mmikulicic committed Jan 2, 2010
Showing with 22 additions and 1 deletion.
  1. +22 −1 src/clarsec.clj
View
@@ -10,6 +10,8 @@
:value value
:rest rest})
+(defn failback [v f] (if (nil? v) f v))
+
(defmonad parser-m
[m-result (fn m-result-parser [x] (fn [strn] (consumed x strn)))
m-bind (fn m-bind-parser [parser func]
@@ -22,6 +24,15 @@
m-zero (fn [strn] (failed))
+ m-plus (fn [& parsers]
+ (fn [strn]
+ (failback
+ (first
+ (drop-while failed?
+ (map #(% strn) parsers)))
+ (failed)
+ )))
+
]
)
@@ -45,14 +56,24 @@
(apply str x))
)
+(defmonadfn optional [parser]
+ (m-plus parser (m-result (failed))))
+
+;(defmonadfn <|> [& args]
+; (apply m-plus args))
+
+(defmacro <|> [& args]
+ (cons 'm-plus args)
+)
(defmonadfn body []
(domonad [x any-char
y any-char] (str y x))
)
(defmonadfn body2 []
- (m-seq [any-char any-char any-char])
+ (domonad [x (<|> (string "ciao") (string "ugo"))]
+ x)
)

0 comments on commit 12be90f

Please sign in to comment.