示例#1
0
  @Override
  public IStrategoTerm invoke(Context context, IStrategoTerm current, IStrategoTerm projectName) {
    if (!isTermString(current) || !isTermString(projectName)) return null;
    String filename = ((IStrategoString) current).stringValue();

    ISourceProject project = findProject(((IStrategoString) projectName).stringValue());
    if (project == null) return null;

    File file = new File(filename);

    Descriptor descriptor = Environment.getDescriptor(LanguageRegistry.findLanguage("SugarTest"));
    IStrategoTerm result = null;
    try {
      IParseController ip = descriptor.createParseController();
      if (ip instanceof DynamicParseController) ip = ((DynamicParseController) ip).getWrapped();
      if (ip instanceof SGLRParseController) {
        SGLRParseController sglrController = (SGLRParseController) ip;
        sglrController.initialize(new Path(filename), project, null);
        // Must lock the parse lock of this controller
        // or hit the assertion in AbstractSGLRI.parse
        sglrController.getParseLock().lock();
        try {
          JSGLRI parser = sglrController.getParser();
          parser.setUseRecovery(false);
          result = parser.parse(new FileInputStream(file), file.getAbsolutePath());
        } finally {
          sglrController.getParseLock().unlock();
        }
      }
    } catch (Exception e) {
      Environment.logException("Could not parse testing string", e);
    }
    return result;
  }
 protected static void createDescriptor() {
   try {
     InputStream descriptorStream =
         ExerciseCParseControllerGenerated.class.getResourceAsStream(DESCRIPTOR);
     InputStream table = ExerciseCParseControllerGenerated.class.getResourceAsStream(TABLE);
     boolean filesystem = false;
     if (descriptorStream == null && new File("./" + DESCRIPTOR).exists()) {
       descriptorStream = new FileInputStream("./" + DESCRIPTOR);
       filesystem = true;
     }
     if (table == null && new File("./" + TABLE).exists()) {
       table = new FileInputStream("./" + TABLE);
       filesystem = true;
     }
     if (descriptorStream == null)
       throw new BadDescriptorException(
           "Could not load descriptor file from "
               + DESCRIPTOR
               + " (not found in plugin: "
               + getPluginLocation()
               + ")");
     if (table == null)
       throw new BadDescriptorException(
           "Could not load parse table from "
               + TABLE
               + " (not found in plugin: "
               + getPluginLocation()
               + ")");
     descriptor =
         DescriptorFactory.load(
             descriptorStream, table, filesystem ? Path.fromPortableString("./") : null);
     descriptor.setAttachmentProvider(ExerciseCParseControllerGenerated.class);
   } catch (BadDescriptorException exc) {
     notLoadingCause = exc;
     Environment.logException("Bad descriptor for " + LANGUAGE + " plugin", exc);
     throw new RuntimeException("Bad descriptor for " + LANGUAGE + " plugin", exc);
   } catch (IOException exc) {
     notLoadingCause = exc;
     Environment.logException("I/O problem loading descriptor for " + LANGUAGE + " plugin", exc);
     throw new RuntimeException("I/O problem loading descriptor for " + LANGUAGE + " plugin", exc);
   }
 }