private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) { if (ceylonEnter.hasRun()) throw new RuntimeException( "Trying to load new source file after CeylonEnter has been called: " + filename); try { String source = readSource.toString(); ANTLRStringStream input = new ANTLRStringStream(source); CeylonLexer lexer = new CeylonLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CeylonParser parser = new CeylonParser(tokens); CompilationUnit cu = parser.compilationUnit(); char[] chars = source.toCharArray(); LineMap map = Position.makeLineMap(chars, chars.length, false); 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 (lexer.getNumberOfSyntaxErrors() != 0) { log.error("ceylon.lexer.failed"); } else if (parser.getNumberOfSyntaxErrors() != 0) { log.error("ceylon.parser.failed"); } else { ModuleManager moduleManager = phasedUnits.getModuleManager(); File sourceFile = new File(filename.toString()); // FIXME: temporary solution VirtualFile file = vfs.getFromFile(sourceFile); VirtualFile srcDir = vfs.getFromFile(getSrcDir(sourceFile)); // FIXME: this is bad in many ways String pkgName = getPackage(filename); // make a Package with no module yet, we will resolve them later com.redhat.ceylon.compiler.typechecker.model.Package p = modelLoader.findOrCreatePackage(null, pkgName == null ? "" : pkgName); PhasedUnit phasedUnit = new CeylonPhasedUnit(file, srcDir, cu, p, moduleManager, ceylonContext, filename, map); phasedUnits.addPhasedUnit(file, phasedUnit); gen.setMap(map); return gen.makeJCCompilationUnitPlaceholder(cu, filename, pkgName, phasedUnit); } } catch (Exception e) { log.error("ceylon", e.getMessage()); } JCCompilationUnit result = make.TopLevel(List.<JCAnnotation>nil(), null, List.<JCTree>of(make.Erroneous())); result.sourcefile = filename; return result; }
/** * Build a map for translating between line numbers and positions in the input. * * @return a LineMap */ public Position.LineMap getLineMap() { char[] buf = getRawCharacters(); return Position.makeLineMap(buf, buf.length, true); }
private JCCompilationUnit ceylonParse(JavaFileObject filename, CharSequence readSource) { if (ceylonEnter.hasRun()) throw new RuntimeException( "Trying to load new source file after CeylonEnter has been called: " + filename); try { ModuleManager moduleManager = phasedUnits.getModuleManager(); 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); if (externalPhasedUnit != null) { phasedUnit = new CeylonPhasedUnit(externalPhasedUnit, filename, map); 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 ANTLRStringStream(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 (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.compiler.typechecker.model.Package p = modelLoader.findOrCreateModulelessPackage(pkgName == null ? "" : pkgName); phasedUnit = new CeylonPhasedUnit( file, srcDir, cu, p, moduleManager, ceylonContext, filename, map); 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; }