Package org.rascalmpl.interpreter.staticErrors

Examples of org.rascalmpl.interpreter.staticErrors.UndeclaredField


        String fieldName = org.rascalmpl.interpreter.utils.Names
            .name(f.getFieldName());
        try {
          fieldIndices[i] = baseType.getFieldIndex(fieldName);
        } catch (UndeclaredFieldException e) {
          throw new UndeclaredField(fieldName, baseType,
              ctx.getCurrentAST());
        }
      }

      if (fieldIndices[i] < 0 || fieldIndices[i] > baseType.getArity()) {
View Full Code Here


  }
 
  @Override
  public <U extends IValue> Result<U> fieldAccess(String name, TypeStore store) {
      if (!getType().hasFieldNames()) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
     
      if (!getType().hasField(name, store)) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
     
      try {
        int index = getType().getFieldIndex(name);
        Type type = getType().getFieldType(index);
        return makeResult(type, getValue().get(index), ctx);
      }
      catch (UndeclaredFieldException e){
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
  }
View Full Code Here

  }
   
  @Override
  public <U extends IValue, V extends IValue> Result<U> fieldUpdate(String name, Result<V> repl, TypeStore store) {
    if (!getType().hasFieldNames()) {
      throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
    }

    try {
      int index = getType().getFieldIndex(name);
      Type type = getType().getFieldType(index);
      if(!type.isSubtypeOf(repl.getType())){
        throw new UnexpectedType(type, repl.getType(), ctx.getCurrentAST());
      }
      return makeResult(getType(), getValue().set(index, repl.getValue()), ctx);
    } catch (UndeclaredFieldException e) {
      throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
    }
  }
View Full Code Here

    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
        return makeResult(result.getType(), result, ctx);
       
      } catch (IOException e) {
        throw RuntimeExceptionFactory.io(vf.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
      }
     
    }

    case "extension" : {
      String path = value.hasPath() ? value.getPath() : "";
      int i = path.lastIndexOf((int)'.');
      if (i != -1) {
        stringResult = path.substring(i + 1);
      }
      else {
        stringResult = "";
      }
      break;
    }
   
    case "params" : {
      String query = value.hasQuery() ? value.getQuery() : "";
      IMapWriter res = vf.mapWriter(tf.stringType(), tf.stringType());
     
      if (query != null && query.length() > 0) {
        String[] params = query.split("&");
        for (String param : params) {
          String[] keyValue = param.split("=");
          res.put(vf.string(keyValue[0]), vf.string(keyValue[1]));
        }
      }
     
      IMap map = res.done();
      return makeResult(map.getType(), map, ctx);
    }

    default:
      throw new UndeclaredField(name, getTypeFactory().sourceLocationType(), ctx.getCurrentAST());
    }

    if (stringResult != null) {
      return makeResult(tf.stringType(), vf.string(stringResult), ctx);
    }
View Full Code Here

        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);
View Full Code Here

 
  @Override
  public <U extends IValue> Result<U> fieldAccess(String name, TypeStore store) {
    try {
      if (!getType().hasField(name, store) && !getType().hasKeywordParameter(name, store)) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }

      Type nodeType = getValue().getConstructorType();
      if (!nodeType.hasField(name) && !nodeType.hasKeywordParameter(name)) {
        throw RuntimeExceptionFactory.noSuchField(name, ctx.getCurrentAST(), null);
View Full Code Here

  }
 
  @Override
  public <U extends IValue, V extends IValue> Result<U> fieldUpdate(String name, Result<V> repl, TypeStore store) {
    if (!getType().hasField(name, store) && !getValue().getConstructorType().hasKeywordParameter(name)) {
      throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
    }
   
    Type nodeType = getValue().getConstructorType();
    if (!nodeType.hasField(name) && !nodeType.hasKeywordParameter(name)) {
      throw RuntimeExceptionFactory.noSuchField(name, ctx.getCurrentAST(), null);
View Full Code Here

    @Override
    public <U extends IValue> Result<U> fieldAccess(String name, TypeStore store) {
      Type tupleType = getType().getFieldTypes()
     
      if (!getType().hasFieldNames()) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
     
      if (!getType().hasField(name, store)) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
     
      try {
        IListWriter w = getValueFactory().listWriter();
        for (IValue e : getValue()) {
          w.append(((ITuple) e).get(tupleType.getFieldIndex(name)));
        }
        return makeResult(getTypeFactory().listType(tupleType.getFieldType(name)), w.done(), ctx);
      }
      // TODO: why catch this exception here?
      catch (UndeclaredFieldException e) {
        throw new UndeclaredField(name, getType(), ctx.getCurrentAST());
      }
    }
View Full Code Here

          String fieldName = org.rascalmpl.interpreter.utils.Names
              .name(f.getFieldName());
          try {
            fieldIndices[i] = baseType.getFieldIndex(fieldName);
          } catch (UndeclaredFieldException e) {
            throw new UndeclaredField(fieldName, baseType,
                ctx.getCurrentAST());
          }
        }

        if (fieldIndices[i] < 0 || (fieldIndices[i] > baseType.getArity() && !getType().getElementType().isBottom())) {
View Full Code Here

      if (receiver.getType().isTuple()) {

        int idx = receiver.getType().getFieldIndex(label);
        if (idx < 0) {
          throw new UndeclaredField(label, receiver.getType(), this);
        }

        __eval.__setValue(__eval.newResult(((ITuple) receiver
            .getValue()).get(idx), __eval.__getValue()));
        IValue result = ((ITuple) receiver.getValue()).set(idx, __eval
            .__getValue().getValue());
        return __eval.recur(this,
            org.rascalmpl.interpreter.result.ResultFactory
            .makeResult(receiver.getType(), result, __eval
                .__getEval()));
      }
      else if (receiver.getType() instanceof NonTerminalType) {
        Result<IValue> result = receiver.fieldUpdate(label, __eval.__getValue(), __eval.getCurrentEnvt().getStore());

        __eval.__setValue(__eval.newResult(receiver.fieldAccess(label, __eval.getCurrentEnvt().getStore()), __eval
            .__getValue()));

        if (!result.getType().isSubtypeOf(receiver.getType())) {
          throw new UnexpectedType(receiver.getType(), result.getType(), __eval.getCurrentAST());
        }
        return __eval.recur(this, result);
      }
      else if (receiver.getType().isConstructor()
          || receiver.getType().isAbstractData()) {
        IConstructor cons = (IConstructor) receiver.getValue();
        Type node = cons.getConstructorType();

        if (node.hasField(label)) {
          int index = node.getFieldIndex(label);

          if (!__eval.__getValue().getType().isSubtypeOf(
              node.getFieldType(index))) {
            throw new UnexpectedType(node.getFieldType(index),
                __eval.__getValue().getType(), this);
          }
          __eval.__setValue(__eval.newResult(cons.get(index), __eval
              .__getValue()));

          IValue result = cons.set(index, __eval.__getValue().getValue());
          return __eval.recur(this,
              org.rascalmpl.interpreter.result.ResultFactory
              .makeResult(receiver.getType(), result, __eval
                  .__getEval()));
        }
        else if (node.hasKeywordParameter(label)) {
          if (!__eval.__getValue().getType().isSubtypeOf(
              node.getKeywordParameterType(label))) {
            throw new UnexpectedType(node.getKeywordParameterType(label),
                __eval.__getValue().getType(), this);
          }

          __eval.__setValue(__eval.newResult(cons.asWithKeywordParameters().getParameter(label), __eval.__getValue()));

          IValue result = cons.asWithKeywordParameters().setParameter(label,  __eval.__getValue().getValue());
          return __eval.recur(this,
              org.rascalmpl.interpreter.result.ResultFactory
              .makeResult(receiver.getType(), result, __eval
                  .__getEval()));
        }
        else {
          throw new UndeclaredField(label, receiver.getValue().getType(), this);
        }

      } else if (receiver.getType().isSourceLocation()) {
        // ISourceLocation loc = (ISourceLocation) receiver.getValue();

        __eval.__setValue(__eval.newResult(receiver.fieldAccess(label,
            __eval.__getEnv().getStore()), __eval.__getValue()));
        return __eval.recur(this, receiver.fieldUpdate(label, __eval
            .__getValue(), __eval.__getEnv().getStore()));
        // return recur(this, eval.sourceLocationFieldUpdate(loc, label,
        // value.getValue(), value.getType(), this));
      } else if (receiver.getType().isDateTime()) {
        __eval.__setValue(__eval.newResult(receiver.fieldAccess(label,
            __eval.__getEnv().getStore()), __eval.__getValue()));
        return __eval.recur(this, receiver.fieldUpdate(label, __eval
            .__getValue(), __eval.__getEnv().getStore()));
      } else {
        throw new UndeclaredField(label, receiver.getType(), this);
      }

    }
View Full Code Here

TOP

Related Classes of org.rascalmpl.interpreter.staticErrors.UndeclaredField

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.