Search in sources :

Example 6 with CompilationUnit

use of net.jangaroo.jooc.ast.CompilationUnit 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 7 with CompilationUnit

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

the class JangarooParser method getCompilationUnit.

public CompilationUnit getCompilationUnit(String qname) {
    CompilationUnit compilationUnit = compilationUnitsByQName.get(qname);
    if (compilationUnit == null) {
        InputSource source = findSource(qname);
        if (source == null) {
            return null;
        }
        compilationUnit = importSource(source);
    }
    return compilationUnit;
}
Also used : CompilationUnit(net.jangaroo.jooc.ast.CompilationUnit) InputSource(net.jangaroo.jooc.input.InputSource)

Example 8 with CompilationUnit

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

the class Jooc method run1.

private CompilationResult run1() {
    InputSource sourcePathInputSource;
    InputSource classPathInputSource;
    try {
        sourcePathInputSource = PathInputSource.fromFiles(getConfig().getSourcePath(), new String[] { "" }, true);
        classPathInputSource = PathInputSource.fromFiles(getConfig().getClassPath(), new String[] { "", JOO_API_IN_JAR_DIRECTORY_PREFIX }, false);
    } catch (IOException e) {
        throw new CompilerError("IO Exception occurred", e);
    }
    setUp(sourcePathInputSource, classPathInputSource);
    HashMap<File, File> outputFileMap = new HashMap<File, File>();
    try {
        for (File sourceFile : getConfig().getSourceFiles()) {
            processSource(sourceFile);
        }
        CompilationUnitSinkFactory codeSinkFactory = createSinkFactory(getConfig(), false);
        CompilationUnitSinkFactory apiSinkFactory = null;
        if (getConfig().isGenerateApi()) {
            apiSinkFactory = createSinkFactory(getConfig(), true);
        }
        for (CompilationUnit unit : compileQueue) {
            unit.analyze(null);
            if (getConfig().getPublicApiViolationsMode() != PublicApiViolationsMode.ALLOW) {
                reportPublicApiViolations(unit);
            }
            File sourceFile = ((FileInputSource) unit.getSource()).getFile();
            File outputFile = null;
            // only generate JavaScript if [Native] annotation and 'native' modifier on primary declaration are not present:
            if (unit.getAnnotation(NATIVE_ANNOTATION_NAME) == null && !unit.getPrimaryDeclaration().isNative()) {
                outputFile = writeOutput(sourceFile, unit, codeSinkFactory, getConfig().isVerbose());
            }
            // always map source file, even if output file is null!
            outputFileMap.put(sourceFile, outputFile);
            if (getConfig().isGenerateApi()) {
                writeOutput(sourceFile, unit, apiSinkFactory, getConfig().isVerbose());
            }
        }
        int result = log.hasErrors() ? CompilationResult.RESULT_CODE_COMPILATION_FAILED : CompilationResult.RESULT_CODE_OK;
        return new CompilationResultImpl(result, outputFileMap);
    } catch (IOException e) {
        throw new CompilerError("IO Exception occurred", e);
    } finally {
        tearDown();
    }
}
Also used : CompilationUnit(net.jangaroo.jooc.ast.CompilationUnit) PathInputSource(net.jangaroo.jooc.input.PathInputSource) InputSource(net.jangaroo.jooc.input.InputSource) FileInputSource(net.jangaroo.jooc.input.FileInputSource) ZipEntryInputSource(net.jangaroo.jooc.input.ZipEntryInputSource) FileInputSource(net.jangaroo.jooc.input.FileInputSource) SingleFileCompilationUnitSinkFactory(net.jangaroo.jooc.backend.SingleFileCompilationUnitSinkFactory) CompilationUnitSinkFactory(net.jangaroo.jooc.backend.CompilationUnitSinkFactory) MergedOutputCompilationUnitSinkFactory(net.jangaroo.jooc.backend.MergedOutputCompilationUnitSinkFactory) HashMap(java.util.HashMap) IOException(java.io.IOException) File(java.io.File)

Example 9 with CompilationUnit

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

the class DeclarationScope method addImport.

@Override
public void addImport(final ImportDirective importDirective) {
    Ide ide = importDirective.getIde();
    String name = ide.getName();
    Ide packageIde = ide.getQualifier();
    String packageName = "";
    final CompilationUnit compilationUnit = getCompilationUnit();
    if (packageIde != null) {
        packageName = packageIde.getQualifiedNameStr();
        packages.add(packageName);
    }
    if (AS3Type.ANY.toString().equals(name)) {
        final List<String> packageIdes = compilationUnit.getCompiler().getPackageIdes(packageName);
        for (String typeToImport : packageIdes) {
            ImportDirective implicitImport = new ImportDirective(packageIde, typeToImport);
            implicitImport.scope(this);
        }
    } else {
        if (importsByName.containsKey(name)) {
            final List<ImportDirective> directiveList = importsByName.get(name);
            if (isImportAlreadyAdded(directiveList, importDirective)) {
                return;
            }
            directiveList.add(importDirective);
        } else {
            List<ImportDirective> list = new LinkedList<ImportDirective>();
            list.add(importDirective);
            importsByName.put(name, list);
        }
        if (ides.containsKey(name)) {
            // name clash with value ide - error according to adobe
            throw new CompilerError(importDirective.getIde().getSymbol(), "attempt to redefine identifier " + name + " by import");
        }
        // define the fully qualified name if not (might be the same string for top level imports):
        final String qualifiedName = ide.getQualifiedNameStr();
        importsByQualifiedName.put(qualifiedName, importDirective);
    }
}
Also used : CompilationUnit(net.jangaroo.jooc.ast.CompilationUnit) ImportDirective(net.jangaroo.jooc.ast.ImportDirective) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) Ide(net.jangaroo.jooc.ast.Ide) LinkedList(java.util.LinkedList)

Example 10 with CompilationUnit

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

the class JangarooParser method resolveImport.

public IdeDeclaration resolveImport(final ImportDirective importDirective) {
    String qname = importDirective.getQualifiedName();
    CompilationUnit compilationUnit;
    try {
        compilationUnit = getCompilationUnit(qname);
    } catch (CompilerError e) {
        throw error(importDirective.getSymbol(), "Unable to import " + qname + ": error while parsing its source (see below).", e);
    }
    if (compilationUnit == null) {
        throw error(importDirective.getSymbol(), "unable to resolve import of " + qname);
    }
    return compilationUnit.getPrimaryDeclaration();
}
Also used : CompilationUnit(net.jangaroo.jooc.ast.CompilationUnit)

Aggregations

CompilationUnit (net.jangaroo.jooc.ast.CompilationUnit)10 File (java.io.File)4 IOException (java.io.IOException)3 InputSource (net.jangaroo.jooc.input.InputSource)3 FileInputSource (net.jangaroo.jooc.input.FileInputSource)2 ZipEntryInputSource (net.jangaroo.jooc.input.ZipEntryInputSource)2 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Symbol (java_cup.runtime.Symbol)1 ConfigClassBuilder (net.jangaroo.exml.as.ConfigClassBuilder)1 Jooc (net.jangaroo.jooc.Jooc)1 JsWriter (net.jangaroo.jooc.JsWriter)1 StdOutCompileLog (net.jangaroo.jooc.StdOutCompileLog)1 Ide (net.jangaroo.jooc.ast.Ide)1 ImportDirective (net.jangaroo.jooc.ast.ImportDirective)1 QualifiedIde (net.jangaroo.jooc.ast.QualifiedIde)1 CompilationUnitSinkFactory (net.jangaroo.jooc.backend.CompilationUnitSinkFactory)1