Awoken
Awoken

Reputation: 79

AST rewrite rule for OR lexer expressions

I have the following ANTLR rule:

procedure
 : ('int' | 'char') IDENT '(' args ')' body -> ^(PROCEDURE IDENT (args)* body)
 ;

I want to capture the ('int' | 'char') portion in the AST. As you can see, on the right hand side it doesn't appear, but I'm not sure how to have the chosen 'int' or 'char' appear in the AST. I'd like the 'int' or 'char' portion to be in the tree under the PROCEDURE root but before the IDENT.

Upvotes: 2

Views: 71

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99889

You can label the pair, and then reference the label in the rewrite:

procedure
    : primType=('int' | 'char') IDENT '(' args ')' body
      -> ^(PROCEDURE $primType IDENT args body)
    ;

Upvotes: 3

Related Questions