piglet.visitor
Class TreeFormatter

java.lang.Object
  extended by piglet.visitor.DepthFirstVisitor
      extended by piglet.visitor.TreeFormatter
All Implemented Interfaces:
Visitor
Direct Known Subclasses:
PigletFormatter

public class TreeFormatter
extends DepthFirstVisitor

A skeleton output formatter for your language grammar. Using the add() method along with force(), indent(), and outdent(), you can easily specify how this visitor will format the given syntax tree. See the JTB documentation for more details. Pass your syntax tree to this visitor, and then to the TreeDumper visitor in order to "pretty print" your tree.


Constructor Summary
TreeFormatter()
          The default constructor assumes an indentation amount of 3 spaces and no line-wrap.
TreeFormatter(int indentAmt, int wrapWidth)
          This constructor accepts an indent amount and a line width which is used to wrap long lines.
 
Method Summary
protected  void add(piglet.visitor.FormatCommand cmd)
          Use this method to add FormatCommands to the command queue to be executed when the next token in the tree is visited.
protected  piglet.visitor.FormatCommand force()
          A Force command inserts a line break and indents the next line to the current indentation level.
protected  piglet.visitor.FormatCommand force(int i)
           
protected  piglet.visitor.FormatCommand indent()
          An Indent command increases the indentation level by one (or a user-specified amount).
protected  piglet.visitor.FormatCommand indent(int i)
           
protected  piglet.visitor.FormatCommand outdent()
          An Outdent command is the reverse of the Indent command: it reduces the indentation level.
protected  piglet.visitor.FormatCommand outdent(int i)
           
protected  void processList(NodeListInterface n)
          Accepts a NodeListInterface object and performs an optional format command between each node in the list (but not after the last node).
protected  void processList(NodeListInterface n, piglet.visitor.FormatCommand cmd)
           
protected  piglet.visitor.FormatCommand space()
          A Space command simply adds one or a user-specified number of spaces between tokens.
protected  piglet.visitor.FormatCommand space(int i)
           
 void visit(BinOp n)
           operator -> Operator() exp -> Exp() exp1 -> Exp()
 void visit(Call n)
           nodeToken -> "CALL" exp -> Exp() nodeToken1 -> "(" nodeListOptional -> ( Exp() )* nodeToken2 -> ")"
 void visit(CJumpStmt n)
           nodeToken -> "CJUMP" exp -> Exp() label -> Label()
 void visit(ErrorStmt n)
           nodeToken -> "ERROR"
 void visit(Exp n)
           nodeChoice -> StmtExp() | Call() | HAllocate() | BinOp() | Temp() | IntegerLiteral() | Label()
 void visit(Goal n)
           nodeToken -> "MAIN" stmtList -> StmtList() nodeToken1 -> "END" nodeListOptional -> ( Procedure() )* nodeToken2 -> <EOF>
 void visit(HAllocate n)
           nodeToken -> "HALLOCATE" exp -> Exp()
 void visit(HLoadStmt n)
           nodeToken -> "HLOAD" temp -> Temp() exp -> Exp() integerLiteral -> IntegerLiteral()
 void visit(HStoreStmt n)
           nodeToken -> "HSTORE" exp -> Exp() integerLiteral -> IntegerLiteral() exp1 -> Exp()
 void visit(IntegerLiteral n)
           nodeToken -> <INTEGER_LITERAL>
 void visit(JumpStmt n)
           nodeToken -> "JUMP" label -> Label()
 void visit(Label n)
           nodeToken -> <IDENTIFIER>
 void visit(MoveStmt n)
           nodeToken -> "MOVE" temp -> Temp() exp -> Exp()
 void visit(NodeToken n)
          Executes the commands waiting in the command queue, then inserts the proper location information into the current NodeToken.
 void visit(NoOpStmt n)
           nodeToken -> "NOOP"
 void visit(Operator n)
           nodeChoice -> "LT" | "PLUS" | "MINUS" | "TIMES"
 void visit(PrintStmt n)
           nodeToken -> "PRINT" exp -> Exp()
 void visit(Procedure n)
           label -> Label() nodeToken -> "[" integerLiteral -> IntegerLiteral() nodeToken1 -> "]" stmtExp -> StmtExp()
 void visit(Stmt n)
           nodeChoice -> NoOpStmt() | ErrorStmt() | CJumpStmt() | JumpStmt() | HStoreStmt() | HLoadStmt() | MoveStmt() | PrintStmt()
 void visit(StmtExp n)
           nodeToken -> "BEGIN" stmtList -> StmtList() nodeToken1 -> "RETURN" exp -> Exp() nodeToken2 -> "END"
 void visit(StmtList n)
           nodeListOptional -> ( ( Label() )? Stmt() )*
 void visit(Temp n)
           nodeToken -> "TEMP" integerLiteral -> IntegerLiteral()
 
Methods inherited from class piglet.visitor.DepthFirstVisitor
visit, visit, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeFormatter

public TreeFormatter()
The default constructor assumes an indentation amount of 3 spaces and no line-wrap. You may alternately use the other constructor to specify your own indentation amount and line width.


TreeFormatter

public TreeFormatter(int indentAmt,
                     int wrapWidth)
This constructor accepts an indent amount and a line width which is used to wrap long lines. If a token's beginColumn value is greater than the specified wrapWidth, it will be moved to the next line and indented one extra level. To turn off line-wrapping, specify a wrapWidth of 0.

Parameters:
indentAmt - Amount of spaces per indentation level.
wrapWidth - Wrap lines longer than wrapWidth. 0 for no wrap.
Method Detail

processList

protected void processList(NodeListInterface n)
Accepts a NodeListInterface object and performs an optional format command between each node in the list (but not after the last node).


processList

protected void processList(NodeListInterface n,
                           piglet.visitor.FormatCommand cmd)

force

protected piglet.visitor.FormatCommand force()
A Force command inserts a line break and indents the next line to the current indentation level. Use "add(force());".


force

protected piglet.visitor.FormatCommand force(int i)

indent

protected piglet.visitor.FormatCommand indent()
An Indent command increases the indentation level by one (or a user-specified amount). Use "add(indent());".


indent

protected piglet.visitor.FormatCommand indent(int i)

outdent

protected piglet.visitor.FormatCommand outdent()
An Outdent command is the reverse of the Indent command: it reduces the indentation level. Use "add(outdent());".


outdent

protected piglet.visitor.FormatCommand outdent(int i)

space

protected piglet.visitor.FormatCommand space()
A Space command simply adds one or a user-specified number of spaces between tokens. Use "add(space());".


space

protected piglet.visitor.FormatCommand space(int i)

add

protected void add(piglet.visitor.FormatCommand cmd)
Use this method to add FormatCommands to the command queue to be executed when the next token in the tree is visited.


visit

public void visit(NodeToken n)
Executes the commands waiting in the command queue, then inserts the proper location information into the current NodeToken. 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.

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Goal n)
 nodeToken -> "MAIN"
 stmtList -> StmtList()
 nodeToken1 -> "END"
 nodeListOptional -> ( Procedure() )*
 nodeToken2 -> <EOF>
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(StmtList n)
 nodeListOptional -> ( ( Label() )? Stmt() )*
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Procedure n)
 label -> Label()
 nodeToken -> "["
 integerLiteral -> IntegerLiteral()
 nodeToken1 -> "]"
 stmtExp -> StmtExp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Stmt n)
 nodeChoice -> NoOpStmt()
       | ErrorStmt()
       | CJumpStmt()
       | JumpStmt()
       | HStoreStmt()
       | HLoadStmt()
       | MoveStmt()
       | PrintStmt()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(NoOpStmt n)
 nodeToken -> "NOOP"
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(ErrorStmt n)
 nodeToken -> "ERROR"
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(CJumpStmt n)
 nodeToken -> "CJUMP"
 exp -> Exp()
 label -> Label()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(JumpStmt n)
 nodeToken -> "JUMP"
 label -> Label()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(HStoreStmt n)
 nodeToken -> "HSTORE"
 exp -> Exp()
 integerLiteral -> IntegerLiteral()
 exp1 -> Exp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(HLoadStmt n)
 nodeToken -> "HLOAD"
 temp -> Temp()
 exp -> Exp()
 integerLiteral -> IntegerLiteral()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(MoveStmt n)
 nodeToken -> "MOVE"
 temp -> Temp()
 exp -> Exp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(PrintStmt n)
 nodeToken -> "PRINT"
 exp -> Exp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Exp n)
 nodeChoice -> StmtExp()
       | Call()
       | HAllocate()
       | BinOp()
       | Temp()
       | IntegerLiteral()
       | Label()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(StmtExp n)
 nodeToken -> "BEGIN"
 stmtList -> StmtList()
 nodeToken1 -> "RETURN"
 exp -> Exp()
 nodeToken2 -> "END"
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Call n)
 nodeToken -> "CALL"
 exp -> Exp()
 nodeToken1 -> "("
 nodeListOptional -> ( Exp() )*
 nodeToken2 -> ")"
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(HAllocate n)
 nodeToken -> "HALLOCATE"
 exp -> Exp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(BinOp n)
 operator -> Operator()
 exp -> Exp()
 exp1 -> Exp()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Operator n)
 nodeChoice -> "LT"
       | "PLUS"
       | "MINUS"
       | "TIMES"
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Temp n)
 nodeToken -> "TEMP"
 integerLiteral -> IntegerLiteral()
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(IntegerLiteral n)
 nodeToken -> <INTEGER_LITERAL>
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor

visit

public void visit(Label n)
 nodeToken -> <IDENTIFIER>
 

Specified by:
visit in interface Visitor
Overrides:
visit in class DepthFirstVisitor