From 29618c96def954e0f852eabc4d6cf6bfa8eda810 Mon Sep 17 00:00:00 2001 From: Greg Sexton Date: Sun, 10 Aug 2014 15:10:05 +0100 Subject: [PATCH] Make elisp parser def* based --- origami-parsers.el | 31 +++++++++++++++++++++++++++---- parser.el | 10 ---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/origami-parsers.el b/origami-parsers.el index 28252ad..0521f57 100644 --- a/origami-parsers.el +++ b/origami-parsers.el @@ -43,8 +43,19 @@ :type 'hook :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) - (let ((pair (parser-paired (parser-char "{") + (let ((pair (origami-pair (parser-char "{") (lambda () (origami-c-style-parser create)) (parser-char "}") create))) @@ -54,9 +65,9 @@ (parser-1? pair)) pair)))) -(defun origami-elisp-parser (create) - (let ((pair (parser-paired (parser-char "(") - (lambda () (origami-elisp-parser create)) +(defun origami-paren-parser (create) + (let ((pair (origami-pair (parser-char "(") + (lambda () (origami-paren-parser create)) (parser-char ")") create))) (parser-0+ (parser-conj @@ -65,6 +76,18 @@ (parser-1? 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) ;;; parser.el ends here diff --git a/parser.el b/parser.el index 3b739f9..b24bf21 100644 --- a/parser.el +++ b/parser.el @@ -186,16 +186,6 @@ recursion. We fail if we don't consume something." nil (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) ;;; parser.el ends here