示例#1
0
  /** Produces a graph from this AST. */
  public GraphSprite toGraph() {
    GraphSprite result;
    if (isDigraph) {
      result = new GraphSprite(new DirectedGraph());
    } else {
      result = new GraphSprite(new UndirectedGraph());
    }

    // TODO: Should we do something with isStrict?
    // TODO: Should we do something with the graph ID?

    Map<String, String> graphAttrs = new HashMap<>();
    Map<String, String> nodeAttrs = new HashMap<>();
    Map<String, String> edgeAttrs = new HashMap<>();
    statementList.updateGraph(result, graphAttrs, nodeAttrs, edgeAttrs);
    return result;
  }
示例#2
0
  /** 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);
    }
  }
示例#3
0
  @Override
  public String stringify() {
    String result = "";

    if (isStrict) {
      result += "strict ";
    }

    if (isDigraph) {
      result += "digraph ";
    } else {
      result += "graph ";
    }

    if (id != null && !"".equals(id)) {
      result += id + " {\n";
    }

    result += statementList.stringify() + "\n";

    result += "}";
    return result;
  }