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 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
force()
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
indent()
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
outdent()
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
space()
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
visit
(Expression n) 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
visit
(NullLiteral n) 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
-
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:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
expression -> Expression() nodeToken -> <EOF>
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
conditionalAndExpression -> ConditionalAndExpression() nodeListOptional -> ( "||" ConditionalAndExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
equalityExpression -> EqualityExpression() nodeListOptional -> ( "invalid input: '&'invalid input: '&'" EqualityExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
relationalExpression -> RelationalExpression() nodeListOptional -> ( ( "==" | "!=" ) RelationalExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
additiveExpression -> AdditiveExpression() nodeListOptional -> ( ( "<" | ">" | "<=" | ">=" ) AdditiveExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
multiplicativeExpression -> MultiplicativeExpression() nodeListOptional -> ( ( "+" | "-" ) MultiplicativeExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
unaryExpression -> UnaryExpression() nodeListOptional -> ( ( "*" | "/" | "%" ) UnaryExpression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeChoice -> ( "~" | "!" | "-" ) UnaryExpression() | PrimaryExpression()
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeChoice -> FunctionCall() | Name() | Literal() | "(" Expression() ")"
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeToken -> <IDENTIFIER> nodeListOptional -> ( "." <IDENTIFIER> )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeToken -> <IDENTIFIER> arguments -> Arguments()
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeToken -> "(" nodeOptional -> [ ArgumentList() ] nodeToken1 -> ")"
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
expression -> Expression() nodeListOptional -> ( "," Expression() )*
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeChoice -> <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <STRING_LITERAL> | BooleanLiteral() | NullLiteral()
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeChoice -> "true" | "false"
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-
visit
nodeToken -> "null"
- Specified by:
visit
in interfaceVisitor
- Overrides:
visit
in classDepthFirstVisitor
-