Search in sources :

Example 1 with GvGraphComment

use of com.randomnoun.build.javaToGraphviz.comment.GvGraphComment in project java-to-graphviz by randomnoun.

the class CommentExtractor method getGvComment.

public CommentText getGvComment(String type, Comment c, int lineNumber, int column, Matcher fgm, String text) {
    List<String> classes = new ArrayList<>();
    String id = null;
    boolean hasColon = fgm.group(0).endsWith(":");
    boolean eolComment = (c instanceof LineComment);
    String dir = null;
    logger.debug("group0 " + fgm.group(0));
    if (fgm.group(1) != null) {
        char ch = fgm.group(1).charAt(0);
        if (ch == '#') {
            id = fgm.group(1).substring(1);
        } else if (ch == '.') {
            classes.add(fgm.group(1).substring(1));
        } else {
            throw new IllegalStateException("expected '#' or '.', found '" + ch + "'");
        }
    }
    if (fgm.groupCount() == 3 && fgm.group(3) != null) {
        // :v
        dir = Text.replaceString(fgm.group(3).substring(1), "-", "");
    }
    int pos = fgm.end(1);
    Matcher gm = gvNextClassPattern.matcher(fgm.group(0));
    while (pos != -1 && gm.find(pos)) {
        classes.add(gm.group(1).substring(1));
        pos = gm.end(1);
    }
    text = text.substring(fgm.end());
    // System.out.println("classes " + classes + " in " + text);
    // if there's anything in curly brackets remaining, then that's a style rule.
    // @TODO handle curlies outside of style rules somehow; quoted/escaped
    String inlineStyleString = null;
    Matcher cm = curlyPattern.matcher(text);
    if (cm.find()) {
        inlineStyleString = cm.group(1).trim();
        text = text.substring(0, cm.start()) + text.substring(cm.end());
    }
    if (!hasColon) {
        if (text.length() > 0 && !Character.isWhitespace(text.charAt(0))) {
            logger.warn("ignoring unknown gv directive '" + type + text + "'");
            return null;
        }
        // hrm
        text = null;
    }
    // convert "\n" in comments to newlines (they'll get converted back later)
    text = Text.replaceString(text, "\\n", "\n");
    return "gv".equals(type) ? new GvComment(c, lineNumber, column, eolComment, id, classes, dir, text, inlineStyleString) : "gv-graph".equals(type) ? new GvGraphComment(c, lineNumber, column, eolComment, id, classes, text, inlineStyleString) : "gv-subgraph".equals(type) ? new GvSubgraphComment(c, lineNumber, column, eolComment, id, classes, text, inlineStyleString) : null;
}
Also used : GvGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvGraphComment) GvSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 2 with GvGraphComment

use of com.randomnoun.build.javaToGraphviz.comment.GvGraphComment in project java-to-graphviz by randomnoun.

the class AstToDagVisitor method processCommentsToTypeOrMethodNode.

private void processCommentsToTypeOrMethodNode(DagNode pdn, int line, DagNode mn) {
    // DagNode lastNode = null;
    while (lastIdx < comments.size() && comments.get(lastIdx).line < line) {
        CommentText ct = comments.get(lastIdx);
        /*
            DagNode dn = new DagNode();
            dn.keepNode = true; // always keep comments
            dn.type = "comment";
            dn.line = ct.line;
            dn.name = dag.getUniqueName("c_" + ct.line);
            dn.label = ct.text;
            dn.astNode = null;
            dn.options = options;
            */
        DagNode dn = null;
        if (ct instanceof GvComment) {
            mn.classes.addAll(((GvComment) ct).classes);
            // last comment wins
            mn.label = ct.text;
        } else if (ct instanceof GvGraphComment) {
            GvGraphComment gc = ((GvGraphComment) ct);
            // to remove the class ast node from the 0th root graph
            if (rootGraphIdx == 0) {
                dag.clear();
                root = new DagSubgraph(dag, null);
                dag.rootGraphs.add(root);
            } else {
                root = new DagSubgraph(dag, null);
                dag.rootGraphs.add(root);
            }
            root.name = gc.id;
            root.classes.addAll(gc.classes);
            // append to existing ?
            root.gvAttributes.put("style", gc.inlineStyleString);
            rootGraphIdx++;
        } else if (ct instanceof GvEndGraphComment) {
            if (rootGraphIdx == 0) {
                throw new IllegalStateException("gv-endGraph without gv-graph");
            } else {
                root = null;
            }
        } else if (ct instanceof GvSubgraphComment) {
            GvSubgraphComment gvsc = (GvSubgraphComment) ct;
            dn = new DagNode();
            // dn.name = dag.getUniqueName("c_" + ct.line);
            // always keep gv comments
            dn.keepNode = true;
            dn.type = "comment";
            dn.lineNumber = ct.line;
            dn.classes.add("comment");
            dn.label = ct.text;
            dn.astNode = null;
            dn.options = options;
            dn.keepNodeMatcher = keepNodeMatcher;
            dn.name = gvsc.id;
            dn.classes.addAll(gvsc.classes);
            dn.classes.add("beginSubgraph");
        } else if (ct instanceof GvEndSubgraphComment) {
            dn = new DagNode();
            // dn.name = dag.getUniqueName("c_" + ct.line);
            // always keep gv comments
            dn.keepNode = true;
            dn.type = "comment";
            dn.lineNumber = ct.line;
            dn.classes.add("comment");
            dn.label = ct.text;
            dn.astNode = null;
            dn.options = options;
            dn.keepNodeMatcher = keepNodeMatcher;
            dn.classes.add("endSubgraph");
        } else if (ct instanceof GvLiteralComment) {
            logger.warn("gv-literal outside of method");
        } else if (ct instanceof GvKeepNodeComment) {
            GvKeepNodeComment knc = ((GvKeepNodeComment) ct);
            boolean defaultKeepNode = "true".equals(options.get("defaultKeepNode"));
            keepNodeMatcher = keepNodeMatcher.getModifiedKeepNodeMatcher(defaultKeepNode, knc.text.trim());
        } else if (ct instanceof GvOptionComment) {
            GvOptionComment oc = ((GvOptionComment) ct);
            options = newOptions(oc.text.trim());
        }
        if (dn != null) {
            if (pdn != null) {
                if (root == null) {
                    throw new IllegalStateException("gv comment outside of graph");
                }
                dag.addNode(root, dn);
                pdn.addChild(dn);
            } else {
                // could add as a root node, but let's see how we go
                throw new IllegalStateException("null pdn in processCommentsToTypeOrMethodNode");
            }
        }
        lastIdx++;
    }
}
Also used : GvGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvGraphComment) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) GvSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment)

Example 3 with GvGraphComment

use of com.randomnoun.build.javaToGraphviz.comment.GvGraphComment in project java-to-graphviz by randomnoun.

the class AstToDagVisitor method processCommentsToStatementNode.

private void processCommentsToStatementNode(DagNode pdn, int line, int column, DagNode currentLineDn) {
    // DagNode lastNode = null;
    while (lastIdx < comments.size() && (comments.get(lastIdx).line < line || (comments.get(lastIdx).line == line && comments.get(lastIdx).column < column))) {
        CommentText ct = comments.get(lastIdx);
        DagNode dn;
        if (currentLineDn == null) {
            dn = new DagNode();
            // dn.name = dag.getUniqueName("c_" + ct.line);
            // always keep gv comments
            dn.keepNode = true;
            dn.type = "comment";
            dn.lineNumber = ct.line;
            dn.classes.add("comment");
            dn.label = ct.text;
            dn.astNode = null;
            dn.options = options;
            dn.keepNodeMatcher = keepNodeMatcher;
        } else {
            dn = currentLineDn;
        }
        if (ct instanceof GvComment) {
            GvComment gc = (GvComment) ct;
            if (Text.isBlank(gc.direction)) {
                DagNode prevDagNode = getStartingDagNodeOnLine(ct.line);
                if (prevDagNode == null) {
                    // no direction
                    annotateDag(dn, gc);
                } else {
                    annotateDag(prevDagNode, gc);
                    dn = null;
                }
            } else if (gc.direction.equals("^")) {
                // apply to previous node instead
                DagNode prevDagNode = getStartingDagNodeAboveLine(ct.line);
                if (prevDagNode == null) {
                    throw new IllegalStateException("Could not find previous statement to associate with '^' comment on line " + ct.line);
                }
                annotateDag(prevDagNode, gc);
                dn = null;
            } else if (gc.direction.equals("<")) {
                // apply to previous ast on this line
                DagNode prevDagNode = getEndDagNodeBeforeLineColumn(ct.line, ct.column);
                if (prevDagNode == null) {
                    throw new IllegalStateException("Could not find previous statement to associate with '<' comment on line " + ct.line);
                }
                annotateDag(prevDagNode, gc);
                dn = null;
            } else if (gc.direction.equals("v")) {
                // apply to first node on next line instead
                nextLineComments.add(gc);
                nextLineFromLine = ct.line;
                dn = null;
            } else if (gc.direction.equals(">")) {
                nextDagComments.add(gc);
                dn = null;
            } else {
                throw new IllegalStateException("Unknown direction '" + gc.direction + "'");
            }
        } else if (ct instanceof GvGraphComment) {
            GvGraphComment gc = ((GvGraphComment) ct);
            // to remove the class ast node from the 0th root graph
            if (rootGraphIdx == 0) {
                dag.clear();
                root = new DagSubgraph(dag, null);
                dag.rootGraphs.add(root);
            } else {
                root = new DagSubgraph(dag, null);
                dag.rootGraphs.add(root);
            }
            root.name = gc.id;
            root.classes.addAll(gc.classes);
            // append to existing ?
            root.gvAttributes.put("style", gc.inlineStyleString);
            rootGraphIdx++;
            dn = null;
        } else if (ct instanceof GvEndGraphComment) {
            if (rootGraphIdx == 0) {
                throw new IllegalStateException("gv-endGraph without gv-graph");
            } else {
                root = null;
            }
            dn = null;
        } else if (ct instanceof GvSubgraphComment) {
            GvSubgraphComment gvsc = (GvSubgraphComment) ct;
            dn.name = gvsc.id;
            dn.classes.addAll(gvsc.classes);
            dn.classes.add("beginSubgraph");
        } else if (ct instanceof GvEndSubgraphComment) {
            dn.classes.add("endSubgraph");
        } else if (ct instanceof GvLiteralComment) {
            GvLiteralComment gvlc = (GvLiteralComment) ct;
            dn.classes.add("gv-literal");
            dn.skipNode = true;
        } else if (ct instanceof GvKeepNodeComment) {
            GvKeepNodeComment knc = ((GvKeepNodeComment) ct);
            boolean defaultKeepNode = "true".equals(options.get("defaultKeepNode"));
            keepNodeMatcher = keepNodeMatcher.getModifiedKeepNodeMatcher(defaultKeepNode, knc.text.trim());
            dn = null;
        } else if (ct instanceof GvOptionComment) {
            GvOptionComment oc = ((GvOptionComment) ct);
            options = newOptions(oc.text.trim());
            dn = null;
        }
        if (dn != null && dn != currentLineDn) {
            if (pdn != null) {
                if (root == null) {
                    throw new IllegalStateException("gv comment outside of graph");
                }
                dag.addNode(root, dn);
                pdn.addChild(dn);
            } else {
                throw new IllegalStateException("null pdn in createCommentNodesToLine");
            // logger.warn("null pdn on " + dn.type + " on line " + dn.line);
            }
        }
        lastIdx++;
    }
}
Also used : GvGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvGraphComment) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) GvSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment)

Aggregations

GvComment (com.randomnoun.build.javaToGraphviz.comment.GvComment)3 GvGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvGraphComment)3 GvSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment)3 CommentText (com.randomnoun.build.javaToGraphviz.comment.CommentText)2 GvEndGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment)2 GvEndSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment)2 GvKeepNodeComment (com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment)2 GvLiteralComment (com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment)2 GvOptionComment (com.randomnoun.build.javaToGraphviz.comment.GvOptionComment)2 DagNode (com.randomnoun.build.javaToGraphviz.dag.DagNode)2 DagSubgraph (com.randomnoun.build.javaToGraphviz.dag.DagSubgraph)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 LineComment (org.eclipse.jdt.core.dom.LineComment)1