Search in sources :

Example 16 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method evaluatesWithException.

protected void evaluatesWithException(final String expression, final Procedure1<? super ConstantExpressionEvaluationException> exceptionAssertions) {
    try {
        final XtendFunction function = this.function((("def void testFoo() { " + expression) + " }"));
        XExpression _expression = function.getExpression();
        final XExpression expr = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
        try {
            this.interpreter.evaluate(expr, null);
            Assert.fail("exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
                final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException) _t;
                exceptionAssertions.apply(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 17 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method evaluatesTo.

protected void evaluatesTo(final Pair<String, String> typeAndExpression, final Procedure1<? super Object> assertions) {
    try {
        final String type = typeAndExpression.getKey();
        final String expression = typeAndExpression.getValue();
        String _elvis = null;
        if (type != null) {
            _elvis = type;
        } else {
            _elvis = "void";
        }
        String _plus = ("def " + _elvis);
        String _plus_1 = (_plus + " testFoo() { ");
        String _plus_2 = (_plus_1 + expression);
        String _plus_3 = (_plus_2 + " }");
        final XtendFunction function = this.function(_plus_3);
        XExpression _expression = function.getExpression();
        final XExpression expr = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
        JvmTypeReference _xifexpression = null;
        if ((type != null)) {
            _xifexpression = function.getReturnType();
        }
        final Object value = this.interpreter.evaluate(expr, _xifexpression);
        assertions.apply(value);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 18 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class AssignmentFeatureCallArgumentsTest method test_02.

@Test
public void test_02() {
    final AssignmentFeatureCallArguments arguments = this.toArguments("String s", "\"\"");
    Assert.assertTrue(arguments.hasUnprocessedArguments());
    Assert.assertEquals(1, arguments.getArgumentCount());
    final IFeatureCallArgumentSlot singleSlot = arguments.getNextUnprocessedArgumentSlot();
    Assert.assertEquals("String", singleSlot.getDeclaredType().getSimpleName());
    Assert.assertFalse(singleSlot.isVarArg());
    Assert.assertFalse(singleSlot.isSuperfluous());
    XExpression _argumentExpression = singleSlot.getArgumentExpression();
    Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
    singleSlot.markProcessed();
    Assert.assertFalse(arguments.hasUnprocessedArguments());
}
Also used : XStringLiteral(org.eclipse.xtext.xbase.XStringLiteral) AssignmentFeatureCallArguments(org.eclipse.xtext.xbase.typesystem.arguments.AssignmentFeatureCallArguments) XExpression(org.eclipse.xtext.xbase.XExpression) IFeatureCallArgumentSlot(org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArgumentSlot) Test(org.junit.Test)

Example 19 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class AssignmentFeatureCallArgumentsTest method test_01.

@Test
public void test_01() {
    final AssignmentFeatureCallArguments arguments = this.toArguments("", "null");
    Assert.assertTrue(arguments.hasUnprocessedArguments());
    final IFeatureCallArgumentSlot singleSlot = arguments.getNextUnprocessedArgumentSlot();
    Assert.assertNull(singleSlot.getDeclaredType());
    Assert.assertFalse(singleSlot.isVarArg());
    Assert.assertFalse(singleSlot.isSuperfluous());
    XExpression _argumentExpression = singleSlot.getArgumentExpression();
    Assert.assertTrue((_argumentExpression instanceof XNullLiteral));
    singleSlot.markProcessed();
    Assert.assertFalse(arguments.hasUnprocessedArguments());
}
Also used : AssignmentFeatureCallArguments(org.eclipse.xtext.xbase.typesystem.arguments.AssignmentFeatureCallArguments) XNullLiteral(org.eclipse.xtext.xbase.XNullLiteral) XExpression(org.eclipse.xtext.xbase.XExpression) IFeatureCallArgumentSlot(org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArgumentSlot) Test(org.junit.Test)

Example 20 with XExpression

use of org.eclipse.xtext.xbase.XExpression in project xtext-xtend by eclipse.

the class XtendCompiler method _toJavaStatement.

@Override
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
    for (XExpression arg : expr.getArguments()) {
        prepareExpression(arg, b);
    }
    if (!isReferenced) {
        b.newLine();
        constructorCallToJavaExpression(expr, b);
        if (expr.eContainer() instanceof AnonymousClass) {
            JvmConstructor constructor = expr.getConstructor();
            JvmDeclaredType declaringType = constructor.getDeclaringType();
            compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
        }
        b.append(";");
    } else if (isVariableDeclarationRequired(expr, b, true)) {
        Later later = new Later() {

            @Override
            public void exec(ITreeAppendable appendable) {
                constructorCallToJavaExpression(expr, appendable);
                if (expr.eContainer() instanceof AnonymousClass) {
                    JvmConstructor constructor = expr.getConstructor();
                    JvmDeclaredType declaringType = constructor.getDeclaringType();
                    compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, appendable);
                }
            }
        };
        declareFreshLocalVariable(expr, b, later);
    }
}
Also used : Later(org.eclipse.xtext.xbase.compiler.Later) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XExpression(org.eclipse.xtext.xbase.XExpression) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable)

Aggregations

XExpression (org.eclipse.xtext.xbase.XExpression)215 Test (org.junit.Test)133 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)96 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)84 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)82 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)82 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)80 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)63 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)36 EObject (org.eclipse.emf.ecore.EObject)28 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)28 XtendField (org.eclipse.xtend.core.xtend.XtendField)23 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)23 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)21 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)18 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)17 XStringLiteral (org.eclipse.xtext.xbase.XStringLiteral)17 XClosure (org.eclipse.xtext.xbase.XClosure)16 IFeatureCallArgumentSlot (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArgumentSlot)16