Search in sources :

Example 21 with PhasedUnit

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;
}
Also used : VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) Warning(org.eclipse.ceylon.compiler.typechecker.analyzer.Warning) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) NewlineFixingStringStream(org.eclipse.ceylon.compiler.typechecker.util.NewlineFixingStringStream) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) ParseError(org.eclipse.ceylon.compiler.typechecker.parser.ParseError) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) CeylonParser(org.eclipse.ceylon.compiler.typechecker.parser.CeylonParser) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) CeylonCompilationUnit(org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(org.eclipse.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(org.eclipse.ceylon.langtools.tools.javac.util.Position.LineMap) CeylonLexer(org.eclipse.ceylon.compiler.typechecker.parser.CeylonLexer) IOException(java.io.IOException) CeylonInterpolatingLexer(org.eclipse.ceylon.compiler.typechecker.parser.CeylonInterpolatingLexer) Package(org.eclipse.ceylon.model.typechecker.model.Package) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) LexError(org.eclipse.ceylon.compiler.typechecker.parser.LexError)

Example 22 with PhasedUnit

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");
}
Also used : ArrayList(java.util.ArrayList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 23 with PhasedUnit

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");
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Example 24 with PhasedUnit

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();
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) Context(org.eclipse.ceylon.compiler.typechecker.context.Context) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) PhasedUnitsModuleManager(org.eclipse.ceylon.compiler.PhasedUnitsModuleManager) ModuleManager(org.eclipse.ceylon.model.typechecker.util.ModuleManager) AssertionVisitor(org.eclipse.ceylon.compiler.typechecker.util.AssertionVisitor) Callable(java.util.concurrent.Callable) LinkedList(java.util.LinkedList) ModuleValidator(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleValidator) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit) PhasedUnitsModuleManager(org.eclipse.ceylon.compiler.PhasedUnitsModuleManager) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) PhasedUnits(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits) LazyModuleSourceMapper(org.eclipse.ceylon.compiler.java.loader.model.LazyModuleSourceMapper) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) LazyModuleSourceMapper(org.eclipse.ceylon.compiler.java.loader.model.LazyModuleSourceMapper) ModuleSourceMapper(org.eclipse.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) ModuleManagerFactory(org.eclipse.ceylon.compiler.typechecker.util.ModuleManagerFactory) File(java.io.File)

Example 25 with PhasedUnit

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();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Package(org.eclipse.ceylon.model.typechecker.model.Package) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PhasedUnit(org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)

Aggregations

PhasedUnit (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnit)28 File (java.io.File)9 Package (org.eclipse.ceylon.model.typechecker.model.Package)7 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)5 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)5 HashMap (java.util.HashMap)4 CeylonCompilationUnit (org.eclipse.ceylon.compiler.java.codegen.CeylonCompilationUnit)4 MetamodelVisitor (org.eclipse.ceylon.compiler.js.loader.MetamodelVisitor)4 PhasedUnits (org.eclipse.ceylon.compiler.typechecker.context.PhasedUnits)4 Node (org.eclipse.ceylon.compiler.typechecker.tree.Node)4 Visitor (org.eclipse.ceylon.compiler.typechecker.tree.Visitor)4 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)4 Module (org.eclipse.ceylon.model.typechecker.model.Module)4 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)4 Map (java.util.Map)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3