remove keywords from parse tree

similar to Shortening the parse tree
i want to make the parse tree more compact

i have this rule

If {
  keyword<"if"> expression
  keyword<"then"> expression
  keyword<"else"> expression

which will parse

if a then b else c

how can i remove the if/then/else keywords from the parse tree?
so that i get

if a then b else c

as workaround im using tokens with lowercase names …
maybe there is a prettier solution?

If {
  hiddenKeywordIf expression
  hiddenKeywordThen expression
  hiddenKeywordElse expression

@tokens {
  hiddenKeywordIf { "if" }
  hiddenKeywordThen { "then" }
  hiddenKeywordElse { "else" }
  @precedence {
    hiddenKeywordIf, hiddenKeywordThen, hiddenKeywordElse,
    otherToken1, otherToken2

That’s pretty much how you’d do this. Using a parameterized rule like keyword to create them also works. You just have to make sure you don’t give the specialized token a name.

1 Like