Java Code Examples for org.eclipse.xtext.xbase.XStringLiteral

The following examples show how to use org.eclipse.xtext.xbase.XStringLiteral. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xtext-extras   Source File: LiteralsCompiler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XStringLiteral) {
		_toJavaStatement((XStringLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XNumberLiteral) {
		_toJavaStatement((XNumberLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XNullLiteral) {
		_toJavaStatement((XNullLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XBooleanLiteral) {
		_toJavaStatement((XBooleanLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XTypeLiteral) {
		_toJavaStatement((XTypeLiteral) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}
 
Example 2
public boolean isConstant(final XExpression expression) {
  if (expression instanceof XAbstractFeatureCall) {
    return _isConstant((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBooleanLiteral) {
    return _isConstant((XBooleanLiteral)expression);
  } else if (expression instanceof XCastedExpression) {
    return _isConstant((XCastedExpression)expression);
  } else if (expression instanceof XNumberLiteral) {
    return _isConstant((XNumberLiteral)expression);
  } else if (expression instanceof XStringLiteral) {
    return _isConstant((XStringLiteral)expression);
  } else if (expression instanceof XTypeLiteral) {
    return _isConstant((XTypeLiteral)expression);
  } else if (expression != null) {
    return _isConstant(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example 3
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example 4
Source Project: xtext-extras   Source File: SerializerTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_01() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XClosure closure = factory.createXClosure();
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	XBlockExpression blockExpression = factory.createXBlockExpression();
	blockExpression.getExpressions().add(stringLiteral);
	closure.setExpression(blockExpression);
	closure.setExplicitSyntax(true);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(closure);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	assertEquals("[|\"value\"] instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example 5
Source Project: xtext-extras   Source File: SerializerTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_02() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XIfExpression ifExpression = factory.createXIfExpression();
	ifExpression.setIf(factory.createXBooleanLiteral());
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	ifExpression.setThen(stringLiteral);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(ifExpression);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=464846
	assertEquals("( if(false) \"value\" ) instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example 6
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_0_1_2_3() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String) {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", null);
}
 
Example 7
Source Project: xtext-extras   Source File: JvmTypesBuilderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 8
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_1() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int, arg1 : String=\"abc\", arg2 : int, arg3 : int, arg4 : String)",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), null, XStringLiteral.class, "abc", null, null, null);
}
 
Example 9
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int, arg1 : String, arg2 : int, arg3 : int, arg4 : String=\"xyz\") {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), null, null, null, null, XStringLiteral.class, "xyz");
}
 
Example 10
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int, arg1 : String, arg2 : int, arg3 : int, arg4 : String=\"xyz\")",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), null, null, null, null, XStringLiteral.class, "xyz");
}
 
Example 11
@Test
public void test_09() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s", "\"\", 1");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(2, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot valid = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(valid.isVarArg());
  Assert.assertFalse(valid.isSuperfluous());
  XExpression _argumentExpression = valid.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", valid.getDeclaredType().getSimpleName());
  valid.markProcessed();
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot superfluous = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(superfluous.isVarArg());
  Assert.assertTrue(superfluous.isSuperfluous());
  XExpression _argumentExpression_1 = superfluous.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XNumberLiteral));
  Assert.assertFalse(arguments.isProcessed(1));
  superfluous.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 12
@Test
public void test_04() {
  final IFeatureCallArguments arguments = this.toArgumentsWithoutReceiver("String s, int i", "\"\", 1");
  Assert.assertEquals(2, arguments.getArgumentCount());
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot firstSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(firstSlot.isVarArg());
  Assert.assertFalse(firstSlot.isSuperfluous());
  XExpression _argumentExpression = firstSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  Assert.assertEquals("String", firstSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(0));
  firstSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  final IFeatureCallArgumentSlot secondSlot = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertTrue(secondSlot.isVarArg());
  Assert.assertFalse(secondSlot.isSuperfluous());
  XExpression _argumentExpression_1 = secondSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XNumberLiteral));
  Assert.assertEquals("int", secondSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(1));
  secondSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 13
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_1_2_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String=\"klm\")",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", XStringLiteral.class, "klm");
}
 
Example 14
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_0_1_2_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String=\"klm\") {",
			"super(null) // must be never null during runtime",
			"System.out.println(arg0)",
			"}",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(), XNumberLiteral.class, "4", XStringLiteral.class, "ghj", XNumberLiteral.class, "18", XNumberLiteral.class, "98", XStringLiteral.class, "klm");
}
 
Example 15
Source Project: sarl   Source File: SARLValidator.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
private static String parseIssueOnCallAnnotation(List<JvmAnnotationValue> values) {
	final StringBuilder message = new StringBuilder();
	for (final JvmAnnotationValue value : values) {
		if (value instanceof JvmStringAnnotationValue) {
			message.append(((JvmStringAnnotationValue) value).getValues());
		} else if (value instanceof JvmCustomAnnotationValue) {
			for (final Object obj : ((JvmCustomAnnotationValue) value).getValues()) {
				if (obj instanceof XStringLiteral) {
					message.append(((XStringLiteral) obj).getValue());
				}
			}
		}
	}
	return message.toString();
}
 
Example 16
Source Project: xtext-extras   Source File: XExpressionHelper.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return whether the expression itself (not its children) possibly causes a side-effect
 */
public boolean hasSideEffects(XExpression expr) {
	if (expr instanceof XClosure
		|| expr instanceof XStringLiteral
		|| expr instanceof XTypeLiteral
		|| expr instanceof XBooleanLiteral
		|| expr instanceof XNumberLiteral
		|| expr instanceof XNullLiteral
		|| expr instanceof XAnnotation
		)
		return false;
	if(expr instanceof XCollectionLiteral) {
		for(XExpression element: ((XCollectionLiteral)expr).getElements()) {
			if(hasSideEffects(element))
				return true;
		}
		return false;
	}
	if (expr instanceof XAbstractFeatureCall) {
		XAbstractFeatureCall featureCall = (XAbstractFeatureCall) expr;
		return hasSideEffects(featureCall, true);
	}
	if (expr instanceof XConstructorCall) {
		XConstructorCall constrCall = (XConstructorCall) expr;
		return findPureAnnotation(constrCall.getConstructor()) == null;
	}
	return true;
}
 
Example 17
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_5p_0_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"	new(arg0 : int=4, arg1 : String, arg2 : int, arg3 : int=56, arg4 : String=\"def\") {",
			"		super(null) // must be never null during runtime",
			"		System.out.println(arg0)",
			"	}",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlBehavior behavior = (SarlBehavior) mas.getXtendTypes().get(0);
	assertEquals("B1", behavior.getName());
	assertNull(behavior.getExtends());
	assertEquals(1, behavior.getMembers().size());
	//
	SarlConstructor constructor = (SarlConstructor) behavior.getMembers().get(0);
	assertParameterNames(constructor.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(constructor.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(constructor.getParameters(),
			XNumberLiteral.class, "4",
			null,
			null,
			XNumberLiteral.class, "56",
			XStringLiteral.class, "def");
}
 
Example 18
Source Project: xtext-extras   Source File: LiteralsCompiler.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected void toJavaStatement(final XStringLiteral expr, ITreeAppendable b, boolean isReferenced, final boolean useUnicodeEscapes) {
	generateComment(new Later() {
		@Override
		public void exec(ITreeAppendable appendable) {
			// we have to escape closing comments in string literals
			String escapedClosingComments = expr.getValue().replace("*/", "* /");
			String javaString = Strings.convertToJavaString(escapedClosingComments, useUnicodeEscapes);
			appendable.append("\"").append(javaString).append("\"");
		}
	}, b, isReferenced);
}
 
Example 19
Source Project: xtext-extras   Source File: LiteralsCompiler.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	if (expr instanceof XBooleanLiteral
		|| expr instanceof XStringLiteral
		|| expr instanceof XNumberLiteral
		|| expr instanceof XTypeLiteral
		|| expr instanceof XClosure
		|| expr instanceof XNullLiteral)
		return false;
	return super.isVariableDeclarationRequired(expr,b, recursive);
}
 
Example 20
Source Project: xtext-extras   Source File: XbaseInterpreter.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param context unused in this context but required for dispatching
 * @param indicator unused in this context but required for dispatching
 */
protected Object _doEvaluate(XStringLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	LightweightTypeReference type = typeResolver.resolveTypes(literal).getActualType(literal);
	if (type != null && (type.isType(Character.TYPE) || type.isType(Character.class))) {
		return literal.getValue().charAt(0);
	}
	return literal.getValue();
}
 
Example 21
Source Project: sarl   Source File: AgentParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void variableDefinition() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"var name : String = \"Hello\"",
			"var number : Integer",
			"}"
			));
	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlAgent agent = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals("A1", agent.getName());
	assertNull(agent.getExtends());
	assertEquals(2, agent.getMembers().size());
	//
	SarlField attr1 = (SarlField) agent.getMembers().get(0);
	assertEquals("name", attr1.getName());
	assertTypeReferenceIdentifier(attr1.getType(), "java.lang.String");
	assertTrue(attr1.getInitialValue() instanceof XStringLiteral);
	assertEquals("Hello", ((XStringLiteral) attr1.getInitialValue()).getValue());
	assertFalse(attr1.isFinal());
	//
	SarlField attr2 = (SarlField) agent.getMembers().get(1);
	assertEquals("number", attr2.getName());
	assertTypeReferenceIdentifier(attr2.getType(), "java.lang.Integer");
	assertNull(attr2.getInitialValue());
	assertFalse(attr2.isFinal());
}
 
Example 22
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_1_2_3() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String=\"ghj\", arg2 : int=18, arg3 : int=98, arg4 : String)",
			"}"
			));

	assertEquals(1, mas.getXtendTypes().size());
	//
	assertTrue(Strings.isNullOrEmpty(mas.getPackage()));
	//
	SarlCapacity capacity = (SarlCapacity) mas.getXtendTypes().get(0);
	assertEquals("C1", capacity.getName());
	assertTypeReferenceIdentifiers(capacity.getExtends());
	assertEquals(1, capacity.getMembers().size());
	//
	SarlAction signature = (SarlAction) capacity.getMembers().get(0);
	assertParameterNames(signature.getParameters(), "arg0", "arg1", "arg2", "arg3", "arg4");
	assertParameterTypes(signature.getParameters(), "int", "java.lang.String", "int", "int", "java.lang.String");
	assertParameterDefaultValues(signature.getParameters(),
			XNumberLiteral.class, "4",
			XStringLiteral.class, "ghj",
			XNumberLiteral.class, "18",
			XNumberLiteral.class, "98",
			null);
}
 
Example 23
public EvaluationResult internalEvaluate(final XExpression it, final EvaluationContext context) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, context);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, context);
  } else if (it instanceof XAbstractFeatureCall) {
    return _internalEvaluate((XAbstractFeatureCall)it, context);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, context);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, context);
  } else if (it instanceof XNullLiteral) {
    return _internalEvaluate((XNullLiteral)it, context);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, context);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, context);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, context);
  } else if (it != null) {
    return _internalEvaluate(it, context);
  } else if (it == null) {
    return _internalEvaluate((Void)null, context);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example 24
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XAbstractFeatureCall) {
    return _internalEvaluate((XAbstractFeatureCall)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example 25
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testClosure_2() throws Exception {
	XClosure closure = (XClosure) expression("[bar|'foo']");
	assertEquals("foo", ((XStringLiteral) ((XBlockExpression)closure.getExpression()).getExpressions().get(0))
			.getValue());
	assertEquals("bar", closure.getFormalParameters().get(0).getName());
	assertNull(closure.getFormalParameters().get(0).getParameterType());
}
 
Example 26
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testClosure_3() throws Exception {
	XClosure closure = (XClosure) expression("[String bar|'foo']");
	assertEquals("foo", ((XStringLiteral) ((XBlockExpression)closure.getExpression()).getExpressions().get(0))
			.getValue());
	assertEquals("bar", closure.getFormalParameters().get(0).getName());
	assertEquals(1, closure.getFormalParameters().size());
	assertNotNull(closure.getFormalParameters().get(0).getParameterType());
}
 
Example 27
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testClosure_4() throws Exception {
	XClosure closure = (XClosure) expression("[foo, String bar|'foo']");
	assertEquals("foo", ((XStringLiteral) ((XBlockExpression)closure.getExpression()).getExpressions().get(0))
			.getValue());
	assertEquals("foo", closure.getFormalParameters().get(0).getName());
	assertEquals("bar", closure.getFormalParameters().get(1).getName());
	assertEquals(2, closure.getFormalParameters().size());
	assertNull(closure.getFormalParameters().get(0).getParameterType());
	assertNotNull(closure.getFormalParameters().get(1).getParameterType());
}
 
Example 28
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testFeatureCall_3() throws Exception {
	XMemberFeatureCall call = (XMemberFeatureCall) expression("'holla'.bar(4)");
	assertNotNull(call);
	assertEquals(2, call.getExplicitArguments().size());
	assertEquals("4", ((XNumberLiteral) call.getExplicitArguments().get(1)).getValue());
	assertTrue(call.getExplicitArguments().get(0) instanceof XStringLiteral);
}
 
Example 29
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSwitch_0() throws Exception {
	XSwitchExpression se = (XSwitchExpression) expression("switch true { case 1==0 : '1' }");
	assertNull(se.getDefault());
	assertEquals(1, se.getCases().size());
	assertNotNull(se.getSwitch());
	XCasePart casePart = se.getCases().get(0);
	assertTrue(casePart.getCase() instanceof XBinaryOperation);
	assertTrue(casePart.getThen() instanceof XStringLiteral);
}
 
Example 30
Source Project: xtext-extras   Source File: XbaseParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSwitch_1() throws Exception {
	XSwitchExpression se = (XSwitchExpression) expression("switch number{case 1:'1' case 2:'2' default:'3'}");
	assertTrue(se.getDefault() instanceof XStringLiteral);
	assertEquals(2, se.getCases().size());
	assertTrue(se.getSwitch() instanceof XFeatureCall);

	XCasePart case1 = se.getCases().get(0);
	assertEquals("1", ((XNumberLiteral) case1.getCase()).getValue());
	assertTrue(case1.getThen() instanceof XStringLiteral);

	XCasePart case2 = se.getCases().get(1);
	assertEquals("2", ((XNumberLiteral) case2.getCase()).getValue());
	assertTrue(case2.getThen() instanceof XStringLiteral);
}