all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: "\"Augustin Chéneau (BTuin)\"" <btuin@mailo.com>
Cc: emacs-devel@gnu.org
Subject: Re: Questions about tree-sitter
Date: Tue, 5 Sep 2023 21:07:58 -0700	[thread overview]
Message-ID: <2B46C452-DC8B-4BD0-A64B-8773235C1FA8@gmail.com> (raw)
In-Reply-To: <52f09345-85c8-4049-b12d-bf8b84b08f75@mailo.com>

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



> On Aug 30, 2023, at 4:28 AM, Augustin Chéneau (BTuin) <btuin@mailo.com> wrote:
> 
> Le 30/08/2023 à 09:03, Yuan Fu a écrit :
>>> On Aug 29, 2023, at 2:26 PM, Augustin Chéneau (BTuin) <btuin@mailo.com> wrote:
>>> 
>>> Hello,
>>> 
>>> I have a few questions about tree-sitter.
>>> 
>>> I'm currently developing a grammar for GNU Bison alongside a tree-sitter
>>> major mode, it's a work in progress.  The grammar is here:
>>> <https://gitlab.com/btuin2/tree-sitter-bison>, still incomplete but so
>>> far able to parse simple files, and the major mode prototype is
>>> attached to this message.
>>> 
>>> So, the questions:
>>> 
>>> 1. Is there a way to reload a grammar?
>>> 
>>> Emacs is pretty nice as a playground for testing grammars, but once a
>>> grammar is loaded, it won't be loaded again until Emacs restarts (as far
>>> as I know).
>>> Is it possible to reload a grammar after modifying it?
>> No, and it’s probably not easy to implement either, since unloading the grammar would require Emacs to purge/invalid all the node/query/parsers using that grammar.
>>> 2. How to mix multiple languages?
>>> 
>>> It would be very useful for Bison since its mixed with C or other languages.
>>> According to the documentation I need to use the function
>>> `treesit-range-rules` to set the variable `treesit-range-settings`, but
>>> it seems to have no effect.  The language in the selected nodes doesn't
>>> change (as attested by `(treesit-language-at (point))`).
>>> 
>>> I did it that way (extracted from the attachment):
>>> 
>>> (setq-local treesit-range-settings
>>>      (treesit-range-rules
>>>       :embed 'c
>>>       :host 'bison
>>>       '((undelimited_code_block) @capture)))
>>> 
>>> Am I missing something?
>> The ranges are set correctly, actually. But the C parse sees all those blocks stitched together as a whole, rather than individual blocks, and the code it sees is obviously not syntactically correct.
>> We should really work on supporting isolated ranges, there has been multiple requests for it. I’ll try to work on that.
>>> 3. Is it possible to trigger a hook when a node is modified?
>>> 
>>> Since Bison supports multiple languages (C, C++, Java and D), I'd like
>>> to watch the declaration "%language LANGUAGE" to change the embedded
>>> language when needed.
>>> Is there a way to do that?
>> treesit-parser-add-notifier might be what you want.
>> Yuan
> 
> I see.  Thank you for your answers and for your great work on tree-sitter!

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


[-- Attachment #2: bison-ts-mode.el --]
[-- Type: application/octet-stream, Size: 2198 bytes --]

;;; bison-ts-mode --- Tree-sitter mode for Bison

;;; 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 bison-ts--font-lock-settings (language)
  (treesit-font-lock-rules
   :language language
   :feature 'comment
   '((comment) @font-lock-comment-face)

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

(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
                '((comment
                   ;; c-ts-mode
                   definition)
                  (declaration
                   ;; c-ts-mode
                   keyword preprocessor string type)
                  (
                   ;; c-ts-mode
                   assignment constant escape-sequence label literal)))

	(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: Type: text/plain, Size: 2 bytes --]




  reply	other threads:[~2023-09-06  4:07 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 [this message]
2023-09-08 11:53       ` Augustin Chéneau (BTuin)
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=2B46C452-DC8B-4BD0-A64B-8773235C1FA8@gmail.com \
    --to=casouri@gmail.com \
    --cc=btuin@mailo.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.