예제 #1
0
  @Override
  protected IStatus run(IProgressMonitor monitor) {
    monitor.beginTask("Parsing", 105);

    try {
      handler.clearMessages();

      IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser();
      parseTree =
          (IConstructor)
              gtd.parse(
                  START_SORT,
                  URI.create(path.toString()),
                  input.toCharArray(),
                  new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(),
                  new UPTRNodeFactory());

    } catch (ParseError pe) {
      int offset = pe.getOffset();
      if (offset == input.length()) --offset;

      handler.handleSimpleMessage(
          "parse error",
          offset,
          offset + pe.getLength(),
          pe.getBeginColumn(),
          pe.getEndColumn(),
          pe.getBeginLine() + 1,
          pe.getEndLine() + 1);
    } catch (Throw e) {
      IValue exc = e.getException();

      if (exc.getType() == RuntimeExceptionFactory.Exception) {
        if (((IConstructor) exc).getConstructorType() == RuntimeExceptionFactory.ParseError) {
          ISourceLocation loc = (ISourceLocation) ((IConstructor) e.getException()).get(0);
          handler.handleSimpleMessage(
              "parse error: " + loc,
              loc.getOffset(),
              loc.getOffset() + loc.getLength(),
              loc.getBeginColumn(),
              loc.getEndColumn(),
              loc.getBeginLine(),
              loc.getEndLine());
        } else {
          Activator.getInstance().logException(e.getMessage(), e);
        }
      }
    } catch (Throwable e) {
      Activator.getInstance().logException("parsing failed: " + e.getMessage(), e);
    } finally {
      monitor.done();
    }

    return Status.OK_STATUS;
  }
 private Object loadFromChars(char[] input, String sourcePath, String sourceName) {
   IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser();
   IConstructor file =
       (IConstructor)
           gtd.parse(
               START_SORT,
               URI.create(sourcePath),
               input,
               new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(),
               new UPTRNodeFactory());
   return loadPT(new IConstructor[] {file}, sourcePath, sourceName);
   // catch (ParseError e) {
   // throw RuntimeExceptionFactory.parseError(
   // vf.sourceLocation(e.getLocation(),
   // e.getOffset(), e.getLength(), e.getBeginLine(), e.getEndLine(),
   // e.getBeginColumn(), e.getEndColumn()), null,
   // Arrays.toString(e.getStackTrace()));
   // }
 }
  public static IConstructor loadForRascal(IValueFactory vf, IString src, ISourceLocation loc) {
    IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser();
    IConstructor file =
        (IConstructor)
            gtd.parse(
                START_SORT,
                loc.getURI(),
                src.getValue().toCharArray(),
                new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(),
                new UPTRNodeFactory());

    String path = loc.getURI().getPath();
    IConstructor[] fileRef = new IConstructor[] {file};

    // ignore the clojure result
    ISetWriter errors = vf.setWriter(Message.Message);
    new UPTRCompiler(vf, errors).loadPT(fileRef, path, new File(path).getName());

    // return the updated parse tree.
    return fileRef[0].setAnnotation("messages", errors.done());
  }