-
Notifications
You must be signed in to change notification settings - Fork 2
/
walkclj.el
159 lines (135 loc) · 5.03 KB
/
walkclj.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
;;; walkclj.el --- Manipulate Clojure parse trees -*- lexical-binding: t -*-
;;
;; Filename: walkclj.el
;; Author: Arne Brasseur
;; Maintainer: Arne Brasseur
;; Created: Mi Jul 18 09:39:10 2018 (+0200)
;; Version: 0.2.0
;; Package-Requires: ((emacs "25") (parseclj "0.1.0") (treepy "0.1.0") (a "1.0.0"))
;; URL: https://github.com/plexus/walkclj
;; Keywords: languages
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; A complementary library to parseclj, providing ways to traverse parse trees
;; as if they were s-expressions, as well as providing higher level
;; interpretations of parsed data.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Copyright (C) 2018 Arne Brasseur
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'treepy)
(require 'parseclj)
(require 'cl-lib)
(require 'a)
(eval-and-compile
(defvar walkclj-function-names '(ffirst
first
last
list-p
list?
meta
name
second
symbol?
symbol-p?
unwrap-meta)
"Names that are recognized inside walkclj-do and rewritten to be
walkclj- prefixed.")
(defun walkclj--update-predicate-suffix (sym)
"Change a symbol SYM that ends in `?' in one that ends in -p."
(let ((s (symbol-name sym)))
(if (equal "?" (substring s (1- (length s))) )
(intern (concat (substring s 0 (1- (length s))) "-p"))
sym))))
(defmacro walkclj-do (&rest body)
"Evaluate BODY with auto-prefixing.
All symbols in the BODY that have a corresponding walkclj-
prefixed version are replaced by their prefixed version."
(cons 'progn
(treepy-postwalk (lambda (x)
(cond
((member x walkclj-function-names)
(intern (concat "walkclj-" (symbol-name (walkclj--update-predicate-suffix x)))))
((and (listp x)
(keywordp (car x)))
(cl-list* 'a-get (cadr x) (car x) (cddr x)))
(t x)))
body)))
(defun walkclj-meta (form)
"Return the metadata of FORM."
(if (eq :with-meta (parseclj-ast-node-type form))
(car (a-get form :children))
form))
(defun walkclj-unwrap-meta (form)
"Strip FORM of its metadata.
Nodes with metadata as parsed with a wrapping :with-meta node,
return the inner node if this is the case."
(if (eq :with-meta (parseclj-ast-node-type form))
(cadr (a-get form :children))
form))
(defun walkclj-first (form)
"Return the first child of FORM."
(if-let (children (a-get form :children))
(car children)
nil))
(defun walkclj-second (form)
"Return the second child of FORM."
(if-let (children (a-get form :children))
(cadr children)
nil))
(defun walkclj-ffirst (form)
"Same as (first (first FORM))."
(walkclj-do (first (first form))))
(defun walkclj-list-p (form)
"Does FORM represent a :list node?"
(eq (parseclj-ast-node-type form) :list))
(defalias 'walkclj-list? 'walkclj-list-p)
(defun walkclj-symbol-p (form)
"Does FORM represent a :symbol node?"
(eq (parseclj-ast-node-type form) :symbol))
(defalias 'walkclj-symbol? 'walkclj-symbol-p)
(defun walkclj-name (form)
"Return the name of FORM.
FORM has to be a string, symbol, or keyword."
(cl-case (parseclj-ast-node-type form)
(:string (a-get form :value))
(:symbol (a-get form :form))
(:keyword (substring (a-get form :form) 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun walkclj-current-ns ()
"Return the Clojure namespace name of the current buffer."
(ignore-errors
(save-excursion
(goto-char 1)
(let ((ns-form (parseclj-parse-clojure :read-one t)))
(walkclj-do
(when (and (list? ns-form) (eq 'ns (a-get (first ns-form) :value)))
(name (unwrap-meta (second ns-form)))))))))
(provide 'walkclj)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; walkclj.el ends here