Use tagged parser in c-ts-mode

Now that we have tags for parsers, use that instead of the
load-name-override hack.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--emacs-c-range-query): Change parser to C.
(c-ts-mode--emacs-set-ranges): Use the tagged parser.
(c-ts-mode): Create the tagged parser.
This commit is contained in:
Yuan Fu 2023-09-04 20:05:50 -07:00
parent 722daf6fff
commit 686d4ddb87
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -1018,12 +1018,11 @@ if `c-ts-mode-emacs-sources-support' is non-nil."
;; FOR_EACH_TAIL, FOR_EACH_TAIL_SAFE, FOR_EACH_FRAME etc., followed by
;; an unbracketed body will mess up the parser, which parses the thing
;; as a function declaration. We "fix" it by adding a shadow parser
;; for a language 'emacs-c' (which is just 'c' but under a different
;; name). We use 'emacs-c' to find each FOR_EACH_* macro with a
;; unbracketed body, and set the ranges of the C parser so that it
;; skips those FOR_EACH_*'s. Note that we only ignore FOR_EACH_*'s
;; with a unbracketed body. Those with a bracketed body parse more
;; or less fine.
;; with the tag `for-each'. We use this parser to find each
;; FOR_EACH_* macro with a unbracketed body, and set the ranges of the
;; default C parser so that it skips those FOR_EACH_*'s. Note that we
;; only ignore FOR_EACH_*'s with a unbracketed body. Those with a
;; bracketed body parse more or less fine.
;;
;; In the meantime, we have a special fontification rule for
;; FOR_EACH_* macros with a bracketed body that removes any applied
@ -1044,12 +1043,12 @@ For BOL see `treesit-simple-indent-rules'."
(defvar c-ts-mode--emacs-c-range-query
(when (treesit-available-p)
(treesit-query-compile
'emacs-c `(((declaration
type: (macro_type_specifier
name: (identifier) @_name)
@for-each-tail)
(:match ,c-ts-mode--for-each-tail-regexp
@_name)))))
'c `(((declaration
type: (macro_type_specifier
name: (identifier) @_name)
@for-each-tail)
(:match ,c-ts-mode--for-each-tail-regexp
@_name)))))
"Query that finds a FOR_EACH_* macro with an unbracketed body.")
(defvar-local c-ts-mode--for-each-tail-ranges nil
@ -1079,9 +1078,11 @@ parser parse the whole buffer."
"Set ranges for the C parser to skip some FOR_EACH_* macros.
BEG and END are described in `treesit-range-rules'."
(let* ((c-parser (treesit-parser-create 'c))
(for-each-parser (treesit-parser-create 'c nil nil 'for-each))
(old-ranges c-ts-mode--for-each-tail-ranges)
(new-ranges (treesit-query-range
'emacs-c c-ts-mode--emacs-c-range-query beg end))
(treesit-parser-root-node for-each-parser)
c-ts-mode--emacs-c-range-query beg end))
(set-ranges (treesit--clip-ranges
(treesit--merge-ranges
old-ranges new-ranges beg end)
@ -1233,16 +1234,10 @@ in your configuration."
:after-hook (c-ts-mode-set-modeline)
(when (treesit-ready-p 'c)
;; Add a fake "emacs-c" language which is just C. Used for
;; skipping FOR_EACH_* macros, see `c-ts-mode--emacs-set-ranges'.
(setf (alist-get 'emacs-c treesit-load-name-override-list)
'("libtree-sitter-c" "tree_sitter_c"))
;; If Emacs source support is enabled, make sure emacs-c parser is
;; after c parser in the parser list. This way various tree-sitter
;; functions will automatically use the c parser rather than the
;; emacs-c parser.
;; Create an "for-each" parser, see `c-ts-mode--emacs-set-ranges'
;; for more.
(when c-ts-mode-emacs-sources-support
(treesit-parser-create 'emacs-c))
(treesit-parser-create 'c nil nil 'for-each))
(treesit-parser-create 'c)
;; Comments.