alias only appears to work on rules that appear in the grammar more than once and are not aliased every time they appear. Please see yitzchak/tree-sitter-alias-mwe for a minimum working example. Specifically, the grammar is:
module.exports = grammar({
name: 'alias_mwe',
rules: {
source: $ => repeat(
choice(
seq(
alias($.foo, 'fu'),
$.bar
),
seq(
$.quux,
alias($.bar, 'baz')
)
)
),
foo: $ => 'foo',
bar: $ => 'bar',
quux: $ => 'quux'
}
});
When scanning foo bar quux bar this should produce (source (fu) (bar) (quux) (baz)) but it actually produces (source (bar) (quuz) (baz)). There is a test in corpus that demonstrates this.
@Aerijo this appears to be the limiting factor on yitzchak/tree-sitter-latex#5
Hey @yitzchak, sorry that I missed this one. Is this still a problem on [email protected]?
@maxbrunsfeld Yes it is, although it does work if one uses named aliases such as $.fu versus anonymous aliases such as "fu". Maybe the documentation just needs a note that aliasing be done via alias($.foo, $.fu) and not via alias($.foo, 'fu')
Oh, alias($.foo, 'fu') should work, so this is still a bug. Thanks!
Okay, please note that I am no longer using it tree-sitter-latex so it is not a high priority for myself.
thx, for documenting that alias($.foo, $.fu) works. was really confused by this not working as I expected.
I just re-read the description, and realized that I don't think there's a bug here.
When scanning foo bar quux bar this should produce
(source (fu) (bar) (quux) (baz))but it actually produces(source (bar) (quuz) (baz))
No, (source (bar) (quuz) (baz)) is correct. Calling alias($.foo, 'fu') means that you want the foo symbol to appear as an anonymous node - as if you had written "fu" in the grammar. For readability, anonymous nodes don't show up in the S-expressions.
The fu node would still be accessible using the node APIs, just like other anonymous nodes like "(", ",", etc.
Most helpful comment
I just re-read the description, and realized that I don't think there's a bug here.
No,
(source (bar) (quuz) (baz))is correct. Callingalias($.foo, 'fu')means that you want thefoosymbol to appear as an anonymous node - as if you had written"fu"in the grammar. For readability, anonymous nodes don't show up in the S-expressions.The
funode would still be accessible using the node APIs, just like other anonymous nodes like"(",",", etc.