JavaCC jjtTree node scope action


Node Scope


Each node is associated with a node scope. User actions within this scope can access the node under construction by using the special identifier jjtThis to refer to the node.


Two type of node decoration:

1. A scope is the expansion unit immediately preceding the node decoration (Usually there is no user action between them). This can be a parenthesized expression.


A() #ANode                    // ANode's scope is A()

( A() B() ) #ABNode           // ABNode's scope is ( A() B() )

In the scope, the jjtThis is refer to the node ANode, or ABNode.


2. When the production signature is decorated (perhaps implicitly with the default node), the scope is the entire right hand side of the production including its declaration block.


void A() #ANode :

{ /* BLOCKA */}

{

   /* BLOCKB */

}


The whole right hand side include BLOCKA and BLOCK are scope of #ANode.

In this scope jjtThis is refer to #ANode.


3. Mixed Case



void A() #ANode :

{ /* BLOCKA */}

{

   P1() P2 ( P3() P4() ) #BNode P5

}


So jjtThis in ( P3() P4() ) are refer to #BNode, and jjtThis in other area are refer to #ANode.



User Action


The final user action in a node scope is different from all the others. When the code within it executes, the node's children have already been popped from the stack and added to the node, which has itself been pushed onto the stack. The children can now be accessed via the node's methods such as jjtGetChild().


User actions other than the final one can only access the children on the stack. They have not yet been added to the node, so they aren't available via the node's methods.



P1() P2 ( P3() { action1; } P4() {action2;}) #BNode P5

Action1 is NOT the final user action of #BNode's scope

Action2 is the final user action of #BNode's scope



P1() P2 ( P3() { action1; } P4() {action2;} {action3} ) #BNode P5

Action1 and action2 is NOT the final user action of #BNode's scope

Action3 is the final user action of #BNode's scope


void P() :

{}

{

    P1() P2() ...

    { action1;}

}

Action1 is the final user action of #PNode



void P() :

{}

{

    P1() P2() ...

    { action1;}

    { action2;}

}

Action1 is not the final user action of #PNode

Action2 is the final user action of #PNode



Sample


void P() :
{}
{
  A() ( B() B() ) #N C()
}


The Node's parent-children relationship is as following:




To better understand this scope and user action, let's mark a few point in the expansion, as:


p1 A() p2 ( p3 B() p4 B() p5 ) p6 #N p7 C() p8 p9


(Usually p6 should not have any user actions, otherwise #N cannot get its preceding expansion, it will got none as preceding expansion, but in fact, we wish ( B() B() ) is its preceding expanding)


See P()'s implementation:

public void P() throws ParseException {
  ASTP jjtn000 = new ASTP(JJTP);
  jjtree.openNodeScope(jjtn000);
// p1: jjtThis = P()
  A();
// p2: jjtThis = P()
  ASTN jjtn001 = new ASTN(JJTN);
  jjtree.openNodeScope(jjtn001);
// p3: jjtThis = N()
  B();  
// p4: jjtThis = N()
  B();  
  jjtree.closeNodeScope(jjtn001, true);
// p5: jjtThis = N(), p5 is the N()'s final user action
// p7: jjtThis = P()
  C();
// p8: jjtThis = P()
  jjtree.closeNodeScope(jjtn000, true);
// p9: jjtThis = P(), p9 is the P()'s final user action
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值