Treesitter grammar for Ada

I have recently completed an Ada grammar for TreeSitter.

TreeSitter is meant to be a fast parser used by editors (currently Emacs and neovim, that I know of) or various platforms when they syntax-highlight code (github uses treesitter I believe). The tree-sitter-ada page above gives some hints on how things should be setup in neovim, but I have no recent experience with Emacs.

I am in the process of submitted that to treesitter-nvim and treesitter projects, to make the whole setup easier.

The grammar is strongly based on the one in the Ada Reference Manual, but also draws some ideas from the similar grammar that Stephen Leak has written for the Emacs ada-mode.

There is a reasonable testsuite in place, and I am able to parse all sources from the GNAT runtime and the 3 million lines of Ada code at my company without errors (which of course doesn’t mean that I get a meaningful tree in all cases).

TreeSitter is actually a multi-staged process:

  • we need a grammar to create a tree (which is supposed to be much simpler than an AST like a compiler or libadalang would use, and could even have ambiguities).
  • each editor then needs various modules to take advantage of treesitter, like supporting syntax highlighting, code folding, jumping to various constructs, indentation,…
  • each language need to provide the actual queries. My github repository provides support for syntax-highlighting, block folding and jumping, but not indentation yet.

Syntax-highlighting is a nice example: compared to regex-based highlighting, we can perform more advanced things, like highlighting subprogram specs, underlining parameter names, italicizing gnatprep if-statements, …

Compared to the current vim ada mode, treesitter provides much better support for code folding (“za”), since it knows the bounds for a subprogram or package body, if-statement and other loops.



This is very cool. Good luck with supporting indentation :slight_smile:

Very nice indeed! I wanted to do this but got lazy … Going to try it soon. Thanks Manu

Nice. It could be useful in GNAT Studio and others.
This is of course not a replacement for ada-language-server. TreeSitter has no notion of semantics. On the other hand, it is much faster to use this than do round-trips the als on every keypress.

Thanks for working on this. I’ll see how well it works for Emacs ada-mode.