public static void recursivelyFixWindowFucntion( CommonTree srcTree) { if (srcTree.getChildCount() == 0) { return; } // process node String text = srcTree.getText(); System.out.println("Currently processed node==> " + text); if (text.equals(PARTITION_BY)) { return; } else if (text.equals(ORDER_BY)) { if (srcTree.getChildCount() == 0) { if (srcTree.getParent() != null) { // found a matching node srcTree.getParent().deleteChild(srcTree.getChildIndex()); srcTree.getParent().freshenParentAndChildIndexes(); } } return; } // process children for (Tree t : children(srcTree)) { recursivelyFixWindowFucntion((CommonTree)t); } }
private static Boolean findAggFuncRecursively( CommonTree tree, List<String> aggregateFuncs) { Boolean ret = false; if (tree.getText().equals(FUNCTION_CALL)) { // function String funcName = tree.getChild(0).getChild(0).getText(); //FUNCTION_CALL->QNAME->xxx if (aggregateFuncs.contains(funcName.toLowerCase())) { return true; } } for (Tree t : children(tree)) { ret = findAggFuncRecursively((CommonTree)t, aggregateFuncs); if (ret == true) { return true; } } return false; }
public static void patchTreeByTreesGroupBy(CommonTree srcTree, CommonTree targetTree) { //System.out.println("Text = " + srcTree.getText() + " ChildCount = " + srcTree.getChildCount() + " Type = " + srcTree.getType()); if (srcTree.getChildCount() == 0) { return; } // process node if (srcTree.getText().equals(QUERY_SPEC_NODE)) { // patch targetTree.setParent(srcTree); srcTree.addChild(targetTree); return; } // process children for (Tree t : children(srcTree)) { patchTreeByTreesGroupBy((CommonTree)t, targetTree); } return; }
private static void gatherTypes(CommonTree srcTree, Map<String, Integer> typesMap) { if (srcTree.getChildCount() == 0) { return; } // process node if (!typesMap.containsKey(srcTree.getText())) { typesMap.put(srcTree.getText(), new Integer(srcTree.getType())); } // process children for (Tree subTree : children(srcTree)) { gatherTypes((CommonTree)subTree, typesMap); } return; }
public static void updateTreeByString(CommonTree tree, String target, String goal) { if (tree.getChildCount() == 0) { // leaf if (tree.getText().equals(target)) { //modifyText(tree, goal+'.'+target); modifyText(tree, goal); //System.out.println("Found 1!!!"); } return; } // process node if (tree.getText().equals(target)) { modifyText(tree, goal+'.'+target); //System.out.println("Found 2!!!"); } // process children for (Tree t : children(tree)) { updateTreeByString((CommonTree)t, target, goal); } return; }
private static String treeToString(Tree tree, int depth) { if (tree.getChildCount() == 0) { return quotedString(tree.toString()); } StringBuilder sb = new StringBuilder(); sb.append("("); sb.append(tree.toString()); for (Tree t : children(tree)) { if (hasSubtree(t) && (leafCount(tree) > 2)) { sb.append("\n"); sb.append(repeat(" ", depth)); } else { sb.append(" "); } sb.append(treeToString(t, depth + 1)); } sb.append(")"); return sb.toString(); }
/** * Gets the children as a List * @param tree Tree * @return either emptyList or the children. */ public static List<Tree> getChildren(Tree tree) { if (tree!=null && tree.getChildCount() > 0) { List<Tree> children = new ArrayList<Tree>(tree.getChildCount()); for (int i = 0; i < tree.getChildCount(); i++) { Tree child = tree.getChild(i); children.add(child); } return children; } //Default return Collections.emptyList(); }
/** Walk ancestors of this node until we find ALT with * alt!=null or leftRecursiveAltInfo!=null. Then grab label if any. * If not a rule element, just returns null. */ public String getAltLabel() { List<? extends Tree> ancestors = this.getAncestors(); if ( ancestors==null ) return null; for (int i=ancestors.size()-1; i>=0; i--) { GrammarAST p = (GrammarAST)ancestors.get(i); if ( p.getType()== ANTLRParser.ALT ) { AltAST a = (AltAST)p; if ( a.altLabel!=null ) return a.altLabel.getText(); if ( a.leftRecursiveAltInfo!=null ) { return a.leftRecursiveAltInfo.altLabel; } } } return null; }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) { if ( t==null ) return false; GrammarAST blk = (GrammarAST)t.getFirstChildWithType(BLOCK); if ( blk==null ) return false; int n = blk.getChildren().size(); for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.getChildren().get(i); Tree first = alt.getChild(0); if ( first==null ) continue; if (first.getType() == ELEMENT_OPTIONS) { first = alt.getChild(1); if (first == null) { continue; } } if ( first.getType()==RULE_REF && first.getText().equals(ruleName) ) return true; Tree rref = first.getChild(1); if ( rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName) ) return true; } return false; }
/** Make sure that action is last element in outer alt; here action, a2, z, and zz are bad, but a3 is ok: (RULE A (BLOCK (ALT {action} 'a'))) (RULE B (BLOCK (ALT (BLOCK (ALT {a2} 'x') (ALT 'y')) {a3}))) (RULE C (BLOCK (ALT 'd' {z}) (ALT 'e' {zz}))) */ protected void checkElementIsOuterMostInSingleAlt(GrammarAST tree) { CommonTree alt = tree.parent; CommonTree blk = alt.parent; boolean outerMostAlt = blk.parent.getType() == RULE; Tree rule = tree.getAncestor(RULE); String fileName = tree.getToken().getInputStream().getSourceName(); if ( !outerMostAlt || blk.getChildCount()>1 ) { ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE; g.tool.errMgr.grammarError(e, fileName, tree.getToken(), rule.getChild(0).getText()); } }
public void replaceWithCount(EntityReference entityRef) { Tree selectedItems = tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS); boolean isDistinct = "DISTINCT".equalsIgnoreCase(selectedItems.getChild(0).getText()); if (!(isDistinct && selectedItems.getChildCount() == 2 || selectedItems.getChildCount() == 1)) throw new IllegalStateException("Cannot replace with count if multiple fields selected"); SelectedItemNode selectedItemNode; if (isDistinct) selectedItems.deleteChild(0); selectedItemNode = (SelectedItemNode) selectedItems.getChild(0); AggregateExpressionNode countNode = createCountNode(entityRef, isDistinct); selectedItemNode.deleteChild(0); selectedItemNode.addChild(countNode); Tree orderBy = tree.getFirstChildWithType(JPA2Lexer.T_ORDER_BY); if (orderBy != null) { tree.deleteChild(orderBy.getChildIndex()); } tree.freshenParentAndChildIndexes(); }
public PathNode getSelectedPathNode() { Tree selectedItems = tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS); boolean isDistinct = "DISTINCT".equalsIgnoreCase(selectedItems.getChild(0).getText()); SelectedItemNode selectedItemNode; if (isDistinct) { if (selectedItems.getChildCount() != 2) throw new IllegalStateException("Cannot select path node if multiple fields selected"); selectedItemNode = (SelectedItemNode) selectedItems.getChild(1); } else { if (selectedItems.getChildCount() != 1) throw new IllegalStateException("Cannot select path node if multiple fields selected"); selectedItemNode = (SelectedItemNode) selectedItems.getChild(0); } if (!(selectedItemNode.getChild(0) instanceof PathNode)) { throw new IllegalStateException("An entity path is assumed to be selected"); } return (PathNode) selectedItemNode.getChild(0); }
private void executeHelp(Tree tree) { if (tree.getChildCount() > 0) { String token = tree.getChild(0).getText(); for (CliCommandHelp ch : getHelp().commands) { if (token.equals(ch.name)) { sessionState.out.println(ch.help); break; } } } else { sessionState.out.println(getHelp().help); } }
/** * Command: CONSISTENCYLEVEL AS (ONE | QUORUM ...) * Tree: ^(NODE_CONSISTENCY_LEVEL AS (ONE | QUORUM ...)) * @param statement - tree representing current statement */ private void executeConsistencyLevelStatement(Tree statement) { if (!CliMain.isConnected()) return; String userSuppliedLevel = statement.getChild(0).getText().toUpperCase(); try { consistencyLevel = ConsistencyLevel.valueOf(userSuppliedLevel); } catch (IllegalArgumentException e) { String elements = "ONE, TWO, THREE, QUORUM, ALL, LOCAL_QUORUM, EACH_QUORUM, ANY"; sessionState.out.println(String.format("'%s' is invalid. Available: %s", userSuppliedLevel, elements)); return; } sessionState.out.println(String.format("Consistency level is set to '%s'.", consistencyLevel)); }
/** * Command: ASSUME <columnFamily> (VALIDATOR | COMPARATOR | KEYS | SUB_COMPARATOR) AS <type> * Tree: ^(NODE_ASSUME <columnFamily> (VALIDATOR | COMPARATOR | KEYS | SUB_COMPARATOR) <type>)) * @param statement - tree representing current statement */ private void executeAssumeStatement(Tree statement) { if (!CliMain.isConnected() || !hasKeySpace()) return; String cfName = CliCompiler.getColumnFamily(statement, currentCfDefs()); // VALIDATOR | COMPARATOR | KEYS | SUB_COMPARATOR String assumptionElement = statement.getChild(1).getText().toUpperCase(); // Could be UTF8Type, IntegerType, LexicalUUIDType etc. String defaultType = CliUtils.unescapeSQLString(statement.getChild(2).getText()); if (applyAssumption(cfName, assumptionElement, defaultType)) { assumptions.addAssumption(keySpace, cfName, assumptionElement, defaultType); sessionState.out.println(String.format("Assumption for column family '%s' added successfully.", cfName)); } }
public CosmasCondition (Tree cond) { String nodeString = cond.toStringTree(); if (nodeString.startsWith("-")) { negated = true; nodeString = nodeString.substring(1); } else if (nodeString.startsWith("+")) { nodeString = nodeString.substring(1); } elem = nodeString.substring(0, 1); nodeString = nodeString.substring(1); position = nodeString.equals("a") ? "frames:startswith" : "frames:endswith"; }
static Tree parseTree(CharSource input) throws IOException { try (Reader reader = input.openStream()) { ThriftLexer lexer = new ThriftLexer(new ANTLRReaderStream(reader)); ThriftParser parser = new ThriftParser(new CommonTokenStream(lexer)); try { Tree tree = (Tree) parser.document().getTree(); if (parser.getNumberOfSyntaxErrors() > 0) { throw new IllegalArgumentException("syntax error"); } return tree; } catch (RecognitionException e) { throw new IllegalArgumentException(e); } } }
public final Column getColumn(Tree columnTree) throws DataLawyerException { Column toRet = null; for (Column c : columns) { if (c.getInfo().equalsByTree(columnTree)) { if (toRet == null) toRet = c; else throw new DataLawyerException("Ambiguous column: " + toRet + " vs " + c + " while parsing column: " + columnTree); } } if (toRet == null) { MyLogger.getLog().info( "This column not found: " + columnTree.toString()); MyLogger.getLog().info("Known columns were: " + columns.toString()); } return toRet; }
@Override public boolean equalsByTree(Tree tree) { if (tree == null) return false; String op = tree.getText(); if (op == null) return false; if (!op.equalsIgnoreCase(fnName)) return false; if (tree.getChildCount() != inputs.size()) return false; for (int i = 0; i < inputs.size(); ++i) { Column c = inputs.get(i); if (!c.getInfo().equalsByTree(tree.getChild(i))) return false; } return true; }
private void processOPEND_OPBEG (Tree node) { // Step I: create group String nodeCat = getNodeCat(node); Map<String, Object> beggroup = new HashMap<String, Object>(); beggroup.put("@type", "koral:reference"); beggroup.put("operation", "operation:focus"); ArrayList<Integer> spanRef = new ArrayList<Integer>(); if (nodeCat.equals("OPBEG")) { spanRef.add(0); spanRef.add(1); } else if (nodeCat.equals("OPEND")) { spanRef.add(-1); spanRef.add(1); } beggroup.put("spanRef", spanRef); beggroup.put("operands", new ArrayList<Object>()); objectStack.push(beggroup); stackedObjects++; // Step II: decide where to put putIntoSuperObject(beggroup, 1); }
private void processOPNHIT (Tree node) { Integer[] classRef = new Integer[] { classCounter + 128 + 1, classCounter + 128 + 2 }; // classRef.add(classCounter + 1); // yes, do this twice (two // classes)! Map<String, Object> group = KoralObjectGenerator.makeReference(classCounter + 128); Map<String, Object> classRefCheck = KoralObjectGenerator.makeClassRefOp( ClassRefOp.INVERSION, classRef, classCounter + 128); ArrayList<Object> operands = new ArrayList<Object>(); operands.add(classRefCheck); group.put("operands", operands); classCounter++; // direct child is OPPROX wrapOperandInClass(node.getChild(0), 1, 128 + classCounter++); wrapOperandInClass(node.getChild(0), 2, 128 + classCounter++); objectStack.push(classRefCheck); stackedObjects++; putIntoSuperObject(group, 1); }
private Predicate<T> toPredicate(Tree r) throws QueryParseException, IllegalArgumentException { switch (r.getType()) { case AND: return and(children(r)); case OR: return or(children(r)); case NOT: return not(toPredicate(onlyChildOf(r))); case DEFAULT_FIELD: return defaultField(onlyChildOf(r)); case FIELD_NAME: return operator(r.getText(), onlyChildOf(r)); default: throw error("Unsupported operator: " + r); } }
private static CommonTree createFunctionCall(String funcName, Tree rest) { CommonTree template = VeroSqlParser.parseExpression(funcName.concat("(").concat("abc").concat(")")); int childIndex = template.getChild(1).getChildIndex(); template.setChild(childIndex, rest); return template; }
private static void recursivelyPatchWindowFunction( CommonTree srcTree, List<String> partitionByToRemove, List<String> orderByToRemove) { if (srcTree.getChildCount() == 0) { return; } // process node if (srcTree.getText().equals(PARTITION_BY)) { treeRemoveItem(srcTree, QNAME, partitionByToRemove); if (srcTree.getChildCount() == 0) { if (srcTree.getParent() != null) { srcTree.getParent().deleteChild(srcTree.getChildIndex()); srcTree.getParent().freshenParentAndChildIndexes(); } } return; } else if (srcTree.getText().equals(ORDER_BY)) { // 20150311 ToDo: need to apply partition_by solution but order by has asc/desc to string... treeRemoveItem(srcTree, ORDER_BY_ITEM, orderByToRemove); if (srcTree.getChildCount() == 0) { if (srcTree.getParent() != null) { srcTree.getParent().deleteChild(srcTree.getChildIndex()); srcTree.getParent().freshenParentAndChildIndexes(); } } return; } // process children for (Tree t : children(srcTree)) { recursivelyPatchWindowFunction((CommonTree)t, partitionByToRemove, orderByToRemove); } }
private static String combineNames(CommonTree node) { List<Tree> children = node.getChildren(); List<String> names = new ArrayList<String>(); for (Tree child : children) { names.add(child.getText()); } Joiner joiner = Joiner.on(".").skipNulls(); return joiner.join(names); }
public static void patchTreeByTree(CommonTree srcTree, String target, CommonTree goalTree) { if (srcTree.getChildCount() == 0) { // leaf //System.out.println("leaf " + srcTree.getToken().getText()); if (srcTree.getToken().getText().equals(target)) { srcTree.deleteChild(0); srcTree.addChild(goalTree); System.out.println("Found 1!!!"); } return; } // process node System.out.println("node " + srcTree.getToken().getText()); if (srcTree.getToken().getText().equals(target)) { srcTree.deleteChild(0); srcTree.addChild(goalTree); //System.out.println("Found 2!!!"); } // process children for (Tree t : children(srcTree)) { patchTreeByTree((CommonTree)t, target, goalTree); } return; }
public static void patchTreeByString(CommonTree tree, List<String> columns, String goal) { if (tree.getChildCount() == 0) { // leaf if (columns.contains(tree.getText())) { Tree parent = tree.getParent(); // only patch tree when it's QNAME node if (parent.getText().equals("QNAME")) { Tree tmpTree = createGenericNode(goal); // remove QNAME at index 0 tmpTree.deleteChild(0); tmpTree.setParent(tree.getParent()); parent.deleteChild(0); parent.addChild(tmpTree); parent.addChild(tree); ((CommonTree)tmpTree).getToken().setText(goal); ((CommonTree)tmpTree).getToken().setType(tree.getType()); } } return; } // process children for (Tree t : children(tree)) { patchTreeByString((CommonTree)t, columns, goal); } return; }
private static List<Tree> children(Tree tree) { List<Tree> list = new ArrayList<Tree>(); for (int i = 0; i < tree.getChildCount(); i++) { list.add(tree.getChild(i)); } return list; }
public static String treeToPureString(Tree tree) { if (tree.getChildCount() == 0) { return quotedString(tree.toString()); } StringBuilder sb = new StringBuilder(); for (Tree t : children(tree)) { sb.append(treeToPureString(t)); } return sb.toString(); }
private static boolean hasSubtree(Tree tree) { for (Tree t : children(tree)) { if (t.getChildCount() > 0) { return true; } } return false; }
private static int leafCount(Tree tree) { if (tree.getChildCount() == 0) { return 1; } int n = 0; for (Tree t : children(tree)) { n += leafCount(t); } return n; }
static private Float findFuzzy(Tree node) { for (int i = 0, l = node.getChildCount(); i < l; i++) { CommonTree child = (CommonTree) node.getChild(i); if (child.getType() == FTSParser.FUZZY) { String fuzzyString = child.getChild(0).getText(); float fuzzy = Float.parseFloat(fuzzyString); return Float.valueOf(fuzzy); } } return null; }
static private String getText(List<? extends Object> nodes) { StringBuilder builder = new StringBuilder(); for(Object node : nodes) { builder.append(getText((Tree)node, false)); } return builder.toString(); }
static private String getText(Tree node) { String text = node.getText(); int index; switch (node.getType()) { case CMIS_FTSParser.FTSWORD: index = text.indexOf('\\'); if (index == -1) { return text; } else { return unescape(text); } case CMIS_FTSParser.FTSPHRASE: String phrase = text.substring(1, text.length() - 1); index = phrase.indexOf('\\'); if (index == -1) { return phrase; } else { return unescape(phrase); } default: return text; } }
/** * Walks a query with a callback for each operation * @param query the query * @param callback a callback */ public static void walk(Query query, WalkerCallback callback) { CommonTree tree = query.getTree(); if (tree != null) { LinkedList<Tree> stack = new LinkedList<Tree>(); stack.push(tree); callbackTree(tree, callback, false); } }
@Override public void registerPersisterSpace(Tree entityName, Tree alias) { String put = entityNameByAlias.put( alias.getText(), entityName.getText() ); if ( put != null && !put.equalsIgnoreCase( entityName.getText() ) ) { throw new UnsupportedOperationException( "Alias reuse currently not supported: alias " + alias.getText() + " already assigned to type " + put ); } propertyHelper.registerEntityAlias( entityName.getText(), alias.getText() ); }
@Override public void registerJoinAlias(Tree alias, PropertyPath path) { PropertyPath put = propertyPathByAlias.put( alias.getText(), path ); if ( put != null && !put.equals( path ) ) { throw new UnsupportedOperationException( "Alias reuse currently not supported: alias " + alias + " already assigned to type " + put ); } }