/** Print out a whole tree in LISP form. Arg nodeTextProvider is used on the * node payloads to get the text for the nodes. * * @since 4.5.1 */ public static String toStringTree(Tree t, TreeTextProvider nodeTextProvider) { if ( t==null ) return "null"; String s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false); if ( t.getChildCount()==0 ) return s; StringBuilder buf = new StringBuilder(); buf.append("("); s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false); buf.append(s); buf.append(' '); for (int i = 0; i<t.getChildCount(); i++) { if ( i>0 ) buf.append(' '); buf.append(toStringTree(t.getChild(i), nodeTextProvider)); } buf.append(")"); return buf.toString(); }
@Override public Iterator<Tree> iterator() { return new Iterator<Tree>() { private int i = 0; @Override public boolean hasNext() { return tree.getChildCount() > i; } @Override public Tree next() { if (!hasNext()) throw new NoSuchElementException(); return tree.getChild(i++); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Override public Iterator<Tree> iterator() { return new Iterator<Tree>() { private int i = tree.getChildCount(); @Override public boolean hasNext() { return i > 0; } @Override public Tree next() { if (!hasNext()) throw new NoSuchElementException(); return tree.getChild(--i); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
public void setTree(Tree root) { if ( root!=null ) { boolean useIdentity = true; // compare node identity this.treeLayout = new TreeLayout<Tree>(getTreeLayoutAdaptor(root), new TreeViewer.VariableExtentProvide(this), new DefaultConfiguration<Tree>(gapBetweenLevels, gapBetweenNodes), useIdentity); // Let the UI display this new AST. updatePreferredSize(); } else { this.treeLayout = null; repaint(); } }
private String toStringTree(Tree tree, List<String> ruleNames) { String s = Utils.escapeWhitespace(getNodeText(tree, ruleNames), false); if(tree.getChildCount() == 0) return s; StringBuilder buf = new StringBuilder(); buf.append("("); s = Utils.escapeWhitespace(getNodeText(tree, ruleNames), false); buf.append(s); buf.append(' '); for(int i = 0; i < tree.getChildCount(); i++) { if(i > 0) buf.append(' '); buf.append(toStringTree(tree.getChild(i), ruleNames)); } buf.append(")"); return buf.toString(); }
public static void dumpAST(PrintWriter printer, Tree ast, int ident) { char[] identChars = new char[ident]; Arrays.fill(identChars, ' '); if (ast == null) { renderNode(identChars, null, printer); return; } for (int i = 0; i < ast.getChildCount(); i++) { Tree node = ast.getChild(i); if (node == null) { throw new NullPointerException("Null AST node"); } renderNode(identChars, node, printer); dumpAST(printer, node, ident + 2); } }
protected static List<OnTriggerSetAssignment> getOnTriggerSetAssignments(EsperEPL2GrammarParser.OnSetAssignmentListContext ctx, Map<Tree, ExprNode> astExprNodeMap) { if (ctx == null || ctx.onSetAssignment().isEmpty()) { return Collections.emptyList(); } List<EsperEPL2GrammarParser.OnSetAssignmentContext> ctxs = ctx.onSetAssignment(); List<OnTriggerSetAssignment> assignments = new ArrayList<OnTriggerSetAssignment>(ctx.onSetAssignment().size()); for (EsperEPL2GrammarParser.OnSetAssignmentContext assign : ctxs) { ExprNode childEvalNode; if (assign.eventProperty() != null) { ExprNode prop = ASTExprHelper.exprCollectSubNodes(assign.eventProperty(), 0, astExprNodeMap).get(0); ExprNode value = ASTExprHelper.exprCollectSubNodes(assign.expression(), 0, astExprNodeMap).get(0); ExprEqualsNode equals = new ExprEqualsNodeImpl(false, false); equals.addChildNode(prop); equals.addChildNode(value); childEvalNode = equals; } else { childEvalNode = ASTExprHelper.exprCollectSubNodes(assign, 0, astExprNodeMap).get(0); } assignments.add(new OnTriggerSetAssignment(childEvalNode)); } return assignments; }
public static ExprNode mathGetExpr(ParseTree ctx, Map<Tree, ExprNode> astExprNodeMap, ConfigurationInformation configurationInformation) { int count = 1; ExprNode base = ASTExprHelper.exprCollectSubNodes(ctx.getChild(0), 0, astExprNodeMap).get(0); while (true) { int token = ASTUtil.getAssertTerminatedTokenType(ctx.getChild(count)); MathArithTypeEnum mathArithTypeEnum = tokenToMathEnum(token); ExprNode right = ASTExprHelper.exprCollectSubNodes(ctx.getChild(count + 1), 0, astExprNodeMap).get(0); ExprMathNode math = new ExprMathNode(mathArithTypeEnum, configurationInformation.getEngineDefaults().getExpression().isIntegerDivision(), configurationInformation.getEngineDefaults().getExpression().isDivisionByZeroReturnsNull()); math.addChildNode(base); math.addChildNode(right); base = math; count += 2; if (count >= ctx.getChildCount()) { break; } } return base; }
public static CreateDataFlowDesc walkCreateDataFlow(EsperEPL2GrammarParser.CreateDataflowContext ctx, Map<Tree, Object> astGraphNodeMap, EngineImportService engineImportService) { String graphName = ctx.name.getText(); List<GraphOperatorSpec> ops = new ArrayList<GraphOperatorSpec>(); List<CreateSchemaDesc> schemas = new ArrayList<CreateSchemaDesc>(); List<EsperEPL2GrammarParser.GopContext> gopctxs = ctx.gopList().gop(); for (EsperEPL2GrammarParser.GopContext gopctx : gopctxs) { if (gopctx.createSchemaExpr() != null) { schemas.add(ASTCreateSchemaHelper.walkCreateSchema(gopctx.createSchemaExpr())); } else { ops.add(parseOp(gopctx, astGraphNodeMap, engineImportService)); } } return new CreateDataFlowDesc(graphName, ops, schemas); }
public static List<ExprNode> getExprNodesLibFunc(EsperEPL2GrammarParser.LibFunctionArgsContext ctx, Map<Tree, ExprNode> astExprNodeMap) { if (ctx == null) { return Collections.emptyList(); } List<EsperEPL2GrammarParser.LibFunctionArgItemContext> args = ctx.libFunctionArgItem(); if (args == null || args.isEmpty()) { return Collections.emptyList(); } List<ExprNode> parameters = new ArrayList<ExprNode>(args.size()); for (EsperEPL2GrammarParser.LibFunctionArgItemContext arg : args) { if (arg.expressionLambdaDecl() != null) { List<String> lambdaparams = getLambdaGoesParams(arg.expressionLambdaDecl()); ExprLambdaGoesNode goes = new ExprLambdaGoesNode(lambdaparams); ExprNode lambdaExpr = ASTExprHelper.exprCollectSubNodes(arg.expressionWithNamed(), 0, astExprNodeMap).get(0); goes.addChildNode(lambdaExpr); parameters.add(goes); } else { ExprNode parameter = ASTExprHelper.exprCollectSubNodes(arg.expressionWithNamed(), 0, astExprNodeMap).get(0); parameters.add(parameter); } } return parameters; }
public static CreateIndexDesc walk(EsperEPL2GrammarParser.CreateIndexExprContext ctx, Map<Tree, ExprNode> astExprNodeMap) { String indexName = ctx.n.getText(); String windowName = ctx.w.getText(); boolean unique = false; if (ctx.u != null) { String ident = ctx.u.getText(); if (ident.toLowerCase(Locale.ENGLISH).trim().equals("unique")) { unique = true; } else { throw ASTWalkException.from("Invalid keyword '" + ident + "' in create-index encountered, expected 'unique'"); } } List<CreateIndexItem> columns = new ArrayList<>(); List<EsperEPL2GrammarParser.CreateIndexColumnContext> cols = ctx.createIndexColumnList().createIndexColumn(); for (EsperEPL2GrammarParser.CreateIndexColumnContext col : cols) { CreateIndexItem item = walk(col, astExprNodeMap); columns.add(item); } return new CreateIndexDesc(unique, indexName, windowName, columns); }
private static CreateIndexItem walk(EsperEPL2GrammarParser.CreateIndexColumnContext col, Map<Tree, ExprNode> astExprNodeMap) { List<ExprNode> expressions = Collections.emptyList(); if (col.i != null) { expressions = ASTExprHelper.exprCollectSubNodes(col.i, 0, astExprNodeMap); } else if (col.expression() != null) { expressions = ASTExprHelper.exprCollectSubNodes(col.expression(), 0, astExprNodeMap); } String type = CreateIndexType.HASH.getNameLower(); if (col.t != null) { type = col.t.getText(); } List<ExprNode> parameters = Collections.<ExprNode>emptyList(); if (col.p != null) { parameters = ASTExprHelper.exprCollectSubNodes(col.p, 0, astExprNodeMap); } return new CreateIndexItem(expressions, type, parameters); }
public static RowRegexExprRepeatDesc walkOptionalRepeat(EsperEPL2GrammarParser.MatchRecogPatternRepeatContext ctx, Map<Tree, ExprNode> astExprNodeMap) { if (ctx == null) { return null; } ExprNode e1 = ctx.e1 == null ? null : ASTExprHelper.exprCollectSubNodes(ctx.e1, 0, astExprNodeMap).get(0); ExprNode e2 = ctx.e2 == null ? null : ASTExprHelper.exprCollectSubNodes(ctx.e2, 0, astExprNodeMap).get(0); if (ctx.comma == null && ctx.e1 != null) { return new RowRegexExprRepeatDesc(null, null, e1); } if (e1 == null && e2 == null) { throw ASTWalkException.from("Invalid match-recognize quantifier '" + ctx.getText() + "', expecting an expression"); } return new RowRegexExprRepeatDesc(e1, e2, null); }
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); }
/** From editor's cursor, find associated parse tree node so we can highlight * in structure view. It wants the parse tree node not a * StructureViewTreeElement. It will try to find a path from root to * that node and highlight it. */ @Nullable public Object getCurrentEditorElement() { if (editor==null) return null; final int offset = editor.getCaretModel().getOffset(); if ( parseTree==null ) return null; Tree selectedNode = Trees.findNodeSuchThat(parseTree, new Predicate<Tree>() { @Override public boolean test(Tree node) { if ( !(node instanceof TerminalNode) ) return false; Token t = ((TerminalNode) node).getSymbol(); return offset>=t.getStartIndex() && offset<=t.getStopIndex(); } }); if ( selectedNode==null ) return null; // now walk up looking for template def node ParseTree p = (ParseTree)selectedNode; while ( p!=null && !(p instanceof STGParser.TemplateContext) ) { p = p.getParent(); } if ( p!=null ) { return ((STGParser.TemplateContext)p).ID(0); } return null; }
private String toStringTree(@NotNull final Tree t, @Nullable final List<String> ruleNames) { if (t.getChildCount() == 0) { return Utils.escapeWhitespace(getNodeText(t, ruleNames), true); } StringBuilder buf = new StringBuilder(); buf.append(" ( "); String s = Utils.escapeWhitespace(getNodeText(t, ruleNames), true); buf.append(s); buf.append(' '); for (int i = 0; i < t.getChildCount(); i++) { if (i > 0) { buf.append(' '); } buf.append(toStringTree(t.getChild(i), ruleNames)); } buf.append(" ) "); return buf.toString(); }
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(); }
@Override public Collection<ParseTree> evaluate(ParseTree t) { // return all children of t that match nodeName List<ParseTree> nodes = new ArrayList<ParseTree>(); for (Tree c : Trees.getChildren(t)) { if ( c instanceof ParserRuleContext ) { ParserRuleContext ctx = (ParserRuleContext)c; if ( (ctx.getRuleIndex() == ruleIndex && !invert) || (ctx.getRuleIndex() != ruleIndex && invert) ) { nodes.add(ctx); } } } return nodes; }
@Override public Collection<ParseTree> evaluate(ParseTree t) { // return all children of t that match nodeName List<ParseTree> nodes = new ArrayList<ParseTree>(); for (Tree c : Trees.getChildren(t)) { if ( c instanceof TerminalNode ) { TerminalNode tnode = (TerminalNode)c; if ( (tnode.getSymbol().getType() == tokenType && !invert) || (tnode.getSymbol().getType() != tokenType && invert) ) { nodes.add(tnode); } } } return nodes; }
public void setTree(Tree root) { if ( root!=null ) { boolean useIdentity = true; // compare node identity this.treeLayout = new TreeLayout<Tree>(new TreeLayoutAdaptor(root), new TreeViewer.VariableExtentProvide(this), new DefaultConfiguration<Tree>(gapBetweenLevels, gapBetweenNodes), useIdentity); // Let the UI display this new AST. updatePreferredSize(); } else { this.treeLayout = null; repaint(); } }
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 String getText(Tree node) { if ( node instanceof PreviewInterpreterRuleContext) { PreviewInterpreterRuleContext inode = (PreviewInterpreterRuleContext)node; Rule r = g.getRule(inode.getRuleIndex()); String[] altLabels = getAltLabels(r); String name = r.name; int outerAltNum = inode.getOuterAltNum(); if ( altLabels!=null ) { if ( outerAltNum>=0 && outerAltNum<altLabels.length ) { return name+":"+altLabels[outerAltNum]; } else { return name; } } else if ( r.getOriginalNumberOfAlts()>1 ) { return name + ":" +outerAltNum; } else { return name; // don't display an alternative number if there's only one } } return Trees.getNodeText(node, Arrays.asList(parser.getRuleNames())); }
private MutableTreeNode wrap(final Tree tree) { if (tree == null) { return null; } DefaultMutableTreeNode root = new DefaultMutableTreeNode(tree) { @Override public String toString() { String name = treeTextProvider.getText((Tree) getUserObject()); if (tree instanceof TerminalNode) { return name.equals("<EOF>") ? name : "\"" + name + "\""; } return name; } }; for (int i = 0; i < tree.getChildCount(); i++) { root.add(wrap(tree.getChild(i))); } return root; }
public void selectNodeAtOffset(int offset) { if (!scrollFromSource) { return; } DefaultMutableTreeNode root = (DefaultMutableTreeNode) myTree.getModel().getRoot(); Tree tree = (Tree) root.getUserObject(); if (tree instanceof ParseTree) { DefaultMutableTreeNode atOffset = getNodeAtOffset(root, offset); if (atOffset != null) { TreePath path = new TreePath(atOffset.getPath()); myTree.getSelectionModel().setSelectionPath(path); myTree.scrollPathToVisible(path); } } }
public static void markFromRoots(final PreviewInterpreterRuleContext t, final PreviewInterpreterRuleContext u) { if ( t==null || u==null ) return; if ( !t.equals(u) ) return; t.reached = true; u.reached = true; int n = Math.min(t.getChildCount(), u.getChildCount()); for (int i = 0; i<n; i++) { // for each leaf of t and u Tree tchild = t.getChild(i); Tree uchild = u.getChild(i); if ( !tchild.equals(uchild) ) { return; // don't consider other kids if ith doesn't match } if ( tchild instanceof PreviewInterpreterRuleContext && uchild instanceof PreviewInterpreterRuleContext ) { markFromRoots((PreviewInterpreterRuleContext) tchild, (PreviewInterpreterRuleContext) uchild); } else { return; // mismatched kids. should be caught above but... } } }
/** Same as loadGrammar(fileName) except import vocab from existing lexer */ // public static Grammar loadGrammar(Tool tool, String fileName, LexerGrammar lexerGrammar) { // GrammarRootAST grammarRootAST = parseGrammar(fileName); // if ( grammarRootAST==null ) return null; // final Grammar g = tool.createGrammar(grammarRootAST); // g.fileName = fileName; // if ( lexerGrammar!=null ) { // g.importVocab(lexerGrammar); // } // tool.process(g, false); // return g; // } public static Tree findOverriddenDecisionRoot(Tree ctx) { return Trees.findNodeSuchThat(ctx, new Predicate<Tree>() { @Override public boolean test(Tree t) { return t instanceof PreviewInterpreterRuleContext ? ((PreviewInterpreterRuleContext) t).isDecisionOverrideRoot() : false; } }); }
public static void _getAllLeaves(Tree t, List<? super Tree> leaves, final int startIndex, final int stopIndex) { int n = t.getChildCount(); if ( n==0 ) { // must be leaf Token tok = ((TerminalNode)t).getSymbol(); int i = tok.getTokenIndex(); if ( i>=startIndex && i<=stopIndex && tok.getType() != Token.INVALID_TYPE ) { leaves.add(t); } return; } for (int i = 0 ; i < n ; i++){ _getAllLeaves(t.getChild(i), leaves, startIndex, stopIndex); } }
public void enterEveryRule(ParserRuleContext ctx) { // Find sibling lists that are children of this parent node Set<Class> completed = new HashSet<>(); // only count sibling list for each subtree type once for (int i = 0; i<ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if ( completed.contains(child.getClass()) ) continue; // avoid counting repeatedly completed.add(child.getClass()); if ( child instanceof TerminalNode ) continue; // tokens are separators at most not siblings // found subtree child List<? extends ParserRuleContext> siblings = ctx.getRuleContexts(((ParserRuleContext) child).getClass()); if ( siblings.size()>1 ) { // we found a list // check for separator by looking between first two siblings (assume all are same) ParserRuleContext first = siblings.get(0); ParserRuleContext second = siblings.get(1); List<Tree> children = Trees.getChildren(ctx); int firstIndex = children.indexOf(first); int secondIndex = children.indexOf(second); if ( firstIndex+1 == secondIndex ) continue; // nothing between first and second so no separator ParseTree between = ctx.getChild(firstIndex+1); if ( between instanceof TerminalNode ) { // is it a token? Token separator = ((TerminalNode) between).getSymbol(); visitNonSingletonWithSeparator(ctx, siblings, separator); } } } }
public static List<Tree> getSeparators(ParserRuleContext ctx, List<? extends ParserRuleContext> siblings) { ParserRuleContext first = siblings.get(0); ParserRuleContext last = siblings.get(siblings.size()-1); int start = BuffUtils.indexOf(ctx, first); int end = BuffUtils.indexOf(ctx, last); List<Tree> elements = Trees.getChildren(ctx).subList(start, end+1); return BuffUtils.filter(elements, c -> c instanceof TerminalNode); }
/** Save this tree in a postscript file */ public static void save(Tree t, Parser parser, String fileName) throws IOException, PrintException { List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; save(t, ruleNames, fileName); }
/** Save this tree in a postscript file using a particular font name and size */ public static void save(Tree t, Parser parser, String fileName, String fontName, int fontSize) throws IOException { List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; save(t, ruleNames, fileName, fontName, fontSize); }
/** Save this tree in a postscript file using a particular font name and size */ public static void save(Tree t, List<String> ruleNames, String fileName, String fontName, int fontSize) throws IOException { writePS(t, ruleNames, fileName, fontName, fontSize); }
public static String getPS(Tree t, List<String> ruleNames, String fontName, int fontSize) { TreePostScriptGenerator psgen = new TreePostScriptGenerator(ruleNames, t, fontName, fontSize); return psgen.getPS(); }
public static void writePS(Tree t, List<String> ruleNames, String fileName, String fontName, int fontSize) throws IOException { String ps = getPS(t, ruleNames, fontName, fontSize); FileWriter f = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(f); try { bw.write(ps); } finally { bw.close(); } }
@Override public double getHeight(Tree tree) { String s = getText(tree); double h = doc.getLineHeight() + nodeHeightPaddingAbove + nodeHeightPaddingBelow; String[] lines = s.split("\n"); return h * lines.length; }
public TreePostScriptGenerator(List<String> ruleNames, Tree root, String fontName, int fontSize) { this.root = root; setTreeTextProvider(new TreeViewer.DefaultTreeTextProvider(ruleNames)); doc = new PostScriptDocument(fontName, fontSize); boolean compareNodeIdentities = true; this.treeLayout = new TreeLayout<Tree>(getTreeLayoutAdaptor(root), new VariableExtentProvide(), new DefaultConfiguration<Tree>(gapBetweenLevels, gapBetweenNodes, Configuration.Location.Bottom), compareNodeIdentities); }