Class JavaPrinter

  • All Implemented Interfaces:
    IVoidVisitor

    public class JavaPrinter
    extends DepthFirstVoidVisitor
    The JavaPrinter visitor reprints (with indentation) JavaCC grammar Java specific productions. CODEJAVA

    Notes :

    • it merely adds spaces and new lines (LS) to the default visitor between identifiers/keywords/punctuation
    • so methods have been copied from the default visitor, variables have been inlined, and for index-loops have been refactored to for-each loops
    • sb.append(spc.spc), sb.append(' ') and sb.append(LS) are done at the highest (calling) level (except for Modifiers() and VariableModifiers() which prints the last space if not empty)
    • sb.append(spc.spc) is done after sb.append(LS)
    • sb.append(' ') is not merged with printing punctuation / operators (to prepare evolutions for other formatting preferences), but is indeed merged with printing keywords

    Visitor maintains state (for a grammar), and not supposed to be run in parallel threads (on the same grammar).

    TODO extract / refactor methods for custom formatting
    TESTCASE some to add
    • Field Detail

      • sb

        protected java.lang.StringBuilder sb
        The buffer to print into
      • spc

        protected Spacing spc
        The indentation object
      • JNCDCP

        public java.lang.String JNCDCP
        The node class comment prefix
    • Constructor Detail

      • JavaPrinter

        protected JavaPrinter​(JTBOptions aJopt,
                              java.lang.StringBuilder aSb,
                              Spacing aSPC)
        Constructor with a given buffer and indentation.
        Parameters:
        aJopt - - the JTB options
        aSb - - the buffer to print into (will be allocated if null)
        aSPC - - the Spacing indentation object (will be allocated and set to a default if null)
    • Method Detail

      • oneNewLine

        protected void oneNewLine​(INode n)
        Prints into the current buffer a node class comment and a new line.
        Parameters:
        n - - the node for the node class comment
      • oneNewLine

        protected void oneNewLine​(INode n,
                                  java.lang.String str)
        Prints into the current buffer a node class comment, an extra given comment, and a new line.
        Parameters:
        n - - the node for the node class comment
        str - - the extra comment
      • twoNewLines

        protected void twoNewLines​(INode n)
        Prints twice into the current buffer a node class comment and a new line.
        Parameters:
        n - - the node for the node class comment
      • twoNewLines

        protected void twoNewLines​(INode n,
                                   java.lang.String str)
        Prints twice into the current buffer a node class comment, an extra given comment, and a new line.
        Parameters:
        n - - the node for the node class comment
        str - - the extra comment
      • visit

        public void visit​(Modifiers n)
        Visits a Modifiers node, whose child is the following :

        f0 -> ( ( %00 "public"
        .. .. . | %01 "static"
        .. .. . | %02 "protected"
        .. .. . | %03 "private"
        .. .. . | %04 "final"
        .. .. . | %05 "abstract"
        .. .. . | %06 "synchronized"
        .. .. . | %07 "native"
        .. .. . | %08 "transient"
        .. .. . | %09 "volatile"
        .. .. . | %10 "strictfp"
        .. .. . | %11 Annotation() ) )*
        s: -2047145049

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ExtendsList n)
        Visits a ExtendsList node, whose children are the following :

        f0 -> "extends"
        f1 -> ClassOrInterfaceType()
        f2 -> ( #0 "," #1 ClassOrInterfaceType() )*
        s: 24279225

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(EnumBody n)
        Visits a EnumBody node, whose children are the following :

        f0 -> "{"
        f1 -> [ #0 EnumConstant()
        .. .. . #1 ( $0 "," $1 EnumConstant() )* ]
        f2 -> [ "," ]
        f3 -> [ #0 ";"
        .. .. . #1 ( ClassOrInterfaceBodyDeclaration() )* ]
        f4 -> "}"
        s: -1338633176

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(TypeParameter n)
        Visits a TypeParameter node, whose children are the following :

        f0 -> < IDENTIFIER > //cp Expansion expLvl==0
        f1 -> [ TypeBound() ] //cp ExpansionChoices only f0
        s: 1306471903

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(TypeBound n)
        Visits a TypeBound node, whose children are the following :

        f0 -> "extends"
        f1 -> ClassOrInterfaceType()
        f2 -> ( #0 "&" #1 ClassOrInterfaceType() )*
        s: -2080520397

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ArrayInitializer n)
        Visits a ArrayInitializer node, whose children are the following :

        f0 -> "{"
        f1 -> [ #0 VariableInitializer()
        .. .. . #1 ( $0 "," $1 VariableInitializer() )* ]
        f2 -> [ "," ]
        f3 -> "}"
        s: -251326055

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(MethodDeclaration n)
        Visits a MethodDeclaration node, whose children are the following :

        f0 -> [ TypeParameters() ]
        f1 -> ResultType()
        f2 -> MethodDeclarator()
        f3 -> [ #0 "throws" #1 NameList() ]
        f4 -> ( %0 Block()
        .. .. | %1 ";" )
        s: -418256626

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ConstructorDeclaration n)
        Visits a ConstructorDeclaration node, whose children are the following :

        f0 -> [ TypeParameters() ]
        f1 -> < IDENTIFIER >
        f2 -> FormalParameters()
        f3 -> [ #0 "throws" #1 NameList() ]
        f4 -> "{"
        f5 -> [ ExplicitConstructorInvocation() ]
        f6 -> ( BlockStatement() )*
        f7 -> "}"
        s: 1258397065

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ExplicitConstructorInvocation n)
        Visits a ExplicitConstructorInvocation node, whose child is the following :

        f0 -> ( %0 #0 [ $0 "<" $1 ReferenceType()
        .. .. . .. .. . $2 ( ?0 "," ?1 ReferenceType() )*
        .. .. . .. .. . $3 ">" ]
        .. .. . .. #1 ( &0 $0 "this" $1 Arguments() $2 ";"
        .. .. . .. .. | &1 $0 "super" $1 Arguments() $2 ";" )
        .. .. | %1 ( #0 PrimaryExpression() #1 "." #2 "super" #3 Arguments() #4 ";" ) )
        s: -492225557

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(Expression n)
        Visits a Expression node, whose children are the following :

        f0 -> ConditionalExpression()
        f1 -> [ #0 AssignmentOperator() #1 Expression() ]
        s: -1186270200

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ShiftExpression n)
        Visits a ShiftExpression node, whose children are the following :

        f0 -> AdditiveExpression()
        f1 -> ( #0 ( %0 "<<"
        .. .. . .. | %1 RUnsignedShift()
        .. .. . .. | %2 RSignedShift() )
        .. .. . #1 AdditiveExpression() )*
        s: 1210478291

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(CastLookahead n)
        Visits a CastLookahead node, whose child is the following :

        f0 -> . %0 #0 "(" #1 PrimitiveType()
        .. .. | %1 #0 "(" #1 Type() #2 "[" #3 "]"
        .. .. | %2 #0 "(" #1 Type() #2 ")"
        .. .. . .. #3 ( &0 "~"
        .. .. . .. .. | &1 "!"
        .. .. . .. .. | &2 "("
        .. .. . .. .. | &3 < IDENTIFIER >
        .. .. . .. .. | &4 "this"
        .. .. . .. .. | &5 "super"
        .. .. . .. .. | &6 "new"
        .. .. . .. .. | &7 Literal() )
        s: 611584359

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(Literal n)
        Visits a Literal node, whose child is the following :

        f0 -> . %0 < INTEGER_LITERAL >
        .. .. | %1 < FLOATING_POINT_LITERAL >
        .. .. | %2 < CHARACTER_LITERAL >
        .. .. | %3 < STRING_LITERAL >
        .. .. | %4 BooleanLiteral()
        .. .. | %5 NullLiteral()
        s: 454259936

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(AllocationExpression n)
        Visits a AllocationExpression node, whose child is the following :

        f0 -> . %0 #0 "new" #1 PrimitiveType() #2 ArrayDimsAndInits()
        .. .. | %1 #0 "new" #1 ClassOrInterfaceType()
        .. .. . .. #2 [ &0 EmptyTypeArguments()
        .. .. . .. .. | &1 TypeArguments() ]
        .. .. . .. #3 ( &0 ArrayDimsAndInits()
        .. .. . .. .. | &1 $0 Arguments()
        .. .. . .. .. . .. $1 [ ClassOrInterfaceBody() ] )
        s: 1688598744

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(Block n)
        Visits a Block node, whose children are the following :

        f0 -> "{"
        f1 -> ( BlockStatement() )*
        f2 -> "}"
        s: -47169424

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(StatementExpression n)
        Visits a StatementExpression node, whose child is the following :

        f0 -> . %0 PreIncrementExpression()
        .. .. | %1 PreDecrementExpression()
        .. .. | %2 #0 PrimaryExpression()
        .. .. . .. #1 [ &0 "++"
        .. .. . .. .. | &1 "--"
        .. .. . .. .. | &2 $0 AssignmentOperator() $1 Expression() ]
        s: 757890000

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(SwitchStatement n)
        Visits a SwitchStatement node, whose children are the following :

        f0 -> "switch"
        f1 -> "("
        f2 -> Expression()
        f3 -> ")"
        f4 -> "{"
        f5 -> ( #0 SwitchLabel()
        .. .. . #1 ( BlockStatement() )* )*
        f6 -> "}"
        s: 645895087

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(SwitchLabel n)
        Visits a SwitchLabel node, whose child is the following :

        f0 -> . %0 #0 "case" #1 Expression() #2 ":"
        .. .. | %1 #0 "default" #1 ":"
        s: 63513165

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(IfStatement n)
        Visits a IfStatement node, whose children are the following :

        f0 -> "if"
        f1 -> "("
        f2 -> Expression()
        f3 -> ")"
        f4 -> Statement()
        f5 -> [ #0 "else" #1 Statement() ]
        s: -1906079982

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(DoStatement n)
        Visits a DoStatement node, whose children are the following :

        f0 -> "do"
        f1 -> Statement()
        f2 -> "while"
        f3 -> "("
        f4 -> Expression()
        f5 -> ")"
        f6 -> ";"
        s: 1162769715

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(ForStatement n)
        Visits a ForStatement node, whose children are the following :

        f0 -> "for"
        f1 -> "("
        f2 -> ( %0 #0 VariableModifiers() #1 Type() #2 < IDENTIFIER > #3 ":" #4 Expression()
        .. .. | %1 #0 [ ForInit() ]
        .. .. . .. #1 ";"
        .. .. . .. #2 [ Expression() ]
        .. .. . .. #3 ";"
        .. .. . .. #4 [ ForUpdate() ] )
        f3 -> ")"
        f4 -> Statement()
        s: 755358653

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(Statement n)
        Visits a Statement node, whose child is the following :

        f0 -> . %00 LabeledStatement()
        .. .. | %01 AssertStatement()
        .. .. | %02 Block()
        .. .. | %03 EmptyStatement()
        .. .. | %04 #0 StatementExpression() #1 ";"
        .. .. | %05 SwitchStatement()
        .. .. | %06 IfStatement()
        .. .. | %07 WhileStatement()
        .. .. | %08 DoStatement()
        .. .. | %09 ForStatement()
        .. .. | %10 BreakStatement()
        .. .. | %11 ContinueStatement()
        .. .. | %12 ReturnStatement()
        .. .. | %13 ThrowStatement()
        .. .. | %14 SynchronizedStatement()
        .. .. | %15 TryStatement()
        s: 1394695492

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(TryStatement n)
        Visits a TryStatement node, whose children are the following :

        f0 -> "try"
        f1 -> Block()
        f2 -> ( #0 "catch" #1 "(" #2 FormalParameter() #3 ")" #4 Block() )*
        f3 -> [ #0 "finally" #1 Block() ]
        s: 1108527850

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(MemberValue n)
        Visits a MemberValue node, whose child is the following :

        f0 -> . %0 Annotation()
        .. .. | %1 MemberValueArrayInitializer()
        .. .. | %2 ConditionalExpression()
        s: -1120846693

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit
      • visit

        public void visit​(AnnotationTypeMemberDeclaration n)
        Visits a AnnotationTypeMemberDeclaration node, whose child is the following :

        f0 -> . %0 #0 Modifiers()
        .. .. . .. #1 ( &0 $0 Type() $1 < IDENTIFIER > $2 "(" $3 ")"
        .. .. . .. .. . .. $4 [ DefaultValue() ]
        .. .. . .. .. . .. $5 ";"
        .. .. . .. .. | &1 ClassOrInterfaceDeclaration()
        .. .. . .. .. | &2 EnumDeclaration()
        .. .. . .. .. | &3 AnnotationTypeDeclaration()
        .. .. . .. .. | &4 FieldDeclaration() )
        .. .. | %1 ";"
        s: -1120210008

        Specified by:
        visit in interface IVoidVisitor
        Overrides:
        visit in class DepthFirstVoidVisitor
        Parameters:
        n - - the node to visit