Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

many and many1

  • Loading branch information...
commit e3ce7cfea84f38bb66c3f6efed80c1bff1d9de1d 1 parent 12be90f
@mmikulicic authored
Showing with 21 additions and 4 deletions.
  1. +21 −4 src/clarsec.clj
View
25 src/clarsec.clj
@@ -43,6 +43,12 @@
(. strn (substring 1))))
)
+(defn char-test [pred]
+ (domonad parser-m
+ [c any-char
+ :when (pred c)]
+ (str c)))
+
(defmonadfn is-char [c]
(char-test (partial = c)))
@@ -59,8 +65,17 @@
(defmonadfn optional [parser]
(m-plus parser (m-result (failed))))
-;(defmonadfn <|> [& args]
-; (apply m-plus args))
+
+(defmonadfn many1 [parser]
+ (domonad [a parser
+ as (optional (many1 parser))]
+ (concat [a] (if (failed? as) [] as)))
+ )
+
+(defmonadfn many [parser]
+ (domonad [x (optional (many1 parser))]
+ (if (failed? x) [] x))
+)
(defmacro <|> [& args]
(cons 'm-plus args)
@@ -72,8 +87,10 @@
)
(defmonadfn body2 []
- (domonad [x (<|> (string "ciao") (string "ugo"))]
- x)
+ (domonad [x (<|> (string "ciao") (string "ugo"))
+ y (optional (is-char \space))
+ z (many (string "mondo"))]
+ z)
)
Please sign in to comment.
Something went wrong with that request. Please try again.