org.eclipse.xtext.xbase.XForLoopExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XForLoopExpression. 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 File: SARLFormatter.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected void _format(XForLoopExpression expr, IFormattableDocument document) {
	final ISemanticRegionsFinder regionFor = this.textRegionExtensions.regionFor(expr);
	document.append(regionFor.keyword(this.keywords.getForKeyword()), ONE_SPACE);

	final JvmFormalParameter declaredParam = expr.getDeclaredParam();
	document.prepend(declaredParam, NO_SPACE);
	document.append(declaredParam, ONE_SPACE);
	document.format(declaredParam);

	final XExpression forExpression = expr.getForExpression();
	document.prepend(forExpression, ONE_SPACE);
	document.append(forExpression, NO_SPACE);
	document.format(forExpression);

	final XExpression eachExpression = expr.getEachExpression();
	if (eachExpression != null) {
		formatBody(eachExpression, true, document);
	} else {
		document.prepend(regionFor.keyword(this.keywords.getSemicolonKeyword()), NO_SPACE);
	}
}
 
Example #2
Source File: XbaseSyntacticSequencer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Syntax: '('*
 */
@Override
protected void emit_XParenthesizedExpression_LeftParenthesisKeyword_0_a(EObject semanticObject,
		ISynNavigable transition, List<INode> nodes) {

	Keyword kw = grammarAccess.getXParenthesizedExpressionAccess().getLeftParenthesisKeyword_0();

	if (nodes == null) {
		if (semanticObject instanceof XIfExpression || semanticObject instanceof XTryCatchFinallyExpression) {
			EObject cnt = semanticObject.eContainer();
			if (cnt instanceof XExpression && !(cnt instanceof XBlockExpression)
					&& !(cnt instanceof XForLoopExpression))
				acceptUnassignedKeyword(kw, kw.getValue(), null);
		}
		if (semanticObject instanceof XConstructorCall) {
			XConstructorCall call = (XConstructorCall) semanticObject;
			if (!call.isExplicitConstructorCall() && call.getArguments().isEmpty()) {
				acceptUnassignedKeyword(kw, kw.getValue(), null);
			}
		}
	}
	acceptNodes(transition, nodes);
}
 
Example #3
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Check for usage of continue inside loops.
 *
 * @param expression the expression to analyze.
 * @since 0.7
 */
@Check
public void checkContinueKeywordUse(SarlContinueExpression expression) {
	final EObject container = Utils.getFirstContainerForPredicate(expression,
		it -> !(it instanceof XExpression) || it instanceof XAbstractWhileExpression
		|| it instanceof XBasicForLoopExpression || it instanceof XForLoopExpression);
	if (container instanceof XExpression) {
		if (!isIgnored(DISCOURAGED_LOOP_BREAKING_KEYWORD_USE)
				&& container instanceof XBasicForLoopExpression) {
			addIssue(
					MessageFormat.format(Messages.SARLValidator_17, this.grammarAccess.getContinueKeyword()),
					expression,
					null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
					DISCOURAGED_LOOP_BREAKING_KEYWORD_USE);
		}
	} else {
		error(MessageFormat.format(Messages.SARLValidator_18, this.grammarAccess.getContinueKeyword()),
				expression,
				null,
				ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
				INVALID_USE_OF_LOOP_BREAKING_KEYWORD);
	}
}
 
Example #4
Source File: XtendSyntacticSequencer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Syntax: '('*
 */
@Override
protected void emit_XParenthesizedExpression_LeftParenthesisKeyword_0_a(EObject semanticObject,
		ISynNavigable transition, List<INode> nodes) {

	Keyword kw = grammarAccess.getXParenthesizedExpressionAccess().getLeftParenthesisKeyword_0();

	if (nodes == null) {
		if (semanticObject instanceof XIfExpression || semanticObject instanceof XTryCatchFinallyExpression) {
			EObject cnt = semanticObject.eContainer();
			if (cnt instanceof XExpression && !(cnt instanceof XBlockExpression)
					&& !(cnt instanceof XForLoopExpression))
				acceptUnassignedKeyword(kw, kw.getValue(), null);
		}
		if (semanticObject instanceof XConstructorCall) {
			XConstructorCall call = (XConstructorCall) semanticObject;
			if (!call.isExplicitConstructorCall() && call.getArguments().isEmpty()) {
				acceptUnassignedKeyword(kw, kw.getValue(), null);
			}
		}
	}
	acceptNodes(transition, nodes);
}
 
Example #5
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Check for usage of break inside loops.
 *
 * @param expression the expression to analyze.
 */
@Check
public void checkBreakKeywordUse(SarlBreakExpression expression) {
	final EObject container = Utils.getFirstContainerForPredicate(expression,
		it -> !(it instanceof XExpression) || it instanceof XAbstractWhileExpression
		|| it instanceof XBasicForLoopExpression || it instanceof XForLoopExpression);
	if (container instanceof XExpression) {
		if (!isIgnored(DISCOURAGED_LOOP_BREAKING_KEYWORD_USE)
				&& container instanceof XBasicForLoopExpression) {
			addIssue(
					MessageFormat.format(Messages.SARLValidator_17, this.grammarAccess.getBreakKeyword()),
					expression,
					null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
					DISCOURAGED_LOOP_BREAKING_KEYWORD_USE);
		}
	} else {
		error(MessageFormat.format(Messages.SARLValidator_18, this.grammarAccess.getBreakKeyword()),
				expression,
				null,
				ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
				INVALID_USE_OF_LOOP_BREAKING_KEYWORD);
	}
}
 
Example #6
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _doEvaluate(XForLoopExpression forLoop, IEvaluationContext context, CancelIndicator indicator) {
	Object iterableOrIterator = internalEvaluate(forLoop.getForExpression(), context, indicator);
	if (iterableOrIterator == null)
		return throwNullPointerException(forLoop.getForExpression(), "iterable evaluated to 'null'");
	Iterator<?> iter = null;
	if (iterableOrIterator instanceof Iterable<?>) {
		iter = ((Iterable<?>) iterableOrIterator).iterator();
	} else if (iterableOrIterator.getClass().isArray()) {
		iter = ((Iterable<?>) Conversions.doWrapArray(iterableOrIterator)).iterator();
	} else {
		return throwClassCastException(forLoop.getForExpression(), iterableOrIterator, java.lang.Iterable.class);
	}
	IEvaluationContext forkedContext = context.fork();
	QualifiedName paramName = QualifiedName.create(forLoop.getDeclaredParam().getName());
	forkedContext.newValue(paramName, null);
	while (iter.hasNext()) {
		Object next = iter.next();
		forkedContext.assignValue(paramName, next);
		internalEvaluate(forLoop.getEachExpression(), forkedContext, indicator);
	}
	return null;
}
 
Example #7
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param forLoop the for-loop.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XForLoopExpression forLoop, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("for "); //$NON-NLS-1$
	final String varName = it.declareUniqueNameVariable(forLoop.getDeclaredParam(), forLoop.getDeclaredParam().getSimpleName());
	it.append(varName);
	it.append(" in "); //$NON-NLS-1$
	generate(forLoop.getForExpression(), it, context);
	it.append(":"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	final XExpression last = generate(forLoop.getEachExpression(), it, context);
	it.decreaseIndentation();
	return last;
}
 
Example #8
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testForLoop_01() throws Exception {
	final XBlockExpression block = (XBlockExpression) expression("{val s = null as Iterable<String>; for(String s: s) s}");
	XForLoopExpression forLoop = (XForLoopExpression) block.getExpressions().get(1);
	XFeatureCall forFeatureCall = (XFeatureCall) forLoop.getForExpression();
	assertEquals(block.getExpressions().get(0), forFeatureCall.getFeature());
	XFeatureCall eachFeatureCall = (XFeatureCall) forLoop.getEachExpression();
	assertFalse(eachFeatureCall.getFeature().eIsProxy());
	assertSame(forLoop.getDeclaredParam(), eachFeatureCall.getFeature());
}
 
Example #9
Source File: XtendHoverSignatureProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSignatureForForLoopVariable() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("def bar(List<String> list){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("for(foo : list){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    final XtendClass clazz = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class));
    XtendMember _get = clazz.getMembers().get(0);
    final XtendFunction xtendFunction = ((XtendFunction) _get);
    XExpression _expression = xtendFunction.getExpression();
    XExpression _get_1 = ((XBlockExpression) _expression).getExpressions().get(0);
    final JvmFormalParameter param = ((XForLoopExpression) _get_1).getDeclaredParam();
    final String signature = this.signatureProvider.getSignature(param);
    Assert.assertEquals("String foo", signature);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #10
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtensionOnForLoopParam_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { for(extension i: 1..2) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XForLoopExpression forLoop = (XForLoopExpression) body.getExpressions().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) forLoop.getDeclaredParam();
	assertTrue(parameter.isExtension());
}
 
Example #11
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testForLoopExpression() throws Exception {
	XForLoopExpression forExp = (XForLoopExpression) expression("for(s : foo) bar");
	assertFeatureCall("foo",forExp.getForExpression());
	assertEquals("s",forExp.getDeclaredParam().getName());
	assertNull(forExp.getDeclaredParam().getParameterType());
	assertFeatureCall("bar",forExp.getEachExpression());
}
 
Example #12
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> exitPoints(final XExpression expression) {
  if (expression instanceof XDoWhileExpression) {
    return _exitPoints((XDoWhileExpression)expression);
  } else if (expression instanceof XWhileExpression) {
    return _exitPoints((XWhileExpression)expression);
  } else if (expression instanceof XAbstractFeatureCall) {
    return _exitPoints((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBasicForLoopExpression) {
    return _exitPoints((XBasicForLoopExpression)expression);
  } else if (expression instanceof XBlockExpression) {
    return _exitPoints((XBlockExpression)expression);
  } else if (expression instanceof XConstructorCall) {
    return _exitPoints((XConstructorCall)expression);
  } else if (expression instanceof XForLoopExpression) {
    return _exitPoints((XForLoopExpression)expression);
  } else if (expression instanceof XIfExpression) {
    return _exitPoints((XIfExpression)expression);
  } else if (expression instanceof XReturnExpression) {
    return _exitPoints((XReturnExpression)expression);
  } else if (expression instanceof XSwitchExpression) {
    return _exitPoints((XSwitchExpression)expression);
  } else if (expression instanceof XSynchronizedExpression) {
    return _exitPoints((XSynchronizedExpression)expression);
  } else if (expression instanceof XThrowExpression) {
    return _exitPoints((XThrowExpression)expression);
  } else if (expression instanceof XTryCatchFinallyExpression) {
    return _exitPoints((XTryCatchFinallyExpression)expression);
  } else if (expression instanceof XVariableDeclaration) {
    return _exitPoints((XVariableDeclaration)expression);
  } else if (expression != null) {
    return _exitPoints(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #13
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XForLoopExpression expression) {
  Collection<IEarlyExitComputer.ExitPoint> exitPoints = this.getExitPoints(expression.getForExpression());
  boolean _isNotEmpty = this.isNotEmpty(exitPoints);
  if (_isNotEmpty) {
    return exitPoints;
  }
  return Collections.<IEarlyExitComputer.ExitPoint>emptyList();
}
 
Example #14
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendForLoopParameter(XForLoopExpression expr, ITreeAppendable appendable) {
	appendable.append("final ");
	JvmTypeReference paramType = getForLoopParameterType(expr);
	serialize(paramType, expr, appendable);
	appendable.append(" ");
	final String name = makeJavaIdentifier(expr.getDeclaredParam().getName());
	String varName = appendable.declareVariable(expr.getDeclaredParam(), name);
	appendable.trace(expr.getDeclaredParam(), TypesPackage.Literals.JVM_FORMAL_PARAMETER__NAME, 0).append(varName);
}
 
Example #15
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	internalToJavaStatement(expr.getForExpression(), b, true);
	b.newLine();
	ITreeAppendable loopAppendable = b.trace(expr);
	loopAppendable.append("for (");
	ITreeAppendable parameterAppendable = loopAppendable.trace(expr.getDeclaredParam());
	appendForLoopParameter(expr, parameterAppendable);
	loopAppendable.append(" : ");
	internalToJavaExpression(expr.getForExpression(), loopAppendable);
	loopAppendable.append(") {").increaseIndentation();
	internalToJavaStatement(expr.getEachExpression(), loopAppendable, false);
	loopAppendable.decreaseIndentation().newLine().append("}");
}
 
Example #16
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Test if the given expression has side effects.
 *
 * @param expression the expression.
 * @param context the list of context expressions.
 * @return {@code true} if the expression has side effects.
 */
protected Boolean _hasSideEffects(XForLoopExpression expression, ISideEffectContext context) {
	context.open();
	try {
		if (hasSideEffects(expression.getForExpression(), context)) {
			return true;
		}
	} finally {
		context.close();
	}
	return hasSideEffects(expression.getEachExpression(), context.branch());
}
 
Example #17
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(final XForLoopExpression object, final ITypeComputationState state) {
	JvmFormalParameter declaredParam = object.getDeclaredParam();
	ITypeComputationState eachState = state.withoutExpectation();
	if (declaredParam != null) {
		LightweightTypeReference parameterType = computeForLoopParameterType(object, state);
		eachState = assignType(declaredParam, parameterType, eachState);
		eachState.withinScope(object);
	}
	eachState.computeTypes(object.getEachExpression());
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	state.acceptActualType(primitiveVoid);
}
 
Example #18
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_3_d() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (Integer x : 1..2) null");
	assertExpected("org.eclipse.xtext.xbase.lib.IntegerRange", loop.getForExpression());
}
 
Example #19
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_a() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (Object x : null as double[]) null");
	assertExpected("double[]", loop.getForExpression());
}
 
Example #20
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_3_h() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (Object x : null as Iterable<? super Integer>) null");
	assertExpected("java.lang.Iterable<? super java.lang.Integer>", loop.getForExpression());
}
 
Example #21
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_b() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (double x : null as int[]) null");
	assertExpected("int[]", loop.getForExpression());
}
 
Example #22
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_c() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (double x : null as Integer[]) null");
	assertExpected("java.lang.Integer[]", loop.getForExpression());
}
 
Example #23
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_d() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (double x : null as Iterable<Integer>) null");
	assertExpected("java.lang.Iterable<java.lang.Integer>", loop.getForExpression());
}
 
Example #24
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_e() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for (double x : null as Iterable<? extends Integer>) null");
	assertExpected("java.lang.Iterable<? extends java.lang.Integer>", loop.getForExpression());
}
 
Example #25
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testForLoopExpression_4_f() throws Exception {
	XForLoopExpression loop = (XForLoopExpression) expression("for(double d: <Integer>newArrayList) { d.toString }");
	assertExpected("java.util.ArrayList<java.lang.Integer>", loop.getForExpression());
}
 
Example #26
Source File: XbaseIdentifiableTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testVariableDeclaration_in_ForExpression_0() throws Exception {
	XForLoopExpression expression = (XForLoopExpression) expression("for(x : null as java.util.List<String>) x.length", true);
	LightweightTypeReference typeRef = typeResolver.resolveTypes(expression).getActualType(expression.getDeclaredParam());
	assertEquals("java.lang.String", typeRef.getIdentifier());
}
 
Example #27
Source File: XbaseIdentifiableTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testVariableDeclaration_in_ForExpression_1() throws Exception {
	XForLoopExpression expression = (XForLoopExpression) expression("for(x : null as Iterable<?>) x.hashCode", true);
	LightweightTypeReference typeRef = typeResolver.resolveTypes(expression).getActualType(expression.getDeclaredParam());
	assertEquals("java.lang.Object", typeRef.getIdentifier());
}
 
Example #28
Source File: XbaseIdentifiableTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testVariableDeclaration_in_ForExpression_2() throws Exception {
	XForLoopExpression expression = (XForLoopExpression) expression("for(x : null as java.util.ArrayList<? extends java.util.List<Integer>>) x.hashCode", true);
	LightweightTypeReference typeRef = typeResolver.resolveTypes(expression).getActualType(expression.getDeclaredParam());
	assertEquals("java.util.List<java.lang.Integer>", typeRef.getIdentifier());
}
 
Example #29
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void appendForLoopParameter(XForLoopExpression expr, ITreeAppendable appendable) {
	appendExtensionAnnotation(expr.getDeclaredParam(), expr, appendable, false);
	super.appendForLoopParameter(expr, appendable);
}
 
Example #30
Source File: XtendAdapterFactory.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Adapter caseXForLoopExpression(XForLoopExpression object)
{
	return createXForLoopExpressionAdapter();
}