use of com.randomnoun.build.javaToGraphviz.comment.GvComment 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;
}
use of com.randomnoun.build.javaToGraphviz.comment.GvComment in project java-to-graphviz by randomnoun.
the class CommentExtractor method getComments.
/**
* Return a list of processed comments from the source file.
*
* GvStyleComment: "// gv-style: { xxx }"
* GvKeepNodeComment: "// gv-keepNode: xxx"
* GvLiteralComment: "// gv-literal: xxx"
* GvDigraphComment: "// gv-graph: xxx"
* GvSubgraphComment: "// gv-subgraph: xxx"
* GvComment: "// gv.className.className.className#id: xxx { xxx }"
*
* @param cu
* @param src
* @return
*/
@SuppressWarnings("unchecked")
public List<CommentText> getComments(CompilationUnit cu, String src) {
// @TODO better regex
// Pattern gvPattern = Pattern.compile("^gv(\\.[a-zA-Z]+)*:"); // gv.some.class.names:
// probably do this right at the end as gv.literal affects how we parse it
// Pattern valPattern = Pattern.compile("(([a-zA-Z]+)\\s*([^;]*);\\s*)*"); // things;separated;by;semicolons;
List<CommentText> comments = new ArrayList<>();
for (Comment c : (List<Comment>) cu.getCommentList()) {
// comment.accept(cv);
boolean eolComment = (c instanceof LineComment);
int start = c.getStartPosition();
int end = start + c.getLength();
String text = src.substring(start, end);
int line = cu.getLineNumber(start);
int column = cu.getColumnNumber(start);
if (c.isBlockComment()) {
if (text.startsWith("/*") && text.endsWith("*/")) {
text = text.substring(2, text.length() - 2).trim();
} else {
throw new IllegalStateException("Block comment does not start with '/*' and end with '*/': '" + text + "'");
}
}
if (c.isLineComment()) {
if (text.startsWith("//")) {
text = text.substring(2).trim();
} else {
throw new IllegalStateException("Line comment does not start with '//': '" + text + "'");
}
}
if (text.startsWith("gv-style:")) {
String s = text.substring(9).trim();
if (s.startsWith("{") && s.endsWith("}")) {
s = s.substring(1, s.length() - 1).trim();
// here be the css
// remove inline comments
// logger.info("maybe here ? " + s);
comments.add(new GvStyleComment(c, line, column, eolComment, text, s));
} else {
throw new IllegalStateException("gv-style does not start with '{' and end with '}': '" + text + "'");
}
} else if (text.startsWith("gv-endGraph")) {
comments.add(new GvEndGraphComment(c, line, column, eolComment));
} else if (text.startsWith("gv-endSubgraph")) {
comments.add(new GvEndSubgraphComment(c, line, column, eolComment));
} else if (text.startsWith("gv-literal:")) {
String s = text.substring(11).trim();
comments.add(new GvLiteralComment(c, line, column, eolComment, s));
} else if (text.startsWith("gv-keepNode:")) {
String s = text.substring(12).trim();
comments.add(new GvKeepNodeComment(c, line, column, eolComment, s));
} else if (text.startsWith("gv-option:")) {
String s = text.substring(10).trim();
comments.add(new GvOptionComment(c, line, column, eolComment, s));
} else {
Matcher fgm;
CommentText gvc = null;
fgm = gvGraphClassPattern.matcher(text);
if (fgm.find()) {
gvc = getGvComment("gv-graph", c, line, column, fgm, text);
} else {
fgm = gvSubgraphClassPattern.matcher(text);
if (fgm.find()) {
gvc = getGvComment("gv-subgraph", c, line, column, fgm, text);
} else {
fgm = gvNodeClassPattern.matcher(text);
if (fgm.find()) {
gvc = getGvComment("gv", c, line, column, fgm, text);
} else {
// regular comment, ignore
}
}
}
if (gvc != null) {
comments.add(gvc);
}
}
}
return comments;
}
use of com.randomnoun.build.javaToGraphviz.comment.GvComment 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++;
}
}
use of com.randomnoun.build.javaToGraphviz.comment.GvComment 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++;
}
}
use of com.randomnoun.build.javaToGraphviz.comment.GvComment in project java-to-graphviz by randomnoun.
the class AstToDagVisitor method preVisit2.
@Override
public boolean preVisit2(ASTNode node) {
DagNode pdn = getClosestDagNodeInRoot(root, node);
int lineNumber = cu.getLineNumber(node.getStartPosition());
int columnNumber = cu.getColumnNumber(node.getStartPosition());
if (node instanceof MethodDeclaration || node instanceof CatchClause || node instanceof Statement || // inside ExpressionStatements
node instanceof Expression || node instanceof VariableDeclaration || node instanceof TypeDeclaration || node instanceof AnonymousClassDeclaration) {
DagNode dn = new DagNode();
String clazz = Text.getLastComponent(node.getClass().getName());
if (clazz.endsWith("Statement") && !clazz.equals("ExpressionStatement")) {
// @TODO remove this and update the css to match
clazz = clazz.substring(0, clazz.length() - 9);
}
String lowerClass = Text.toFirstLower(clazz);
// ok we treat all Expressions as DagNodes here so we can edge them up later
// and then if it turns out that we're not edging them, we bubble up any comments associated with
// those DagNodes to the containing statement.
// "if";
dn.type = clazz;
dn.name = null;
// now set by gv-labelFormat
dn.label = null;
dn.classes.add(lowerClass);
dn.lineNumber = lineNumber;
dn.parentDagNode = pdn;
dn.astNode = node;
// these comments may start a new graph, when that happens we need to reset the pdn
int beforeRootGraphIdx = rootGraphIdx;
if (node instanceof TypeDeclaration) {
// comments get assigned to this node
processCommentsToTypeOrMethodNode(pdn, lineNumber, dn);
} else if (node instanceof MethodDeclaration) {
// comments get assigned to this node
processCommentsToTypeOrMethodNode(pdn, lineNumber, dn);
} else {
// within methods, comments can have their own nodes
processCommentsToStatementNode(pdn, lineNumber, columnNumber, null);
}
if (rootGraphIdx != beforeRootGraphIdx) {
// new graph, this node is a root node
pdn = null;
}
// options and keepNodeMatcher may be affected by gv comments
dn.options = options;
dn.keepNodeMatcher = keepNodeMatcher;
if (keepNodeMatcher.matches(lowerClass)) {
dn.keepNode = true;
}
// omit nodes outside of graphs
if (root != null) {
dag.addNode(root, dn);
if (pdn != null) {
pdn.addChild(dn);
} else {
logger.debug("preVisit: null pdn on " + dn.type + " on line " + dn.lineNumber);
// each typeDeclaration is it's own subgraph
// think these need to go with the rootGraph rather than the dag
// actually maybe there should be a list of dags rather than a list of subgraphs
dag.addRootNode(dn);
}
if (nextDagComments.size() > 0) {
for (GvComment gc : nextDagComments) {
annotateDag(dn, gc);
}
nextDagComments.clear();
}
if (nextLineComments.size() > 0 && nextLineFromLine != dn.lineNumber) {
for (GvComment gc : nextLineComments) {
annotateDag(dn, gc);
}
nextLineComments.clear();
nextLineFromLine = 0;
}
processCommentsToStatementNode(pdn, lineNumber, columnNumber, dn);
}
/*
if (lastIdx < comments.size() && comments.get(lastIdx).line == lineNumber) {
CommentText ct = comments.get(lastIdx);
dn.keepNode = true; // always keep commented nodes
if (ct instanceof GvComment) {
// just undirected comments at the end of the line. probably.
GvComment gc = (GvComment) ct;
if (Text.isBlank(gc.direction)) {
annotateDag(dn, gc);
}
// if this comment start col is before columnNumber then it's not for this statement
} else if (ct instanceof GvGraphComment) {
// @TODO something
} else if (ct instanceof GvSubgraphComment) {
GvSubgraphComment gvsc = (GvSubgraphComment) ct;
dn.label = gvsc.text; // if not blank ?
dn.name = gvsc.id;
dn.classes.addAll(gvsc.classes);
dn.classes.add("beginGraph");
} else if (ct instanceof GvEndSubgraphComment) {
dn.classes.add("endGraph");
} else if (ct instanceof GvLiteralComment) {
GvLiteralComment gvlc = (GvLiteralComment) ct;
dn.classes.add("literal");
}
lastIdx++;
}
*/
// lastDagNode = dn;
}
return true;
}
Aggregations