(modify-syntax-entry ?\' "_" table)
table))
+(defvar ietf-drums-comment-syntax-table
+ (let ((table (copy-syntax-table ietf-drums-syntax-table)))
+ (modify-syntax-entry ?\" "w" table)
+ table)
+ "In comments, DQUOTE is normal and does not start a string.")
+
+(defun ietf-drums--skip-comment ()
+ ;; From just before the start of a comment, go to the end. Returns
+ ;; point. If the comment is unterminated, go to point-max.
+ (condition-case ()
+ (with-syntax-table ietf-drums-comment-syntax-table
+ (forward-sexp 1))
+ (scan-error (goto-char (point-max))))
+ (point))
+
(defun ietf-drums-token-to-list (token)
"Translate TOKEN into a list of characters."
(let ((i 0)
(forward-sexp 1)
(error (goto-char (point-max)))))
((eq c ?\()
- (delete-region
- (point)
- (condition-case nil
- (with-syntax-table (copy-syntax-table ietf-drums-syntax-table)
- (modify-syntax-entry ?\" "w")
- (forward-sexp 1)
- (point))
- (error (point-max)))))
+ (delete-region (point) (ietf-drums--skip-comment)))
(t
(forward-char 1))))
(buffer-string))))
(setq c (char-after))
(cond
((eq c ?\")
- (forward-sexp 1))
+ (condition-case ()
+ (forward-sexp 1)
+ (scan-error (goto-char (point-max)))))
((eq c ?\()
- (forward-sexp 1))
+ (ietf-drums--skip-comment))
((memq c '(?\ ?\t ?\n ?\r))
(delete-char 1))
(t
(should (equal (ietf-drums-remove-comments
"random (first) (second (and)) (third) not fourth")
"random not fourth"))
+ ;; Test some unterminated comments.
+ (should (equal (ietf-drums-remove-comments "test an (unterminated comment")
+ "test an "))
+ (should (equal (ietf-drums-remove-comments "test an \"unterminated quote")
+ ;; returns the string unchanged (and doesn't barf).
+ "test an \"unterminated quote"))
+ (should (equal (ietf-drums-remove-comments
+ ;; note that double-quote is not special.
+ "test (unterminated comments with \"quoted (\" )stuff")
+ "test "))
;; ietf-drums-remove-whitespace
(should (equal (ietf-drums-remove-whitespace "random string")
(should (equal (ietf-drums-remove-whitespace
"random (first) (second (and)) (third) not fourth")
"random(first)(second (and))(third)notfourth"))
+ ;; Test some unterminated comments and quotes.
+ (should (equal (ietf-drums-remove-whitespace
+ "random (first) (second (and)) (third unterminated")
+ "random(first)(second (and))(third unterminated"))
+ (should (equal (ietf-drums-remove-whitespace "random \"non terminated string")
+ "random\"non terminated string"))
;; ietf-drums-strip
(should (equal (ietf-drums-strip "random string") "randomstring"))