Class TreeFormatter

java.lang.Object
com.randomnoun.common.jexl.visitor.DepthFirstVisitor
com.randomnoun.common.jexl.visitor.TreeFormatter
All Implemented Interfaces:
Visitor

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

    Constructors
    Constructor
    Description
    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

    Modifier and Type
    Method
    Description
    protected void
    add(com.randomnoun.common.jexl.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 com.randomnoun.common.jexl.visitor.FormatCommand
    A Force command inserts a line break and indents the next line to the current indentation level.
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    force(int i)
     
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    An Indent command increases the indentation level by one (or a user-specified amount).
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    indent(int i)
     
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    An Outdent command is the reverse of the Indent command: it reduces the indentation level.
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    outdent(int i)
     
    protected void
    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, com.randomnoun.common.jexl.visitor.FormatCommand cmd)
     
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    A Space command simply adds one or a user-specified number of spaces between tokens.
    protected com.randomnoun.common.jexl.visitor.FormatCommand
    space(int i)
     
    void
    multiplicativeExpression -> MultiplicativeExpression() nodeListOptional -> ( ( "+" | "-" ) MultiplicativeExpression() )*
    void
    expression -> Expression() nodeListOptional -> ( "," Expression() )*
    void
    nodeToken -> "(" nodeOptional -> [ ArgumentList() ] nodeToken1 -> ")"
    void
    nodeChoice -> "true" | "false"
    void
    equalityExpression -> EqualityExpression() nodeListOptional -> ( "invalid input: '&'invalid input: '&'" EqualityExpression() )*
    void
    relationalExpression -> RelationalExpression() nodeListOptional -> ( ( "==" | "!=" ) RelationalExpression() )*
    void
    conditionalAndExpression -> ConditionalAndExpression() nodeListOptional -> ( "||" ConditionalAndExpression() )*
    void
    nodeToken -> <IDENTIFIER> arguments -> Arguments()
    void
    nodeChoice -> <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <STRING_LITERAL> | BooleanLiteral() | NullLiteral()
    void
    unaryExpression -> UnaryExpression() nodeListOptional -> ( ( "*" | "/" | "%" ) UnaryExpression() )*
    void
    nodeToken -> <IDENTIFIER> nodeListOptional -> ( "." <IDENTIFIER> )*
    void
    Executes the commands waiting in the command queue, then inserts the proper location information into the current NodeToken.
    void
    nodeToken -> "null"
    void
    nodeChoice -> FunctionCall() | Name() | Literal() | "(" Expression() ")"
    void
    additiveExpression -> AdditiveExpression() nodeListOptional -> ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*
    void
    expression -> Expression() nodeToken -> <EOF>
    void
    nodeChoice -> ( "~" | "!" | "-" ) UnaryExpression() | PrimaryExpression()

    Methods inherited from class com.randomnoun.common.jexl.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 Details

    • 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 Details

    • 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, com.randomnoun.common.jexl.visitor.FormatCommand cmd)
    • force

      protected com.randomnoun.common.jexl.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 com.randomnoun.common.jexl.visitor.FormatCommand force(int i)
    • indent

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

      protected com.randomnoun.common.jexl.visitor.FormatCommand indent(int i)
    • outdent

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

      protected com.randomnoun.common.jexl.visitor.FormatCommand outdent(int i)
    • space

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

      protected com.randomnoun.common.jexl.visitor.FormatCommand space(int i)
    • add

      protected void add(com.randomnoun.common.jexl.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(TopLevelExpression n)
       expression -> Expression()
       nodeToken -> <EOF>
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(Expression n)
       conditionalAndExpression -> ConditionalAndExpression()
       nodeListOptional -> ( "||" ConditionalAndExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

       equalityExpression -> EqualityExpression()
       nodeListOptional -> ( "invalid input: '&'invalid input: '&'" EqualityExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(EqualityExpression n)
       relationalExpression -> RelationalExpression()
       nodeListOptional -> ( ( "==" | "!=" ) RelationalExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(RelationalExpression n)
       additiveExpression -> AdditiveExpression()
       nodeListOptional -> ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(AdditiveExpression n)
       multiplicativeExpression -> MultiplicativeExpression()
       nodeListOptional -> ( ( "+" | "-" ) MultiplicativeExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

       unaryExpression -> UnaryExpression()
       nodeListOptional -> ( ( "*" | "/" | "%" ) UnaryExpression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(UnaryExpression n)
       nodeChoice -> ( "~" | "!" | "-" ) UnaryExpression()
             | PrimaryExpression()
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(PrimaryExpression n)
       nodeChoice -> FunctionCall()
             | Name()
             | Literal()
             | "(" Expression() ")"
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(Name n)
       nodeToken -> <IDENTIFIER>
       nodeListOptional -> ( "." <IDENTIFIER> )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(FunctionCall n)
       nodeToken -> <IDENTIFIER>
       arguments -> Arguments()
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(Arguments n)
       nodeToken -> "("
       nodeOptional -> [ ArgumentList() ]
       nodeToken1 -> ")"
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(ArgumentList n)
       expression -> Expression()
       nodeListOptional -> ( "," Expression() )*
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(Literal n)
       nodeChoice -> <INTEGER_LITERAL>
             | <FLOATING_POINT_LITERAL>
             | <CHARACTER_LITERAL>
             | <STRING_LITERAL>
             | BooleanLiteral()
             | NullLiteral()
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(BooleanLiteral n)
       nodeChoice -> "true"
             | "false"
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor
    • visit

      public void visit(NullLiteral n)
       nodeToken -> "null"
       
      Specified by:
      visit in interface Visitor
      Overrides:
      visit in class DepthFirstVisitor