Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

What's the workflow for integrating a new language? #157

Closed
jthaman opened this issue Jul 22, 2021 · 6 comments
Closed

What's the workflow for integrating a new language? #157

jthaman opened this issue Jul 22, 2021 · 6 comments
Labels
question Not a bug report or feature request

Comments

@jthaman
Copy link

jthaman commented Jul 22, 2021

I found this grammar for the R language, but I can't tell how I would actually go about integrating it with emacs-tree-sitter. Any help appreciated.

@theHamsta
Copy link
Contributor

theHamsta commented Jul 22, 2021

Have a look at https://github.com/ubolonton/tree-sitter-langs (sorry, that I can't provide more detailed explanations. Thought you would appreciate a quick feedback)

@sogaiu
Copy link
Contributor

sogaiu commented Jul 23, 2021

I think there's some info within some issues:

The first one listed wasn't quite up to date at the time I worked on something, but it had important background.

The second one has some of the extra bits that needed tweaking for my attempt.

I don't know what the current state of things is.

@shackra shackra added the question Not a bug report or feature request label Jul 23, 2021
@jthaman
Copy link
Author

jthaman commented Jul 24, 2021

Thanks everyone, I'll try to put the puzzle together.

@abradd
Copy link

abradd commented Jul 25, 2021

I'm still reasonably new to this, but here is what I've been doing.

I assume you already have emacs-tree-sitter and tree-sitter-langs installed. The instructions here will get you started with emacs-tree-sitter. I'll repeat some of them below.

You will need to generate the shared library from the repo you linked either by compiling parser.c or running tree-sitter parse or tree-sitter test in the repo (might be useful if you are actively working on the grammar).

As an example, I've compiled a grammar with:

gcc -o r.so -shared src/parser.c  -Os -I./src

but I am primarily using the tree-sitter commands.

The tree-sitter-load-path variable defines where emacs-tree-sitter looks for the shared library. Either place the shared library in one of those paths or modify the variable.

Then you will need to add the language to tree-sitter-major-mode-language-alist with something like:

(add-to-list 'tree-sitter-major-mode-language-alist '(r-mode . r))

I don't know what mode you will be using but replace "r-mode" with the mode of interest. The "r" is for the name of the grammar which according to the grammar in the repo you linked is "r". You can find the name in module.exports = grammar({....

I'm primarily testing a verilog grammar at the moment which uses the name "verilog" and generates a verilog.so in ~/.tree-sitter/bin/ folder when I run tree-sitter test.

With this configuration you should be able to run tree-sitter-debug-mode and view the syntax tree of a buffer with r code inside it.

Andrea provides a similar description here. Their instructions for using the tree-sitter commands might be useful.

At the moment, I'm using tree-sitter-langs to setup highlights. I'm not sure if this is the right way to do it, but here is what I do:

(defun ab-extract-verilog-ts-highlights ()
  (with-temp-buffer
    (insert-file-contents "location/of/your/highlights.scm")
    (goto-char (point-max))
    (insert "\n")
    (buffer-string)))

(defun ab-set-ts-hl-defaults ()
  (setq tree-sitter-hl-default-patterns (ab-extract-verilog-ts-highlights)))

(add-hook 'verilog-mode-hook #'ab-set-ts-hl-defaults)

You can replace all the verilog references with r-specific ones.

I just re-read some of the highlighting docs here and it seems like one could also use tree-sitter-hl-add-patterns to configure highlighting.

@jthaman
Copy link
Author

jthaman commented Jul 27, 2021

After noodling on this for a few days, what worked for me was to model a script based on csharp-tree-sitter.el. I'll try to get indentation working next. Very happy that I don't have to write any regexs in emacs-lisp :)

Thanks everyone, this pet project is coming together one bit at a time.

@jthaman jthaman closed this as completed Jul 27, 2021
@sogaiu
Copy link
Contributor

sogaiu commented Jul 27, 2021

@jthaman May be you know already, but FWIW, regarding indentation, there is this: https://codeberg.org/FelipeLema/tree-sitter-indent.el

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
question Not a bug report or feature request
Projects
None yet
Development

No branches or pull requests

5 participants