/** 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 the given object. * * @param closure the closure. * @param it the target for the generated content. * @param context the context. * @return the closure. */ @SuppressWarnings("static-method") protected XExpression _generate(XClosure closure, IAppendable it, IExtraLanguageGeneratorContext context) { if (it.hasName(closure)) { appendReturnIfExpectedReturnedExpression(it, context); it.append(it.getName(closure)).append("()"); //$NON-NLS-1$ } return closure; }
/** 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; }
/** 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; }
if (!it.hasName(closure)) { final LightweightTypeReference closureType0 = getExpectedType(closure); LightweightTypeReference closureType = closureType0; final String closureName = it.declareSyntheticVariable(closure, "__Jclosure_" //$NON-NLS-1$ + closureType.getSimpleName()); final JvmDeclaredType rawType = (JvmDeclaredType) closureType.getType(); it.openPseudoScope(); 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()); for (final JvmFormalParameter param : closure.getFormalParameters()) { it.append(", "); //$NON-NLS-1$ final String name = it.declareUniqueNameVariable(param, param.getName()); it.append(name); it.append("):"); //$NON-NLS-1$ it.increaseIndentation().newLine(); if (closure.getExpression() != null) { LightweightTypeReference returnType = closureType0; it.append("pass"); //$NON-NLS-1$ it.decreaseIndentation().decreaseIndentation().newLine(); it.closeScope();
private static void appendReturnIfExpectedReturnedExpression(IAppendable it, IExtraLanguageGeneratorContext context) { if (context.getExpectedExpressionType() != null) { it.append("return "); //$NON-NLS-1$ } }
/** 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; }
XExpression last = block; if (block.getExpressions().isEmpty()) { it.append("pass"); //$NON-NLS-1$ } else { it.openScope(); if (context.getExpectedExpressionType() == null) { boolean first = true; first = false; } else { it.newLine(); for (int i = 0; i < exprs.size() - 1; ++i) { if (i > 0) { it.newLine(); it.closeScope();
if (!it.hasName(anonClass) && it instanceof PyAppendable) { final LightweightTypeReference jvmAnonType = getExpectedType(anonClass); final String anonName = it.declareSyntheticVariable(anonClass, jvmAnonType.getSimpleName()); QualifiedName anonQualifiedName = QualifiedName.create( jvmAnonType.getType().getQualifiedName().split(Pattern.quote("."))); //$NON-NLS-1$ it.openPseudoScope(); final IRootGenerator rootGenerator = context.getRootGenerator(); assert rootGenerator instanceof PyGenerator; context, null); it.closeScope();
protected String getVarName(JvmIdentifiableElement ex, IAppendable appendable) { return appendable.getName(ex); } }
@Pure public String formatMultilineComment(String doc, String indentation) { IAppendable appendable = new StringBuilderBasedAppendable(); formatMultilineComment(doc, indentation, appendable); return appendable.getContent(); }
final String varName; if (switchStatement.getDeclaredParam() != null) { varName = it.declareUniqueNameVariable(switchStatement.getDeclaredParam(), switchStatement.getDeclaredParam().getSimpleName()); } else { varName = it.declareSyntheticVariable(switchStatement, "___expression"); //$NON-NLS-1$ it.openPseudoScope(); it.append(varName).append(" = "); //$NON-NLS-1$ generate(switchStatement.getSwitch(), it, context); it.newLine(); boolean first = true; boolean fallThrough = false; for (final XCasePart caseExpression : switchStatement.getCases()) { if (fallThrough) { it.append(") or ("); //$NON-NLS-1$ } else if (first) { it.append("if ("); //$NON-NLS-1$ first = false; } else { it.append("elif ("); //$NON-NLS-1$ it.append("isinstance(").append(varName); //$NON-NLS-1$ it.append(", ").append(caseExpression.getTypeGuard().getType()); //$NON-NLS-1$ it.append(")"); //$NON-NLS-1$ if (caseExpression.getCase() != null) { it.append(" and ("); //$NON-NLS-1$ generate(caseExpression.getCase(), it, context); it.append(")"); //$NON-NLS-1$
/** 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; }
final JvmTypeReference event = handler.getName(); 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$ for (final Pair<XExpression, String> guardDesc : entry.getValue()) { it.newLine(); if (guardDesc.getKey() == null) { it.append(eventHandleName).append(".add(").append(guardDesc.getValue()).append(")"); //$NON-NLS-1$ //$NON-NLS-2$ } else { 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.newLine().append("return ").append(eventHandleName); //$NON-NLS-1$ it.decreaseIndentation().newLine();
private String computeBuffer() { IAppendable appendable = new FakeTreeAppendable(); applyReplacements(appendable, getCommentText(), getReplacements()); return appendable.getContent(); } public String toString() {
/** 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 literal the literal. * @param it the target for the generated content. * @param context the context. * @return the literal. */ @SuppressWarnings("static-method") protected XExpression _generate(XStringLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) { appendReturnIfExpectedReturnedExpression(it, context); it.append("u\"").append(Strings.convertToJavaString(literal.getValue())).append("\""); //$NON-NLS-1$//$NON-NLS-2$ return literal; }
/** 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; }