use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class LanguageCompiler method ceylonParse.
private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) {
if (ceylonEnter.hasRun())
throw new RunTwiceException("Trying to load new source file after CeylonEnter has been called: " + filename);
try {
ModuleManager moduleManager = phasedUnits.getModuleManager();
ModuleSourceMapper moduleSourceMapper = phasedUnits.getModuleSourceMapper();
File sourceFile = new File(filename.getName());
// FIXME: temporary solution
VirtualFile file = vfs.getFromFile(sourceFile);
VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile));
String source = readSource.toString();
char[] chars = source.toCharArray();
LineMap map = Position.makeLineMap(chars, chars.length, false);
PhasedUnit phasedUnit = null;
PhasedUnit externalPhasedUnit = compilerDelegate.getExternalSourcePhasedUnit(srcDir, file);
String suppressWarnings = options.get(Option.CEYLONSUPPRESSWARNINGS);
final EnumSet<Warning> suppressedWarnings;
if (suppressWarnings != null) {
if (suppressWarnings.trim().isEmpty()) {
suppressedWarnings = EnumSet.allOf(Warning.class);
} else {
suppressedWarnings = EnumSet.noneOf(Warning.class);
for (String name : suppressWarnings.trim().split(" *, *")) {
suppressedWarnings.add(Warning.valueOf(name));
}
}
} else {
suppressedWarnings = EnumSet.noneOf(Warning.class);
}
if (externalPhasedUnit != null) {
phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map);
phasedUnit.setSuppressedWarnings(suppressedWarnings);
phasedUnits.addPhasedUnit(externalPhasedUnit.getUnitFile(), phasedUnit);
gen.setMap(map);
String pkgName = phasedUnit.getPackage().getQualifiedNameString();
if ("".equals(pkgName)) {
pkgName = null;
}
return gen.makeJCCompilationUnitPlaceholder(phasedUnit.getCompilationUnit(), filename, pkgName, phasedUnit);
}
if (phasedUnit == null) {
ANTLRStringStream input = new NewlineFixingStringStream(source);
CeylonLexer lexer = new CeylonLexer(input);
CommonTokenStream tokens = new CommonTokenStream(new CeylonInterpolatingLexer(lexer));
CeylonParser parser = new CeylonParser(tokens);
CompilationUnit cu = parser.compilationUnit();
java.util.List<LexError> lexerErrors = lexer.getErrors();
for (LexError le : lexerErrors) {
printError(le, le.getMessage(), "ceylon.lexer", map);
}
java.util.List<ParseError> parserErrors = parser.getErrors();
for (ParseError pe : parserErrors) {
printError(pe, pe.getMessage(), "ceylon.parser", map);
}
// if we continue and it's not a descriptor, we don't care about errors
if ((options.get(Option.CEYLONCONTINUE) != null && !ModuleManager.MODULE_FILE.equals(sourceFile.getName()) && !ModuleManager.PACKAGE_FILE.equals(sourceFile.getName())) || // otherwise we care about errors
(lexerErrors.size() == 0 && parserErrors.size() == 0)) {
// FIXME: this is bad in many ways
String pkgName = getPackage(filename);
// make a Package with no module yet, we will resolve them later
/*
* Stef: see javadoc for findOrCreateModulelessPackage() for why this is here.
*/
Package p = modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName);
phasedUnit = new CeylonPhasedUnit(file, srcDir, cu, p, moduleManager, moduleSourceMapper, ceylonContext, filename, map);
phasedUnit.setSuppressedWarnings(suppressedWarnings);
phasedUnits.addPhasedUnit(file, phasedUnit);
gen.setMap(map);
return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit);
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
JCCompilationUnit result = make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous()));
result.sourcefile = filename;
return result;
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class ClassOrPackageDoc method writeParameterAssertions.
private void writeParameterAssertions(Declaration decl, Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions) throws IOException {
if (parameterAssertions == null || parameterAssertions.isEmpty()) {
return;
}
PhasedUnit pu = tool.getUnit(decl);
open("div class='assertions' title='Parameter assertions'");
open("ul");
for (Tree.Assertion assertion : parameterAssertions.keySet()) {
List<Annotation> annotations = new ArrayList<Annotation>();
buildAnnotations(assertion.getAnnotationList(), annotations);
String doc = Util.getRawDoc(decl.getUnit(), annotations);
if (!Util.isEmpty(doc)) {
open("li");
write("<i class='icon-assertion'></i>");
write(Util.wikiToHTML(doc, linkRenderer()));
close("li");
} else {
for (Tree.Condition c : parameterAssertions.get(assertion)) {
String sourceCode = getSourceCode(pu, c);
open("li");
write("<i class='icon-assertion'></i>");
around("code", sourceCode);
close("li");
}
}
}
close("ul");
close("div");
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class ClassOrPackageDoc method writeConstantValue.
private void writeConstantValue(Value v) throws IOException {
Node node = tool.getNode(v);
PhasedUnit pu = tool.getUnit(v);
if (pu == null || !(node instanceof Tree.AttributeDeclaration)) {
return;
}
Tree.AttributeDeclaration attribute = (Tree.AttributeDeclaration) node;
Tree.SpecifierOrInitializerExpression specifierExpression = attribute.getSpecifierOrInitializerExpression();
if (specifierExpression == null) {
return;
}
String value = getSourceCode(pu, specifierExpression);
int newLineIndex = value.indexOf("\n");
String valueFirstLine = newLineIndex != -1 ? value.substring(0, newLineIndex) : value;
around("span class='specifier'", valueFirstLine);
if (newLineIndex != -1) {
around("a class='specifier-ellipsis' href='#' title='Click for expand the rest of value.'", "...");
open("div class='specifier-rest'");
write(value.substring(newLineIndex + 1));
close("div");
}
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class CeylonDocTool method initialize.
@Override
public void initialize(CeylonTool mainTool) throws Exception {
super.initialize(mainTool);
TypeCheckerBuilder builder = new TypeCheckerBuilder();
for (File src : sourceFolders) {
builder.addSrcDirectory(src);
}
// set up the artifact repository
RepositoryManager repository = getRepositoryManager();
builder.setRepositoryManager(repository);
// make a destination repo
outputRepositoryManager = getOutputRepositoryManager();
// create the actual list of modules to process
List<File> srcs = FileUtil.applyCwd(cwd, sourceFolders);
Collection<String> expandedModules = ModuleWildcardsHelper.expandWildcards(srcs, moduleSpecs, null);
final List<ModuleSpec> modules = ModuleSpec.parseEachList(expandedModules);
final Callable<PhasedUnits> getPhasedUnits = new Callable<PhasedUnits>() {
@Override
public PhasedUnits call() throws Exception {
return typeChecker.getPhasedUnits();
}
};
// we need to plug in the module manager which can load from .cars
builder.moduleManagerFactory(new ModuleManagerFactory() {
@Override
public ModuleManager createModuleManager(Context context) {
return new PhasedUnitsModuleManager(getPhasedUnits, context, modules, outputRepositoryManager, bootstrapCeylon, getLogger());
}
@Override
public ModuleSourceMapper createModuleManagerUtil(Context context, ModuleManager moduleManager) {
return new LazyModuleSourceMapper(context, (PhasedUnitsModuleManager) moduleManager, null, false, null, getEncoding());
}
});
// only parse what we asked for
List<String> moduleFilters = new LinkedList<String>();
for (ModuleSpec spec : modules) {
moduleFilters.add(spec.getName());
if (spec.getName().equals(Module.LANGUAGE_MODULE_NAME) && !bootstrapCeylon) {
throw new CeylondException("error.languageModuleBootstrapOptionMissing");
}
}
builder.setModuleFilters(moduleFilters);
String fileEncoding = getEncoding();
if (fileEncoding == null) {
fileEncoding = CeylonConfig.get(DefaultToolOptions.DEFAULTS_ENCODING);
}
if (fileEncoding != null) {
builder.encoding(fileEncoding);
}
// We do this ourselves, so we can report on the resolution errors before
// running typeChecker.process();
builder.skipDependenciesVerification();
typeChecker = builder.getTypeChecker();
{
PhasedUnits phasedUnits = typeChecker.getPhasedUnits();
phasedUnits.getModuleManager().prepareForTypeChecking();
phasedUnits.visitModules();
phasedUnits.getModuleManager().modulesVisited();
}
ModuleValidator moduleValidator = new ModuleValidator(typeChecker.getContext(), typeChecker.getPhasedUnits());
moduleValidator.verifyModuleDependencyTree();
AssertionVisitor av = new AssertionVisitor();
for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
pu.getCompilationUnit().visit(av);
}
if (haltOnError && av.getErrors() > 0) {
throw new CeylondException("error.failedParsing", new Object[] { av.getErrors() }, null);
}
typeChecker.process();
if (haltOnError && typeChecker.getErrors() > 0) {
throw new CeylondException("error.failedTypechecking", new Object[] { typeChecker.getErrors() }, null);
}
initModules(modules);
initPhasedUnits();
}
use of org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit in project ceylon by eclipse.
the class CeylonDocTool method copySourceFiles.
private void copySourceFiles() throws FileNotFoundException, IOException {
for (PhasedUnit pu : phasedUnits) {
Package pkg = pu.getUnit().getPackage();
if (!shouldInclude(pkg)) {
continue;
}
File file = new File(getFolder(pu.getPackage()), pu.getUnitFile().getName() + ".html");
File dir = file.getParentFile();
if (!dir.exists() && !FileUtil.mkdirs(dir)) {
throw new IOException(CeylondMessages.msg("error.couldNotCreateDirectory", file));
}
Writer writer = openWriter(file);
try {
Markup markup = new Markup(writer);
markup.write("<!DOCTYPE html>");
markup.open("html xmlns='http://www.w3.org/1999/xhtml'");
markup.open("head");
markup.tag("meta charset='UTF-8'");
markup.around("title", pu.getUnit().getFilename());
markup.tag("link href='" + getResourceUrl(pkg, "favicon.ico") + "' rel='shortcut icon'");
markup.tag("link href='" + getResourceUrl(pkg, "ceylon.css") + "' rel='stylesheet' type='text/css'");
markup.tag("link href='" + getResourceUrl(pkg, "ceylondoc.css") + "' rel='stylesheet' type='text/css'");
markup.tag("link href='//fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'");
markup.open("script type='text/javascript'");
markup.write("var resourceBaseUrl = '" + getResourceUrl(pkg, "") + "'");
markup.close("script");
markup.around("script src='" + getResourceUrl(pkg, "jquery-1.8.2.min.js") + "' type='text/javascript'");
markup.around("script src='" + getResourceUrl(pkg, "rainbow.min.js") + "' type='text/javascript'");
markup.around("script src='" + getResourceUrl(pkg, "rainbow.linenumbers.js") + "' type='text/javascript'");
markup.around("script src='" + getResourceUrl(pkg, "ceylon.js") + "' type='text/javascript'");
markup.around("script src='" + getResourceUrl(pkg, "ceylondoc.js") + "' type='text/javascript'");
markup.close("head");
markup.open("body", "pre", "code data-language='ceylon' style='font-family: Inconsolata, Monaco, Courier, monospace'");
String encoding = getEncoding();
if (encoding == null) {
encoding = CeylonConfig.get(DefaultToolOptions.DEFAULTS_ENCODING);
}
InputStreamReader isr = encoding != null ? new InputStreamReader(pu.getUnitFile().getInputStream(), encoding) : new InputStreamReader(pu.getUnitFile().getInputStream());
try (BufferedReader input = new BufferedReader(isr)) {
String line = input.readLine();
while (line != null) {
markup.text(line, "\n");
line = input.readLine();
}
}
markup.close("code", "pre", "body", "html");
} finally {
writer.close();
}
}
}
Aggregations