origami.el/origami-parsers.el

218 lines
11 KiB
EmacsLisp
Raw Normal View History

2014-08-09 23:02:37 +10:00
;;; origami-parsers.el --- Collection of parsers -*- lexical-binding: t -*-
;; Author: Greg Sexton <gregsexton@gmail.com>
;; Version: 1.0
;; Keywords: parsers
;; URL: https://github.com/gregsexton/
;; The MIT License (MIT)
;; Copyright (c) 2014 Greg Sexton
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;;; Commentary:
;;; Code:
2014-11-16 03:38:50 +11:00
(require 'cl)
2015-04-09 06:31:14 +10:00
(require 'dash)
2014-08-09 23:02:37 +10:00
(defcustom origami-parser-alist
2015-04-09 06:31:14 +10:00
'((java-mode . origami-java-parser)
2014-08-09 23:02:37 +10:00
(c-mode . origami-c-style-parser)
(c++-mode . origami-c-style-parser)
2014-11-23 08:57:01 +11:00
(perl-mode . origami-c-style-parser)
2014-11-01 21:06:20 +11:00
(cperl-mode . origami-c-style-parser)
2014-11-23 08:57:01 +11:00
(js-mode . origami-c-style-parser)
(js2-mode . origami-c-style-parser)
(js3-mode . origami-c-style-parser)
2015-04-26 00:20:06 +10:00
(go-mode . origami-c-style-parser)
(php-mode . origami-c-style-parser)
2014-08-09 23:02:37 +10:00
(emacs-lisp-mode . origami-elisp-parser)
2014-08-25 01:53:46 +10:00
(lisp-interaction-mode . origami-elisp-parser)
2014-08-26 03:53:52 +10:00
(clojure-mode . origami-clj-parser))
2014-08-09 23:02:37 +10:00
"alist mapping major-mode to parser function."
:type 'hook
:group 'origami)
(defun origami-get-positions (content regex)
2015-04-09 06:31:14 +10:00
"Returns a list of positions where REGEX matches in CONTENT. A
position is a cons cell of the character and the numerical
position in the CONTENT."
(with-temp-buffer
(insert content)
2014-11-17 01:12:06 +11:00
(goto-char (point-min))
(let (acc)
(while (re-search-forward regex nil t)
2014-11-16 08:15:28 +11:00
(let ((match (match-string 0)))
(setq acc (cons (cons match (- (point) (length match)))
acc))))
(reverse acc))))
2015-04-26 20:40:34 +10:00
;;; TODO: support tabs
(defun origami-indent-parser (create)
(cl-labels ((lines (string)
(-> string
(origami-get-positions ".*?\r?\n")
(->> (-filter (lambda (line)
(not (s-blank? (s-trim (car line)))))))))
(annotate-levels (lines)
(-map (lambda (line)
(let ((beg (cdr line))
(end (+ (cdr line) (length (car line)) -1)))
(vector (length (car (s-match "^ *" (car line))))
beg
end
(- end beg))))
lines))
(indent (line) (aref line 0))
(beg (line) (aref line 1))
(end (line) (aref line 2))
(offset (line) (aref line 3))
(collapse-same-level (levels)
(reverse (-reduce-from (lambda (acc level)
(if (= (indent level) (indent (car acc)))
(cons (vector (indent (car acc))
(beg (car acc))
(end level)
(offset (car acc)))
(cdr acc))
(cons level acc)))
(list (car levels))
(cdr levels))))
(create-tree (levels)
(if (null levels)
levels
(let ((curr-indent (indent (car levels))))
(->> levels
(-partition-by (lambda (l) (= (indent l) curr-indent)))
(-partition-all 2)
(-map (lambda (x) (cons (caar x) (create-tree (cadr x)))))))))
(build-nodes (tree)
(if (null tree) (cons 0 nil)
;; complexity here is due to having to find the end of the children so that the
;; parent encompasses them
(-reduce-r-from (lambda (nodes acc)
(destructuring-bind (children-end . children) (build-nodes (cdr nodes))
(let ((this-end (max children-end (end (car nodes)))))
(cons (max this-end (car acc))
(cons (funcall create
(beg (car nodes))
this-end
(offset (car nodes))
children)
(cdr acc))))))
'(0 . nil)
tree))))
(lambda (content)
(-> content
lines
annotate-levels
collapse-same-level
create-tree
build-nodes
cdr))))
2014-11-16 03:22:45 +11:00
(defun origami-build-pair-tree (create open close positions)
2014-11-16 03:38:50 +11:00
(cl-labels ((build (positions)
;; this is so horrible, but fast
(let (acc beg (should-continue t))
(while (and should-continue positions)
(cond ((equal (caar positions) open)
(if beg ;go down a level
(let* ((res (build positions))
(new-pos (car res))
(children (cdr res)))
(setq positions (cdr new-pos))
2014-11-16 08:15:28 +11:00
(setq acc (cons (funcall create beg (cdar new-pos) (length open) children)
acc))
2014-11-16 03:38:50 +11:00
(setq beg nil))
;; begin a new pair
(setq beg (cdar positions))
(setq positions (cdr positions))))
((equal (caar positions) close)
(if beg
(progn ;close with no children
2014-11-16 08:15:28 +11:00
(setq acc (cons (funcall create beg (cdar positions) (length close) nil)
acc))
2014-11-16 03:38:50 +11:00
(setq positions (cdr positions))
(setq beg nil))
(setq should-continue nil)))))
(cons positions (reverse acc)))))
(cdr (build positions))))
2014-08-11 00:10:05 +10:00
2015-04-09 06:31:14 +10:00
;;; TODO: tag these nodes? have ability to manipulate nodes that are
;;; tagged? in a scoped fashion?
(defun origami-javadoc-parser (create)
(lambda (content)
(let ((positions (->> (origami-get-positions content "/\\*\\*\\|\\*/")
(-filter (lambda (position)
(eq (get-text-property 0 'face (car position))
'font-lock-doc-face))))))
(origami-build-pair-tree create "/**" "*/" positions))))
2014-08-09 23:02:37 +10:00
(defun origami-c-style-parser (create)
(lambda (content)
2015-04-09 06:31:14 +10:00
(let ((positions (->> (origami-get-positions content "[{}]")
(remove-if (lambda (position)
(let ((face (get-text-property 0 'face (car position))))
(-any? (lambda (f)
(memq f '(font-lock-doc-face
font-lock-comment-face
font-lock-string-face)))
(if (listp face) face (list face)))))))))
2014-11-16 03:38:50 +11:00
(origami-build-pair-tree create "{" "}" positions))))
2014-08-09 23:02:37 +10:00
2015-04-09 06:31:14 +10:00
(defun origami-java-parser (create)
(let ((c-style (origami-c-style-parser create))
(javadoc (origami-javadoc-parser create)))
(lambda (content)
(origami-fold-children
(origami-fold-shallow-merge (origami-fold-root-node (funcall c-style content))
(origami-fold-root-node (funcall javadoc content)))))))
2014-08-26 03:53:52 +10:00
(defun origami-lisp-parser (create regex)
(lambda (content)
(with-temp-buffer
(insert content)
2014-11-17 01:12:06 +11:00
(goto-char (point-min))
(beginning-of-defun -1)
(let (beg end offset acc)
(while (< (point) (point-max))
(setq beg (point))
2014-08-26 03:53:52 +10:00
(search-forward-regexp regex nil t)
(setq offset (- (point) beg))
(end-of-defun)
2014-11-16 08:15:28 +11:00
(backward-char) ;move point to one after the last paren
(setq end (1- (point))) ;don't include the last paren in the fold
2014-08-11 01:54:21 +10:00
(when (> offset 0)
(setq acc (cons (funcall create beg end offset nil) acc)))
(beginning-of-defun -1))
(reverse acc)))))
2014-08-11 00:10:05 +10:00
2014-08-26 03:53:52 +10:00
(defun origami-elisp-parser (create)
(origami-lisp-parser create "(def\\w*\\s-*\\(\\s_\\|\\w\\|[?!]\\)*\\([ \\t]*(.*?)\\)?"))
(defun origami-clj-parser (create)
(origami-lisp-parser create "(def\\(\\w\\|-\\)*\\s-*\\(\\s_\\|\\w\\|[?!]\\)*\\([ \\t]*\\[.*?\\]\\)?"))
2014-08-09 23:02:37 +10:00
(provide 'origami-parsers)
;;; origami-parsers.el ends here