[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Questions about tree-sitter
From: |
Yuan Fu |
Subject: |
Re: Questions about tree-sitter |
Date: |
Wed, 30 Aug 2023 00:03:03 -0700 |
> 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