stadelmanma / tree-sitter-fortran

Fortran grammar for tree-sitter
MIT License
30 stars 15 forks source link

Create unnamed nodes for keywords #51

Closed ghost closed 3 years ago

ghost commented 3 years ago

Currently keywords are handled by caseInsensitve which is based on regex. tree-sitter only creates unnamed nodes for string expressions, not for regular expressions (See https://github.com/tree-sitter/tree-sitter/issues/668#issuecomment-656789375). It means that none of the fortran keywords can be captured and used in tree-sitter queries.

The modified implementation aliases the case insensitive keywords to corresponding strings, so that they can be queried.

A new function whiteSpacedKeyword handles fortran keywords with can also contain white spaced keywords, for example "end if", "end do", "go to", etc. It takes a prefix ("end"/"go") and a suffix ("if"/"to") and returns a string alias which be used in query files.

Finally, implementation of blockStructureEnding is also changed in similar manner. The suffix is optional for blockStructureEnding.

The implementation of caseInsensitive is based on the keyword function implemented here: https://github.com/tree-sitter/tree-sitter-php/blob/b0c0367d4b7058921fdc4ba11e257441a64ab809/grammar.js#L1111

Folks at nvim-treesitter helped me to figure it out. Here's the link to discussion: https://nvim-treesitter.zulipchat.com/#narrow/stream/252271-general/topic/fortran-queries