/** Manually create a graph node for the DOT AST. */ public GraphAST(boolean isStrict, boolean isDigraph, String id, Stmt_listAST statementList) { this.isStrict = isStrict; this.isDigraph = isDigraph; this.id = id; this.statementList = statementList; Debug.debugln("Produced graph"); Debug.debugln(" strict: " + isStrict); Debug.debugln(" isDigraph: " + isDigraph); Debug.debugln(" ID: " + id); }
/** Attempts to produce the root graph AST node for a DOT source string. */ public static ParseResult<GraphAST> parse(StringSlice src) { Debug.debugln("Parsing graph from: \n" + src); if (src == null) { return new ParseFail<GraphAST>("Could not parse null."); } src = src.trimLeft(); // Try to match the declaration part of the graph. Matcher declMatcher = getDeclRegex().matcher(src); if (declMatcher.find() && declMatcher.start() == 0) { boolean isStrict = (declMatcher.group(1) != null); boolean isDigraph = (declMatcher.group(2).equals("digraph")); String id = declMatcher.group(4); if (id == null) { id = DotIO.makeAnonID(); } // Find the left curly brace containing the contents of the graph. int leftCurly = src.indexOf("{"); if (leftCurly < 0) { return new ParseFail<GraphAST>("DOT graph missing {: " + src); } // Find the right curly brace containing the contents of the graph. int rightCurly = ParseUtils.findRight(src, leftCurly); if (rightCurly < 0) { return new ParseFail<GraphAST>("DOT graph missing }: " + src); } StringSlice stmtsSlice = src.subslice(leftCurly + 1, rightCurly); ParseResult<Stmt_listAST> statementList = Stmt_listAST.parse(stmtsSlice, isDigraph); GraphAST result = new GraphAST(isStrict, isDigraph, id, statementList.getAST()); return new ParseSuccess(result, src.start(), src.start() + rightCurly + 1); } else { return new ParseFail<GraphAST>("Could not parse DOT graph: " + src); } }