net.sourceforge.czt.print.z
Class PrecedenceParenAnnVisitor

java.lang.Object
  extended by net.sourceforge.czt.print.z.PrecedenceParenAnnVisitor
All Implemented Interfaces:
net.sourceforge.czt.base.visitor.TermVisitor, ApplicationVisitor, OperatorApplicationVisitor, net.sourceforge.czt.util.Visitor, net.sourceforge.czt.z.visitor.ExprVisitor, net.sourceforge.czt.z.visitor.PredVisitor, net.sourceforge.czt.z.visitor.ProdExprVisitor

public class PrecedenceParenAnnVisitor
extends java.lang.Object
implements net.sourceforge.czt.base.visitor.TermVisitor, net.sourceforge.czt.z.visitor.PredVisitor, net.sourceforge.czt.z.visitor.ExprVisitor, net.sourceforge.czt.z.visitor.ProdExprVisitor, ApplicationVisitor, OperatorApplicationVisitor

This visitor visits an printable AST and adds necessary parentheses.

The input tree must be a tree created by the AstToPrintTreeVisitor. This visitor adds parenthesis annotations to terms where parenthesis are needed to preserve the syntactical structure of the operators when printed. That is, parenthesis annotations are added when a term does not already has one, and either the parent term has a higher priority, or the parent has the same priority but the parenthesis is enforced by associativity.

For instance, a conjunction where one of the arguments is an implication needs parenthesis around the implication, since conjunction has higher priority than implication. For the second example, recall that implication is right associative. An implication that has an implication as its first (left) argument needs parenthesis around, whereas an implication as second (right) argument does not need parenthesis.

Author:
Petra Malik

Constructor Summary
PrecedenceParenAnnVisitor()
           
 
Method Summary
protected  void addParenAnn(net.sourceforge.czt.base.ast.Term term)
           
protected  void addParenAnnIfNecessary(java.lang.Object object, Precedence parentPrec)
          Adds parenthesis annotations to the given object if it is an annotable term and the precedence of the parent is greater than the precedence of the given term.
protected  void addParenAnnIfNecessary(net.sourceforge.czt.base.ast.Term term, Precedence parentPrec)
          Adds parenthesis annotations to the given term if the precedence of the parent is greater than the precedence of the given term.
protected  boolean isInfix(net.sourceforge.czt.z.util.OperatorName opName)
           
 Precedence precedence(net.sourceforge.czt.base.ast.Term term)
           
protected  void preservePrecedence(net.sourceforge.czt.base.ast.Term term)
           
 void run(net.sourceforge.czt.base.ast.Term term)
          Visits a term and its children and adds ParenAnn to terms where this is needed to enforce the given precedence and associativity.
 java.lang.Object visitApplication(Application appl)
           
 java.lang.Object visitExpr(net.sourceforge.czt.z.ast.Expr term)
           
 java.lang.Object visitOperatorApplication(OperatorApplication appl)
           
 java.lang.Object visitPred(net.sourceforge.czt.z.ast.Pred term)
           
 java.lang.Object visitProdExpr(net.sourceforge.czt.z.ast.ProdExpr prodExpr)
           
 java.lang.Object visitTerm(net.sourceforge.czt.base.ast.Term term)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrecedenceParenAnnVisitor

public PrecedenceParenAnnVisitor()
Method Detail

run

public void run(net.sourceforge.czt.base.ast.Term term)

Visits a term and its children and adds ParenAnn to terms where this is needed to enforce the given precedence and associativity.


visitTerm

public java.lang.Object visitTerm(net.sourceforge.czt.base.ast.Term term)
Specified by:
visitTerm in interface net.sourceforge.czt.base.visitor.TermVisitor

visitPred

public java.lang.Object visitPred(net.sourceforge.czt.z.ast.Pred term)
Specified by:
visitPred in interface net.sourceforge.czt.z.visitor.PredVisitor

visitExpr

public java.lang.Object visitExpr(net.sourceforge.czt.z.ast.Expr term)
Specified by:
visitExpr in interface net.sourceforge.czt.z.visitor.ExprVisitor

preservePrecedence

protected void preservePrecedence(net.sourceforge.czt.base.ast.Term term)

visitApplication

public java.lang.Object visitApplication(Application appl)
Specified by:
visitApplication in interface ApplicationVisitor

isInfix

protected boolean isInfix(net.sourceforge.czt.z.util.OperatorName opName)

visitOperatorApplication

public java.lang.Object visitOperatorApplication(OperatorApplication appl)
Specified by:
visitOperatorApplication in interface OperatorApplicationVisitor

visitProdExpr

public java.lang.Object visitProdExpr(net.sourceforge.czt.z.ast.ProdExpr prodExpr)
Specified by:
visitProdExpr in interface net.sourceforge.czt.z.visitor.ProdExprVisitor

addParenAnnIfNecessary

protected void addParenAnnIfNecessary(java.lang.Object object,
                                      Precedence parentPrec)
Adds parenthesis annotations to the given object if it is an annotable term and the precedence of the parent is greater than the precedence of the given term.

Parameters:
object - the term to which annotations are added, if necesasry.
parentPrec - the precedence of the parent.

addParenAnnIfNecessary

protected void addParenAnnIfNecessary(net.sourceforge.czt.base.ast.Term term,
                                      Precedence parentPrec)
Adds parenthesis annotations to the given term if the precedence of the parent is greater than the precedence of the given term.

Parameters:
term - the term to which annotations are added, if necessary.
parentPrec - the precedence of the parent.

addParenAnn

protected void addParenAnn(net.sourceforge.czt.base.ast.Term term)

precedence

public Precedence precedence(net.sourceforge.czt.base.ast.Term term)


Copyright © 2003-2007 Community Z Tools Project. All Rights Reserved.