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;
}
};
}
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;
}
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();
}
}
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);
}
}
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();
}
Aggregations