1
votes

I'm building an AST using ANTLR. I want to write a production that matches this string:

${identifier}

In my grammar file I have:

reference
  : DOLLAR LBRACE IDENT RBRACE -> ^(NODE_VAR_REFERENCE IDENT)
;

This works fine. I'm using my own adaptor to emit tree nodes. The rewrite rule used creates for me two nodes: one for NODE_VAR_REFERENCE and one for IDENT.

What I want to do is create only one node (for the NODE_VAR_REFERENCE token), and this node must have the IDENT token in its "token" field.

Is this possible using a rewrite rule? Thanks.

1

1 Answers

2
votes

Well, to let IDENT be the token of the node NODE_VAR_REFERENCE would mean there isn't any NODE_VAR_REFERENCE at all. A token consists of a type (NODE_VAR_REFERENCE or IDENT) and some text this token matched. To let a tree node's token become IDENT would mean both the type and text would be that of IDENT (losing the NODE_VAR_REFERENCE token, or type).

What you probably mean is to have single node with type NODE_VAR_REFERENCE and the text of IDENT, in which case you could do something like this:

reference
 : DOLLAR LBRACE IDENT RBRACE -> NODE_VAR_REFERENCE[$IDENT.text]
 ;