Class Unparser


  • public class Unparser
    extends java.lang.Object
    Unparses (un-compiles) an AST to a Writer. See main(String[]) for a usage example.
    • Field Detail

      • importUnparser

        private final Visitor.ImportVisitor<java.lang.Void,​java.lang.RuntimeException> importUnparser
      • atomUnparser

        private final Visitor.AtomVisitor<java.lang.Void,​java.lang.RuntimeException> atomUnparser
      • typeUnparser

        private final Visitor.TypeVisitor<java.lang.Void,​java.lang.RuntimeException> typeUnparser
      • rvalueUnparser

        private final Visitor.RvalueVisitor<java.lang.Void,​java.lang.RuntimeException> rvalueUnparser
      • lvalueUnparser

        private final Visitor.LvalueVisitor<java.lang.Void,​java.lang.RuntimeException> lvalueUnparser
      • annotationUnparser

        private final Visitor.AnnotationVisitor<java.lang.Void,​java.lang.RuntimeException> annotationUnparser
      • modifierUnparser

        private final Visitor.ModifierVisitor<java.lang.Void,​java.lang.RuntimeException> modifierUnparser
      • lambdaBodyUnparser

        private final Visitor.LambdaBodyVisitor<java.lang.Void,​java.lang.RuntimeException> lambdaBodyUnparser
      • pw

        protected final java.io.PrintWriter pw
        Where the visit...() methods print their text. Notice that this PrintWriter does not print to the output directly, but through an AutoIndentWriter.
      • LEFT_ASSOCIATIVE_OPERATORS

        private static final java.util.Set<java.lang.String> LEFT_ASSOCIATIVE_OPERATORS
      • RIGHT_ASSOCIATIVE_OPERATORS

        private static final java.util.Set<java.lang.String> RIGHT_ASSOCIATIVE_OPERATORS
      • UNARY_OPERATORS

        private static final java.util.Set<java.lang.String> UNARY_OPERATORS
      • OPERATOR_PRECEDENCE

        private static final java.util.Map<java.lang.String,​java.lang.Integer> OPERATOR_PRECEDENCE
        Maps (pseudo-)operators like "?:" and "x++" to precedences (higher value means higher precedence).
    • Constructor Detail

      • Unparser

        public Unparser​(java.io.Writer w)
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Testing of parsing/unparsing.

        Reads compilation units from the files named on the command line and unparses them to System.out.

        Throws:
        java.lang.Exception
      • close

        public void close()
        Flushes all generated code.
      • unparseAbstractCompilationUnit

        public void unparseAbstractCompilationUnit​(Java.AbstractCompilationUnit cu)
        Parameters:
        cu - The compilation unit to unparse
      • unparseLambdaBody

        public void unparseLambdaBody​(Java.LambdaBody body)
      • unparseType

        public void unparseType​(Java.Type t)
      • unparseAtom

        public void unparseAtom​(Java.Atom a)
      • unparseRvalue

        public void unparseRvalue​(Java.Rvalue rv)
      • unparseLvalue

        public void unparseLvalue​(Java.Lvalue lv)
      • unparseUnaryOperation

        private void unparseUnaryOperation​(Java.Rvalue operand,
                                           java.lang.String unaryOperator)
        Iff the operand is unnatural for the unaryOperator, encloses the operand in parentheses. Example: "a+b" is an unnatural operand for unary "!x".
        Parameters:
        unaryOperator - ++x --x +x -x ~x !x x++ x--
      • unparseLhs

        private void unparseLhs​(Java.Atom lhs,
                                java.lang.String binaryOperator)
        Iff the lhs is unnatural for the binaryOperator, encloses the lhs in parentheses. Example: "a+b" is an unnatural lhs for operator "*".
        Parameters:
        binaryOperator - = +=... ?: || && | ^ & == != < > <= >= instanceof << >> >>> + - * / % cast
      • unparseRhs

        private void unparseRhs​(Java.Rvalue rhs,
                                java.lang.String binaryOperator)
        Iff the rhs is unnatural for the binaryOperator, enclose the rhs in parentheses. Example: "a+b" is an unnatural rhs for operator "*".
      • unparse

        private void unparse​(Java.Atom operand,
                             boolean natural)
      • isRightAssociate

        private static boolean isRightAssociate​(java.lang.String operator)
        Returns true iff operator is right associative e.g. a = b = c evaluates as a = (b = c).
        Returns:
        Return true iff operator is right associative
      • isLeftAssociate

        private static boolean isLeftAssociate​(java.lang.String operator)
        Returns true iff operator is left associative e.g. a - b - c evaluates as (a - b) - c.
      • comparePrecedence

        private static int comparePrecedence​(java.lang.String operator,
                                             Java.Atom operand)
        Returns a value
        • < 0 iff the operator has lower precedence than the operand
        • ==; 0 iff the operator has equal precedence than the operand
        • > 0 iff the operator has higher precedence than the operand
      • getOperatorPrecedence

        private static int getOperatorPrecedence​(java.lang.String operator)
      • unparseAnnotations

        private void unparseAnnotations​(Java.Annotation[] annotations)
      • unparseModifiers

        private void unparseModifiers​(Java.Modifier[] modifiers)
      • unparseTypeParameter

        private void unparseTypeParameter​(Java.TypeParameter typeParameter)
      • unparseFunctionInvocationArguments

        private void unparseFunctionInvocationArguments​(Java.Rvalue[] arguments)
      • unparseVariableDeclarators

        private void unparseVariableDeclarators​(Java.VariableDeclarator[] variableDeclarators)