/** Generate a Python block comment with the given comment. * * @param comment the comment. * @param it the receiver of the block comment. * @return {@code true} if the block comment is added, {@code false} otherwise. */ protected static boolean generateBlockComment(String comment, PyAppendable it) { final String cmt = comment == null ? null : comment.trim(); if (!Strings.isEmpty(cmt)) { assert cmt != null; for (final String line : cmt.split("[\n\r\f]+")) { //$NON-NLS-1$ it.append("# ").append(line).newLine(); //$NON-NLS-1$ } return true; } return false; }
/** Generate a Python docstring with the given comment. * * @param comment the comment. * @param it the receiver of the docstring. * @return {@code true} if the docstring is added, {@code false} otherwise. */ protected static boolean generateDocString(String comment, PyAppendable it) { final String cmt = comment == null ? null : comment.trim(); if (!Strings.isEmpty(cmt)) { assert cmt != null; it.append("\"\"\"").increaseIndentation(); //$NON-NLS-1$ for (final String line : cmt.split("[\n\r\f]+")) { //$NON-NLS-1$ it.newLine().append(line); } it.decreaseIndentation().newLine(); it.append("\"\"\"").newLine(); //$NON-NLS-1$ return true; } return false; }
@Override protected void generateFileHeader(QualifiedName qualifiedName, ExtraLanguageAppendable appendable, IExtraLanguageGeneratorContext context) { appendable.append(PYTHON_FILE_HEADER); appendable.newLine(); appendable.append(getGenerationComment(context)); appendable.newLine().newLine(); }
/** Generate the given object. * * @param forLoop the for-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(XBasicForLoopExpression forLoop, IAppendable it, IExtraLanguageGeneratorContext context) { for (final XExpression expr : forLoop.getInitExpressions()) { generate(expr, it, context); it.newLine(); } it.append("while "); //$NON-NLS-1$ generate(forLoop.getExpression(), it, context); it.append(":"); //$NON-NLS-1$ it.increaseIndentation().newLine(); final XExpression last = generate(forLoop.getEachExpression(), it, context); for (final XExpression expr : forLoop.getUpdateExpressions()) { it.newLine(); generate(expr, it, context); } it.decreaseIndentation(); return last; }
/** 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; }
/** 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; }
final String handleName = it.declareUniqueNameVariable(handler, "__on_" + event.getSimpleName() + "__"); //$NON-NLS-1$ //$NON-NLS-2$ it.append("def ").append(handleName).append("(self, occurrence):"); //$NON-NLS-1$ //$NON-NLS-2$ it.increaseIndentation().newLine(); generateDocString(getTypeBuilder().getDocumentation(handler), it); if (handler.getExpression() != null) { it.append("pass"); //$NON-NLS-1$ it.decreaseIndentation().newLine(); final String key = this.qualifiedNameProvider.getFullyQualifiedName(handler.getDeclaringType()).toString(); final Map<String, Map<String, List<Pair<XExpression, String>>>> map = context.getMapData(EVENT_GUARDS_MEMENTO);
it.append(entry.getKey().replaceAll("[^a-zA-Z0-9_]+", "_")); //$NON-NLS-1$ //$NON-NLS-2$ it.append("__(self, occurrence):"); //$NON-NLS-1$ it.increaseIndentation().newLine(); it.append("it = occurrence").newLine(); //$NON-NLS-1$ final String eventHandleName = it.declareUniqueNameVariable(new Object(), "__event_handles"); //$NON-NLS-1$ it.append(eventHandleName).append(" = list"); //$NON-NLS-1$ it.append("if "); //$NON-NLS-1$ generate(guardDesc.getKey(), null, it, context); it.append(":").increaseIndentation().newLine(); //$NON-NLS-1$ it.append(eventHandleName).append(".add(").append(guardDesc.getValue()).append(")"); //$NON-NLS-1$ //$NON-NLS-2$ it.decreaseIndentation(); it.decreaseIndentation().newLine();
/** 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; }
/** 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(XWhileExpression whileLoop, IAppendable it, IExtraLanguageGeneratorContext context) { 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; }
generate(ifStatement.getIf(), it, context); it.append(":"); //$NON-NLS-1$ it.increaseIndentation().newLine(); if (ifStatement.getThen() != null) { generate(ifStatement.getThen(), context.getExpectedExpressionType(), it, context); 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();
it.increaseIndentation().newLine(); generateDocString(comment, it); return true;
/** 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; }
/** 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; }
it.append("class ").append(closureName).append("(") //$NON-NLS-1$//$NON-NLS-2$ .append(closureType).append(",").append(objType.getType()).append("):") //$NON-NLS-1$ //$NON-NLS-2$ .increaseIndentation().newLine().append("def ") //$NON-NLS-1$ .append(function.getSimpleName()).append("(") //$NON-NLS-1$ .append(getExtraLanguageKeywordProvider().getThisKeywordLambda().apply()); it.increaseIndentation().newLine(); if (closure.getExpression() != null) { LightweightTypeReference returnType = closureType0; it.append("pass"); //$NON-NLS-1$ it.decreaseIndentation().decreaseIndentation().newLine(); it.closeScope();