Subject: bug#72388: 31.0.50; Use tree-sitter-cuda grammar but with tree-sitter-cpp's font-lock/indentation rules

From: Ergus <[email-masked]>
Date: Thu, 16 Jan 2025 20:35:39 +0100

Hi Yuan:

It has been a long time since this emails.. But today finally I made my
serious attempt to make a `cuda-ts-mode`.

While the treesit-language-remap-alist helped to reuse an important part
of the code, there were a few issues I needed to manage manually and
maybe they could be improved somehow:

1. The indentation rules still uses the 'cpp prefix, so I had to
manually tune the code:

```
(defun cuda-ts-mode--simple-indent-rules ()
   (let ((cpp-rules (c-ts-mode--simple-indent-rules
		    'cpp c-ts-mode-indent-style)))
     `((cuda . ,(alist-get 'cpp cpp-rules)))))

(setq-local treesit-simple-indent-rules
		(cuda-ts-mode--simple-indent-rules))
```

In order to manage it properly

2. I see that font-lock is working, but the fontlock rules check is
using the cpp parser.

That's because the c-ts-mode--font-lock-settings still receives cpp as input.
I am trying to find a way to get it from c++ and then change the
:language cpp
with
:language cuda
because if I call
(c-ts-mode--font-lock-settings 'cuda) I obviously get an error.

Is there some way to go around these in a cleaner way?

Thanks in advance and thanks,
Ergus

On Mon, Aug 19, 2024 at 08:44:41PM -0700, Yuan Fu wrote:
>
>Hey Ergus, sorry for the delay.
>
>> On Aug 12, 2024, at 4:10 PM, Ergus via Bug reports for GNU Emacs, the Swiss army knife of text editors <[email-masked]> wrote:
>>
>> Hi Yuan:
>>
>> I am just trying to use the new treesit-language-remap-alist and so far
>> I understand your idea of how to make it work for the basics. But it is
>> not clear how can I use the new var to add the new syntax entries.
>>
>> I mean, looking at the grammar:
>>
>> https://github.com/tree-sitter-grammars/tree-sitter-cuda/blob/master/grammar.js
>>
>> You can see that it is the inherited C++ syntax, with extra entries in
>> some fields (like _declaration_modifiers or delete_expression). For this
>> part the new variable may work perfectly.
>>
>> However there are a few "new" entries like: kernel_call_expression,
>> kernel_call_syntax and launch_bounds that will need special handling.
>>
>> What's your suggestion?
>>
>> I mean, for the latest what we really need is to extend (partially
>> redefine) the c-ts-mode--indent-styles, c-ts-mode--keywords and so on.
>>
>> Is that the intended approach?
>
>Yeah, you can define your own cuda-indent-styles by grabbing c-ts-mode—indent-styles and adding your rules on top of it, something like
>
>(append your-styles
>        (c-ts-mode--indent-styles 'c++))
>
>For font-lock, something like
>
>(append (treesit-font-lock-rules
>         :language 'cuda
>         :faeture 'xxx
>         '(queries))
>        (c-ts-mode--font-lock-settings 'c++))
>
>The new treesit-language-remap-alist will ensure that even though the config you borrow from c-ts-mode calls for c++, Emacs will actually use cuda grammar in your major mode.
>
>Yuan