예제 #1
0
파일: ProgramLoader.java 프로젝트: pdaian/k
  /**
   * Load program file to ASTNode.
   *
   * <p>Write pgm.xml cache in given dotk folder.
   *
   * @param kappize If true, then apply KAppModifier to AST.
   */
  public static ASTNode loadPgmAst(
      String content, String filename, Boolean kappize, String startSymbol, Context context)
      throws IOException, TransformerException {
    File tbl = new File(context.kompiled.getCanonicalPath() + "/pgm/Program.tbl");

    // ------------------------------------- import files in Stratego
    ASTNode out;

    if (GlobalSettings.fastKast) {
      // out = Sglri.run_sglri(context.kompiled.getAbsolutePath() + "/pgm/Program.tbl", startSymbol,
      // content);
      JavaClassesFactory.startConstruction(context);
      out =
          Sglr.run_sglri(
              context.kompiled.getAbsolutePath() + "/pgm/Program.tbl",
              startSymbol,
              content,
              filename);
      out.accept(new ReportErrorsVisitor(context, "file"));
      JavaClassesFactory.endConstruction();
    } else {
      org.kframework.parser.concrete.KParser.ImportTblPgm(tbl.getAbsolutePath());
      String parsed =
          org.kframework.parser.concrete.KParser.ParseProgramString(content, startSymbol);
      Document doc = XmlLoader.getXMLDoc(parsed);

      XmlLoader.addFilename(doc.getFirstChild(), filename);
      XmlLoader.reportErrors(doc);
      FileUtil.save(context.kompiled.getAbsolutePath() + "/pgm.xml", parsed);
      JavaClassesFactory.startConstruction(context);
      out =
          JavaClassesFactory.getTerm(
              (Element) doc.getDocumentElement().getFirstChild().getNextSibling());
      JavaClassesFactory.endConstruction();
    }

    out = out.accept(new PriorityFilter(context));
    out = out.accept(new PreferAvoidFilter(context));
    out = out.accept(new AmbFilter(context));
    out = out.accept(new RemoveBrackets(context));

    if (kappize) out = out.accept(new FlattenSyntax(context));

    return out;
  }