;; 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
(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
"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)
: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.