/** * 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); }
/** * Executes the commands waiting in the command queue, then inserts the proper location * information into the current NodeToken. * * <p>If there are any special tokens preceding this token, they will be given the current * location information. The token will follow on the next line, at the proper indentation level. * If this is not the behavior you want from special tokens, feel free to modify this method. */ public void visit(NodeToken n) { for (Enumeration<FormatCommand> e = cmdQueue.elements(); e.hasMoreElements(); ) { FormatCommand cmd = e.nextElement(); switch (cmd.getCommand()) { case FormatCommand.FORCE: curLine += cmd.getNumCommands(); curColumn = curIndent + 1; break; case FormatCommand.INDENT: curIndent += indentAmt * cmd.getNumCommands(); break; case FormatCommand.OUTDENT: if (curIndent >= indentAmt) curIndent -= indentAmt * cmd.getNumCommands(); break; case FormatCommand.SPACE: curColumn += cmd.getNumCommands(); break; default: throw new TreeFormatterException("Invalid value in command queue."); } } cmdQueue.removeAllElements(); // // Handle all special tokens preceding this NodeToken // if (n.numSpecials() > 0) for (Enumeration<NodeToken> e = n.specialTokens.elements(); e.hasMoreElements(); ) { NodeToken special = e.nextElement(); // // -Place the token. // -Move cursor to next line after the special token. // -Don't update curColumn--want to keep current indent level. // placeToken(special, curLine, curColumn); curLine = special.endLine + 1; } placeToken(n, curLine, curColumn); curLine = n.endLine; curColumn = n.endColumn; }