Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Tree queries #16

Closed
omentic opened this issue Mar 2, 2023 · 15 comments
Closed

Tree queries #16

omentic opened this issue Mar 2, 2023 · 15 comments

Comments

@omentic
Copy link
Contributor

omentic commented Mar 2, 2023

Hi aMOPel, I've been working on some highlighting queries based off of this repository's grammar. They're currently in development over at helix-editor/helix#6123: once they're done I can PR them to this repo, neovim, and anywhere else that uses tree-sitter.

@aMOPel
Copy link
Owner

aMOPel commented Mar 2, 2023

Hi :)
I honestly am pretty stuck on the last few syntax nodes. Unless somebody is gonna help me with that grammar, who has a better understanding of tree sitter I probably won't finish it.

Your efforts are probably better spent on

https://github.com/alaviss/tree-sitter-nim/tree/rewrite

@omentic
Copy link
Contributor Author

omentic commented Mar 3, 2023

I saw that, I'll probably rewrite the queries for it once alaviss finishes (originally I wrote them for alaviss's first grammar). Yours is already quite comprehensive though, it looked like objects are the big thing missing?

Did you manage to find any good documentation for tree-sitter when you were working on the grammar? I've been writing the queries by reading through other existing queries and grammars and sort of guessing how they should be written, for lack of anything better to base them off of: which hasn't worked terribly well. I've been having a particularly hard time with keywords: they have to line up with the grammar.js file somehow and I haven't quite figured out how.

@aMOPel
Copy link
Owner

aMOPel commented Mar 3, 2023

it looked like objects are the big thing missing?

I've made issues describing the missing features.
One big thing missing are the complex expressions, ie if-expressions etc. And another thing are the various parentheses. There is this construct called "par" in the grammar spec which works like this:

let a = (
  echo "hi"
  5
)
echo a
# prints 
# hi
# 5

But there are also the arbitrary parentheses around expressions like

let a = (1+1)*2

It's a pain to disambiguate those from tuples and function calls, but probably doable.
The complex expressions are the big thing I got stuck on though.

Also another big issue is that the grammar.js is so big and complex now, that the resulting parser is absolutely enormous in size. What's pushed right now is 15MB large. When I added more things it grew to 28MB or something.

Did you manage to find any good documentation for tree-sitter when you were working on the grammar?

Can't say so. I haven't even written any queries myself. Your best bet is probably to take inspiration from preexisting code. The nvim-treesitter repo has a huge collection of queries you could probably learn a lot from those.

@omentic
Copy link
Contributor Author

omentic commented Mar 3, 2023

TIL about par, I don't think I've ever seen that in practice.

By any chance, do you know of a good way to throw this grammar at an example file to generate syntax trees like in tests/corpus? I think neovim has some support for it on their devel branch but it'd be nice if I could just use the grammar directly.

@omentic
Copy link
Contributor Author

omentic commented Mar 3, 2023

Also, how do you build parser.c?

@aMOPel
Copy link
Owner

aMOPel commented Mar 4, 2023

do you know of a good way to throw this grammar at an example file to generate syntax

https://tree-sitter.github.io/tree-sitter/creating-parsers#command-parse

This will parse the file and generate the AST.

I don't know if this helps you but

https://nim-lang.github.io/Nim/macros.html#treeRepr%2CNimNode

This can give you the AST from parser of the Nim compiler

If your questions was about good example files to parse:

https://github.com/nim-lang/Nim/blob/devel/compiler/parser.nim

How about this one?

Also, how do you build parser.c

https://tree-sitter.github.io/tree-sitter/creating-parsers#command-generate

@omentic
Copy link
Contributor Author

omentic commented Mar 4, 2023

Thanks, I got tree-sitter dumping trees, which is pretty helpful.

On further testing the highlighting doesn't work as well as I thought it did: there's a lot of edge cases, incorrect tree representations, and occasional errors (mostly around type declarations). It's still better than no highlighting though...

@aMOPel
Copy link
Owner

aMOPel commented Mar 4, 2023

If it's just about highlights in neovim, I am using this plugin atm:

https://github.com/alaviss/nim.nvim

@aMOPel
Copy link
Owner

aMOPel commented Mar 4, 2023

If you care enough you could make an issue listing the failing edge cases, and I could have a look at it

@aMOPel
Copy link
Owner

aMOPel commented Mar 11, 2023

I actually fixed some issues with the type descriptions etc.

If you still find something, please report it.

I tested it on various large files from the nim repo and it did everything except

  1. complex expressions (like if expressions, case expressions etc.) and
  2. par (parentheses to group things like - (a + b)).

Of course I expect there to be various bugs still.

@omentic
Copy link
Contributor Author

omentic commented Mar 12, 2023

I'll open issues for any new problems I find 👍

@omentic omentic closed this as completed Mar 12, 2023
@aMOPel
Copy link
Owner

aMOPel commented Mar 12, 2023

Btw I have written some highlight queries that comply with the nvim tree sitter interface. Gonna push later

@aMOPel
Copy link
Owner

aMOPel commented Mar 12, 2023

@aMOPel aMOPel reopened this Mar 12, 2023
@omentic
Copy link
Contributor Author

omentic commented Mar 13, 2023

Oh shoot, I wish I saw this earlier. I rewrote my Helix queries to work with object and concept types and generally be less jank (currently pushed at helix-editor/helix#6123).

I have some known issues with my queries:

  • Modules like std/[one, two] will get highlighted as variables (while std/one is fine)
  • tuple[one, two: string] and tuple[one: string, two: string] are highlighted incorrectly (seq[seq[int]] is fine
  • I couldn't figure out a way to distinguish between variables and types in case statements (if there isn't one, probably should do it by case)

@omentic
Copy link
Contributor Author

omentic commented Mar 13, 2023

Mmm, note to self, helix and nvim-treesitter queries have reversed precedence (later entries override in nvim, vice versa for helix)

@omentic omentic closed this as completed Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants