use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method dotExpression.
protected Expression dotExpression(AST node) {
// let's decide if this is a property invocation or a method call
AST leftNode = node.getFirstChild();
if (leftNode != null) {
AST identifierNode = leftNode.getNextSibling();
if (identifierNode != null) {
Expression leftExpression = expression(leftNode);
if (isType(SELECT_SLOT, identifierNode)) {
Expression field = expression(identifierNode.getFirstChild(), true);
AttributeExpression attributeExpression = new AttributeExpression(leftExpression, field, node.getType() != DOT);
if (node.getType() == SPREAD_DOT) {
attributeExpression.setSpreadSafe(true);
}
configureAST(attributeExpression, node);
return attributeExpression;
}
if (isType(SLIST, identifierNode)) {
Statement code = statementList(identifierNode);
ClosureExpression closureExpression = new ClosureExpression(Parameter.EMPTY_ARRAY, code);
configureAST(closureExpression, identifierNode);
final PropertyExpression propertyExpression = new PropertyExpression(leftExpression, closureExpression);
if (node.getType() == SPREAD_DOT) {
propertyExpression.setSpreadSafe(true);
}
configureAST(propertyExpression, node);
return propertyExpression;
}
Expression property = expression(identifierNode, true);
// we correct that here into a ConstantExpression
if (property instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) property;
property = new ConstantExpression(ve.getName());
}
PropertyExpression propertyExpression = new PropertyExpression(leftExpression, property, node.getType() != DOT);
if (node.getType() == SPREAD_DOT) {
propertyExpression.setSpreadSafe(true);
}
configureAST(propertyExpression, node);
return propertyExpression;
}
}
return methodCallExpression(node);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method synchronizedStatement.
protected Statement synchronizedStatement(AST syncNode) {
AST node = syncNode.getFirstChild();
Expression expression = expression(node);
Statement code = statement(node.getNextSibling());
SynchronizedStatement synchronizedStatement = new SynchronizedStatement(expression, code);
configureAST(synchronizedStatement, syncNode);
return synchronizedStatement;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method blockExpression.
protected Expression blockExpression(AST node) {
AST codeNode = node.getFirstChild();
if (codeNode == null)
return ConstantExpression.NULL;
if (codeNode.getType() == EXPR && codeNode.getNextSibling() == null) {
// Simplify common case of {expr} to expr.
return expression(codeNode);
}
Parameter[] parameters = Parameter.EMPTY_ARRAY;
Statement code = statementListNoChild(codeNode, node);
ClosureExpression closureExpression = new ClosureExpression(parameters, code);
configureAST(closureExpression, node);
// Call it immediately.
String callName = "call";
Expression noArguments = new ArgumentListExpression();
MethodCallExpression call = new MethodCallExpression(closureExpression, callName, noArguments);
configureAST(call, node);
return call;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method forStatement.
protected Statement forStatement(AST forNode) {
AST inNode = forNode.getFirstChild();
Expression collectionExpression;
Parameter forParameter;
if (isType(CLOSURE_LIST, inNode)) {
forStatementBeingDef = true;
ClosureListExpression clist = closureListExpression(inNode);
forStatementBeingDef = false;
int size = clist.getExpressions().size();
if (size != 3) {
throw new ASTRuntimeException(inNode, "3 expressions are required for the classic for loop, you gave " + size);
}
collectionExpression = clist;
forParameter = ForStatement.FOR_LOOP_DUMMY;
} else {
AST variableNode = inNode.getFirstChild();
AST collectionNode = variableNode.getNextSibling();
ClassNode type = ClassHelper.OBJECT_TYPE;
if (isType(VARIABLE_DEF, variableNode)) {
AST node = variableNode.getFirstChild();
// skip the final modifier if it's present
if (isType(MODIFIERS, node)) {
int modifiersMask = modifiers(node, new ArrayList<AnnotationNode>(), 0);
// only final modifier allowed
if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
throw new ASTRuntimeException(node, "Only the 'final' modifier is allowed in front of the for loop variable.");
}
node = node.getNextSibling();
}
type = makeTypeWithArguments(node);
variableNode = node.getNextSibling();
}
String variable = identifier(variableNode);
collectionExpression = expression(collectionNode);
forParameter = new Parameter(type, variable);
configureAST(forParameter, variableNode);
}
final AST node = inNode.getNextSibling();
Statement block;
if (isType(SEMI, node)) {
block = EmptyStatement.INSTANCE;
} else {
block = statement(node);
}
ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
configureAST(forStatement, forNode);
return forStatement;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AntlrParserPlugin method switchStatement.
protected Statement switchStatement(AST switchNode) {
AST node = switchNode.getFirstChild();
Expression expression = expression(node);
Statement defaultStatement = EmptyStatement.INSTANCE;
List list = new ArrayList();
for (node = node.getNextSibling(); isType(CASE_GROUP, node); node = node.getNextSibling()) {
Statement tmpDefaultStatement;
AST child = node.getFirstChild();
if (isType(LITERAL_case, child)) {
List cases = new LinkedList();
// default statement can be grouped with previous case
tmpDefaultStatement = caseStatements(child, cases);
list.addAll(cases);
} else {
tmpDefaultStatement = statement(child.getNextSibling());
}
if (tmpDefaultStatement != EmptyStatement.INSTANCE) {
if (defaultStatement == EmptyStatement.INSTANCE) {
defaultStatement = tmpDefaultStatement;
} else {
throw new ASTRuntimeException(switchNode, "The default case is already defined.");
}
}
}
if (node != null) {
unknownAST(node);
}
SwitchStatement switchStatement = new SwitchStatement(expression, list, defaultStatement);
configureAST(switchStatement, switchNode);
return switchStatement;
}
Aggregations