Package org.antlr.runtime.tree

Examples of org.antlr.runtime.tree.CommonTree


                    Class<?> _return = classForName(parserName+"$"+testRuleName+"_return");
                    Method[] methods = _return.getDeclaredMethods();
                    for(Method method : methods) {
                      if ( method.getName().equals("getTree") ) {
                        Method returnName = _return.getMethod("getTree");
                          CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);
                          astString = tree.toStringTree();
                      }
                      else if ( method.getName().equals("getTemplate") ) {
                        Method returnName = _return.getMethod("getTemplate");
                        StringTemplate st = (StringTemplate) returnName.invoke(ruleReturn);
                        stString = st.toString();
View Full Code Here


            /** Invoke grammar rule, and get the return value */
            Object ruleReturn = ruleName.invoke(parObj);

            Class<?> _return = classForName(parserName+"$"+testRuleName+"_return");
          Method returnName = _return.getMethod("getTree");
          CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);

          // Walk resulting tree; create tree nodes stream first
          CommonTreeNodeStream nodes;
          if ( customTreeAdaptor!=null ) {
            nodes = new CommonTreeNodeStream(customTreeAdaptor, tree);
          }
          else {
            nodes = new CommonTreeNodeStream(tree);
          }
          // AST nodes have payload that point into token stream
          nodes.setTokenStream(tokens);
          // Create a tree walker attached to the nodes stream
          treeParser = classForName(treeParserPath).asSubclass(TreeParser.class);
            Constructor<? extends TreeParser> treeParConstructor = treeParser.getConstructor(TreeNodeStream.class);
            TreeParser treeParObj = treeParConstructor.newInstance(nodes)// makes new instance of tree parser
          // Invoke the tree rule, and store the return value if there is
            Method treeRuleName = treeParser.getMethod(testTreeRuleName);
            Object treeRuleReturn = treeRuleName.invoke(treeParObj);

            String astString = null;
            String stString = null;
            /** If tree rule has return value, determine if it contains an AST or a ST */
            if ( treeRuleReturn!=null ) {
                if ( treeRuleReturn.getClass().toString().indexOf(testTreeRuleName+"_return")>0 ) {
                  try // NullPointerException may happen here...
                    Class<?> _treeReturn = classForName(treeParserPath+"$"+testTreeRuleName+"_return");
                    Method[] methods = _treeReturn.getDeclaredMethods();
                  for(Method method : methods) {
                      if ( method.getName().equals("getTree") ) {
                        Method treeReturnName = _treeReturn.getMethod("getTree");
                          CommonTree returnTree = (CommonTree) treeReturnName.invoke(treeRuleReturn);
                            astString = returnTree.toStringTree();
                      }
                      else if ( method.getName().equals("getTemplate") ) {
                        Method treeReturnName = _return.getMethod("getTemplate");
                        StringTemplate st = (StringTemplate) treeReturnName.invoke(treeRuleReturn);
                        stString = st.toString();
View Full Code Here

    newRoot.sanityCheckParentAndChildIndexes();
  }

  @Test public void testBecomeRoot5() throws Exception {
    // ^(nil 5) becomes new root of ^(101 102 103)
    CommonTree newRoot = new CommonTree((Token)null);
    newRoot.addChild(new CommonTree(new CommonToken(5)));

    CommonTree oldRoot = new CommonTree(new CommonToken(101));
    oldRoot.addChild(new CommonTree(new CommonToken(102)));
    oldRoot.addChild(new CommonTree(new CommonToken(103)));

    TreeAdaptor adaptor = new CommonTreeAdaptor();
    adaptor.becomeRoot(newRoot, oldRoot);
    newRoot.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

    newRoot.sanityCheckParentAndChildIndexes();
  }

  @Test public void testBecomeRoot6() throws Exception {
    // emulates construction of ^(5 6)
    CommonTree root_0 = (CommonTree)adaptor.nil();
    CommonTree root_1 = (CommonTree)adaptor.nil();
    root_1 = (CommonTree)adaptor.becomeRoot(new CommonTree(new CommonToken(5)), root_1);

    adaptor.addChild(root_1, new CommonTree(new CommonToken(6)));

    adaptor.addChild(root_0, root_1);

    root_0.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

  // Test replaceChildren

  @Test(expected = IllegalArgumentException.class)
  public void testReplaceWithNoChildren() throws Exception {
    CommonTree t = new CommonTree(new CommonToken(101));
    CommonTree newChild = new CommonTree(new CommonToken(5));
    t.replaceChildren(0, 0, newChild);
  }
View Full Code Here

    t.replaceChildren(0, 0, newChild);
  }

  @Test public void testReplaceWithOneChildren() throws Exception {
    // assume token type 99 and use text
    CommonTree t = new CommonTree(new CommonToken(99,"a"));
    CommonTree c0 = new CommonTree(new CommonToken(99, "b"));
    t.addChild(c0);

    CommonTree newChild = new CommonTree(new CommonToken(99, "c"));
    t.replaceChildren(0, 0, newChild);
    String expecting = "(a c)";
    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }

  @Test public void testReplaceInMiddle() throws Exception {
    CommonTree t = new CommonTree(new CommonToken(99, "a"));
    t.addChild(new CommonTree(new CommonToken(99, "b")));
    t.addChild(new CommonTree(new CommonToken(99, "c"))); // index 1
    t.addChild(new CommonTree(new CommonToken(99, "d")));

    CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
    t.replaceChildren(1, 1, newChild);
    String expecting = "(a b x d)";
    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }

  @Test public void testReplaceAtLeft() throws Exception {
    CommonTree t = new CommonTree(new CommonToken(99, "a"));
    t.addChild(new CommonTree(new CommonToken(99, "b"))); // index 0
    t.addChild(new CommonTree(new CommonToken(99, "c")));
    t.addChild(new CommonTree(new CommonToken(99, "d")));

    CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
    t.replaceChildren(0, 0, newChild);
    String expecting = "(a x c d)";
    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }

  @Test public void testReplaceAtRight() throws Exception {
    CommonTree t = new CommonTree(new CommonToken(99, "a"));
    t.addChild(new CommonTree(new CommonToken(99, "b")));
    t.addChild(new CommonTree(new CommonToken(99, "c")));
    t.addChild(new CommonTree(new CommonToken(99, "d"))); // index 2

    CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
    t.replaceChildren(2, 2, newChild);
    String expecting = "(a b c x)";
    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }
View Full Code Here

    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
  }

  @Test public void testReplaceOneWithTwoAtLeft() throws Exception {
    CommonTree t = new CommonTree(new CommonToken(99, "a"));
    t.addChild(new CommonTree(new CommonToken(99, "b")));
    t.addChild(new CommonTree(new CommonToken(99, "c")));
    t.addChild(new CommonTree(new CommonToken(99, "d")));

    CommonTree newChildren = (CommonTree)adaptor.nil();
    newChildren.addChild(new CommonTree(new CommonToken(99,"x")));
    newChildren.addChild(new CommonTree(new CommonToken(99,"y")));

    t.replaceChildren(0, 0, newChildren);
    String expecting = "(a x y c d)";
    assertEquals(expecting, t.toStringTree());
    t.sanityCheckParentAndChildIndexes();
View Full Code Here

TOP

Related Classes of org.antlr.runtime.tree.CommonTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.