Make parser lookup major-mode derived
This commit is contained in:
parent
f9858348ff
commit
0f90c2a96b
70
origami-parsers.el
Normal file
70
origami-parsers.el
Normal file
@ -0,0 +1,70 @@
|
||||
;;; 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:
|
||||
|
||||
(require 'parser)
|
||||
|
||||
(defcustom origami-parser-alist
|
||||
'((java-mode . origami-c-style-parser)
|
||||
(c-mode . origami-c-style-parser)
|
||||
(c++-mode . origami-c-style-parser)
|
||||
(emacs-lisp-mode . origami-elisp-parser)
|
||||
(lisp-interaction-mode . origami-elisp-parser))
|
||||
"alist mapping major-mode to parser function."
|
||||
:type 'hook
|
||||
:group 'origami)
|
||||
|
||||
(defun origami-c-style-parser (create)
|
||||
(let ((pair (parser-paired (parser-char "{")
|
||||
(lambda () (origami-c-style-parser create))
|
||||
(parser-char "}")
|
||||
create)))
|
||||
(parser-0+ (parser-conj
|
||||
(parser-do
|
||||
(parser-drop-until-regex "[{}]")
|
||||
(parser-1? pair))
|
||||
pair))))
|
||||
|
||||
(defun origami-elisp-parser (create)
|
||||
(let ((pair (parser-paired (parser-char "(")
|
||||
(lambda () (origami-elisp-parser create))
|
||||
(parser-char ")")
|
||||
create)))
|
||||
(parser-0+ (parser-conj
|
||||
(parser-do
|
||||
(parser-drop-until-regex "[()]")
|
||||
(parser-1? pair))
|
||||
pair))))
|
||||
|
||||
(provide 'origami-parsers)
|
||||
|
||||
;;; parser.el ends here
|
31
origami.el
31
origami.el
@ -36,6 +36,7 @@
|
||||
(require 's)
|
||||
(require 'cl)
|
||||
(require 'parser)
|
||||
(require 'origami-parsers)
|
||||
|
||||
;;; overlay manipulation
|
||||
|
||||
@ -65,7 +66,7 @@
|
||||
(origami-fold-postorder-each node 'origami-show-node-overlay))
|
||||
|
||||
(defun origami-change-overlay-from-fold-node-fn (old new)
|
||||
(if (origami-fold-open-p new)
|
||||
(if (origami-fold-open? new)
|
||||
(origami-show-node-overlay old)
|
||||
(origami-hide-node-overlay new)))
|
||||
|
||||
@ -120,7 +121,7 @@
|
||||
(aref node 1)
|
||||
(overlay-end (origami-fold-data node)))))
|
||||
|
||||
(defun origami-fold-open-p (node) (when node (aref node 2)))
|
||||
(defun origami-fold-open? (node) (when node (aref node 2)))
|
||||
|
||||
(defun origami-fold-open-set (node value)
|
||||
(when node
|
||||
@ -138,7 +139,7 @@
|
||||
(when node
|
||||
(origami-fold-node-raw (origami-fold-beg node)
|
||||
(origami-fold-end node)
|
||||
(origami-fold-open-p node)
|
||||
(origami-fold-open? node)
|
||||
children
|
||||
(origami-fold-data node))))
|
||||
|
||||
@ -149,7 +150,7 @@
|
||||
(equal (origami-fold-end a) (origami-fold-end b))))
|
||||
|
||||
(defun origami-fold-state-equal (a b)
|
||||
(equal (origami-fold-open-p a) (origami-fold-open-p b)))
|
||||
(equal (origami-fold-open? a) (origami-fold-open? b)))
|
||||
|
||||
(defun origami-fold-replace-child (node old new)
|
||||
(origami-fold-children-set node
|
||||
@ -291,7 +292,7 @@ was last built."
|
||||
|
||||
(defun origami-was-previously-open? (tree beg end)
|
||||
(-if-let (node (-last-item (origami-fold-find-path-with-range tree beg end)))
|
||||
(origami-fold-open-p node)
|
||||
(origami-fold-open? node)
|
||||
t))
|
||||
|
||||
(defun origami-build-tree (buffer parser)
|
||||
@ -303,26 +304,16 @@ was last built."
|
||||
car
|
||||
origami-fold-root-node)))))
|
||||
|
||||
(defun origami-test-parser (create)
|
||||
(let ((pair (parser-paired (parser-char "{")
|
||||
(lambda () (origami-test-parser create))
|
||||
(parser-char "}")
|
||||
create)))
|
||||
(parser-0+ (parser-conj
|
||||
(parser-do
|
||||
(parser-drop-until-regex "[{}]")
|
||||
(parser-1? pair))
|
||||
pair))))
|
||||
|
||||
(defun origami-get-parser (buffer)
|
||||
;; TODO: remove hardcoding!
|
||||
(let ((create (lambda (beg end children)
|
||||
(origami-fold-node beg end
|
||||
(origami-was-previously-open? (origami-get-cached-tree buffer) beg end)
|
||||
buffer
|
||||
children
|
||||
(origami-get-cached-tree buffer)))))
|
||||
(origami-test-parser create)))
|
||||
(-when-let (parser-gen (cdr (assoc (buffer-local-value 'major-mode buffer)
|
||||
origami-parser-alist)))
|
||||
(funcall parser-gen create))))
|
||||
|
||||
(defun origami-get-fold-tree (buffer)
|
||||
"Facade. Build the tree if it hasn't already been built
|
||||
@ -398,7 +389,7 @@ as to ensure seeing where POINT is."
|
||||
(origami-apply-new-tree buffer tree (origami-fold-assoc
|
||||
path (lambda (node)
|
||||
(origami-fold-open-set
|
||||
node (not (origami-fold-open-p
|
||||
node (not (origami-fold-open?
|
||||
(-last-item path))))))))))
|
||||
|
||||
(defun origami-forward-toggle-node (buffer point)
|
||||
@ -418,7 +409,7 @@ as to ensure seeing where POINT is."
|
||||
(origami-apply-new-tree buffer tree (origami-fold-assoc
|
||||
path (lambda (node)
|
||||
(origami-fold-open-set
|
||||
node (not (origami-fold-open-p
|
||||
node (not (origami-fold-open?
|
||||
(-last-item path)))))))))))))
|
||||
|
||||
(defun origami-open-all-nodes (buffer)
|
||||
|
@ -188,6 +188,7 @@ recursion. We fail if we don't consume something."
|
||||
|
||||
(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)
|
||||
|
Loading…
Reference in New Issue
Block a user