Search in sources :

Example 1 with JsWriter

use of net.jangaroo.jooc.JsWriter in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method getParameterInitializerCodeGenerator.

public CodeGenerator getParameterInitializerCodeGenerator(final Parameters params) {
    return new CodeGenerator() {

        @Override
        public void generate(JsWriter out, boolean first) throws IOException {
            // collect the ... (rest) parameter and all optional parameters with their position index:
            int restParamIndex = -1;
            Parameter restParam = null;
            Map<Integer, Parameter> paramByIndex = new HashMap<Integer, Parameter>();
            Parameters parameters = params;
            for (int paramIndex = 0; parameters != null; parameters = parameters.getTail()) {
                Parameter param = parameters.getHead();
                if (param.isRest()) {
                    restParamIndex = paramIndex;
                    restParam = param;
                    break;
                }
                if (param.hasInitializer()) {
                    paramByIndex.put(paramIndex, param);
                }
                ++paramIndex;
            }
            generateParameterInitializers(out, paramByIndex);
            if (restParam != null) {
                generateRestParamCode(restParam, restParamIndex);
            }
        }
    };
}
Also used : Parameters(net.jangaroo.jooc.ast.Parameters) HashMap(java.util.HashMap) AnnotationParameter(net.jangaroo.jooc.ast.AnnotationParameter) Parameter(net.jangaroo.jooc.ast.Parameter) CodeGenerator(net.jangaroo.jooc.CodeGenerator) JsWriter(net.jangaroo.jooc.JsWriter)

Example 2 with JsWriter

use of net.jangaroo.jooc.JsWriter in project jangaroo-tools by CoreMedia.

the class SingleFileCompilationUnitSinkFactory method createSink.

public CompilationUnitSink createSink(PackageDeclaration packageDeclaration, IdeDeclaration primaryDeclaration, File sourceFile, final boolean verbose) {
    final File outFile = getOutputFile(sourceFile, packageDeclaration.getQualifiedName());
    String fileName = outFile.getName();
    String classPart = fileName.substring(0, fileName.lastIndexOf('.'));
    String className = primaryDeclaration.getName();
    if (!classPart.equals(className)) {
        Jooc.warning(primaryDeclaration.getSymbol(), "class name should be equal to file name: expected " + classPart + ", found " + className);
    }
    createOutputDirs(outFile);
    return new CompilationUnitSink() {

        public File writeOutput(CompilationUnit compilationUnit) {
            if (verbose) {
                // NOSONAR this is a cmd line tool
                System.out.println("writing file: '" + outFile.getAbsolutePath() + "'");
            }
            try {
                OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
                try {
                    if (generateApi) {
                        ApiModelGenerator apiModelGenerator = new ApiModelGenerator(isExcludeClassByDefault(getOptions()));
                        apiModelGenerator.generateModel(compilationUnit).visit(new ActionScriptCodeGeneratingModelVisitor(writer));
                    } else {
                        JsWriter out = new JsWriter(writer);
                        try {
                            out.setOptions(getOptions());
                            compilationUnit.visit(new JsCodeGenerator(out));
                        } finally {
                            out.close();
                        }
                    }
                } catch (IOException e) {
                    // noinspection ResultOfMethodCallIgnored
                    // NOSONAR
                    outFile.delete();
                    throw Jooc.error("error writing file: '" + outFile.getAbsolutePath() + "'", outFile, e);
                }
            } catch (IOException e) {
                throw Jooc.error("cannot open output file for writing: '" + outFile.getAbsolutePath() + "'", outFile, e);
            }
            return outFile;
        }
    };
}
Also used : CompilationUnit(net.jangaroo.jooc.ast.CompilationUnit) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) JsWriter(net.jangaroo.jooc.JsWriter)

Example 3 with JsWriter

use of net.jangaroo.jooc.JsWriter in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitForInStatement.

@Override
public void visitForInStatement(final ForInStatement forInStatement) throws IOException {
    final Ide exprAuxIde = forInStatement.getExprAuxIde();
    IdeDeclaration exprType = forInStatement.getExpr().getType();
    String exprTypeName = exprType != null ? exprType.getQualifiedNameStr() : "";
    boolean iterateArrayMode = "Array".equals(exprTypeName) || "Vector$object".equals(exprTypeName);
    if (exprAuxIde != null && !iterateArrayMode) {
        new SemicolonTerminatedStatement(new VariableDeclaration(SYM_VAR, exprAuxIde, null, null), SYM_SEMICOLON).visit(this);
    }
    out.writeSymbol(forInStatement.getSymKeyword());
    final boolean isForEach = forInStatement.getSymEach() != null;
    if (isForEach) {
        out.beginComment();
        out.writeSymbol(forInStatement.getSymEach());
        out.endComment();
    }
    out.writeSymbol(forInStatement.getLParen());
    if (isForEach || iterateArrayMode) {
        new VariableDeclaration(SYM_VAR, forInStatement.getAuxIde(), null, null).visit(this);
    } else {
        if (forInStatement.getDecl() != null) {
            forInStatement.getDecl().visit(this);
        } else {
            forInStatement.getLValue().visit(this);
        }
    }
    if (iterateArrayMode) {
        String indexVarName = forInStatement.getAuxIde().getName();
        out.write("=0");
        if (exprAuxIde != null) {
            out.write(",");
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(";");
        out.write(indexVarName);
        out.write("<");
        if (exprAuxIde != null) {
            out.writeToken(exprAuxIde.getName());
        } else {
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(".length;");
        out.write("++" + indexVarName);
    } else {
        out.writeSymbol(forInStatement.getSymIn());
        if (exprAuxIde != null) {
            // assign the expression value to the auxiliary expression value variable once:
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
        }
        forInStatement.getExpr().visit(this);
    }
    out.writeSymbol(forInStatement.getRParen());
    if (isForEach || iterateArrayMode) {
        // inject synthesized statement into loop body:
        if (!(forInStatement.getBody() instanceof BlockStatement)) {
            forInStatement.setBody(new BlockStatement(SYM_LBRACE, Arrays.<Directive>asList(forInStatement.getBody()), SYM_RBRACE));
        }
        ((BlockStatement) forInStatement.getBody()).addBlockStartCodeGenerator(new CodeGenerator() {

            @Override
            public void generate(JsWriter out, boolean first) throws IOException {
                // synthesize assigning the correct index to the variable given in the original for each statement:
                if (forInStatement.getDecl() != null) {
                    forInStatement.getDecl().visit(JsCodeGenerator.this);
                } else {
                    forInStatement.getLValue().visit(JsCodeGenerator.this);
                }
                out.writeToken("=");
                if (!isForEach) {
                    out.write("String(" + forInStatement.getAuxIde().getName() + ")");
                } else {
                    if (exprAuxIde == null) {
                        forInStatement.getExpr().visit(JsCodeGenerator.this);
                    } else {
                        out.write(exprAuxIde.getName());
                    }
                    out.write("[" + forInStatement.getAuxIde().getName() + "]");
                }
                out.write(";");
            }
        });
    }
    forInStatement.getBody().visit(this);
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) BlockStatement(net.jangaroo.jooc.ast.BlockStatement) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) CodeGenerator(net.jangaroo.jooc.CodeGenerator) IOException(java.io.IOException) SemicolonTerminatedStatement(net.jangaroo.jooc.ast.SemicolonTerminatedStatement) UseNamespaceDirective(net.jangaroo.jooc.ast.UseNamespaceDirective) Directive(net.jangaroo.jooc.ast.Directive) ImportDirective(net.jangaroo.jooc.ast.ImportDirective) JsWriter(net.jangaroo.jooc.JsWriter)

Aggregations

JsWriter (net.jangaroo.jooc.JsWriter)3 IOException (java.io.IOException)2 CodeGenerator (net.jangaroo.jooc.CodeGenerator)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 AnnotationParameter (net.jangaroo.jooc.ast.AnnotationParameter)1 BlockStatement (net.jangaroo.jooc.ast.BlockStatement)1 CompilationUnit (net.jangaroo.jooc.ast.CompilationUnit)1 Directive (net.jangaroo.jooc.ast.Directive)1 Ide (net.jangaroo.jooc.ast.Ide)1 IdeDeclaration (net.jangaroo.jooc.ast.IdeDeclaration)1 ImportDirective (net.jangaroo.jooc.ast.ImportDirective)1 NamespacedIde (net.jangaroo.jooc.ast.NamespacedIde)1 Parameter (net.jangaroo.jooc.ast.Parameter)1 Parameters (net.jangaroo.jooc.ast.Parameters)1 QualifiedIde (net.jangaroo.jooc.ast.QualifiedIde)1 SemicolonTerminatedStatement (net.jangaroo.jooc.ast.SemicolonTerminatedStatement)1 TypedIdeDeclaration (net.jangaroo.jooc.ast.TypedIdeDeclaration)1