Example #1
0
  private static Iterable<? extends StaticError> compilerPhases(
      Path path, String file, Option<String> out, boolean link) throws UserError {
    GraphRepository bcr = null;
    Debug.debug(Debug.Type.FORTRESS, 2, "Compiling file ", file);
    APIName name = null;
    try {
      bcr = specificRepository(path);
      name = cuName(file);

      if (isApi(file)) {
        Api a = (Api) bcr.getApi(name).ast();
        if (out.isSome())
          ASTIO.writeJavaAst(
              a, // defaultRepository.getApi(name).ast(),
              out.unwrap());
      } else if (isComponent(file)) {
        Component c;
        if (link) c = (Component) bcr.getLinkedComponent(name).ast();
        else c = (Component) bcr.getComponent(name).ast();
        if (out.isSome()) {
          ASTIO.writeJavaAst(
              c, // defaultRepository.getComponent(name).ast(),
              out.unwrap());
          bcr.deleteComponent(name, true);
        }
      } else {
        throw new UserError(
            "What kind of file is " + file + "? Name should end with .fsi or .fss.");
      }
    } catch (ProgramError pe) {
      Iterable<? extends StaticError> se = pe.getStaticErrors();
      if (se == null) return IterUtil.singleton(new WrappedException(pe, Debug.stackTraceOn()));
      else return flattenErrors(se);
    } catch (RepositoryError ex) {
      throw ex;
    } catch (FileNotFoundException ex) {
      throw new WrappedException(ex);
    } catch (IOException e) {
      throw new WrappedException(e);
    } catch (StaticError ex) {
      return flattenErrors(ex);
    } catch (CompilerBug e) {
      return IterUtil.singleton(new WrappedException(e, Debug.stackTraceOn()));
    } catch (InterpreterBug e) {
      return IterUtil.singleton(new WrappedException(e, Debug.stackTraceOn()));
    } catch (FortressException e) {
      failureBoilerplate(e);
      System.exit(1);
    } finally {
      if (bcr != null && name != null) bcr.deleteComponent(name, false);
    }

    if (bcr != null && bcr.verbose()) System.err.println("Compiling done.");

    return IterUtil.empty();
  }
Example #2
0
 private static void unparse(
     String file, Option<String> out, boolean unqualified, boolean unmangle) throws IOException {
   String code =
       ASTIO.readJavaAst(file).unwrap().accept(new FortressAstToConcrete(unqualified, unmangle));
   if (out.isSome()) {
     try {
       BufferedWriter writer = Useful.filenameToBufferedWriter(out.unwrap());
       writer.write(code);
       writer.close();
     } catch (IOException e) {
       throw new IOException("IOException " + e + "while writing " + out.unwrap());
     }
   } else {
     System.out.println(code);
   }
 }
Example #3
0
 private static int parse(String file, Option<String> out) throws UserError, IOException {
   int return_code = 0;
   try {
     CompilationUnit unit = Parser.parseFileConvertExn(new File(file));
     System.out.println("Ok");
     if (out.isSome()) {
       try {
         ASTIO.writeJavaAst(unit, out.unwrap());
         System.out.println("Dumped parse tree to " + out.unwrap());
       } catch (IOException e) {
         throw new IOException("IOException " + e + "while writing " + out.unwrap());
       }
     }
   } catch (ParserError e) {
     if (Debug.stackTraceOn()) {
       System.err.println(e.getMessage());
       e.printStackTrace();
     } else {
       System.err.println(turnOnDebugMessage);
     }
     return_code = 1;
   } catch (ProgramError e) {
     failureBoilerplate(e);
     return_code = 1;
   } catch (CompilerBug e) {
     failureBoilerplate(e);
     return_code = 1;
   } catch (InterpreterBug e) {
     failureBoilerplate(e);
     return_code = 1;
   } catch (FileNotFoundException f) {
     throw new UserError(file + " not found");
   } finally {
     try {
       Files.rm(ProjectProperties.preparserErrorLog(file));
     } catch (IOException e) {
     }
   }
   return return_code;
 }