Package org.eclipse.imp.pdb.facts

Examples of org.eclipse.imp.pdb.facts.ISourceLocation


      IProblem[] problems = compilUnit.getProblems();
      for (i = 0; i < problems.length; i++) {
        int offset = problems[i].getSourceStart();
        int length = problems[i].getSourceEnd() - offset + 1;
        int sl = problems[i].getSourceLineNumber();
        ISourceLocation pos = values.sourceLocation(loc, offset, length, sl, sl, 0, 0);
        org.eclipse.imp.pdb.facts.type.Type constr;
        if (problems[i].isError()) {
          constr = typeStore.lookupConstructor(this.typeStore.lookupAbstractDataType("Message"), "error", args);
        } else {
          constr = typeStore.lookupConstructor(this.typeStore.lookupAbstractDataType("Message"), "warning", args);
View Full Code Here


      IValueFactory vf = eval.getValueFactory();
      prod = ProductionAdapter.setDefined(prod, vf.constructor(Factory.Symbol_Label, vf.string("$parsed"), sym));
      return TreeAdapter.setProduction(TreeAdapter.setArg(tree, "parts", fragment), prod);
    }
    catch (ParseError e) {
      ISourceLocation loc = TreeAdapter.getLocation(tree);
      ISourceLocation src = eval.getValueFactory().sourceLocation(loc.getURI(), loc.getOffset() + e.getOffset(), loc.getLength(), loc.getBeginLine() + e.getBeginLine() - 1, loc.getEndLine() + e.getEndLine() - 1, loc.getBeginColumn() + e.getBeginColumn(), loc.getBeginColumn() + e.getEndColumn());
      eval.getMonitor().warning("parse error in concrete syntax", src);
      return tree.asAnnotatable().setAnnotation("parseError", src);
    }
    catch (StaticError e) {
      ISourceLocation loc = TreeAdapter.getLocation(tree);
      ISourceLocation src = eval.getValueFactory().sourceLocation(loc.getURI(), loc.getOffset(), loc.getLength(), loc.getBeginLine(), loc.getEndLine(), loc.getBeginColumn(), loc.getBeginColumn());
      eval.getMonitor().warning(e.getMessage(), e.getLocation());
      return tree.asAnnotatable().setAnnotation("can not parse fragment due to " + e.getMessage(), src);
    }
    catch (UndeclaredNonTerminalException e) {
      ISourceLocation loc = TreeAdapter.getLocation(tree);
      ISourceLocation src = eval.getValueFactory().sourceLocation(loc.getURI(), loc.getOffset(), loc.getLength(), loc.getBeginLine(), loc.getEndLine(), loc.getBeginColumn(), loc.getBeginColumn());
      eval.getMonitor().warning(e.getMessage(), src);
      return tree.asAnnotatable().setAnnotation("can not parse fragment due to " + e.getMessage(), src);
    }
  }
View Full Code Here

    if (params.isVarArgs() && formals.size() > 0) {
      // deal with varags, change the last argument to a list if its not a pattern
      Expression last = formals.get(formals.size() - 1);
      if (last.isTypedVariable()) {
        org.rascalmpl.ast.Type oldType = last.getType();
        ISourceLocation origin = last.getLocation();
        Structured newType = ASTBuilder.make("Type","Structured", origin, ASTBuilder.make("StructuredType",origin, ASTBuilder.make("BasicType","List", origin), Arrays.asList(ASTBuilder.make("TypeArg","Default", origin,oldType))));
        last = ASTBuilder.make("Expression","TypedVariable",origin, newType, last.getName());
        formals = replaceLast(formals, last);
      }
      else if (last.isQualifiedName()) {
        ISourceLocation origin = last.getLocation();
        org.rascalmpl.ast.Type newType = ASTBuilder.make("Type","Structured",origin, ASTBuilder.make("StructuredType",origin, ASTBuilder.make("BasicType","List", origin), Arrays.asList(ASTBuilder.make("TypeArg",origin, ASTBuilder.make("Type","Basic", origin, ASTBuilder.make("BasicType","Value", origin))))));
        last = ASTBuilder.makeExp("TypedVariable", origin, newType, Names.lastName(last.getQualifiedName()));
        formals = replaceLast(formals, last);
      }
      else {
View Full Code Here

  public static boolean isRascalLexical(IConstructor tree) {
    return SymbolAdapter.isLex(getType(tree));
  }

  public static IConstructor locateDeepestContextFreeNode(IConstructor tree, int offset) {
    ISourceLocation l = TreeAdapter.getLocation(tree);
 
    if (l == null) {
      throw new IllegalArgumentException(
          "locate assumes position information on the tree");
    }
 
    if (TreeAdapter.isLexical(tree)) {
      if (l.getOffset() <= offset
          && offset < l.getOffset() + l.getLength()) {
        return tree;
      }
 
      return null;
    }
 
    if (TreeAdapter.isAmb(tree)) {
      return null;
    }
 
    if (TreeAdapter.isAppl(tree)) {
      IList children = TreeAdapter.getASTArgs(tree);
 
      for (IValue child : children) {
        ISourceLocation childLoc = TreeAdapter
            .getLocation((IConstructor) child);
 
        if (childLoc == null) {
          continue;
        }
 
        if (childLoc.getOffset() <= offset
            && offset < childLoc.getOffset() + childLoc.getLength()) {
          IConstructor result = locateDeepestContextFreeNode((IConstructor) child,
              offset);
 
          if (result != null) {
            return result;
View Full Code Here

  @Override
  public <U extends IValue> Result<U> fieldAccess(String name, TypeStore store) {
    IValueFactory vf = getValueFactory();
    TypeFactory tf = getTypeFactory();

    ISourceLocation value = getValue();
    String stringResult = null;
    Integer intResult = null;
    Integer tupleA = null;
    Integer tupleB = null;
    switch (name) {
    case "scheme":
      stringResult = value.getScheme();
      break;

    case "authority":
      stringResult = value.hasAuthority() ? value.getAuthority() : "";
      break;

    case "host":
    case "user":
    case "port":
      URI uri = value.getURI();
      if (!ctx.getResolverRegistry().supportsHost(uri)) {
        throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the " + name + " field, use authority instead.", tf.sourceLocationType(), ctx.getCurrentAST());
      }
      if (name.equals("host")) {
        stringResult = uri.getHost();
      }
      else if (name.equals("user")) {
        stringResult = uri.getUserInfo();
      }
      else {
        intResult = uri.getPort();
      }
      if (stringResult == null && intResult == null) {
        stringResult = "";
      }
      break;

    case "path":
      stringResult = value.hasPath() ? value.getPath() : "";
      break;

    case "query":
      stringResult = value.hasQuery() ? value.getQuery() : "";
      break;

    case "fragment":
      stringResult = value.hasFragment() ? value.getFragment() : "";
      break;

    case "length":
      if (value.hasOffsetLength()) {
        intResult = value.getLength();
        break;
      }
      throw RuntimeExceptionFactory.unavailableInformation(ctx.getCurrentAST(), ctx.getStackTrace());

    case "offset":
      if (value.hasOffsetLength()) {
        intResult = value.getOffset();
        break;
      }
      throw RuntimeExceptionFactory.unavailableInformation(ctx.getCurrentAST(), ctx.getStackTrace());

    case "begin":
      if (value.hasLineColumn()) {
        tupleA = value.getBeginLine();
        tupleB = value.getBeginColumn();
        break;
      }
      throw RuntimeExceptionFactory.unavailableInformation(ctx.getCurrentAST(), ctx.getStackTrace());

    case "end":
      if (value.hasLineColumn()) {
        tupleA = value.getEndLine();
        tupleB = value.getEndColumn();
        break;
      }
      throw RuntimeExceptionFactory.unavailableInformation(ctx.getCurrentAST(), ctx.getStackTrace());
   
    case "uri":
      stringResult = value.getURI().toString();
      break;

    case "top":
      return makeResult(tf.sourceLocationType(), vf.sourceLocation(value.getURI()), ctx);

    // now the calculated fields
    case "parent": {
      String path = value.hasPath() ? value.getPath() : "";
      if (path.equals("") || path.equals("/")) {
        throw RuntimeExceptionFactory.noParent(getValue(), ctx.getCurrentAST(), ctx.getStackTrace());
      }
      // remove one or more /'s at the end
      if (path.endsWith("/")) {
        path = path.substring(0, path.length() -1);
      }
      int i = path.lastIndexOf((int)'/');
      if (i != -1) {
        path = path.substring(0, i);
        if (value.getScheme().equalsIgnoreCase("file")) {
          // there is a special case for file references to windows paths.
          // the root path should end with a / (c:/ not c:)
          if (path.lastIndexOf((int)'/') == 0 && path.endsWith(":")) {
            path += "/";
          }
        }
        return fieldUpdate("path", makeResult(tf.stringType(), vf.string(path), ctx), store);
      }
      throw RuntimeExceptionFactory.noParent(getValue(), ctx.getCurrentAST(), ctx.getStackTrace());
    }

    case "file": {
      String path = value.hasPath() ? value.getPath() : "";
     
      if (path.equals("")) {
        throw RuntimeExceptionFactory.noParent(getValue(), ctx.getCurrentAST(), ctx.getStackTrace());
      }
      int i = path.lastIndexOf((int)'/');
     
      if (i != -1) {
        stringResult = path.substring(i+1);
      }
      else {
        stringResult = path;
      }
      break;
    }

    case "ls": {
      try {
        ISourceLocation resolved = ctx.getHeap().resolveSourceLocation(value);
        if (!ctx.getResolverRegistry().isDirectory(resolved.getURI())) {
          throw RuntimeExceptionFactory.io(vf.string("You can only access ls on a directory, or a container."), ctx.getCurrentAST(), ctx.getStackTrace());
        }
        Result<IValue> resRes = makeResult(getType(), resolved, ctx);

        IListWriter w = ctx.getValueFactory().listWriter();
        Type stringType = tf.stringType();

        for (String elem : ctx.getResolverRegistry().listEntries(resolved.getURI())) {
          w.append(resRes.add(makeResult(stringType, vf.string(elem), ctx)).getValue());
        }

        IList result = w.done();
        // a list of loc's
View Full Code Here

    throw new RuntimeException("A case not handled? " + name);
  }

  @Override
  public <U extends IValue, V extends IValue> Result<U> fieldUpdate(String name, Result<V> repl, TypeStore store) {
    ISourceLocation loc = getValue();
    int iLength = loc.hasOffsetLength() ? loc.getLength() : -1;
    int iOffset = loc.hasOffsetLength() ? loc.getOffset() : -1;
    int iBeginLine = loc.hasLineColumn() ? loc.getBeginLine() : -1;
    int iBeginColumn = loc.hasLineColumn() ? loc.getBeginColumn() : -1;
    int iEndLine = loc.hasLineColumn() ? loc.getEndLine() : -1;
    int iEndColumn = loc.hasLineColumn() ? loc.getEndColumn() : -1;
    //URI uri = loc.getURI();
    boolean uriPartChanged = false;
    String scheme = loc.getScheme();
    String authority = loc.hasAuthority() ? loc.getAuthority() : null;
    String path = loc.hasPath() ? loc.getPath() : null;
    String query = loc.hasQuery() ? loc.getQuery() : null;
    String fragment = loc.hasFragment() ? loc.getFragment() : null;
   

    Type replType = repl.getType();
    IValue replValue = repl.getValue();

    try {
      String newStringValue = null;
      if (replType.isString()) {
        newStringValue = ((IString)replValue).getValue();
      }
      if (name.equals("uri")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        URI uri = URIUtil.createFromEncoded(newStringValue);
        // now destruct it again
        scheme = uri.getScheme();
        authority = uri.getAuthority();
        path = uri.getPath();
        query = uri.getQuery();
        fragment = uri.getFragment();
        uriPartChanged = true;
      }
      else if (name.equals("scheme")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        scheme = newStringValue;
        uriPartChanged = true;
      }
      else if (name.equals("authority")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        authority = newStringValue;
        uriPartChanged = true;
      }
      else if (name.equals("host")) {
        URI uri = value.getURI();
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        if (!ctx.getResolverRegistry().supportsHost(uri)) {
          throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the host field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
        }
        uri = URIUtil.changeHost(uri, newStringValue);
        authority = uri.getAuthority();
        uriPartChanged = true;
      }
      else if (name.equals("path")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        path = newStringValue;
        uriPartChanged = true;
      }
      else if (name.equals("file")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        int i = path.lastIndexOf("/");
       
        if (i != -1) {
          path = path.substring(0, i) + "/" + newStringValue;
        }
        else {
          path = path + "/" + newStringValue; 
        }
        uriPartChanged = true;
      }
      else if (name.equals("parent")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
       
        int i = path.lastIndexOf("/");
        String parent = newStringValue;
       
        if (!parent.startsWith("/")) {
          parent = "/" + parent;
        }
        if (i != -1) {
          path =parent + path.substring(i);
        }
        else {
          path = parent;
        }
        uriPartChanged = true;
      }
      else if (name.equals("ls")) {
        throw new UnsupportedOperation("can not update the children of a location", ctx.getCurrentAST());
      }
      else if (name.equals("extension")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        String ext = newStringValue;
       
        if (path.length() > 1) {
          int index = path.lastIndexOf('.');

          if (index == -1 && !ext.isEmpty()) {
            path = path + (!ext.startsWith(".") ? "." : "") + ext;
          }
          else if (!ext.isEmpty()) {
            path = path.substring(0, index) + (!ext.startsWith(".") ? "." : "") + ext;
          }
          else {
            path = path.substring(0, index);
          }
        }
        uriPartChanged = true;
      }
      else if (name.equals("top")) {
        if (replType.isString()) {
          URI uri = URIUtil.assumeCorrect(newStringValue);
          scheme = uri.getScheme();
          authority = uri.getAuthority();
          path = uri.getPath();
          query = uri.getQuery();
          fragment = uri.getFragment();
        }
        else if (replType.isSourceLocation()) {
          ISourceLocation rep = ((ISourceLocation) repl.getValue());
          scheme = rep.getScheme();
          authority = rep.hasAuthority() ? rep.getAuthority() : null;
          path = rep.hasPath() ? rep.getPath() : null;
          query = rep.hasQuery() ? rep.getQuery() : null;
          fragment = rep.hasFragment() ? rep.getFragment() : null;
        }
        else {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        uriPartChanged = true;
      }
      else if (name.equals("fragment")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        fragment = newStringValue;
        uriPartChanged = true;
      }
      else if (name.equals("query")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        query= newStringValue;
        uriPartChanged = true;
      }
      else if (name.equals("user")) {
        if (!replType.isString()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
        URI uri = loc.getURI();
        if (!ctx.getResolverRegistry().supportsHost(uri)) {
          throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the user field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
        }
        if (uri.getHost() != null) {
          uri = URIUtil.changeUserInformation(uri, newStringValue);
        }
        authority = uri.getAuthority();
        uriPartChanged = true;
      }
      else if (name.equals("port")) {
        if (!replType.isInteger()) {
          throw new UnexpectedType(getTypeFactory().stringType(), replType, ctx.getCurrentAST());
        }
       
        URI uri = loc.getURI();
        if (!ctx.getResolverRegistry().supportsHost(uri)) {
          throw new UndeclaredField(name, "The scheme " + uri.getScheme() + " does not support the port field, use authority instead.", getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
        }
        if (uri.getHost() != null) {
          int port = Integer.parseInt(((IInteger) repl.getValue()).getStringRepresentation());
          uri = URIUtil.changePort(uri, port);
        }
        authority = uri.getAuthority();
        uriPartChanged = true;
      }
      else if (name.equals("length")){
        if (!replType.isInteger()) {
          throw new UnexpectedType(getTypeFactory().integerType(), replType, ctx.getCurrentAST());
        }
        iLength = ((IInteger) replValue).intValue();
       
        if (iLength < 0) {
          throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
        }
      }
      else if (name.equals("offset")){
        if (!replType.isInteger()) {
          throw new UnexpectedType(getTypeFactory().integerType(), replType, ctx.getCurrentAST());
        }
        iOffset = ((IInteger) replValue).intValue();
       
        if (iOffset < 0) {
          RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
        }
      }
      else if (name.equals("begin")) {
        if (!replType.isSubtypeOf(intTuple)) {
          throw new UnexpectedType(intTuple, replType, ctx.getCurrentAST());
        }
        iBeginLine = ((IInteger) ((ITuple) replValue).get(0)).intValue();
        iBeginColumn = ((IInteger) ((ITuple) replValue).get(1)).intValue();
       
        if (iBeginColumn < 0 || iBeginLine < 0) {
          throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
        }
      }
      else if (name.equals("end")) {
        if (!replType.isSubtypeOf(intTuple)) {
          throw new UnexpectedType(intTuple, replType, ctx.getCurrentAST());
        }
        iEndLine = ((IInteger) ((ITuple) replValue).get(0)).intValue();
        iEndColumn = ((IInteger) ((ITuple) replValue).get(1)).intValue();
       
        if (iEndLine < 0 || iEndColumn < 0) {
          throw RuntimeExceptionFactory.illegalArgument(replValue, ctx.getCurrentAST(), ctx.getStackTrace());
        }
      }
      else {
        // TODO: is this the right exception? How so "undeclared"?
        throw new UndeclaredField(name, getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
      }
     
      ISourceLocation newLoc = loc;
      if (uriPartChanged) {
        newLoc = getValueFactory().sourceLocation(scheme, authority, path, query, fragment);
      }
      if (loc.hasLineColumn()) {
        // was a complete loc, and thus will be now
View Full Code Here

    return that.lessThanSourceLocation(this);
  }
 
  @Override
  protected LessThanOrEqualResult lessThanOrEqualSourceLocation(SourceLocationResult that) {
    ISourceLocation left = that.getValue();
    ISourceLocation right = this.getValue();
   
    int compare = left.getURI().toString().compareTo(right.getURI().toString());
    if (compare < 0) {
      return new LessThanOrEqualResult(true, false, ctx);
    }
    else if (compare > 0) {
      return new LessThanOrEqualResult(false, false, ctx);
    }

    // but the uri's are the same
    // note that line/column information is superfluous and does not matter for ordering
   
    if (left.hasOffsetLength()) {
      if (!right.hasOffsetLength()) {
        return new LessThanOrEqualResult(false, false, ctx);
      }

      int roffset = right.getOffset();
      int rlen = right.getLength();
      int loffset = left.getOffset();
      int llen = left.getLength();

      if (loffset == roffset) {
        return new LessThanOrEqualResult(llen < rlen, llen == rlen, ctx);
      }

      return new LessThanOrEqualResult(roffset < loffset && roffset + rlen >= loffset + llen, false, ctx);
    }
    else if (compare == 0) {
      return new LessThanOrEqualResult(false, true, ctx);
    }

    if (!right.hasOffsetLength()) {
      throw new ImplementationError("assertion failed");
    }

    return new LessThanOrEqualResult(false, false, ctx);
  }
View Full Code Here

  }
 
  protected int compareSourceLocationInt(SourceLocationResult that) {
    // Note args have already been reversed.
   
    ISourceLocation left = this.getValue();
    ISourceLocation right = that.getValue();
    if (left.isEqual(right)) {
      return 0;
    }
   
    // they are not the same
    int compare = left.getURI().toString().compareTo(right.getURI().toString());
    if (compare != 0) {
      return compare;
    }
   
    // but the uri's are the same
    // note that line/column information is superfluous and does not matter for ordering
   
    if (left.hasOffsetLength()) {
      if (!right.hasOffsetLength()) {
        return 1;
      }
      int roffset = right.getOffset();
      int rlen = right.getLength();
      int loffset = left.getOffset();
      int llen = left.getLength();
      if(loffset == roffset){
        return (llen < rlen) ? -1 : ((llen == rlen) ? 0 : 1);
      }
      if(roffset < loffset && roffset + rlen >= loffset + llen)
        return -1;
      else
        return 1;
    }
   
    if (!right.hasOffsetLength()) {
      throw new ImplementationError("assertion failed");
    }
   
    return -1;
  }
View Full Code Here

    Integer nlocals = ((IInteger) declaration.get("nlocals")).intValue();
    IMap localNames = ((IMap) declaration.get("localNames"));
    Integer nformals = ((IInteger) declaration.get("nformals")).intValue();
    Integer maxstack = ((IInteger) declaration.get("maxStack")).intValue();
    IList code = (IList) declaration.get("instructions");
    ISourceLocation src = (ISourceLocation) declaration.get("src");
    CodeBlock codeblock = new CodeBlock(vf);
    // Loading instructions
    try {
    for (int i = 0; i < code.length(); i++) {
      IConstructor instruction = (IConstructor) code.get(i);
View Full Code Here

        String nested = "";
        URI uri = URIUtil.assumeCorrect("tmp:///");
       
        if (stRandom.nextBoolean()) {
          RandomValueTypeVisitor visitor = descend();
          ISourceLocation loc = (ISourceLocation) visitor.generate(type);
          uri = loc.getURI();
          nested = uri.getPath();
        }
       
        path = path.startsWith("/") ? path : "/" + path;
        uri = URIUtil.changePath(uri, nested.length() > 0 && !nested.equals("/") ? nested + path : path);
View Full Code Here

TOP

Related Classes of org.eclipse.imp.pdb.facts.ISourceLocation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.