[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Scheme Mode and Regular Expression Literals
From: |
Toshi Umehara |
Subject: |
Re: Scheme Mode and Regular Expression Literals |
Date: |
Tue, 19 Mar 2024 12:06:24 +0900 |
Thank you very much for your comments, Stefan. As you point out, the
previous code is found to fail when regular expressions and sexp
comments exist in the same line.
Now I put the logic to scan the beginning of sexp comments and regular
expressions in syntax-propertize-rules macro. I use different functions
to be invoked by them, because the beginning of regular expressions
need to be canceled when they are already in normal strings or
comments. (This kind of logic might be required also for sexp comment,
but the current implementation does not do that and does not seem to
cause any harms.)
Therefore, if I use the same function, it requires an extra conditional
code only for regular expressions, which is a repetition of what is done
in syntax-propertize-rules level. (At syntax-propertize-rules leve, we
already know it's a beginning of a sexp comment or a possible beginning
of regular expression).
The following explains the functions implemented.
- scheme-syntax-propertize-sexp-comment
No change from the current built-in implementation
- scheme-syntax-propertize-regexp-end
If the posistion is already in regular expressions and not in
comments, it searches regular expression end (/) ignoring backslash
slash (\/).
- scheme-syntax-propertize-regexp
This is invoked in syntax-propertize-rules. It cancels syntax class
assignment to # of #/ if the # part is already in strings or
comments. (Precisely, it assigns @ syntax class to # .) Otherwise it
continues to scan the end of regular expression by calling
scheme-syntax-propertize-regexp-end.
Thanks.
#+BEGIN_SRC
(add-hook
'scheme-mode-hook
(lambda ()
(setq-local
syntax-propertize-function
(lambda (beg end)
(goto-char beg)
(scheme-syntax-propertize-sexp-comment (point) end)
(scheme-syntax-propertize-regexp-end (point) end)
(funcall
(syntax-propertize-rules
("\\(#\\);" (1 (prog1 "< cn"
(scheme-syntax-propertize-sexp-comment
(point) end))))
("\\(#\\)/" (1 (prog1 "|"
(scheme-syntax-propertize-regexp
(point) end))))
)
(point) end)
))))
(defun scheme-syntax-propertize-sexp-comment (_ end)
(let ((state (syntax-ppss)))
(when (eq 2 (nth 7 state))
;; It's a sexp-comment. Tell parse-partial-sexp where it ends.
(condition-case nil
(progn
(goto-char (+ 2 (nth 8 state)))
;; FIXME: this doesn't handle the case where the sexp
;; itself contains a #; comment.
(forward-sexp 1)
(put-text-property (1- (point)) (point)
'syntax-table (string-to-syntax "> cn")))
(scan-error (goto-char end))))))
(defun scheme-syntax-propertize-regexp-end (_ end)
(let* ((state (syntax-ppss))
(within-str (nth 3 state))
(within-comm (nth 4 state))
(start-delim-pos (nth 8 state)))
(if (and (not within-comm)
(and within-str
(string=
(buffer-substring-no-properties
start-delim-pos
(1+ start-delim-pos))
"#")))
(let ((end-found nil))
(while (and
(not end-found)
(re-search-forward "\\(/\\)" end t))
(progn
(if
(not (char-equal
(char-before (match-beginning 1))
?\\ ))
(progn
(put-text-property
(match-beginning 1)
(1+ (match-beginning 1))
'syntax-table (string-to-syntax "|"))
(setq end-found t)
)))))
)))
(defun scheme-syntax-propertize-regexp (_ end)
(let* ((match-start-state (save-excursion
(syntax-ppss (match-beginning 1))))
(within-str (nth 3 match-start-state))
(within-comm (nth 4 match-start-state)))
(if (or within-str within-comm)
(put-text-property ;; Cancel regular expression start
(match-beginning 1)
(1+ (match-beginning 1))
'syntax-table (string-to-syntax "@"))
(scheme-syntax-propertize-regexp-end _ end)
)))
#+END_SRC
scheme-regexp.v2.patch
Description: deals with scheme regular expression syntax
--
Toshi (Toshihiro Umehara)