private void enterParseTree(ParseTree tree){ int cc = tree.getChildCount(); for(int i=0;i<cc;i++){ ParseTree c = tree.getChild(i); if(c instanceof RuleNode){ enterParseTree(c); }else if (c instanceof TerminalNode){ Token t = ((TerminalNode)c).getSymbol(); index2token.put(tokenCounter, t); token2tree.put(t, c); tokenCounter ++; }else{ System.err.println("unknown node:" + c); } } }
@SuppressWarnings("unchecked") @Override public Object visitChildren(final RuleNode node) { final int n = node.getChildCount(); for (int i = 0; i < n; i++) { final ParseTree c = node.getChild(i); c.accept(this); } final String textToFind = node.getText(); if (StringUtils.containsIgnoreCase(textToFind, tempText) || StringUtils.containsIgnoreCase(tempText, textToFind)) { nodes.add(new ParsedNode(node)); } return null; }
protected void interpretVariable(ParseTree tree) { String varName = null; Set<PQLTask> varValue = null; for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_varName: varName = interpretName(child); break; case PQLParser.RULE_setOfTasks: varValue = interpretSetOfTasks(child); break; } } } this.variables.put(varName,varValue); }
protected void interpretAttribute(ParseTree tree) { ParseTree child = tree.getChild(0); if (child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_universe : this.attributes.add(new PQLAttribute()); break; case PQLParser.RULE_attributeName : this.attributes.add(new PQLAttribute(this.interpretString(child))); break; } } }
protected void interpretLocation(ParseTree tree) { ParseTree child = tree.getChild(0); if (child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_universe : this.locations.add(new PQLLocation()); break; case PQLParser.RULE_locationPath : this.locations.add(new PQLLocation(this.interpretString(child))); break; } } }
protected boolean interpretPredicate(ParseTree tree) { if (tree==null) return true; // WHERE clause is not specified for this query! boolean result = false; ParseTree child = tree.getChild(0); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_proposition: result = interpretProposition(child); break; case PQLParser.RULE_conjunction: result = interpretConjunction(child); break; case PQLParser.RULE_disjunction: result = interpretDisjunction(child); break; case PQLParser.RULE_logicalTest: result = interpretLogicalTest(child); break; } return result; }
protected boolean interpretSetComparison(ParseTree tree) { Set<PQLTask> A = interpretSetOfTasks(tree.getChild(0)); Set<PQLTask> B = interpretSetOfTasks(tree.getChild(2)); ParseTree child = tree.getChild(1).getChild(0); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_identical: return A.equals(B) ? true : false; case PQLParser.RULE_different: return A.equals(B) ? false : true; case PQLParser.RULE_overlapsWith: for (PQLTask task : A) if (B.contains(task)) return true; return false; case PQLParser.RULE_subsetOf: return B.containsAll(A) ? true : false; case PQLParser.RULE_properSubsetOf: return B.containsAll(A) && !A.equals(B) ? true : false; } return false; }
protected PQLTask interpretTask(ParseTree tree) { String label = ""; double similarity = 1.0; for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if (child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_approximate: similarity = labelMngr.getDefaultLabelSimilarityThreshold(); break; case PQLParser.RULE_label: label = this.interpretLabel(child); break; case PQLParser.RULE_similarity: similarity = this.interpretSimilarity(child); break; } } } return new PQLTask(label, similarity); }
protected PQLTrace interpretInsertTrace(ParseTree tree) { PQLTrace trace = new PQLTrace(); for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i).getChild(0); //getting a task or '*' if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); if (ruleIndex == PQLParser.RULE_task) { //is task trace.addTask(this.interpretTask(child)); } else { // is '*' trace.addTask(this.interpretTraceUniverse()); trace.setHasAsterisk(true); } } } return trace; }
protected boolean interpretParentheses(ParseTree tree) { ParseTree child = tree.getChild(1); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_proposition: return interpretProposition(child); case PQLParser.RULE_conjunction: return interpretConjunction(child); case PQLParser.RULE_disjunction: return interpretDisjunction(child); case PQLParser.RULE_logicalTest: return interpretLogicalTest(child); } return false; }
protected boolean interpretLogicalTest(ParseTree tree) { ParseTree child = tree.getChild(0); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_isTrue: return interpretLogicalTest(child,true,true); case PQLParser.RULE_isNotTrue: return interpretLogicalTest(child,false,true); case PQLParser.RULE_isFalse: return interpretLogicalTest(child,true,false); case PQLParser.RULE_isNotFalse: return interpretLogicalTest(child,false,false); } return false; }
protected Set<PQLTask> interpretSetOfTasks(ParseTree tree) { ParseTree child = tree.getChild(0); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_tasks: return interpretTasks(child); case PQLParser.RULE_union: return interpretUnion(child); case PQLParser.RULE_difference: return interpretDifference(child); case PQLParser.RULE_intersection: return interpretIntersection(child); } return new HashSet<PQLTask>(); }
protected Set<PQLTask> interpretTasks(ParseTree tree) { ParseTree child = tree.getChild(0); if (child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_varName: return this.interpretVarNameSetOfTasks(child); case PQLParser.RULE_setOfAllTasks: return this.interpretSetOfAllTasks(child); case PQLParser.RULE_setOfTasksLiteral: return interpretSetOfTasksLiteral(child); case PQLParser.RULE_setOfTasksConstruction: return interpretSetOfTasksConstruction(child); case PQLParser.RULE_setOfTasksParentheses: return interpretSetOfTasksParentheses(child); } } return new HashSet<PQLTask>(); }
protected Set<PQLTask> interpretSetOfTasksConstruction(ParseTree tree) { ParseTree child = tree.getChild(0); if (child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_unaryPredicateConstruction: return interpretUnaryPredicateConstruction(child); case PQLParser.RULE_binaryPredicateConstruction: return interpretBinaryPredicateConstruction(child); } } return new HashSet<PQLTask>(); }
protected Set<PQLTask> interpretSetOfTasksLiteral(ParseTree tree) { Set<PQLTask> result = new HashSet<PQLTask>(); for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); if (ruleIndex == PQLParser.RULE_task) { result.add(this.interpretTask(child)); } } } return result; }
protected Set<PQLTask> interpretUnion(ParseTree tree) { Set<PQLTask> result = new HashSet<PQLTask>(); for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_tasks: result.addAll(interpretTasks(child)); break; case PQLParser.RULE_difference: result.addAll(interpretDifference(child)); break; case PQLParser.RULE_intersection: result.addAll(interpretIntersection(child)); break; } } } return result; }
protected Set<PQLTask> interpretIntersection(ParseTree tree) { Set<PQLTask> result = null; for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_tasks: if (result==null) result = this.interpretTasks(child); else result.retainAll(this.interpretTasks(child)); break; case PQLParser.RULE_difference: if (result==null) result = this.interpretDifference(child); else result.retainAll(this.interpretDifference(child)); break; } } } return result; }
protected Set<PQLTask> interpretDifference(ParseTree tree) { ParseTree child = tree.getChild(2); int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); Set<PQLTask> set1 = this.interpretTasks(tree.getChild(0)); Set<PQLTask> set2 = new HashSet<PQLTask>(); switch (ruleIndex) { case PQLParser.RULE_tasks: set2 = interpretTasks(child); break; case PQLParser.RULE_difference: set2 = interpretDifference(child); break; } set1.removeAll(set2); return set1; }
protected boolean interpretDisjunction(ParseTree tree) { boolean result = false; for (int i = 0; i < tree.getChildCount(); i++) { ParseTree child = tree.getChild(i); if(child instanceof RuleNode) { int ruleIndex = ((RuleNode)child).getRuleContext().getRuleIndex(); switch (ruleIndex) { case PQLParser.RULE_proposition: result |= interpretProposition(child); break; case PQLParser.RULE_logicalTest: result |= interpretLogicalTest(child); break; } if (result==true) return result; } } return result; }
public void testGetPropertyName() throws Exception { final String PROPERTY = "a('aa').b[1].c"; // Should parse and result in the exact same property name Pair<Tree, CommonTokenStream> parsed = SupportParserHelper.parseEventProperty(PROPERTY); Tree propertyNameExprNode = parsed.getFirst().getChild(0); ASTUtil.dumpAST(propertyNameExprNode); String propertyName = ((RuleNode) propertyNameExprNode).getText(); assertEquals(PROPERTY, propertyName); // Try AST with tokens separated, same property name parsed = SupportParserHelper.parseEventProperty("a( 'aa' ). b [ 1 ] . c"); propertyNameExprNode = parsed.getFirst().getChild(0); propertyName = ((RuleNode) propertyNameExprNode).getText(); assertEquals(PROPERTY, propertyName); }
public void walk(ParseTreeListener listener, ParseTree t) { if ( t instanceof ErrorNode) { listener.visitErrorNode((ErrorNode)t); return; } else if ( t instanceof TerminalNode) { listener.visitTerminal((TerminalNode)t); return; } RuleNode r = (RuleNode)t; enterRule(listener, r); int n = r.getChildCount(); for (int i = 0; i<n; i++) { walk(listener, r.getChild(i)); } exitRule(listener, r); }
@Override public ParseTree visit(ParseTree node) { ParseTree result = null; if (!this.match) { if (matches(node, this.currentSelector)) { if (this.iterator.hasNext()) { this.currentSelector = this.iterator.next(); result = super.visit(node); } else if (focusNode.equals(node)) { match = true; } } else if (node.getChildCount() > 0) { if (!this.match) { result = super.visitChildren((RuleNode) node); } } } return result; }
public String visitChildren(RuleNode node) { int n = node.getChildCount(); if (n > 1) { StringBuilder result = new StringBuilder(); ParseTree child; String childResult; for (int i = 0; i < n; i++) { child = node.getChild(i); childResult = child.accept(this); if (childResult != null) { if (i > 0) { result.append(' '); } result.append(childResult); } } return result.toString(); } else { if (n == 1) { return node.getChild(0).accept(this); } else { return node.getText(); } } }
public List<String> visitChildren(RuleNode node) { List<String> result = defaultResult(); List<String> childResult; ParseTree child; int n = node.getChildCount(); for (int i = 0; i < n; i++) { if (!shouldVisitNextChild(node, result)) { break; } child = node.getChild(i); childResult = child.accept(this); if (childResult != null) { if (result != null) { result.addAll(childResult); } else { result = childResult; } } } return result; }
@Override @RuleDependencies({ @RuleDependency(recognizer=GrammarParser.class, rule=GrammarParser.RULE_modeSpec, version=3, dependents=Dependents.PARENTS), @RuleDependency(recognizer=GrammarParser.class, rule=GrammarParser.RULE_ruleSpec, version=3, dependents=Dependents.PARENTS), }) public Tuple2<? extends ParseTree, Integer> visitModeSpec(ModeSpecContext ctx) { // use the preceeding rule (if any), otherwise relative to mode for (int i = priorSiblings.size() - 2; i >= 0; i--) { ParseTree sibling = priorSiblings.get(i); if (!(sibling instanceof RuleNode)) { continue; } RuleContext context = ((RuleNode)sibling).getRuleContext(); if (context.getRuleIndex() == GrammarParser.RULE_ruleSpec) { return Tuple.create(context, 0); } } return Tuple.create(ctx, getCodeStyle().getIndentSize()); }
public static Interval getSourceInterval(@NonNull ParseTree context) { Parameters.notNull("context", context); if (context instanceof TerminalNode) { TerminalNode terminalNode = (TerminalNode)context; Token token = terminalNode.getSymbol(); return new Interval(token.getStartIndex(), token.getStopIndex()); } else if (context instanceof RuleNode) { RuleNode ruleNode = (RuleNode)context; RuleContext ruleContext = ruleNode.getRuleContext(); if (ruleContext instanceof ParserRuleContext) { return getSourceInterval((ParserRuleContext)ruleContext); } else { Token startSymbol = getStartSymbol(context); Token stopSymbol = getStopSymbol(context); if (startSymbol == null || stopSymbol == null) { return Interval.INVALID; } return new Interval(startSymbol.getStartIndex(), stopSymbol.getStopIndex()); } } else { return Interval.INVALID; } }
public static Token getStartSymbol(ParseTree context) { TerminalNode node = getStartNode(context); if (node != null) { return node.getSymbol(); } if (!(context instanceof RuleNode)) { return null; } RuleContext ruleContext = ((RuleNode)context).getRuleContext(); if (ruleContext instanceof ParserRuleContext) { return ((ParserRuleContext)ruleContext).getStart(); } return null; }
@CheckForNull public static RuleNode findAncestor(@NonNull ParseTree tree, @NonNull BitSet ruleIndexes) { for (ParseTree current = tree; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; } RuleNode ruleNode = (RuleNode)current; int ruleIndex = ruleNode.getRuleContext().getRuleIndex(); if (ruleIndex < 0) { continue; } if (ruleIndexes.get(ruleIndex)) { return ruleNode; } } return null; }
@CheckForNull public static <ContextClass> ContextClass findAncestor(@NonNull ParseTree tree, @NonNull Class<ContextClass> nodeType) { for (ParseTree current = tree; current != null; current = current.getParent()) { if (!(current instanceof RuleNode)) { continue; } RuleNode ruleNode = (RuleNode)current; RuleContext ruleContext = ruleNode.getRuleContext(); if (nodeType.isInstance(ruleContext)) { return nodeType.cast(ruleContext); } } return null; }
private String getNodeText(@NotNull final Tree t, @Nullable final List<String> ruleNames) { if (ruleNames != null) { if (t instanceof RuleNode) { int ruleIndex = ((RuleNode) t).getRuleContext().getRuleIndex(); String ruleName = ruleNames.get(ruleIndex); return ruleName; } else if (t instanceof ErrorNode) { return "<" + t.toString() + ">"; } else if (t instanceof TerminalNode) { Token symbol = ((TerminalNode) t).getSymbol(); if (symbol != null) { String s = symbol.getText(); return "'" + s + "'"; } } } // no recog for rule names Object payload = t.getPayload(); if (payload instanceof Token) { return ((Token) payload).getText(); } return t.getPayload().toString(); }
public static String getNodeText(Tree t, String[] ruleNames, String[] tokenNames) { if (ruleNames != null) { if (t instanceof RuleNode) { int ruleIndex = ((RuleNode) t).getRuleContext().getRuleIndex(); return ruleNames[ruleIndex]; } else if (t instanceof ErrorNode) { return t.toString(); } else if (t instanceof TerminalNode) { Token symbol = ((TerminalNode) t).getSymbol(); if (symbol != null) { return "'" + symbol.getText() + "'"; } } } // no recog for rule names Object payload = t.getPayload(); if (payload instanceof Token) { return ((Token) payload).getText(); } return t.getPayload().toString(); }
@Override public Integer visitChildren(RuleNode arg0) { int iHash = 1; boolean bVisit = false; for (int i = arg0.getChildCount() - 1; i >= 0; i--) { Integer iVisit = visit(arg0.getChild(i)); // If the visit resulted no hash ignore the hash. if (iVisit != null) { iHash = iHash * PRIME + iVisit; bVisit = true; } } if (bVisit) { return iHash; } else { return defaultResult(); // No result. } }
public final SubPath propertyOrKey(String string) { checkNotNull(string); try { return newParser(string, true).parseProperty().accept(new PropertyPathBaseVisitor<SubPath>() { @Override public SubPath visitProperty(PropertyPathParser.PropertyContext ctx) { return new Property(PropertyPath.this, ctx.getText()); } @Override protected boolean shouldVisitNextChild(RuleNode node, SubPath currentResult) { return currentResult == null; } }); } catch (SilentParseException e) { return new Key(this, string); } }
public String visitChildren(RuleNode node, List<Integer> withoutNodes) { if(node == null) return ""; String result = this.defaultResult(); int n = node.getChildCount(); for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) { if(withoutNodes != null && withoutNodes.contains(i)) continue; ParseTree c = node.getChild(i); String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this); result = this.aggregateResult(result, childResult); } return result; }
public String visitWithoutTerminals(RuleNode node) { String result = this.defaultResult(); int n = node.getChildCount(); for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) { ParseTree c = node.getChild(i); if(c instanceof TerminalNode) continue; String childResult = c.accept(this); result = this.aggregateResult(result, childResult); } return result; }