Java Code Examples for org.eclipse.xtext.xbase.compiler.IAppendable#append()

The following examples show how to use org.eclipse.xtext.xbase.compiler.IAppendable#append() . 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: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param anonClass the anonymous class.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the class definition.
 */
protected XExpression _generate(AnonymousClass anonClass, IAppendable it, IExtraLanguageGeneratorContext context) {
	if (it.hasName(anonClass)) {
		appendReturnIfExpectedReturnedExpression(it, context);
		it.append(it.getName(anonClass)).append("("); //$NON-NLS-1$
		boolean firstArg = true;
		for (final XExpression arg : anonClass.getConstructorCall().getArguments()) {
			if (firstArg) {
				firstArg = false;
			} else {
				it.append(", "); //$NON-NLS-1$
			}
			generate(arg, it, context);
		}
		it.append(")"); //$NON-NLS-1$
	}
	return anonClass;
}
 
Example 2
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param tryStatement the try-catch-finally statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XTryCatchFinallyExpression tryStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("try:"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	generate(tryStatement.getExpression(), context.getExpectedExpressionType(), it, context);
	it.decreaseIndentation().newLine();
	for (final XCatchClause clause : tryStatement.getCatchClauses()) {
		it.append("except "); //$NON-NLS-1$
		it.append(clause.getDeclaredParam().getParameterType().getType());
		it.append(", "); //$NON-NLS-1$
		it.append(it.declareUniqueNameVariable(clause.getDeclaredParam(), clause.getDeclaredParam().getSimpleName()));
		it.append(":"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(clause.getExpression(), context.getExpectedExpressionType(), it, context);
		it.decreaseIndentation().newLine();
	}
	if (tryStatement.getFinallyExpression() != null) {
		it.append("finally:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(tryStatement.getFinallyExpression(), it, context);
		it.decreaseIndentation();
	}
	return tryStatement;
}
 
Example 3
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the list literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
protected XExpression _generate(XListLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("["); //$NON-NLS-1$
	boolean first = true;
	for (final XExpression value : literal.getElements()) {
		if (first) {
			first = false;
		} else {
			it.append(", "); //$NON-NLS-1$
		}
		generate(value, it, context);
	}
	it.append("]"); //$NON-NLS-1$
	return literal;
}
 
Example 4
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param varDeclaration the variable declaration.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XVariableDeclaration varDeclaration, IAppendable it, IExtraLanguageGeneratorContext context) {
	final String name = it.declareUniqueNameVariable(varDeclaration, varDeclaration.getName());
	it.append(name);
	it.append(" = "); //$NON-NLS-1$
	if (varDeclaration.getRight() != null) {
		generate(varDeclaration.getRight(), it, context);
	} else if (varDeclaration.getType() != null) {
		it.append(toDefaultValue(varDeclaration.getType()));
	} else {
		it.append("None"); //$NON-NLS-1$
	}
	if (context.getExpectedExpressionType() != null) {
		it.newLine();
		it.append("return ").append(name); //$NON-NLS-1$
	}
	return varDeclaration;
}
 
Example 5
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param operation the unary operation.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the operation.
 */
protected XExpression _generate(XUnaryOperation operation, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	final String operator = getOperatorSymbol(operation);
	if (operator != null) {
		it.append("("); //$NON-NLS-1$
		switch (operator) {
		case "+": //$NON-NLS-1$
			generate(operation.getOperand(), it, context);
			break;
		case "-": //$NON-NLS-1$
			it.append("-"); //$NON-NLS-1$
			generate(operation.getOperand(), it, context);
			break;
		default:
			throw new IllegalArgumentException(MessageFormat.format(Messages.PyExpressionGenerator_0, operator));
		}
		it.append(")"); //$NON-NLS-1$
	}
	return operation;
}
 
Example 6
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param operation the postfix operator.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the operation.
 */
protected XExpression _generate(XPostfixOperation operation, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	final String operator = getOperatorSymbol(operation);
	if (operator != null) {
		it.append("("); //$NON-NLS-1$
		switch (operator) {
		case "++": //$NON-NLS-1$
			generate(operation.getOperand(), it, context);
			it.append(" += 1"); //$NON-NLS-1$
			break;
		case "--": //$NON-NLS-1$
			generate(operation.getOperand(), it, context);
			it.append(" -= 1"); //$NON-NLS-1$
			break;
		default:
			throw new IllegalArgumentException(MessageFormat.format(Messages.PyExpressionGenerator_0, operator));
		}
		it.append(")"); //$NON-NLS-1$
	}
	return operation;
}
 
Example 7
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param assertStatement the assert statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(SarlAssertExpression assertStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	final boolean haveAssert = !assertStatement.isIsStatic() && assertStatement.getCondition() != null;
	if (haveAssert) {
		it.append("assert (lambda:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(assertStatement.getCondition(), it, context);
		it.decreaseIndentation().newLine();
		it.append(")()"); //$NON-NLS-1$
	}
	if (context.getExpectedExpressionType() != null) {
		if (haveAssert) {
			it.newLine();
		}
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
	}
	return assertStatement;
}
 
Example 8
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param assignment the assignment operator.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the assignment.
 */
protected XExpression _generate(XAssignment assignment, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("("); //$NON-NLS-1$
	newFeatureCallGenerator(context, it).generate(assignment);
	it.append(" = "); //$NON-NLS-1$
	generate(assignment.getValue(), it, context);
	it.append(")"); //$NON-NLS-1$
	return assignment;
}
 
Example 9
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param continueStatement the continue statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
@SuppressWarnings("static-method")
protected XExpression _generate(SarlContinueExpression continueStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	if (context.getExpectedExpressionType() == null) {
		it.append("continue"); //$NON-NLS-1$
	} else {
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
	}
	return continueStatement;
}
 
Example 10
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param breakStatement the break statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
@SuppressWarnings("static-method")
protected XExpression _generate(SarlBreakExpression breakStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	if (context.getExpectedExpressionType() == null) {
		it.append("break"); //$NON-NLS-1$
	} else {
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
	}
	return breakStatement;
}
 
Example 11
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param whileLoop the while-loop.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the last statement in the loop or {@code null}.
 */
protected XExpression _generate(XDoWhileExpression whileLoop, IAppendable it, IExtraLanguageGeneratorContext context) {
	generate(whileLoop.getBody(), it, context);
	it.newLine();
	it.append("while "); //$NON-NLS-1$
	generate(whileLoop.getPredicate(), it, context);
	it.append(":"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	final XExpression last = generate(whileLoop.getBody(), it, context);
	it.decreaseIndentation();
	return last;
}
 
Example 12
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param ifStatement the if-then-else statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XIfExpression ifStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("if "); //$NON-NLS-1$
	generate(ifStatement.getIf(), it, context);
	it.append(":"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	if (ifStatement.getThen() != null) {
		generate(ifStatement.getThen(), context.getExpectedExpressionType(), it, context);
	} else if (context.getExpectedExpressionType() == null) {
		it.append("pass"); //$NON-NLS-1$
	} else {
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
	}
	it.decreaseIndentation();
	if (ifStatement.getElse() != null) {
		it.newLine().append("else:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(ifStatement.getElse(), context.getExpectedExpressionType(), it, context);
		it.decreaseIndentation();
	} else if (context.getExpectedExpressionType() != null) {
		it.newLine().append("else:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
		it.decreaseIndentation();
	}
	return ifStatement;
}
 
Example 13
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param operator the instance-of operator.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the expression.
 */
protected XExpression _generate(XInstanceOfExpression operator, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("isinstance("); //$NON-NLS-1$
	generate(operator.getExpression(), it, context);
	it.append(", "); //$NON-NLS-1$
	it.append(operator.getType().getType());
	it.append(")"); //$NON-NLS-1$
	return operator;
}
 
Example 14
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Generate the given object.
 *
 * @param operation the binary operation.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the operation.
 */
@SuppressWarnings("checkstyle:cyclomaticcomplexity")
protected XExpression _generate(XBinaryOperation operation, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	final String operator = getOperatorSymbol(operation);
	if (operator != null) {
		it.append("("); //$NON-NLS-1$
		generate(operation.getLeftOperand(), it, context);
		switch (operator) {
		case "-": //$NON-NLS-1$
		case "+": //$NON-NLS-1$
		case "*": //$NON-NLS-1$
		case "/": //$NON-NLS-1$
		case "%": //$NON-NLS-1$
		case "-=": //$NON-NLS-1$
		case "+=": //$NON-NLS-1$
		case "*=": //$NON-NLS-1$
		case "/=": //$NON-NLS-1$
		case "%=": //$NON-NLS-1$
		case "<": //$NON-NLS-1$
		case ">": //$NON-NLS-1$
		case "<=": //$NON-NLS-1$
		case ">=": //$NON-NLS-1$
		case "==": //$NON-NLS-1$
		case "!=": //$NON-NLS-1$
		case "<<": //$NON-NLS-1$
		case ">>": //$NON-NLS-1$
			it.append(" ").append(operator).append(" "); //$NON-NLS-1$ //$NON-NLS-2$
			break;
		case "&&": //$NON-NLS-1$
			it.append(" and "); //$NON-NLS-1$
			break;
		case "||": //$NON-NLS-1$
			it.append(" or "); //$NON-NLS-1$
			break;
		case "===": //$NON-NLS-1$
			it.append(" is "); //$NON-NLS-1$
			break;
		case "!==": //$NON-NLS-1$
			it.append(" is not "); //$NON-NLS-1$
			break;
		default:
			throw new IllegalArgumentException(MessageFormat.format(Messages.PyExpressionGenerator_0, operator));
		}
		generate(operation.getRightOperand(), it, context);
		it.append(")"); //$NON-NLS-1$
	}
	return operation;
}
 
Example 15
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 4 votes vote down vote up
private static void appendReturnIfExpectedReturnedExpression(IAppendable it, IExtraLanguageGeneratorContext context) {
	if (context.getExpectedExpressionType() != null) {
		it.append("return "); //$NON-NLS-1$
	}
}
 
Example 16
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 3 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
@SuppressWarnings("static-method")
protected XExpression _generate(XNumberLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append(literal.getValue());
	return literal;
}
 
Example 17
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 3 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the type literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
@SuppressWarnings("static-method")
protected XExpression _generate(XTypeLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append(literal.getType());
	return literal;
}
 
Example 18
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 3 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the null literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
@SuppressWarnings("static-method")
protected XExpression _generate(XNullLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("None"); //$NON-NLS-1$
	return literal;
}
 
Example 19
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Generate the given object.
 *
 * @param returnStatement the return statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XReturnExpression returnStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("return "); //$NON-NLS-1$
	generate(returnStatement.getExpression(), it, context);
	return returnStatement;
}
 
Example 20
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Generate the given object.
 *
 * @param throwStatement the throw statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XThrowExpression throwStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("raise "); //$NON-NLS-1$
	generate(throwStatement.getExpression(), it, context);
	return throwStatement;
}