Class TreeFormatter
java.lang.Object
com.randomnoun.common.jexl.visitor.DepthFirstVisitor
com.randomnoun.common.jexl.visitor.TreeFormatter
- All Implemented Interfaces:
Visitor
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
ConstructorsConstructorDescriptionThe 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 TypeMethodDescriptionprotected voidadd(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.FormatCommandforce()A Force command inserts a line break and indents the next line to the current indentation level.protected com.randomnoun.common.jexl.visitor.FormatCommandforce(int i) protected com.randomnoun.common.jexl.visitor.FormatCommandindent()An Indent command increases the indentation level by one (or a user-specified amount).protected com.randomnoun.common.jexl.visitor.FormatCommandindent(int i) protected com.randomnoun.common.jexl.visitor.FormatCommandoutdent()An Outdent command is the reverse of the Indent command: it reduces the indentation level.protected com.randomnoun.common.jexl.visitor.FormatCommandoutdent(int i) protected voidAccepts a NodeListInterface object and performs an optional format command between each node in the list (but not after the last node).protected voidprocessList(NodeListInterface n, com.randomnoun.common.jexl.visitor.FormatCommand cmd) protected com.randomnoun.common.jexl.visitor.FormatCommandspace()A Space command simply adds one or a user-specified number of spaces between tokens.protected com.randomnoun.common.jexl.visitor.FormatCommandspace(int i) voidmultiplicativeExpression -> MultiplicativeExpression() nodeListOptional -> ( ( "+" | "-" ) MultiplicativeExpression() )*voidexpression -> Expression() nodeListOptional -> ( "," Expression() )*voidnodeToken -> "(" nodeOptional -> [ ArgumentList() ] nodeToken1 -> ")"voidnodeChoice -> "true" | "false"voidequalityExpression -> EqualityExpression() nodeListOptional -> ( "invalid input: '&'invalid input: '&'" EqualityExpression() )*voidrelationalExpression -> RelationalExpression() nodeListOptional -> ( ( "==" | "!=" ) RelationalExpression() )*voidvisit(Expression n) conditionalAndExpression -> ConditionalAndExpression() nodeListOptional -> ( "||" ConditionalAndExpression() )*voidnodeToken -> <IDENTIFIER> arguments -> Arguments()voidnodeChoice -> <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <STRING_LITERAL> | BooleanLiteral() | NullLiteral()voidunaryExpression -> UnaryExpression() nodeListOptional -> ( ( "*" | "/" | "%" ) UnaryExpression() )*voidnodeToken -> <IDENTIFIER> nodeListOptional -> ( "." <IDENTIFIER> )*voidExecutes the commands waiting in the command queue, then inserts the proper location information into the current NodeToken.voidvisit(NullLiteral n) nodeToken -> "null"voidnodeChoice -> FunctionCall() | Name() | Literal() | "(" Expression() ")"voidadditiveExpression -> AdditiveExpression() nodeListOptional -> ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*voidexpression -> Expression() nodeToken -> <EOF>voidnodeChoice -> ( "~" | "!" | "-" ) UnaryExpression() | PrimaryExpression()Methods inherited from class com.randomnoun.common.jexl.visitor.DepthFirstVisitor
visit, visit, visit, visit
-
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
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
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
A Force command inserts a line break and indents the next line to the current indentation level. Use "add(force());". -
force
-
indent
An Indent command increases the indentation level by one (or a user-specified amount). Use "add(indent());". -
indent
-
outdent
An Outdent command is the reverse of the Indent command: it reduces the indentation level. Use "add(outdent());". -
outdent
-
space
A Space command simply adds one or a user-specified number of spaces between tokens. Use "add(space());". -
space
-
add
Use this method to add FormatCommands to the command queue to be executed when the next token in the tree is visited. -
visit
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:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
expression -> Expression() nodeToken -> <EOF>
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
conditionalAndExpression -> ConditionalAndExpression() nodeListOptional -> ( "||" ConditionalAndExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
equalityExpression -> EqualityExpression() nodeListOptional -> ( "invalid input: '&'invalid input: '&'" EqualityExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
relationalExpression -> RelationalExpression() nodeListOptional -> ( ( "==" | "!=" ) RelationalExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
additiveExpression -> AdditiveExpression() nodeListOptional -> ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
multiplicativeExpression -> MultiplicativeExpression() nodeListOptional -> ( ( "+" | "-" ) MultiplicativeExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
unaryExpression -> UnaryExpression() nodeListOptional -> ( ( "*" | "/" | "%" ) UnaryExpression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeChoice -> ( "~" | "!" | "-" ) UnaryExpression() | PrimaryExpression()- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeChoice -> FunctionCall() | Name() | Literal() | "(" Expression() ")"- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeToken -> <IDENTIFIER> nodeListOptional -> ( "." <IDENTIFIER> )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeToken -> <IDENTIFIER> arguments -> Arguments()
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeToken -> "(" nodeOptional -> [ ArgumentList() ] nodeToken1 -> ")"- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
expression -> Expression() nodeListOptional -> ( "," Expression() )*
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeChoice -> <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <STRING_LITERAL> | BooleanLiteral() | NullLiteral()- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeChoice -> "true" | "false"- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-
visit
nodeToken -> "null"
- Specified by:
visitin interfaceVisitor- Overrides:
visitin classDepthFirstVisitor
-