tree-sitter-rust icon indicating copy to clipboard operation
tree-sitter-rust copied to clipboard

Hard to get some symbols inside macro calls and definitions on the user side

Open GrayJack opened this issue 5 years ago • 7 comments
trafficstars

Not sure if it's a proper bug, but I got myself unable to classify some symbols on Atom inside macros.

Like, when I define something like:

'"+"' : 'keyword.operator.add'

It will classify all + signs, except inside macro invocations and macro definitions

The same are true for all operator-like symbols and the in, but other keywords work just fine.

Looking at the code, I suspect that this happens inside _non_special_token, where in is not there and the operator-like symbols are defined by a regular expression /[/_\-=->,;:::!=?.@*=/='&=#%=^=+<>|~]+/, not separated strings.

Again, not sure if it can be considered a bug or if it should be handled in some way on the user side.

GrayJack avatar Mar 16 '20 23:03 GrayJack

No, I think it's reasonable to change the definition of non_special_token to treat all of the tokens separately. I can't remember exactly why it's currently defined with one single token.

maxbrunsfeld avatar Mar 16 '20 23:03 maxbrunsfeld

Maybe because it is allowed any kind of string, for example I could do mymacro!(1 <--> 10) or some kind of haskell like operator, are all valid in most cases.

But I would like to at least the normal Rust operators be able to be captured inside macros.

GrayJack avatar Mar 17 '20 00:03 GrayJack

In the case of Atom, it should be re-parsing the body of the macro with the Rust parser, in order to highlight it correctly. There aren't well-documented APIs for accessing these nested parse trees, but I think you can use this method to access nodes from 'syntax layers' other than the root layer.

maxbrunsfeld avatar Mar 17 '20 02:03 maxbrunsfeld

I can use this method inside of the Atom tree-sitter grammar definition? And how so?

GrayJack avatar Mar 17 '20 09:03 GrayJack

Oh, I thought you were wanting to access the symbols programatically. For highlighting, I believe that should already work out of the box, because Atom re-parses the contents of token trees as rust source code. This is set up here: https://github.com/atom/atom/blob/0b34be796312a94331acbc4602944cfe3666c76a/packages/language-rust-bundled/lib/main.js#L2-L13.

maxbrunsfeld avatar Mar 17 '20 16:03 maxbrunsfeld

Yeah, true, doing that it does reparse the token tree, but that I would like to happen on token_tree_pattern as well on macro_definition, that is possible?

GrayJack avatar Mar 20 '20 09:03 GrayJack

This seems to be of similar concern to https://github.com/tree-sitter/tree-sitter-rust/issues/98

resolritter avatar Jan 07 '21 20:01 resolritter

They're treated separately now

amaanq avatar Feb 17 '24 21:02 amaanq