0

The question is about how to get access to current tree node inside {"action"} block in the grammar.

I'm talking about Java target, so inside generated rule-method I'd like to gain access to object root_1 (see below, standard generated code, SUBJECT here is the token inside my grammar, it doesn't matter):

  {
    Object root_1 = (Object)adaptor.nil();
    root_1 = (Object)adaptor.becomeRoot((Object)adaptor.create(SUBJECT, "SUBJECT"), root_1);
    adaptor.addChild(root_1, stream_noun.nextTree());
    adaptor.addChild(root_0, root_1);
  }

Inside grammar I'd like to have an ability to do something like:

subject :   noun -> ^(SUBJECT noun) { ... place code here to work with `root_1` object ... } ;

Could somebody suggest any idea? Thanks.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
Andremoniy
  • 34,031
  • 20
  • 135
  • 241

2 Answers2

1

I believe you're looking for the $tree property.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Well, thanks for your answer. But as I see, `$tree` is just the reference to variable `root_0`. It also could be placed only in `@after` construction (before that it contains just `null` value). So, my question is still opened, because I'm looking for handsome way how to get reference to `root_1` variable. – Andremoniy Mar 27 '13 at 17:17
  • Furthermore, as `$tree` is actually `retval.tree = root_0`, it also must be cast to `CommonTree` type. – Andremoniy Mar 27 '13 at 17:19
  • @Andremoniy you need to set the `options{ASTLabelType=CommonTree;}` option for `$tree` to be that type. – Sam Harwell Mar 27 '13 at 18:03
  • That changes things, thanks. I think, that our answers must be mixed for full picture. – Andremoniy Mar 27 '13 at 18:09
0

Well, I found some ugly method. I can get an access to mentioned root_1 object, using @after construction, and gaining access to root_0 object:

subject
@after{
    // ...((CommonTree)root_0).getChild(0)... - this is `root_1` object.
}
    :   noun -> ^( SUBJECT noun);

But if somebody will suggest more appropriate method - it will be welcomed.

UPD: as dear 280z28 mentioned, this one ((CommonTree)root_0) could be replaced with $tree with set options{ASTLabelType=CommonTree;}.

Community
  • 1
  • 1
Andremoniy
  • 34,031
  • 20
  • 135
  • 241