Skip to content
Permalink
Browse files
GROOVY-10598: declaration is assignment
  • Loading branch information
eric-milles committed May 1, 2022
1 parent e183dc8 commit 5c921800247481fdd538205f30a7bca38c343bf7
Showing 3 changed files with 53 additions and 59 deletions.
@@ -24,6 +24,8 @@
import org.codehaus.groovy.syntax.Token;
import org.codehaus.groovy.syntax.Types;

import static java.util.Objects.requireNonNull;

/**
* Represents two expressions and an operation
*/
@@ -34,18 +36,13 @@ public class BinaryExpression extends Expression {
private final Token operation;
private boolean safe = false;

public BinaryExpression(Expression leftExpression,
Token operation,
Expression rightExpression) {
public BinaryExpression(final Expression leftExpression, final Token operation, final Expression rightExpression) {
this.leftExpression = leftExpression;
this.operation = operation;
this.rightExpression = rightExpression;
this.operation = requireNonNull(operation);
}

public BinaryExpression(Expression leftExpression,
Token operation,
Expression rightExpression,
boolean safe) {
public BinaryExpression(final Expression leftExpression, final Token operation, final Expression rightExpression, final boolean safe) {
this(leftExpression, operation, rightExpression);
this.safe = safe;
}
@@ -46,41 +46,44 @@
public class DeclarationExpression extends BinaryExpression {

/**
* Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'".
* Creates a declaration like "def v" or "int w = 0".
*
* @param left
* the left hand side of a variable declaration
* @param operation
* the operation, typically an assignment operator
* the operation, assumed to be assignment operator
* @param right
* the right hand side of a declaration
* the right hand side of a declaration; {@link EmptyExpression} for no initial value
*/
public DeclarationExpression(VariableExpression left, Token operation, Expression right) {
super(left,operation,right);
public DeclarationExpression(final VariableExpression left, final Token operation, final Expression right) {
this((Expression) left, operation, right);
}

/**
* Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"
* Creates a declaration like "def v" or "int w = 0" or "def (x, y) = [1, 2]".
*
* @param left
* the left hand side of a declaration. Must be either a VariableExpression or
* a TupleExpression with at least one element.
* the left hand side of a declaration -- either a {@link VariableExpression} or
* a {@link TupleExpression} with at least one element
* @param operation
* the operation, typically an assignment operator
* the operation, assumed to be assignment operator
* @param right
* the right hand side of a declaration
*/
public DeclarationExpression(Expression left, Token operation, Expression right) {
super(left,operation,right);
public DeclarationExpression(final Expression left, final Token operation, final Expression right) {
super(left, Token.newSymbol("=", operation.getStartLine(), operation.getStartColumn()), right);
check(left);
}

private static void check(Expression left) {
private static void check(final Expression left) {
if (left instanceof VariableExpression) {
//nothing
// all good
} else if (left instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) left;
if (tuple.getExpressions().isEmpty()) throw new GroovyBugError("one element required for left side");
if (tuple.getExpressions().isEmpty())
throw new GroovyBugError("one element required for left side");
} else {
throw new GroovyBugError("illegal left expression for declaration: "+left);
throw new GroovyBugError("illegal left expression for declaration: " + left);
}
}

@@ -27,47 +27,45 @@
* @see Types
*/
public class Token extends CSTNode {
public static final Token NULL = new Token();

public static final Token EOF = new Token(Types.EOF, "", -1, -1);
public static final Token NULL = new Token(Types.UNKNOWN, "", -1, -1);

//---------------------------------------------------------------------------
// TOKEN INITIALIZATION AND SUCH
//--------------------------------------------------------------------------

private int type = Types.UNKNOWN; // the actual type identified by the lexer
private int meaning = Types.UNKNOWN; // an interpretation applied to the token after the fact
/** the actual type identified by the lexer */
private final int type;
/** an interpretation applied to the token after the fact */
private int meaning;

private String text = ""; // the text of the token
private int startLine = -1; // the source line on which the token begins
private int startColumn = -1; // the source column on which the token begins
/** the text of the token */
private String text;
/** the source line on which the token begins */
private final int startLine;
/** the source column on which the token begins */
private final int startColumn;

/**
* Initializes the Token with the specified information.
*/
public Token(int type, String text, int startLine, int startColumn) {
public Token(final int type, final String text, final int startLine, final int startColumn) {
this.text = text;
this.type = type;
this.meaning = type;
this.text = text;
this.startLine = startLine;
this.startColumn = startColumn;
}

/**
* Initializes the NULL Token.
*/
private Token() {
}

/**
* Returns a copy of this Token.
*/
public Token dup() {
Token token = new Token(this.type, this.text, this.startLine, this.startColumn);
token.setMeaning(this.meaning);

return token;
}

//---------------------------------------------------------------------------
//--------------------------------------------------------------------------
// NODE IDENTIFICATION AND MEANING

/**
@@ -85,7 +83,7 @@ public int getMeaning() {
* convenience.
*/
@Override
public CSTNode setMeaning(int meaning) {
public CSTNode setMeaning(final int meaning) {
this.meaning = meaning;
return this;
}
@@ -99,7 +97,7 @@ public int getType() {
return type;
}

//---------------------------------------------------------------------------
//--------------------------------------------------------------------------
// MEMBER ACCESS

/**
@@ -176,7 +174,7 @@ public int getStartColumn() {
return startColumn;
}

//---------------------------------------------------------------------------
//--------------------------------------------------------------------------
// OPERATIONS

/**
@@ -218,73 +216,69 @@ public Reduction asReduction(CSTNode second, CSTNode third, CSTNode fourth) {
return created;
}

//---------------------------------------------------------------------------
//--------------------------------------------------------------------------
// TOKEN FACTORIES

/**
* Creates a token that represents a keyword. Returns null if the
* specified text isn't a keyword.
*/
public static Token newKeyword(String text, int startLine, int startColumn) {

public static Token newKeyword(final String text, final int startLine, final int startColumn) {
int type = Types.lookupKeyword(text);
if (type != Types.UNKNOWN) {
return new Token(type, text, startLine, startColumn);
}

return null;
}

/**
* Creates a token that represents a double-quoted string.
*/
public static Token newString(String text, int startLine, int startColumn) {
public static Token newString(final String text, final int startLine, final int startColumn) {
return new Token(Types.STRING, text, startLine, startColumn);
}

/**
* Creates a token that represents an identifier.
*/
public static Token newIdentifier(String text, int startLine, int startColumn) {
public static Token newIdentifier(final String text, final int startLine, final int startColumn) {
return new Token(Types.IDENTIFIER, text, startLine, startColumn);
}

/**
* Creates a token that represents an integer.
*/
public static Token newInteger(String text, int startLine, int startColumn) {
public static Token newInteger(final String text, final int startLine, final int startColumn) {
return new Token(Types.INTEGER_NUMBER, text, startLine, startColumn);
}

/**
* Creates a token that represents a decimal number.
*/
public static Token newDecimal(String text, int startLine, int startColumn) {
public static Token newDecimal(final String text, final int startLine, final int startColumn) {
return new Token(Types.DECIMAL_NUMBER, text, startLine, startColumn);
}

/**
* Creates a token that represents a symbol, using a library for the text.
*/
public static Token newSymbol(int type, int startLine, int startColumn) {
public static Token newSymbol(final int type, final int startLine, final int startColumn) {
return new Token(type, Types.getText(type), startLine, startColumn);
}

/**
* Creates a token that represents a symbol, using a library for the type.
*/
public static Token newSymbol(String type, int startLine, int startColumn) {
return new Token(Types.lookupSymbol(type), type, startLine, startColumn);
public static Token newSymbol(final String text, final int startLine, final int startColumn) {
return new Token(Types.lookupSymbol(text), text, startLine, startColumn);
}

/**
* Creates a token with the specified meaning.
*/
public static Token newPlaceholder(int type) {
public static Token newPlaceholder(final int meaning) {
Token token = new Token(Types.UNKNOWN, "", -1, -1);
token.setMeaning(type);

token.setMeaning(meaning);
return token;
}

}

0 comments on commit 5c92180

Please sign in to comment.