Search in sources :

Example 6 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class TreeViewer method paint.

@Override
public void paint(Graphics g) {
    super.paint(g);
    if (treeLayout == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g;
    // anti-alias the lines
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Anti-alias the text
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    //		AffineTransform at = g2.getTransform();
    //        g2.scale(
    //            (double) this.getWidth() / 400,
    //            (double) this.getHeight() / 400);
    //
    //		g2.setTransform(at);
    paintEdges(g, getTree().getRoot());
    // paint the boxes
    for (Tree Tree : treeLayout.getNodeBounds().keySet()) {
        paintBox(g, Tree);
    }
}
Also used : Tree(org.antlr.v4.runtime.tree.Tree)

Example 7 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class TreeViewer method fillTree.

private static void fillTree(TreeNodeWrapper node, Tree tree, TreeViewer viewer) {
    if (tree == null) {
        return;
    }
    for (int i = 0; i < tree.getChildCount(); i++) {
        Tree childTree = tree.getChild(i);
        TreeNodeWrapper childNode = new TreeNodeWrapper(childTree, viewer);
        node.add(childNode);
        fillTree(childNode, childTree, viewer);
    }
}
Also used : Tree(org.antlr.v4.runtime.tree.Tree)

Example 8 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class BasicSemanticChecks method enterLabeledLexerElement.

@Override
protected void enterLabeledLexerElement(GrammarAST tree) {
    Token label = ((GrammarAST) tree.getChild(0)).getToken();
    g.tool.errMgr.grammarError(ErrorType.V3_LEXER_LABEL, g.fileName, label, label.getText());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Token(org.antlr.runtime.Token)

Example 9 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class BasicSemanticChecks method checkElementIsOuterMostInSingleAlt.

/**
	 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());
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree)

Example 10 with Tree

use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.

the class ParseTreePattern method findAll.

/**
	 * Find all nodes using XPath and then try to match those subtrees against
	 * this tree pattern.
	 *
	 * @param tree The {@link ParseTree} to match against this pattern.
	 * @param xpath An expression matching the nodes
	 *
	 * @return A collection of {@link ParseTreeMatch} objects describing the
	 * successful matches. Unsuccessful matches are omitted from the result,
	 * regardless of the reason for the failure.
	 */
public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) {
    Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser());
    List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>();
    for (ParseTree t : subtrees) {
        ParseTreeMatch match = match(t);
        if (match.succeeded()) {
            matches.add(match);
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)30 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)21 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)15 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)12 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 Tree (org.antlr.v4.runtime.tree.Tree)6 IOException (java.io.IOException)5 Tree (org.antlr.runtime.tree.Tree)4 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3