branch: elpa/parseclj
commit 07739abe2cd126530d903ea4aa5dcdf5ba470277
Author: Arne Brasseur <[email protected]>
Commit: Arne Brasseur <[email protected]>
support keywords
---
clj-lex-test.el | 17 ++++++++++++++++-
clj-lex.el | 17 +++++++++++++++++
clj-parse-test.el | 7 ++++++-
clj-parse.el | 2 ++
4 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/clj-lex-test.el b/clj-lex-test.el
index 426698303a..baea9dcd0f 100644
--- a/clj-lex-test.el
+++ b/clj-lex-test.el
@@ -94,7 +94,22 @@
(with-temp-buffer
(insert "\"\\u0078\\o170\"")
(goto-char 1)
- (should (equal (clj-lex-next) (clj-lex-token :string "\"\\u0078\\o170\""
1)))))
+ (should (equal (clj-lex-next) (clj-lex-token :string "\"\\u0078\\o170\""
1))))
+
+ (with-temp-buffer
+ (insert ":hello-world")
+ (goto-char 1)
+ (should (equal (clj-lex-next) (clj-lex-token :keyword ":hello-world" 1))))
+
+ (with-temp-buffer
+ (insert "::hello-world")
+ (goto-char 1)
+ (should (equal (clj-lex-next) (clj-lex-token :keyword "::hello-world" 1))))
+
+ (with-temp-buffer
+ (insert ":::hello-world")
+ (goto-char 1)
+ (should (equal (clj-lex-next) (clj-lex-token :lex-error ":::" 1
'error-type :invalid-keyword)))))
(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 765cc578f9..94874aa6d0 100644
--- a/clj-lex.el
+++ b/clj-lex.el
@@ -146,6 +146,20 @@
(right-char)
(clj-lex-token :character (buffer-substring-no-properties pos (point))
pos)))))
+(defun clj-lex-keyword ()
+ (let ((pos (point)))
+ (right-char)
+ (when (equal (char-after (point)) ?:)
+ (right-char))
+ (if (clj-lex-symbol-start? (char-after (point)))
+ (progn
+ (while (clj-lex-symbol-rest? (char-after (point)))
+ (right-char))
+ (clj-lex-token :keyword (buffer-substring-no-properties pos (point))
pos))
+ (progn
+ (right-char)
+ (clj-lex-token :lex-error (buffer-substring-no-properties pos (point))
pos 'error-type :invalid-keyword)))))
+
(defun clj-lex-next ()
(if (clj-lex-at-eof?)
(clj-lex-token :eof nil (point))
@@ -175,6 +189,9 @@
((equal char ?\\)
(clj-lex-character))
+ ((equal char ?:)
+ (clj-lex-keyword))
+
":("))))
(provide 'clj-lex)
diff --git a/clj-parse-test.el b/clj-parse-test.el
index 9636e964d6..d026bbc172 100644
--- a/clj-parse-test.el
+++ b/clj-parse-test.el
@@ -74,7 +74,12 @@
(with-temp-buffer
(insert "\"\\u0078 \\o171\"")
(goto-char 1)
- (should (equal (clj-parse) '("x y")))))
+ (should (equal (clj-parse) '("x y"))))
+
+ (with-temp-buffer
+ (insert ":foo-bar")
+ (goto-char 1)
+ (should (equal (clj-parse) '(:foo-bar)))))
(provide 'clj-parse-test)
diff --git a/clj-parse.el b/clj-parse.el
index 004901090b..6cf49aac9e 100644
--- a/clj-parse.el
+++ b/clj-parse.el
@@ -35,6 +35,7 @@
:true
:false
:symbol
+ :keyword
:string
:character)
"Tokens that represent leaf nodes in the AST.")
@@ -87,6 +88,7 @@
(:true (cons t stack))
(:false (cons nil stack))
(:symbol (cons (intern (cdr (assq 'form token))) stack))
+ (:keyword (cons (intern (cdr (assq 'form token))) stack))
(:string (cons (clj-parse-string (cdr (assq 'form token))) stack))
(:character (cons (clj-parse-character (cdr (assq 'form token))) stack))))