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

The following examples show how to use org.eclipse.xtext.xbase.XNumberLiteral. 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: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_5p_0_3_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String, arg2 : int, arg3 : int=56, arg4 : String=\"def\")",
			"}"
			));

	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", null, null, XNumberLiteral.class, "56", XStringLiteral.class, "def");
}
 
Example 2
Source Project: xtext-extras   Source File: NumberLiterals.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected String getTypeQualifier(XNumberLiteral literal) {
	String valueAsLowerCase = literal.getValue().toLowerCase();
	switch (getBase(literal)) {
		case 16:
			int index = valueAsLowerCase.indexOf('#');
			if (index != -1)
				return valueAsLowerCase.substring(index + 1);
			else
				return "";
		case 10:
			if (valueAsLowerCase.endsWith("bi") || valueAsLowerCase.endsWith("bd"))
				return valueAsLowerCase.substring(valueAsLowerCase.length() - 2);
			char lastChar = valueAsLowerCase.charAt(literal.getValue().length() - 1);
			switch (lastChar) {
				case 'l':
				case 'd':
				case 'f':
					return Character.toString(lastChar);
				default:
					return "";
			}
		default:
			throw new IllegalArgumentException("Invalid number literal base " + getBase(literal));
	}
}
 
Example 3
Source Project: sarl   Source File: DataTypeParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void doubleToDouble_1() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"	var internalTime : Double = 0.0",
			"}"
			));
	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(1, agent.getMembers().size());
	//
	SarlField attr = (SarlField) agent.getMembers().get(0);
	assertEquals("internalTime", attr.getName());
	assertTypeReferenceIdentifier(attr.getType(), "java.lang.Double");
	assertXExpression(attr.getInitialValue(), XNumberLiteral.class, "0.0");
}
 
Example 4
Source Project: sarl   Source File: Bug92Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void attributeDeclarationSyntax_inferredDouble() throws Exception {
	SarlScript mas = file(getParseHelper(), multilineString(
			"agent A1 {",
			"  var myDouble = 0d",
			"}"
			));
	validate(getValidationHelper(), getInjector(), mas).assertNoErrors();
	assertEquals(1, mas.getXtendTypes().size());
	assertInstance(SarlAgent.class, mas.getXtendTypes().get(0));
	SarlAgent ag = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals(1, ag.getMembers().size());
	assertInstance(SarlField.class, ag.getMembers().get(0));
	SarlField attr = (SarlField) ag.getMembers().get(0);
	assertEquals("myDouble", attr.getName());
	assertFalse(attr.isFinal());
	assertNull(attr.getType());
	assertInstance(XNumberLiteral.class, attr.getInitialValue());
	XNumberLiteral literal = (XNumberLiteral) attr.getInitialValue();
	assertEquals("0d", literal.getValue());
}
 
Example 5
Source Project: xtext-extras   Source File: NumberLiterals.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected Class<? extends Number> getExplicitJavaType(XNumberLiteral literal) {
	String typeQualifier = getTypeQualifier(literal);
	if (equal("", typeQualifier))
		return null;
	else if (equal("f", typeQualifier))
		return Float.TYPE;
	else if (equal("l", typeQualifier))
		return Long.TYPE;
	else if (equal("d", typeQualifier))
		return Double.TYPE;
	else if (equal("bi", typeQualifier))
		return BigInteger.class;
	else if (equal("bd", typeQualifier))
		return BigDecimal.class;
	else
		throw new IllegalArgumentException("Invalid type qualifier " + typeQualifier);
}
 
Example 6
Source Project: xtext-extras   Source File: NumberLiterals.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public String getExponent(XNumberLiteral literal, String digits) {
	if (isHex(literal))
		return null;
	int e = digits.indexOf('e');
	if (e == -1) {
		e = digits.indexOf('E');
	}
	if (e != -1) {
		if (e != digits.length() - 1 && (digits.charAt(e + 1) == '+' || digits.charAt(e + 1) == '-')) {
			e++;
		}
		if (e < digits.length() - 1) {
			String exponent = digits.substring(e + 1);
			return exponent;
		}
	}
	return null;
}
 
Example 7
Source Project: sarl   Source File: DataTypeParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void doubleToDouble_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"	var internalTime : Double = 0d",
			"}"
			));
	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(1, agent.getMembers().size());
	//
	SarlField attr = (SarlField) agent.getMembers().get(0);
	assertEquals("internalTime", attr.getName());
	assertTypeReferenceIdentifier(attr.getType(), "java.lang.Double");
	assertXExpression(attr.getInitialValue(), XNumberLiteral.class, "0d");
}
 
Example 8
Source Project: xtext-extras   Source File: LiteralsCompiler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
	if (obj instanceof XStringLiteral) {
		_toJavaExpression((XStringLiteral) obj, appendable);
	} else if (obj instanceof XNumberLiteral) {
		_toJavaExpression((XNumberLiteral) obj, appendable);
	} else if (obj instanceof XNullLiteral) {
		_toJavaExpression((XNullLiteral) obj, appendable);
	} else if (obj instanceof XBooleanLiteral) {
		_toJavaExpression((XBooleanLiteral) obj, appendable);
	} else if (obj instanceof XTypeLiteral) {
		_toJavaExpression((XTypeLiteral) obj, appendable);
	} else {
		super.internalToConvertedExpression(obj, appendable);
	}
}
 
Example 9
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 10
Source Project: xtext-extras   Source File: XbaseInterpreter.java    License: Eclipse Public License 2.0 6 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
 */
@SuppressWarnings("unchecked")
protected Object _doEvaluate(XNumberLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference expectedType = resolvedTypes.getExpectedType(literal);
	Class<? extends Number> type = numberLiterals.getJavaType(literal);
	if (expectedType != null && expectedType.isSubtypeOf(Number.class)) {
		try {
			Class<?> expectedClassType = getJavaType(expectedType.toJavaCompliantTypeReference().getType());
			if (expectedClassType.isPrimitive()) {
				expectedClassType = ReflectionUtil.getObjectType(expectedClassType);
			}
			if (Number.class != expectedClassType && Number.class.isAssignableFrom(expectedClassType)) {
				type = (Class<? extends Number>) expectedClassType;
			}
		} catch (ClassNotFoundException e) {
		}
	}
	return numberLiterals.numberValue(literal, type);
}
 
Example 11
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_2_4() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String, arg2 : int=18, arg3 : int, arg4 : String=\"def\")",
			"}"
			));

	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", null, XNumberLiteral.class, "18", null, XStringLiteral.class, "def");
}
 
Example 12
@Test
public void testTypeOnlyRegisteredOnce_04() throws Exception {
	XNumberLiteral expression = ((XNumberLiteral) expression("1"));
	resolver.initializeFrom(expression);
	PublicResolvedTypes resolution = new PublicResolvedTypes(resolver);
	AnyTypeReference any = resolution.getReferenceOwner().newAnyTypeReference();
	new ExpressionBasedRootTypeComputationState(resolution, resolver.getBatchScopeProvider().newSession(expression.eResource()),
			expression, any).computeTypes();
	Map<XExpression, List<TypeData>> expressionTypes = resolution.basicGetExpressionTypes();
	TreeIterator<EObject> allContents = expression.eAllContents();
	while (allContents.hasNext()) {
		EObject o = allContents.next();
		List<TypeData> types = expressionTypes.get((o));
		assertEquals(types.toString(), 1, size(filter(types, it -> !it.isReturnType())));
	}
}
 
Example 13
@Test
public void test_10() {
  final IFeatureCallArguments arguments = this.toArgumentsWithReceiver("String s", "\"\", 1");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(3, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot valid = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertFalse(valid.isVarArg());
  Assert.assertFalse(valid.isSuperfluous());
  XExpression _argumentExpression = valid.getArgumentExpression();
  Assert.assertTrue((_argumentExpression instanceof XStringLiteral));
  valid.markProcessed();
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertTrue(arguments.isProcessed(1));
  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(2));
  superfluous.markProcessed();
  Assert.assertTrue(arguments.isProcessed(2));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 14
@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 15
@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.assertTrue(valid.isVarArg());
  Assert.assertFalse(valid.isSuperfluous());
  final List<XExpression> expressions = valid.getArgumentExpressions();
  Assert.assertEquals(2, expressions.size());
  XExpression _head = IterableExtensions.<XExpression>head(expressions);
  Assert.assertTrue((_head instanceof XStringLiteral));
  XExpression _last = IterableExtensions.<XExpression>last(expressions);
  Assert.assertTrue((_last instanceof XNumberLiteral));
  valid.markProcessed();
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 16
@Test
public void test_10() {
  final IFeatureCallArguments arguments = this.toArgumentsWithReceiver("String s", "\"\", 1");
  Assert.assertTrue(arguments.hasUnprocessedArguments());
  Assert.assertEquals(3, arguments.getArgumentCount());
  final IFeatureCallArgumentSlot valid = arguments.getNextUnprocessedArgumentSlot();
  Assert.assertTrue(valid.isVarArg());
  Assert.assertFalse(valid.isSuperfluous());
  final List<XExpression> expressions = valid.getArgumentExpressions();
  Assert.assertEquals(2, expressions.size());
  XExpression _head = IterableExtensions.<XExpression>head(expressions);
  Assert.assertTrue((_head instanceof XStringLiteral));
  XExpression _last = IterableExtensions.<XExpression>last(expressions);
  Assert.assertTrue((_last instanceof XNumberLiteral));
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertFalse(arguments.isProcessed(1));
  Assert.assertFalse(arguments.isProcessed(2));
  valid.markProcessed();
  Assert.assertTrue(arguments.isProcessed(0));
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertTrue(arguments.isProcessed(2));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 17
@Test
public void test_01() {
  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 XNumberLiteral));
  Assert.assertEquals("int", 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.assertFalse(secondSlot.isVarArg());
  Assert.assertFalse(secondSlot.isSuperfluous());
  XExpression _argumentExpression_1 = secondSlot.getArgumentExpression();
  Assert.assertTrue((_argumentExpression_1 instanceof XClosure));
  Assert.assertEquals("String", secondSlot.getDeclaredType().getSimpleName());
  Assert.assertFalse(arguments.isProcessed(1));
  secondSlot.markProcessed();
  Assert.assertTrue(arguments.isProcessed(1));
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 18
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 19
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() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int=4, arg1 : String, 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(), XNumberLiteral.class, "4", null, null, null, null);
}
 
Example 20
Source Project: sarl   Source File: ShouldExtensions.java    License: Apache License 2.0 6 votes vote down vote up
/** Ensure that the given number literal is equal to the given value.
 *
 * @param actual the number literal to test.
 * @param expected the expected value.
 * @return the validation status
 */
public static boolean shouldBe(XNumberLiteral actual, Object expected) {
	if (actual == null) {
		return false;
	}
	final Number number;
	if (expected instanceof Number) {
		number = (Number) expected;
	} else {
		try {
			number = NumberFormat.getInstance().parse(expected.toString());
		} catch (Throwable exception) {
			return false;
		}
	}
	final Number anumber = cleanNumber(actual.getValue());
	return number.doubleValue() == anumber.doubleValue();
}
 
Example 21
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_3() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg0 : int, arg1 : String, arg2 : int, arg3 : int = 34, 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, null, null, XNumberLiteral.class, "34", null);
}
 
Example 22
Source Project: sarl   Source File: Bug92Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void attributeDeclarationSyntax_Double() throws Exception {
	SarlScript mas = file(getParseHelper(), multilineString(
			"agent A1 {",
			"  var myDouble : Double = 0d",
			"}"
			));
	validate(getValidationHelper(), getInjector(), mas).assertNoErrors();
	assertEquals(1, mas.getXtendTypes().size());
	assertInstance(SarlAgent.class, mas.getXtendTypes().get(0));
	SarlAgent ag = (SarlAgent) mas.getXtendTypes().get(0);
	assertEquals(1, ag.getMembers().size());
	assertInstance(SarlField.class, ag.getMembers().get(0));
	SarlField attr = (SarlField) ag.getMembers().get(0);
	assertEquals("myDouble", attr.getName());
	assertFalse(attr.isFinal());
	assertInstance(JvmParameterizedTypeReference.class, attr.getType());
	JvmParameterizedTypeReference type = (JvmParameterizedTypeReference) attr.getType();
	assertEquals(createType(Double.class).getQualifiedName(), type.getType().getQualifiedName());
	assertInstance(XNumberLiteral.class, attr.getInitialValue());
	XNumberLiteral literal = (XNumberLiteral) attr.getInitialValue();
	assertEquals("0d", literal.getValue());
}
 
Example 23
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_1p() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"capacity C1 {",
			"def myaction(arg : int=4)",
			"}"
			));

	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(), "arg");
	assertParameterTypes(signature.getParameters(), "int");
	assertParameterDefaultValues(signature.getParameters(), XNumberLiteral.class, "4");
}
 
Example 24
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void constructor_3p_vararg_1() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"new(arg0 : int, arg1 : int=45, arg2 : int*) {",
			"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");
	assertParameterTypes(constructor.getParameters(), "int", "int", "int");
	assertParameterDefaultValues(constructor.getParameters(),
			null,
			XNumberLiteral.class, "45",
			null);
	assertParameterVarArg(constructor.getParameters());
}
 
Example 25
Source Project: xtext-extras   Source File: NumberLiterals.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isHex(XNumberLiteral literal) {
	String text = literal.getValue();
	if (text.length() >= 2) {
		char second = text.charAt(1);
		return text.charAt(0) == '0' && (second == 'X' || second =='x');
	}
	return false;
}
 
Example 26
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_3p_vararg_0_1() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"def myaction(arg0 : int=45, arg1 : int=56, arg2 : int*) {",
			"System.out.println(arg0)",
			"}",
			"}"
			));
	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(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2");
	assertParameterTypes(action.getParameters(), "int", "int", "int");
	assertParameterDefaultValues(action.getParameters(),
			XNumberLiteral.class, "45",
			XNumberLiteral.class, "56",
			null);
	assertParameterVarArg(action.getParameters());
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example 27
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 28
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 29
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void action_3p_vararg_0() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"agent A1 {",
			"	def myaction(arg0 : int=45, arg1 : int, arg2 : int*) {",
			"		System.out.println(arg0)",
			"	}",
			"}"
			));
	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(1, agent.getMembers().size());
	//
	SarlAction action = (SarlAction) agent.getMembers().get(0);
	assertEquals("myaction", action.getName());
	assertTypeReferenceIdentifiers(action.getFiredEvents());
	assertParameterNames(action.getParameters(), "arg0", "arg1", "arg2");
	assertParameterTypes(action.getParameters(), "int", "int", "int");
	assertParameterDefaultValues(action.getParameters(),
			XNumberLiteral.class, "45",
			null,
			null);
	assertParameterVarArg(action.getParameters());
	assertTypeReferenceIdentifier(action.getReturnType(), "void");
}
 
Example 30
Source Project: sarl   Source File: ArgDefaultValueParsingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Tag("sarlParsing")
public void multipleActionDefinitionsInBehavior() throws Exception {
	SarlScript mas = file(getParseHelper(), getValidationHelper(), multilineString(
			"behavior B1 {",
			"	def myaction(arg0 : int, arg1 : int=42, arg2 : int*) {",
			"		System.out.println(\"valid\")",
			"	}",
			"	def myaction(arg0 : int, arg1 : int*) {",
			"		System.out.println(\"invalid\")",
			"	}",
			"}"
			));

	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(2, behavior.getMembers().size());
	//
	SarlAction action1 = (SarlAction) behavior.getMembers().get(0);
	assertEquals("myaction", action1.getName());
	assertTypeReferenceIdentifier(action1.getReturnType(), "void");
	assertParameterNames(action1.getParameters(), "arg0", "arg1", "arg2");
	assertParameterTypes(action1.getParameters(), "int", "int", "int");
	assertParameterDefaultValues(action1.getParameters(), null, XNumberLiteral.class, "42", null);
	assertParameterVarArg(action1.getParameters());
	//
	SarlAction action2 = (SarlAction) behavior.getMembers().get(1);
	assertEquals("myaction", action2.getName());
	assertTypeReferenceIdentifier(action2.getReturnType(), "void");
	assertParameterNames(action2.getParameters(), "arg0", "arg1");
	assertParameterTypes(action2.getParameters(), "int", "int");
	assertParameterDefaultValues(action2.getParameters(), null, null);
	assertParameterVarArg(action2.getParameters());
}