Java Code Examples for org.antlr.runtime.tree.Tree#addChild()

The following examples show how to use org.antlr.runtime.tree.Tree#addChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteQueryTreeRenderer.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Flattens a subtree of kind "op[A, op[op[B, C], ...]]"
 * (or similar) into "op[A, B, C, ...]". Useful for nodes
 * representing multipart dis/conjunctions and arithmetic
 * expressions to skip unnecessary parentheses
 */
private static void flattenSubtree(Tree node) {
	if ( firstChildOfType( node, node.getType() ) == null ) {
		return;
	}
	Deque<Tree> s = new ArrayDeque<>();
	Deque<Tree> m = new ArrayDeque<>();
	s.add( node );
	Tree n;
	while ( ( n = s.pollLast() ) != null ) {
		if ( n.getType() == node.getType() ) {
			for ( int i = 0; i < n.getChildCount(); ++i ) {
				s.add( n.getChild( i ) );
			}
		}
		else {
			m.add( n );
		}
	}
	for ( int i = 0; i < node.getChildCount(); ++i ) {
		node.setChild( i, m.pollLast() );
	}
	while ( ( n = m.pollLast() ) != null ) {
		node.addChild( n );
	}
}
 
Example 2
Source File: QueryTreeTransformer.java    From cuba with Apache License 2.0 5 votes vote down vote up
public void orderById(String entityVariable, String pkName) {
    if (queryTree.getAstOrderByNode() == null) {
        Tree orderBy = createOrderBy();
        queryTree.getAstTree().addChild(orderBy);
        queryTree.getAstTree().freshenParentAndChildIndexes();

        OrderByFieldNode orderByField = new OrderByFieldNode(JPA2Lexer.T_ORDER_BY_FIELD);
        orderByField.addChild(createPathNode(entityVariable, pkName));
        orderByField.addChild(createDesc());
        orderByField.freshenParentAndChildIndexes();

        orderBy.addChild(orderByField);
        orderBy.freshenParentAndChildIndexes();
    }
}
 
Example 3
Source File: QueryTreeTransformer.java    From cuba with Apache License 2.0 5 votes vote down vote up
public void addEntityInGroupBy(String entityVariable) {
    Tree groupBy = queryTree.getAstGroupByNode();
    if (groupBy != null) {
        groupBy.addChild(createPathNode(entityVariable));
        groupBy.freshenParentAndChildIndexes();
    }
}
 
Example 4
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testPushPop() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
	String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3";
	String found = stream.toString();
	assertEquals(expecting, found);

	// Assume we want to hit node 107 and then "call 102" then return

	int indexOf102 = 2;
	int indexOf107 = 12;
	for (int k=1; k<=indexOf107; k++) { // consume til 107 node
		stream.consume();
	}
	// CALL 102
	assertEquals(107, ((Tree)stream.LT(1)).getType());
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN
	stream.pop();
	assertEquals(107, ((Tree)stream.LT(1)).getType());
}
 
Example 5
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testNestedPushPop() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);

	// Assume we want to hit node 107 and then "call 102", which
	// calls 104, then return

	int indexOf102 = 2;
	int indexOf107 = 12;
	for (int k=1; k<=indexOf107; k++) { // consume til 107 node
		stream.consume();
	}
	assertEquals(107, ((Tree)stream.LT(1)).getType());
	// CALL 102
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103

	// CALL 104
	int indexOf104 = 6;
	stream.push(indexOf104);
	assertEquals(104, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(105, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to UP node in 102 subtree)
	stream.pop();

	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(107, ((Tree)stream.LT(1)).getType());
}
 
Example 6
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testPushPopFromEOF() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);

	while ( stream.LA(1)!=Token.EOF ) {
		stream.consume();
	}
	int indexOf102 = 2;
	int indexOf104 = 6;
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());

	// CALL 102
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());

	// CALL 104
	stream.push(indexOf104);
	assertEquals(104, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(105, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
}