branch: elpa/parseclj
commit da1929be0bf6b1ad4ed663a9957feb4dddf20663
Author: Arne Brasseur <[email protected]>
Commit: Arne Brasseur <[email protected]>
Add vector support
---
clj-lex-test.el | 9 ++++++++-
clj-lex.el | 8 ++++++++
clj-parse-test.el | 7 ++++++-
clj-parse.el | 15 +++++++++------
4 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/clj-lex-test.el b/clj-lex-test.el
index baea9dcd0f..8da315ca50 100644
--- a/clj-lex-test.el
+++ b/clj-lex-test.el
@@ -109,7 +109,14 @@
(with-temp-buffer
(insert ":::hello-world")
(goto-char 1)
- (should (equal (clj-lex-next) (clj-lex-token :lex-error ":::" 1
'error-type :invalid-keyword)))))
+ (should (equal (clj-lex-next) (clj-lex-token :lex-error ":::" 1
'error-type :invalid-keyword))))
+
+ (with-temp-buffer
+ (insert "[123]")
+ (goto-char 1)
+ (should (equal (clj-lex-next) (clj-lex-token :lbracket "[" 1)))
+ (should (equal (clj-lex-next) (clj-lex-token :number "123" 2)))
+ (should (equal (clj-lex-next) (clj-lex-token :rbracket "]" 5)))))
(ert-deftest clj-lex-test-at-number? ()
(dolist (str '("123" ".9" "+1" "0" "-456"))
diff --git a/clj-lex.el b/clj-lex.el
index 94874aa6d0..377e8c766b 100644
--- a/clj-lex.el
+++ b/clj-lex.el
@@ -177,6 +177,14 @@
(right-char)
(clj-lex-token :rparen ")" pos))
+ ((equal char ?\[)
+ (right-char)
+ (clj-lex-token :lbracket "[" pos))
+
+ ((equal char ?\])
+ (right-char)
+ (clj-lex-token :rbracket "]" pos))
+
((clj-lex-at-number?)
(clj-lex-number))
diff --git a/clj-parse-test.el b/clj-parse-test.el
index d026bbc172..8f2869cd06 100644
--- a/clj-parse-test.el
+++ b/clj-parse-test.el
@@ -79,7 +79,12 @@
(with-temp-buffer
(insert ":foo-bar")
(goto-char 1)
- (should (equal (clj-parse) '(:foo-bar)))))
+ (should (equal (clj-parse) '(:foo-bar))))
+
+ (with-temp-buffer
+ (insert "[123]")
+ (goto-char 1)
+ (should (equal (clj-parse) '([123])))))
(provide 'clj-parse-test)
diff --git a/clj-parse.el b/clj-parse.el
index 6cf49aac9e..7a12152357 100644
--- a/clj-parse.el
+++ b/clj-parse.el
@@ -97,7 +97,8 @@
(cl-case type
(:whitespace :ws)
(:number coll)
- (:list (-butlast (cdr coll))))
+ (:list (-butlast (cdr coll)))
+ (:vector (apply #'vector (-butlast (cdr coll)))))
stack))
;; TODO move this to clj-lex
@@ -105,15 +106,15 @@
(and (listp token)
(cdr (assq 'type token))))
-(defun clj-parse--reduce-list (stack reducN)
+(defun clj-parse--reduce-coll (stack open-token coll-type reducN)
(let ((coll nil))
(while (and stack
- (not (eq (clj-parse--token-type (car stack)) :lparen)))
+ (not (eq (clj-parse--token-type (car stack)) open-token)))
(push (pop stack) coll))
- (if (eq (clj-parse--token-type (car stack)) :lparen)
+ (if (eq (clj-parse--token-type (car stack)) open-token)
(progn
(push (pop stack) coll)
- (funcall reduceN stack :list coll))
+ (funcall reduceN stack coll-type coll))
;; Unwound the stack without finding a matching paren: return the
original stack
(reverse list))))
@@ -132,7 +133,9 @@
(cons token stack)))
(cl-case (clj-parse--token-type (car stack))
- (:rparen (setf stack (clj-parse--reduce-list stack reduceN))))
+ (:rparen (setf stack (clj-parse--reduce-coll stack :lparen :list
reduceN)))
+ (:rbracket (setf stack (clj-parse--reduce-coll stack :lbracket :vector
reduceN))))
+
(setq token (clj-lex-next)))