JavaCC jjtTree node scope action

来源:互联网 发布:淘宝网完美芦荟胶 编辑:程序博客网 时间:2024/06/04 17:54


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 #Np7 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
原创粉丝点击