Make elisp parser def* based

This commit is contained in:
Greg Sexton 2014-08-10 15:10:05 +01:00
parent 0f90c2a96b
commit 29618c96de
2 changed files with 27 additions and 14 deletions

View File

@ -43,8 +43,19 @@
:type 'hook :type 'hook
:group 'origami) :group 'origami)
(defun origami-pair (start children end create)
;; TODO: make this a macro so I don't have to pass in the thunk?
"CHILDREN should be a zero-arg lambda -- a thunk -- returning a
parser to allow for recursive nesting of a parser. CREATE is a
function that should build state taking the beginning, end and
children of the pair."
(parser-do (begin <- start)
(children <- (funcall children))
(end <- end)
(parser-return (funcall create begin end children))))
(defun origami-c-style-parser (create) (defun origami-c-style-parser (create)
(let ((pair (parser-paired (parser-char "{") (let ((pair (origami-pair (parser-char "{")
(lambda () (origami-c-style-parser create)) (lambda () (origami-c-style-parser create))
(parser-char "}") (parser-char "}")
create))) create)))
@ -54,9 +65,9 @@
(parser-1? pair)) (parser-1? pair))
pair)))) pair))))
(defun origami-elisp-parser (create) (defun origami-paren-parser (create)
(let ((pair (parser-paired (parser-char "(") (let ((pair (origami-pair (parser-char "(")
(lambda () (origami-elisp-parser create)) (lambda () (origami-paren-parser create))
(parser-char ")") (parser-char ")")
create))) create)))
(parser-0+ (parser-conj (parser-0+ (parser-conj
@ -65,6 +76,18 @@
(parser-1? pair)) (parser-1? pair))
pair)))) pair))))
(defun origami-elisp-parser (create)
(let ((def-regex "(def\\w*\\s-*\\(\\s_\\|\\w\\)*"))
(let ((pair (origami-pair (parser-regex def-regex)
(lambda () (origami-paren-parser (lambda (&rest _) nil)))
(parser-char ")")
create)))
(parser-0+ (parser-conj
(parser-do
(parser-drop-until-regex (concat def-regex "\\|)"))
(parser-1? pair))
pair)))))
(provide 'origami-parsers) (provide 'origami-parsers)
;;; parser.el ends here ;;; parser.el ends here

View File

@ -186,16 +186,6 @@ recursion. We fail if we don't consume something."
nil nil
(parser-run (parser-item) content)))) (parser-run (parser-item) content))))
(defun parser-paired (start children end create)
;; TODO: make this a macro so I don't have to pass in the thunk?
;; TODO: move this to origami-parsers?
"CHILDREN should be a zero-arg lambda -- a thunk -- returning a
parser to allow for recursive nesting of a parser."
(parser-do (begin <- start)
(children <- (funcall children))
(end <- end)
(parser-return (funcall create begin end children))))
(provide 'parser) (provide 'parser)
;;; parser.el ends here ;;; parser.el ends here