all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Augustin Chéneau (BTuin)" <btuin@mailo.com>
To: Yuan Fu <casouri@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Questions about tree-sitter
Date: Fri, 8 Sep 2023 13:53:37 +0200	[thread overview]
Message-ID: <3b3f90e8-a318-4b63-915e-6477701de897@mailo.com> (raw)
In-Reply-To: <2B46C452-DC8B-4BD0-A64B-8773235C1FA8@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1690 bytes --]

Le 06/09/2023 à 06:07, Yuan Fu a écrit :
> 
> I added local parser support to master. If everything goes right, you just need to add a :local t flag in treesit-range-rules. Check out the modified bision-ts-mode.el that I hacked up for an example. BTW, it’s vital that you define treesit-language-at-point-function for a multi-language mode.
> 
> Yuan

Thanks a lot!

I did some tests and it's working pretty well.

Do you think it's a good idea to add a prefix to bison feature names in 
font-lock settings to avoid conflicts with C names (as I did)?

I have a few issues though:

- I first defined `treesit-language-at-point-function` using
`treesit-node-at`.  However, `treesit-node-at` itself uses
`treesit-language-at-point-function` which causes an infinite recursion.
So I instead used `treesit-local-parsers-at` to check if a local parser 
is used.  Is it a good solution?


- When I try to indent C code by using c-ts-mode indent rules, I get the 
following error:

Debugger entered--Lisp error: (wrong-type-argument treesit-node-p 
#<treesit-parser for c>)
   treesit-node-parser(#<treesit-parser for c>)
   treesit--indent-1()
   treesit-indent-region(1075 1176)
   indent-region(1075 1176)
   indent-for-tab-command(nil)
   funcall-interactively(indent-for-tab-command nil)
   call-interactively(indent-for-tab-command nil nil)
   command-execute(indent-for-tab-command)

There seems to be a mistake in `treesit--indent-1` in the `cond` at the 
line `(local-parsers (car local-parsers))`, since a parser is returned 
while it should be a node.



I attached a newer version of bison-ts-mode and a small patch to
c-ts-mode so I can reuse its `treesit-font-lock-feature-list`.

[-- Attachment #2: bison-ts-mode.el --]
[-- Type: text/x-emacs-lisp, Size: 4307 bytes --]

;;; bison-ts-mode --- Tree-sitter mode for Bison -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'treesit)
(require 'c-ts-mode)

(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-search-subtree "treesit.c")
(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-next-sibling "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-string "treesit.c")
(declare-function treesit-query-compile "treesit.c")
(declare-function treesit-query-capture "treesit.c")
(declare-function treesit-parser-add-notifier "treesit.c")
(declare-function treesit-parser-buffer "treesit.c")
(declare-function treesit-parser-list "treesit.c")


(defun treesit--merge-feature-lists (l1 l2)
  "Merge the lists of lists L1 and L2.
The first sublist of L1 is merged with the first sublist of L2 and so on.
L1 and L2 don't need to have the same size."
  (let ((res ()))
    (while (or l1 l2)
      (setq res (push (nconc (car l1) (car l2)) res))
      (setq l1 (cdr l1) l2 (cdr l2)))
    (nreverse res)))


;; (defun bison-ts--language-at-point-function (position)
;;   "Return the language at POSITION."
;;   (let* ((node (treesit-node-at position)))
;; 	(if (treesit-parent-until
;; 		 node
;; 		 (lambda (n) (let ((type (treesit-node-type n)))
;; 			       (or (equal "code_block" type)
;; 				   (equal "undelimited_code_block" type))))
;; 		 t)
;; 	    'c
;; 	  'bison)))


(defun bison-ts--language-at-point-function (position)
  "Return the language at POSITION."
  (let* ((parser (treesit-local-parsers-at position)))
    (if parser
	'c
      'bison)))


(defun bison-ts--font-lock-settings (language)
  (treesit-font-lock-rules
   :language language
   :feature 'bison-comment
   '((comment) @font-lock-comment-face)

   :language language
   :feature 'bison-declaration
   '((declaration (declaration_name) @font-lock-keyword-face))

   :language language
   :feature 'bison-type
   '((type) @font-lock-type-face)

   :language language
   :feature 'bison-grammar-rule-usage
   '((grammar_rule_identifier) @font-lock-variable-use-face)

   :language language
   :feature 'bison-grammar-rule-declaration
   :override t
   '((grammar_rule (grammar_rule_identifier)  @font-lock-variable-name-face))

   :language language
   :feature 'bison-string
   :override t
   '((string) @font-lock-string-face)

   :language language
   :feature 'bison-literal
   :override t
   '((char_literal) @font-lock-keyword-face
     (number_literal) @font-lock-number-face)

   :language language
   :feature 'bison-directive-grammar-rule
   :override t
   '((grammar_rule (directive_empty) @font-lock-keyword-face))

   :language language
   :feature 'bison-operator
   :override t
   '(["|"] @font-lock-operator-face)

   :language language
   :feature 'bison-delimiter
   :override t
   '([";"] @font-lock-delimiter-face)))


(defvar bison-ts-mode--font-lock-feature-list
  '(( bison-comment bison-declaration bison-type
      bison-grammar-rule-usage bison-grammar-rule-declaration
      bison-string bison-literal bison-directive-grammar-rule
      bison-operator bison-delimiter)))


(define-derived-mode bison-ts-mode prog-mode "Bison"
  "A mode for Bison."
  (when (treesit-ready-p 'bison)
    (setq-local treesit-font-lock-settings
                (append (bison-ts--font-lock-settings 'bison)
                        (c-ts-mode--font-lock-settings 'c)))

    (setq-local treesit-font-lock-feature-list
                (treesit--merge-feature-lists
		 bison-ts-mode--font-lock-feature-list
		 c-ts-mode--font-lock-feature-list))

    (setq-local treesit-simple-indent-rules
                (c-ts-mode--get-indent-style 'c))

    (setq-local treesit-language-at-point-function 'bison-ts--language-at-point-function)

    (setq-local treesit-range-settings
		(treesit-range-rules
		 :embed 'c
		 :host 'bison
		 :local t
		 '((undelimited_code_block) @capture)
		 ))

    (treesit-major-mode-setup)))

(provide 'bison-ts-mode)
;;; bison-ts-mode.el ends here

[-- Attachment #3: 0001-Put-font-lock-features-of-c-ts-mode-in-a-variable-to.patch --]
[-- Type: text/x-patch, Size: 1580 bytes --]

From 5f8d4435a316855c60b9b2db5c0bb291a28d4b32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Augustin=20Ch=C3=A9neau?= <btuin@mailo.com>
Date: Fri, 8 Sep 2023 13:20:51 +0200
Subject: [PATCH] Put font-lock features of c-ts-mode in a variable to allow
 reuse

---
 lisp/progmodes/c-ts-mode.el | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 5b698eb09f4..c551f9f06b4 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -1123,6 +1123,13 @@ c-ts-mode--emacs-set-ranges
     (setq-local c-ts-mode--for-each-tail-ranges set-ranges)
     (treesit-parser-set-included-ranges c-parser reversed-ranges)))
 
+(defvar c-ts-mode--font-lock-feature-list
+  '(( comment definition)
+    ( keyword preprocessor string type)
+    ( assignment constant escape-sequence label literal)
+    ( bracket delimiter error function operator property variable)))
+
+
 ;;; Modes
 
 (defvar-keymap c-ts-base-mode-map
@@ -1213,11 +1220,7 @@ c-ts-base-mode
                                 eos)
                    c-ts-mode--defun-for-class-in-imenu-p nil))))
 
-  (setq-local treesit-font-lock-feature-list
-              '(( comment definition)
-                ( keyword preprocessor string type)
-                ( assignment constant escape-sequence label literal)
-                ( bracket delimiter error function operator property variable))))
+  (setq-local treesit-font-lock-feature-list c-ts-mode--font-lock-feature-list))
 
 (defvar treesit-load-name-override-list)
 
-- 
2.42.0


  reply	other threads:[~2023-09-08 11:53 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 21:26 Questions about tree-sitter Augustin Chéneau (BTuin)
2023-08-30  7:03 ` Yuan Fu
2023-08-30 11:28   ` Augustin Chéneau (BTuin)
2023-09-06  4:07     ` Yuan Fu
2023-09-08 11:53       ` Augustin Chéneau (BTuin) [this message]
2023-09-08 16:43         ` Yuan Fu
2023-09-09 16:39           ` Augustin Chéneau (BTuin)
2023-09-12  0:22             ` Yuan Fu
2023-09-13 12:43               ` Augustin Chéneau (BTuin)
2023-09-14  4:11                 ` Yuan Fu
2023-09-18 17:04                   ` Augustin Chéneau (BTuin)
2023-09-19  4:00                     ` Yuan Fu
2023-09-01  2:39   ` Madhu
2023-09-01  6:53     ` Eli Zaretskii
2023-09-01  9:15       ` Madhu
2023-09-01 10:45         ` Dmitry Gutov
2023-09-01 10:58         ` Eli Zaretskii
2023-11-27  7:16           ` Madhu
2023-09-06 16:11   ` Lynn Winebarger
2023-09-07 23:42     ` Yuan Fu
2023-09-08  0:11       ` Lynn Winebarger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3b3f90e8-a318-4b63-915e-6477701de897@mailo.com \
    --to=btuin@mailo.com \
    --cc=casouri@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.