public class PrecedenceParenAnnVisitor extends Object implements net.sourceforge.czt.base.visitor.TermVisitor<Object>, PredVisitor<Object>, ExprVisitor<Object>, ProdExprVisitor<Object>, ApplicationVisitor<Object>, OperatorApplicationVisitor<Object>
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.
Constructor and Description |
---|
PrecedenceParenAnnVisitor() |
Modifier and Type | Method and Description |
---|---|
protected void |
addParenAnn(net.sourceforge.czt.base.ast.Term term) |
protected void |
addParenAnnIfNecessary(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(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.
|
Object |
visitApplication(Application appl) |
Object |
visitExpr(Expr term) |
Object |
visitOperatorApplication(OperatorApplication appl) |
Object |
visitPred(Pred term) |
Object |
visitProdExpr(ProdExpr prodExpr) |
Object |
visitTerm(net.sourceforge.czt.base.ast.Term term) |
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.
term
- public Object visitTerm(net.sourceforge.czt.base.ast.Term term)
visitTerm
in interface net.sourceforge.czt.base.visitor.TermVisitor<Object>
public Object visitPred(Pred term)
visitPred
in interface PredVisitor<Object>
public Object visitExpr(Expr term)
visitExpr
in interface ExprVisitor<Object>
protected void preservePrecedence(net.sourceforge.czt.base.ast.Term term)
public Object visitApplication(Application appl)
visitApplication
in interface ApplicationVisitor<Object>
protected boolean isInfix(OperatorName opName)
public Object visitOperatorApplication(OperatorApplication appl)
visitOperatorApplication
in interface OperatorApplicationVisitor<Object>
public Object visitProdExpr(ProdExpr prodExpr)
visitProdExpr
in interface ProdExprVisitor<Object>
protected void addParenAnnIfNecessary(Object object, Precedence parentPrec)
object
- the term to which annotations are added, if necesasry.parentPrec
- the precedence of the parent.protected void addParenAnnIfNecessary(net.sourceforge.czt.base.ast.Term term, Precedence parentPrec)
term
- the term to which annotations are added, if necessary.parentPrec
- the precedence of the parent.protected void addParenAnn(net.sourceforge.czt.base.ast.Term term)
public Precedence precedence(net.sourceforge.czt.base.ast.Term term)
Copyright © 2003–2016 Community Z Tools Project. All rights reserved.