Example #1
0
  /**
   * Dumps the current NodeToken to the output stream being used.
   *
   * @throws IllegalStateException if the token position is invalid relative to the current
   *     position, i.e. its location places it before the previous token.
   */
  public void visit(NodeToken n) {
    if (n.beginLine == -1 || n.beginColumn == -1) {
      printToken(n.tokenImage);
      return;
    }

    //
    // Handle special tokens
    //
    if (printSpecials && n.numSpecials() > 0)
      for (Enumeration<NodeToken> e = n.specialTokens.elements(); e.hasMoreElements(); )
        visit(e.nextElement());

    //
    // Handle startAtNextToken option
    //
    if (startAtNextToken) {
      curLine = n.beginLine;
      curColumn = 1;
      startAtNextToken = false;

      if (n.beginColumn < curColumn) out.println();
    }

    //
    // Check for invalid token position relative to current position.
    //
    if (n.beginLine < curLine)
      throw new IllegalStateException(
          "at token \""
              + n.tokenImage
              + "\", n.beginLine = "
              + Integer.toString(n.beginLine)
              + ", curLine = "
              + Integer.toString(curLine));
    else if (n.beginLine == curLine && n.beginColumn < curColumn)
      throw new IllegalStateException(
          "at token \""
              + n.tokenImage
              + "\", n.beginColumn = "
              + Integer.toString(n.beginColumn)
              + ", curColumn = "
              + Integer.toString(curColumn));

    //
    // Move output "cursor" to proper location, then print the token
    //
    if (curLine < n.beginLine) {
      curColumn = 1;
      for (; curLine < n.beginLine; ++curLine) out.println();
    }

    for (; curColumn < n.beginColumn; ++curColumn) out.print(" ");

    printToken(n.tokenImage);
  }
Example #2
0
  private void printToken(String s) {
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) == '\n') {
        ++curLine;
        curColumn = 1;
      } else curColumn++;

      out.print(s.charAt(i));
    }

    out.flush();
  }
Example #3
0
 /** Flushes the OutputStream or Writer that this TreeDumper is using. */
 public void flushWriter() {
   out.flush();
 }