Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6784d57ea1
Fetching contributors…

Cannot retrieve contributors at this time

718 lines (624 sloc) 21.405 kb
(require 'monads)
(require 'utils)
(require 'cl)
(require 'defn)
(require 'functional)
(require 'multi-methods)
;;; Uniform Input Represention:
(defstruct buffer-input (buffer :read-only) (index :read-only))
(defun create-buffer-input (buf)
"Create an initialized buffer-input struct for the buffer BUF."
(make-buffer-input :buffer (get-buffer buf) :index 1))
(defun set-buffer-input-index (b i)
"Functionally modify the buffer-input B's index to I. Original structure
remains unmodified. If B is a regular buffer, it is promoted to a
((bufferp b) (make-buffer-input :buffer (get-buffer b) :index i))
((buffer-input-p b)
(buffer-input-buffer b)
:index i))))
(defun incr-buffer-input-index (b)
"Functionally increment a buffer input index."
((bufferp b) (incr-buffer-input-index (create-buffer-input b)))
((buffer-input-p b)
(let ((i (buffer-input-index b)))
(set-buffer-input-index b (+ i 1))))))
(defstruct stateful-input (input :read-only) (state :read-only))
(defun make-input-stateful (input &optional initial-state)
(make-stateful-input :input input :state initial-state))
(defun stateful-input-store/alist (s k v)
(make-stateful-input :input
(stateful-input-input s)
(alist>> (stateful-input-state s) k v)))
(defun stateful-input-fetch/alist (s k &optional or-value)
(alist-or (stateful-input-state s) k or-value))
(defun input-dispatcher (&rest args)
(let ((thing (car args)))
"Dispatch function for generic parser input methods."
((stringp thing) :string)
((listp thing) :list)
((buffer-input-p thing) :buffer-input)
((bufferp thing) :buffer)
((stateful-input-p thing) :stateful-input))))
;;; Define predicate methods for detecting empty inputs.
(defmulti input-empty? #'input-dispatcher "Returns t if an input is empty.")
(defunmethod input-empty? :string (s) (= (length s) 0))
(defunmethod input-empty? :list (l) (not l))
(defun point-max-of (buffer)
"Retrieve (point-max) for a BUFFER."
(with-current-buffer buffer
(defunmethod input-empty? :buffer-input (b)
(>= (buffer-input-index b) (point-max-of (buffer-input-buffer b))))
(defunmethod input-empty? :buffer (b)
(if (= 1 (point-max-of b)) t nil))
(defunmethod input-empty? :stateful-input (s)
(input-empty? (stateful-input-input s)))
(defalias 'input-emptyp #'input-empty?)
(defmulti input-first #'input-dispatcher "Get the first element of an input.")
(defunmethod input-first :string (s)
(if (> (length s) 0) (substring s 0 1)
(error "Can't take the first element of an empty string.")))
(defunmethod input-first :list (l)
(if l (car l) (error "Can't take the fist element of an empty list.")))
(defunmethod input-first :buffer (b)
(input-first (create-buffer-input b)))
(defunmethod input-first :buffer-input (bi)
(with-current-buffer (buffer-input-buffer bi)
(let ((i (buffer-input-index bi)))
(buffer-substring i (+ i 1)))))
(defunmethod input-first :stateful-input (s)
(input-first (stateful-input-input s)))
(defmulti input-rest #'input-dispatcher "Get an input representing subsequent elements of an input.")
(defunmethod input-rest :string (s)
(substring s 1))
(defunmethod input-rest :list (l)
(cdr l))
(defunmethod input-rest :buffer (b)
(input-rest (create-buffer-input b)))
(defunmethod input-rest :buffer-input (bi)
(incr-buffer-input-index bi))
(defunmethod input-rest :stateful-input (s)
(make-stateful-input :input
(input-rest (stateful-input-input s))
:state (stateful-input-state s)))
(defmulti store-uncurried #'input-dispatcher "Uncurried storage multimethod.")
(defunmethod store-uncurried :stateful-input (s k v)
(list (cons v (stateful-input-store/alist s k v))))
($ :list derives-from :non-stateful-input)
($ :buffer derives-from :non-stateful-input)
($ :string derives-from :non-stateful-input)
(defunmethod store-uncurried :non-stateful-input (s k v)
(store-uncurried (make-input-stateful s) k v))
(defun =>store (k v)
"Parser which stores the value V at K in the STATEFUL portion of the input."
(k v)
(lambda (s) (store-uncurried s k v))))
(defmulti fetch-uncurried #'input-dispatcher "Uncurried fetch multimethod.")
(defunmethod fetch-uncurried :stateful-input (s k &optional or-value)
(list (cons (stateful-input-fetch/alist s k or-value) s)))
(defunmethod fetch-uncurried :non-stateful-input (s k &optional or-value)
(fetch-uncurried (make-input-stateful s) k or-value))
(defun =>fetch (k &optional or-value)
"Parser which fetches the value at K from the parse state.
Returns NIL if no such value is present, OR-VALUE if not, which
defaults to nil."
(enclose (k or-value)
(lambda (s)
(fetch-uncurried s k or-value))))
(defun push-dispatcher (item input)
(input-dispatcher input))
(defmulti input-push #'push-dispatcher "Add an item to the front of an input")
(defunmethod input-push :string (item input) (concat (if (stringp item) item (format "%S" item)) input))
(defunmethod input-push :list (item input) (cons item input))
(defunmethod input-push :buffer (item input) (input-push item (create-buffer-input input)))
(defunmethod input-push :buffer-input (item input)
(warn "Pushing an input onto a buffer is non-functional.")
(with-current-buffer (buffer-input-buffer input)
(goto-char (buffer-input-index input))
(insert (if (stringp item) item (format "%S" item)))))
;;; Monadic Functions:
;;; Parsers start with =
;;; Functions returning parsers start with =>
;;; It is convenient to treat parsers as both values and functions. This
;;; form defines them as both.
(defmacro defun/var (name args &rest body)
"Simultaneously define a function and set NAME's SYMBOL-VALUE to that function."
(defun ,name ,args ,@body)
(defvar ,name nil ,@(if (stringp (car body)) (list (car body)) nil))
(setq ,name #',name)))
(defun/var =nil (input)
"The nil parser. The zero of the parser monad."
(defun/var =item (input)
"The one-item parser. Parse the first item from the input, regardless of what it is."
(unless (input-empty? input)
(list (cons (input-first input)
(input-rest input)))))
(defun/var =rest (input)
"Get the parser state itself. Useful for checking for the end of the input
within the monad."
(list (cons input input)))
(defun =>items (n)
"Produce the parser that gets the N first items from the parser
input or as many as possible when the input contains fewer
(lambda (input)
((acc '())
(input input)
(n n))
((or (= n 0)
(input-empty? input))
(list (cons (reverse acc) input)))
(recur (cons (input-first input) acc)
(input-rest input)
(- n 1))))))))
(defun parser-bind (parser =>parser)
"Produce a new parser which represents the parser produced by =>PARSER on the
monadic return values of PARSER."
(parser =>parser)
((acc '())
(rs (funcall parser input)))
(if (empty? rs) acc
(let ((pair (car rs))
(rest (cdr rs)))
(recur (append (funcall
(funcall =>parser (car pair))
(cdr pair)) acc)
(defun parser-return (&rest things)
"Produce a parser which leaves its input unmodified and which
monadically returns THING."
(lambda (input)
(mapcar (par #'cons input) things))))
(defun parser-plus (p1 p2)
"Parser monadic plus operation - returns the parser which
parses P1 and then P2, with the monadic return value of P2."
(parser-bind p1 (lambda (stub) p2)))
(defvar monad-parse
:m-return #'parser-return
:m-bind #'parser-bind
:m-zero =nil
:m-plus #'parser-plus)
"The (better) parser monad.")
(defun parse/first-result (parser input)
"Parse INPUT with PARSER and return the FIRST monadic return value."
(car (car (funcall parser input))))
;;; Now we can use monadic binding to build some parsers.
(defun =>items->string-unspecialized (n)
"Pull N items from the input (or fewer if fewer are in input)
and return the results concatenated into a string. Handles any
case with appropriate multimethod definitions."
(items-list <- (=>items n))
(m-return (reduce #'concat items-list)))))
(defun =>items->string (n)
"Pull N items from the input (or fewer if fewer are in input)
and return the results concatenated into a string. Efficiently
handles the string case."
(lambda (input)
(if (stringp input)
(let* ((in-len (length input))
(actual-n (min in-len n))
(result (substring input 0 actual-n))
(new-input (substring input actual-n)))
(list (cons result new-input)))
(=>items->string-unspecialized n)))))
(defun =>satisfies (fun)
"Parse one item IF it FUN is true for that item."
(item <- =item)
(if (funcall fun item)
(m-return item)
(defun =>list (parser)
"Wraps the monadic return value of PARSER into a LIST."
(monadic-do monad-parse
(item <- parser)
(m-return (list item)))))
(defun =>maybe (parser)
"Produces a parser that always succeeds. If parser succeeds,
the monadic return value comes from PARSER. Otherwise,
monadically returns NIL."
(lambda (input)
(let ((rs (funcall parser input)))
(if rs rs
(list (cons nil input)))))))
;;; The dreaded zero plus more combinator.
(defun zero-plus-more-step (substate parser)
"Apply PARSER to the CDR of substate. If it succeeds, cons the
result onto the list in the CAR of substate and indicate CONTINUE
for MAPCAR/DEAL. If PARSER on CDR of substate FAILS, then
reverse the CAR of SUBSTATE and return this value consed with the
last INPUT state."
(let* ((mrv (car substate))
(input (cdr substate))
(r (funcall parser input)))
(if r (list
(mapcar (lambda (subr)
(let ((r (car subr))
(rest (cdr subr)))
(cons (cons r mrv) rest)))
(list :terminate
(cons (reverse mrv) input)))))
(defun =>zero-plus-more (p)
"Produce a parser which parses P zero or more times and monadically
returns the results in a list."
((terminals nil)
(continuers (funcall (=>list p) input)))
(if (empty? continuers)
(if (empty? terminals)
(list (cons nil input))
(let* ((split-tbl
(mapcar/deal (par #'zero-plus-more-step p) continuers))
(new-continuers (alist split-tbl :continue))
(new-terminals (alist split-tbl :terminate)))
(recur (append terminals new-terminals)
(reduce #'append new-continuers))))))))
(defun =>zero-plus-more->string (p)
"Like =>ZERO-PLUS-MORE, but the monadic return is concatenated
into a string."
(=>reduce-concat (=>zero-plus-more p)))
(defun =>zero-or-more (p)
"Alias for =>ZERO-PLUS-MORE."
(=>zero-plus-more p))
(defun =>one-plus-more (p)
"Parse P at least once and then accumlate as many P as possible."
(monadic-do monad-parse
(first <- p)
(rest <- (=>zero-plus-more p))
(m-return (cons first rest)))))
(defun =>one-or-more (p)
"Alias for =>ZERO-PLUS-MORE."
(=>one-plus-more p))
(defun =>or2 (p1 p2)
"Produce a PARSER which succeeds on P1 or P2. Returns the
successful parser's value."
(p1 p2)
(lambda (input)
(let ((rs (funcall p1 input)))
(if rs rs
(funcall p2 input))))))
(defun =>or (&rest ps)
"Produce a parser which succeeds if one of PS is true."
(reduce #'=>or2 ps))
(defun =>and2 (p1 p2)
"Produce a parser which succeeds if P1 and P2 both succeed."
(defun =>and (&rest ps)
"Produces a parser if all PS succeed. Result is last parser's result."
(reduce #'=>and2 ps))
(defun =>not (p)
"Succeeds only if P fails. Returns one item."
(lambda (input)
(let ((rs (funcall p input)))
(if rs =nil
(defun =>reduce-concat (p)
"Return a new parser that reduces the result of P with concat."
(r <- p)
(m-return (reduce #'concat r))))
;;; We need to start writing
(defmacro defvar/fun (name lambda &optional doc)
(defvar ,name nil ,@(if doc (list doc) (list)))
(setq ,name ,lambda)
(defalias ',name ,name)))
(defun to-char (s)
"Convert s to the character code representing its first
(car (coerce s 'list)))
((low-chars "abcdefghijklmnopqrstuvwxyz")
(low-chars-list (coerce low-chars 'list))
(high-chars-list (coerce (upcase low-chars) 'list))
(both-list (append low-chars-list high-chars-list)))
(defvar/fun =alpha
(lambda (x) (in (to-char x) both-list)))
"Parse an alphabetical character.")
(defvar/fun =alpha-upper
(lambda (x) (in (to-char x) high-chars-list)))
"Parse an uppercast alphabetical character.")
(defvar/fun =alpha-lower
(lambda (x) (in (to-char x) low-chars-list)))
"Parse a lowercase alphabetical character"))
((digits (coerce "1234567890" 'list)))
(defvar/fun =digit
(lambda (x) (in (to-char x) digits)))))
(defun/var =input-type (input)
"Monadically return the type of the input being parsed."
(list (cons (input-dispatcher input) input)))
(defun =>string (&rest s)
"Produces a parser which, if the input is a string or buffer,
matches N characters matching the string S. If the input is a list, then it
succeeds only when the item is a string which matches S."
(if (= 1 (length s))
(lexical-let* ((s (car s))
(n (length s)))
(type <- =input-type)
(if (eq :list type)
(item <- =item)
(if (and (stringp item) (string= item s))
(m-return item)
(items <- (=>items->string n))
(if (string= items s) (m-return items)
(apply #'=>or (mapcar #'=>string s))))
(defun =>stringi (&rest s)
"Produces a parser which, if the input is a string or buffer,
matches N characters matching the string S. If the input is a list, then it
succeeds only when the item is a string which matches S."
(if (= 1 (length s))
(lexical-let* ((s (car s))
(n (length s)))
(type <- =input-type)
(if (eq :list type)
(item <- =item)
(if (and (stringp item) (stringi= item s))
(m-return item)
(items <- (=>items->string n))
(if (stringi= items s) (m-return items)
(apply #'=>or (mapcar #'=>stringi s))))
(defvar/fun =number-from-list
(item <- =item)
(if (numberp item) (m-return item) =nil))
"Parse a number from a list. This parser will always fail on
character based inputs.")
(defvar/fun =sign
(=>or (=>string "+") (=>string "-")))
(defvar/fun =string-of-digits
(=>one-plus-more =digit))
(defvar/fun =string-of-digits->string
(items <- =string-of-digits)
(if (empty? items) "" (reduce #'concat items)))))
(defvar/fun =dot (=>string "."))
;; (defvar/fun =number-char
;; (monadic-do
;; monad-parse
;; (sign <- (=>maybe =sign))
;; (pre <- =string-of-digits->string)
;; (dot <- (=>maybe =dot))
;; (rest <- =string-of-digits->string)
;; (m-return
;; (string-to-number
;; (let ((sign (if sign sign "")))
;; (if dot (concat sign pre dot rest)
;; (concat sign pre)))))))
(defvar/fun =number-char
(sign <- (=>maybe =sign))
(pre-decimal <- (=>maybe =string-of-digits->string))
(maybe-dot <- (=>maybe =dot))
((and (not maybe-dot)
(not pre-decimal))
((and maybe-dot
(rest <- (=>maybe =string-of-digits->string))
(if rest (string-to-number (concat pre-decimal "." rest))
(string-to-number pre-decimal)))))
((and pre-decimal (not maybe-dot))
(m-return (string-to-number pre-decimal)))
((and (not pre-decimal)
(post-decimal <- =string-of-digits->string)
(m-return (string-to-number (concat "." post-decimal)))))
(t =nil))))
(defvar/fun =number
(input-type <- =input-type)
(if (eq input-type :list)
(defmacro parser (&rest body)
"Create a parser by using the parser monad to sequence the
monadic values represented by the forms in BODY. Each form must
either be a monadic value or a binding form of the type (SYMBOL
<- EXPR), where the expression is a monadic value. SYMBOL is
bound to the monadic return value thereof in subsequent
`(monadic-do monad-parse
(defmacro defparser-fun (name args maybe-doc &rest body)
"Create a parser-producing function by evaluating the BODY
expressions as in a MONADIC-DO in the body of a function named
NAME with lexically bound arguments ARGS. If MAYBE-DOC is a string,
it is counted as the doc-string for the function."
`(lex-defun ,name ,args
,@(if (stringp maybe-doc) (list maybe-doc) nil)
,@(if (not (stringp maybe-doc)) (list maybe-doc) nil)
(defmacro defparser-val (name maybe-doc &rest body)
"Creates a parser value/function binding by evaluating the BODY
as if in a monadic-do form in the parser monad. If MAYBE-DOC is
a string, this is treated as the doc-string."
`(defvar/fun ,name
(parser ,@(if (not (stringp maybe-doc)) (list maybe-doc) nil)
,@(if (stringp maybe-doc) (list maybe-doc) nil)))
(defmacro defparser (name/args maybe-doc &rest body)
"Combines DEFPARSER-VAL and DEFPARSER-FUN in one easy to use,
Scheme-like defining form. If NAME/ARGS is a symbol, this
expression defines a parser as in DEFPARSER-VAL. If it is a list,
then the car of the list is taken to be the function name and the
CDR the arguments in a DEFPARSER-FUN expression."
(if (symbolp name/args) `(defparser-val ,name/args ,maybe-doc ,@body)
(let ((name (car name/args))
(args (cdr name/args)))
`(defparser-fun ,name ,args ,maybe-doc ,@body))))
(defparser (=>this-symbol s)
"Return a parser matching S only. Such a parser always fails
for string and buffer input."
(item <- =item)
(if (and (symbolp item) (eq item s))
(m-return item)
(coerce "~`!@#$%^&*()_-+={[}]|\\/?.,<>;:'\"" 'list)))
(defparser =punctuation
"Matches any punctuation mark."
(item <- =item)
(if (in (to-char item) punctuation)
(m-return item)
(defparser (=>equal what)
"Returns a parser which succeeds when an ITEM is EQUAL to WHAT."
(item <- =item)
(if (equal item what) (m-return item) =nil))
(defparser (=>eq what)
"Returns a parser which succeeds when an ITEM is EQ to WHAT."
(item <- =item)
(if (eq item what) (m-return item) =nil))
(defparser (=>n-equal n to)
"Returns a parser which succeeds when N ITEMS are equal to the list TO."
(items <- (=>items n))
(if (equal items to)
(m-return items)
(defun =>unparse (what)
"An unparser: put WHAT onto the input."
(lambda (input)
(list (cons what
(input-push what input))))))
(defun/var =peek (input)
"Return the next ITEM without removing it from the input."
(list (cons (input-first input) input)))
(defparser (=>items-upto predicate)
"Collect items until PREDICATE is true for one, which is left on the input."
(peek <- =peek)
(if (funcall predicate peek) (m-return nil)
(item <- =item)
(rest <- (=>items-upto predicate))
(m-return (cons item
(defparser (=>items-upto->string predicate)
"As =>ITEMS-UPTO but concatenates list of results."
(=>reduce-concat (=>items-upto predicate)))
(defun/var =input (input)
"Get the input state as it is."
(list (cons input input)))
(defun =>set-input (to)
"Set the input state to the value TO. Monadically return T."
(enclose (to)
(lambda (input)
(list (cons t to)))))
(defparser (=>do-to-input fun)
"Apply a transformation FUN directly to the parser INPUT state, replace the result."
(input <- =input)
(lexical-let ((new-input (funcall fun input)))
(=>set-input new-input)
(m-return new-input))))
(defparser (=>number equal-to)
"Parse an expression equal to the specific number EQUAL-TO. Fail otherwise."
(n <- =number)
(if (= n equal-to)
(m-return n)
;; (defmacro defparser-w/delim (nameish delim-parser &rest expressions)
;; "Defines a parser as in DEFPARSER where each term is separated by the DELIM-PARSER."
;; (with-gensyms
;; (d_)
;; `(lexical-let ((,d_ ,delim-parser))
;; (defparser ,nameish ,@(intersperse d_ expressions)))))
(defun =>alist>> (&rest args)
"Returns the parser which returns the ALIST created by passing ARGS to ALIST>>."
(parser-return (apply #'alist>> args)))
(defun =>. (f &rest args)
"Return F on ARGS."
(parser-return (apply f args)))
(defalias '=> #'parser-return)
(defun parser-return. (f &rest args)
"Return F on ARGS."
(parser-return (apply f args)))
(provide 'better-monad-parse)
Jump to Line
Something went wrong with that request. Please try again.