blob: 5112173013501a8182bd3668736b68dec4f380dd [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2005, 2016 IBM Corporation and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
*******************************************************************************/
package org.eclipse.dltk.python.parser.ast.expressions;
import org.eclipse.dltk.ast.ASTVisitor;
import org.eclipse.dltk.ast.DLTKToken;
import org.eclipse.dltk.ast.expressions.Expression;
import org.eclipse.dltk.ast.statements.Statement;
import org.eclipse.dltk.utils.CorePrinter;
/**
* Unary expression.
*
* Such as ++x, --x, +x, -x and other.
*
*/
public class UnaryExpression extends Expression {
/**
* Expression.
*/
protected Statement expression;
/**
* Unary kind.
*/
protected int kind;
/**
* Construct from position information, kind and expression.
*
* @param start -
* start position.
* @param end -
* end position.
* @param knd -
* unary kind.
* @see ExpressionConstants.
* @param expression -
* expression.
*/
public UnaryExpression(int start, int end, int knd, Statement expression) {
super(start, end);
this.kind = knd;
this.expression = expression;
}
/**
* Construct from ANTLR token, kind and expression.
*
* @param t -
* ANTLR token.
* @param knd -
* unary expression lind.
* @see ExpresionConstants.
* @param expression -
* expression.
*/
public UnaryExpression(DLTKToken t, int knd, Expression expression) {
super(t);
if (expression != null) {
this.setEnd(expression.sourceEnd());
}
this.kind = knd;
this.expression = expression;
}
/**
* Traverse.
*/
@Override
public void traverse(ASTVisitor pVisitor) throws Exception {
if (pVisitor.visit(this)) {
if (expression != null) {
expression.traverse(pVisitor);
}
pVisitor.endvisit(this);
}
}
/**
* Return unary expression kind.
*/
@Override
public int getKind() {
return kind;
}
/**
* Return expression.
*
* @return
*/
public Statement getExpression() {
return this.expression;
}
/**
* Testing purposes only. Used to print unary expression.
*/
@Override
public void printNode(CorePrinter output) {
output.formatPrintLn(this.getOperator());
if (this.expression != null) {
this.expression.printNode(output);
}
}
}