branch: elpa/parseclj
commit 0afb8c5f09922165e72b3ddc807e63057c246dea
Author: Daniel Barreto <[email protected]>
Commit: Daniel Barreto <[email protected]>
Add `parseclj-lex-error-token` helper
---
parseclj-lex.el | 25 +++++++++++++++++--------
test/parseclj-lex-test.el | 2 +-
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/parseclj-lex.el b/parseclj-lex.el
index ea8b65783c..11bb9663d5 100644
--- a/parseclj-lex.el
+++ b/parseclj-lex.el
@@ -57,6 +57,19 @@ Tokens at a mimimum have these attributes
Other ATTRIBUTES can be given as a flat list of key-value pairs."
(apply 'a-list :token-type type :form form :pos pos attributes))
+(defun parseclj-lex-error-token (pos &optional error-type)
+ "Create a lexer error token starting at POS.
+ERROR-TYPE is an optional keyword to attach to the created token,
+as the means for providing more information on the error."
+ (when (= pos (point))
+ (right-char))
+ (apply #'parseclj-lex-token
+ :lex-error
+ (buffer-substring-no-properties pos (point))
+ pos
+ (when error-type
+ (list :error-type error-type))))
+
(defun parseclj-lex-token-p (token)
"Is the given TOKEN a parseclj-lex TOKEN.
@@ -193,11 +206,7 @@ A token is an association list with :token-type as its
first key."
(and (member char '(?. ?* ?+ ?! ?- ?_ ?? ?$ ?& ?= ?<
?> ?/)))))
(progn
(right-char)
- (parseclj-lex-token :lex-error
- (buffer-substring-no-properties pos (point))
- pos
- :error-type :invalid-number-format))
-
+ (parseclj-lex-error-token pos :invalid-number-format))
(parseclj-lex-token :number
(buffer-substring-no-properties pos (point))
pos)))))
@@ -270,7 +279,7 @@ token is returned."
(progn
(right-char)
(parseclj-lex-token :string (buffer-substring-no-properties pos
(point)) pos))
- (parseclj-lex-token :lex-error (buffer-substring-no-properties pos
(point)) pos))))
+ (parseclj-lex-error-token pos :invalid-string))))
(defun parseclj-lex-lookahead (n)
"Return a lookahead string of N characters after point."
@@ -322,7 +331,7 @@ See `parseclj-lex-symbol', `parseclj-lex-symbol-start-p'."
(if (equal (char-after (point)) ?:) ;; three colons in a row => lex-error
(progn
(right-char)
- (parseclj-lex-token :lex-error (buffer-substring-no-properties pos
(point)) pos :error-type :invalid-keyword))
+ (parseclj-lex-error-token pos :invalid-keyword))
(progn
(while (or (parseclj-lex-symbol-rest-p (char-after (point)))
(equal (char-after (point)) ?#))
@@ -408,7 +417,7 @@ See `parseclj-lex-token'."
(while (not (or (parseclj-lex-at-whitespace-p)
(parseclj-lex-at-eof-p)))
(right-char))
- (parseclj-lex-token :lex-error (buffer-substring-no-properties pos
(point)) pos :error-type :invalid-hashtag-dispatcher)))))
+ (parseclj-lex-error-token pos :invalid-hashtag-dispatcher)))))
(t
(concat ":(" (char-to-string char)))))))
diff --git a/test/parseclj-lex-test.el b/test/parseclj-lex-test.el
index c1ebc09ce1..34813e3ac7 100644
--- a/test/parseclj-lex-test.el
+++ b/test/parseclj-lex-test.el
@@ -283,7 +283,7 @@
(with-temp-buffer
(insert "\"abc")
(goto-char 1)
- (should (equal (parseclj-lex-string) (parseclj-lex-token :lex-error
"\"abc" 1))))
+ (should (equal (parseclj-lex-string) (parseclj-lex-token :lex-error
"\"abc" 1 :error-type :invalid-string))))
(with-temp-buffer
(insert "\"abc\\\"\"")"abc\""