Skip to content

Instantly share code, notes, and snippets.

@aboekhoff
Created June 28, 2010 09:23
Show Gist options
  • Save aboekhoff/455628 to your computer and use it in GitHub Desktop.
Save aboekhoff/455628 to your computer and use it in GitHub Desktop.
(defmonad
:name :parser
:do parsing
:bind (fn [mv f]
(fn [s]
(when-let [[v s*] (mv s)]
((f v) s*))))
:return (fn [v] (fn [s] [v s]))
:zero (fn [s] nil)
:plus (fn [a b] (fn [s] (or (a s) (b s))))
:modules [base state maybe])
;;;; snip
(defparser identifier
a <- ident-start
b <- (zero-or-more (<> ident-start ident-cont))
c <- spacing
(return (symbol (apply str (cons a b)))))
;; string literals
(def escape-map
{\\ \\
\r \return
\n \newline
\f \formfeed
\t \tab
\" \"})
(defparser escape-char
a <- BACKSLASH
b <- (any-of "\"\\bntfr")
(return (escape-map b)))
(defparser string
a <- QUOTE
b <- (zero-or-more (<!> QUOTE (<> escape-char any)))
c <- QUOTE
(return (apply str b)))
(defparser (not-followed-by A B)
a <- A
s <- get-state
:when-not (B s)
(return a))
(defn delimited-by
[open close between]
(fmap second (m-seq open between close)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment