Search in sources :

Example 1 with ModuleSourceMapper

use of com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper in project ceylon-compiler by ceylon.

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(OptionName.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(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(OptionName.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.
                     */
                com.redhat.ceylon.model.typechecker.model.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(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Warning(com.redhat.ceylon.compiler.typechecker.analyzer.Warning) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) NewlineFixingStringStream(com.redhat.ceylon.compiler.typechecker.util.NewlineFixingStringStream) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) ParseError(com.redhat.ceylon.compiler.typechecker.parser.ParseError) ModuleSourceMapper(com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(com.sun.tools.javac.util.Position.LineMap) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) IOException(java.io.IOException) Package(com.redhat.ceylon.model.typechecker.model.Package) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File) LexError(com.redhat.ceylon.compiler.typechecker.parser.LexError)

Example 2 with ModuleSourceMapper

use of com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper in project ceylon-compiler by ceylon.

the class CeylonDocTool method initialize.

@Override
public void initialize(CeylonTool 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);
    List<String> expandedModules = ModuleWildcardsHelper.expandWildcards(srcs, moduleSpecs, null);
    final List<ModuleSpec> modules = ModuleSpec.parseEachList(expandedModules);
    // 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 CeylonDocModuleManager(CeylonDocTool.this, context, modules, outputRepositoryManager, bootstrapCeylon, log);
        }

        @Override
        public ModuleSourceMapper createModuleManagerUtil(Context context, ModuleManager moduleManager) {
            return new CeylonDocModuleSourceMapper(context, (CeylonDocModuleManager) moduleManager, CeylonDocTool.this);
        }
    });
    // 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);
    }
    typeChecker = builder.getTypeChecker();
    // collect all units we are typechecking
    initTypeCheckedUnits(typeChecker);
    typeChecker.process();
    if (haltOnError && typeChecker.getErrors() > 0) {
        throw new CeylondException("error.failedParsing", new Object[] { typeChecker.getErrors() }, null);
    }
    initModules(modules);
    initPhasedUnits();
}
Also used : Context(com.redhat.ceylon.compiler.typechecker.context.Context) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) TypeCheckerBuilder(com.redhat.ceylon.compiler.typechecker.TypeCheckerBuilder) ModuleManager(com.redhat.ceylon.model.typechecker.util.ModuleManager) LinkedList(java.util.LinkedList) ModuleSpec(com.redhat.ceylon.common.tools.ModuleSpec) RepositoryManager(com.redhat.ceylon.cmr.api.RepositoryManager) ModuleSourceMapper(com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper) ModuleManagerFactory(com.redhat.ceylon.compiler.typechecker.util.ModuleManagerFactory) File(java.io.File)

Aggregations

ModuleSourceMapper (com.redhat.ceylon.compiler.typechecker.analyzer.ModuleSourceMapper)2 ModuleManager (com.redhat.ceylon.model.typechecker.util.ModuleManager)2 File (java.io.File)2 ArtifactContext (com.redhat.ceylon.cmr.api.ArtifactContext)1 RepositoryManager (com.redhat.ceylon.cmr.api.RepositoryManager)1 ModuleSpec (com.redhat.ceylon.common.tools.ModuleSpec)1 CeylonCompilationUnit (com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit)1 TypeCheckerBuilder (com.redhat.ceylon.compiler.typechecker.TypeCheckerBuilder)1 Warning (com.redhat.ceylon.compiler.typechecker.analyzer.Warning)1 Context (com.redhat.ceylon.compiler.typechecker.context.Context)1 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)1 VirtualFile (com.redhat.ceylon.compiler.typechecker.io.VirtualFile)1 CeylonLexer (com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer)1 CeylonParser (com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)1 LexError (com.redhat.ceylon.compiler.typechecker.parser.LexError)1 ParseError (com.redhat.ceylon.compiler.typechecker.parser.ParseError)1 CompilationUnit (com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit)1 ModuleManagerFactory (com.redhat.ceylon.compiler.typechecker.util.ModuleManagerFactory)1 NewlineFixingStringStream (com.redhat.ceylon.compiler.typechecker.util.NewlineFixingStringStream)1 Package (com.redhat.ceylon.model.typechecker.model.Package)1