[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 686d4ddb87c 2/7: Use tagged parser in c-ts-mode
From: |
Yuan Fu |
Subject: |
master 686d4ddb87c 2/7: Use tagged parser in c-ts-mode |
Date: |
Wed, 6 Sep 2023 00:03:49 -0400 (EDT) |
branch: master
commit 686d4ddb87ca2d87bfc0cd8a29ac84decab90ff6
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
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.
---
lisp/progmodes/c-ts-mode.el | 39 +++++++++++++++++----------------------
1 file changed, 17 insertions(+), 22 deletions(-)
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 165bdd668c3..7350ef3f4b8 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -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.
- master updated (aa872f25403 -> d1f916b0462), Yuan Fu, 2023/09/06
- master 686d4ddb87c 2/7: Use tagged parser in c-ts-mode,
Yuan Fu <=
- master d392a5d3c11 5/7: Fix c-ts-mode BSD style indentation, Yuan Fu, 2023/09/06
- master 722daf6fffe 1/7: Add tag to tree-sitter parsers, Yuan Fu, 2023/09/06
- master cf0986401ce 3/7: Allow filter by tag in treesit-parser-list, Yuan Fu, 2023/09/06
- master d05494a9ff8 4/7: Support tree-sitter local parsers, Yuan Fu, 2023/09/06
- master 6b387d2edd1 6/7: ; Update Ftreesit_parser_list docstring., Yuan Fu, 2023/09/06
- master d1f916b0462 7/7: Support local parser in treesit-node-at/on, Yuan Fu, 2023/09/06