Package org.eclipse.imp.pdb.facts.type

Examples of org.eclipse.imp.pdb.facts.type.TypeFactory


      }
    }
  }
 
  public IConstructor convertMapToGrammar(IMap definition) {
    TypeFactory TF = TypeFactory.getInstance();
    TypeStore TS = new TypeStore();
    Type Grammar = TF.abstractDataType(TS, "Grammar");
    Type Symbol = TF.abstractDataType(TS, "Symbol");
    Type Production = TF.abstractDataType(TS, "Production");
    Type grammar = TF.constructor(TS, Grammar, "grammar", TF.setType(Symbol), "starts", TF.mapType(Symbol, Production), "rules");

    return vf.constructor(grammar, vf.set(), definition);
  }
View Full Code Here


  private void initMethodAndStatusValues(final IEvaluatorContext ctx) {
    if (methodValues.isEmpty() || statusValues.isEmpty()) {
      Environment env = ctx.getHeap().getModule("util::Webserver");
      Type methodType = env.getAbstractDataType("Method");
      TypeFactory tf = TypeFactory.getInstance();
      methodValues.put(Method.DELETE, vf.constructor(env.getConstructor(methodType, "delete", tf.voidType())));
      methodValues.put(Method.GET, vf.constructor(env.getConstructor(methodType, "get", tf.voidType())));
      methodValues.put(Method.HEAD, vf.constructor(env.getConstructor(methodType, "head", tf.voidType())));
      methodValues.put(Method.POST, vf.constructor(env.getConstructor(methodType, "post", tf.voidType())));
      methodValues.put(Method.PUT, vf.constructor(env.getConstructor(methodType, "put", tf.voidType())));
     
      Type statusType = env.getAbstractDataType("Status");
                       
      statusValues.put(vf.constructor(env.getConstructor(statusType, "ok", tf.voidType())), Status.OK);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "created", tf.voidType())), Status.CREATED);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "accepted", tf.voidType())), Status.ACCEPTED);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "noContent", tf.voidType())), Status.NO_CONTENT);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "partialContent", tf.voidType())), Status.PARTIAL_CONTENT);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "redirect", tf.voidType())), Status.REDIRECT);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "notModified", tf.voidType())), Status.NOT_MODIFIED);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "badRequest", tf.voidType())), Status.BAD_REQUEST);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "unauthorized", tf.voidType())), Status.UNAUTHORIZED);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "forbidden", tf.voidType())), Status.FORBIDDEN);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "notFound", tf.voidType())), Status.NOT_FOUND);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "rangeNotSatisfiable", tf.voidType())), Status.RANGE_NOT_SATISFIABLE);
      statusValues.put(vf.constructor(env.getConstructor(statusType, "internalError", tf.voidType())), Status.INTERNAL_ERROR);
      // yes, we acknowledge our sins
      argTypes[1] = methodType;
    }
  }
View Full Code Here

  private IValue callGenerator(Type type, int depthLimit) {
    if (depthLimit < 0) {
      return null;
    }
    TypeFactory tf = TypeFactory.getInstance();

    ICallableValue generator = generators.get(type);
    Result<IValue> result = generator.call(new Type[] { tf.integerType() },
        new IValue[] { vf.integer(depthLimit) }, null);
    return result.getValue();
  }
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
        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);
    }
    if (intResult != null) {
      return makeResult(tf.integerType(), vf.integer(intResult), ctx);
    }
    if (tupleA != null && tupleB != null) {
      return makeResult(intTuple, vf.tuple(vf.integer(tupleA), vf.integer(tupleB)), ctx);
    }
    throw new RuntimeException("A case not handled? " + name);
View Full Code Here

  /**
   * @return list of built-in color names
   */
  public IList colorNames() {
    TypeFactory types = TypeFactory.getInstance();
    IListWriter w = vf.listWriter(types.stringType());
    String strings[] = new String[colorNames.size()];
    Arrays.sort(colorNames.keySet().toArray(strings));
    for (String s : strings) {
      w.append(vf.string(s));
    }
View Full Code Here

  public void initElem(IFigureConstructionEnv env, MouseOver mparent, boolean swtSeen, boolean visible, NameResolver resolver){
    if(innerFig != null){
      innerFig.destroy(env);
    }
    TypeFactory tf = TypeFactory.getInstance();
    IList originalsL = VF.list(originals.toArray(tmpArray));
    IConstructor figureCons = (IConstructor) env.getCallBackEnv().executeRascalCallBackSingleArgument(whole,tf.listType(tf.valueType()),originalsL).getValue();
    innerFig = FigureFactory.make(env, figureCons, prop, childProps);
    innerFig.registerIds(env, resolver);
    innerFig.registerConverts(resolver);
    setInnerFig( innerFig);
    prop.stealExternalPropertiesFrom(innerFig.prop);
View Full Code Here

  public List<Type> getOptionalQueryParameterTypes() {
    return new ArrayList<Type>();
  }

  protected Type makeOptionalParameterType(Type t) {
    TypeFactory tf = TypeFactory.getInstance();
    TypeStore ts = new TypeStore();

    Type paramType = tf.parameterType("T");
    Type adtType = tf.abstractDataType(ts, "Option", paramType);
    Map<Type,Type> bindings = new HashMap<Type,Type>();
    bindings.put(paramType, t);
    adtType = adtType.instantiate(bindings);

    return adtType;
View Full Code Here

 
  @SuppressWarnings("deprecation")
  M3Converter(final TypeStore typeStore) {
    super(typeStore, true);
    this.DATATYPE_M3_NODE_TYPE = this.typeStore.lookupAbstractDataType(DATATYPE_M3_NODE);
    TypeFactory tf = TypeFactory.getInstance();
    this.CONSTRUCTOR_M3= this.typeStore.lookupConstructor(DATATYPE_M3_NODE_TYPE, "m3", tf.tupleType(tf.sourceLocationType()));
    this.DATATYPE_TYPESYMBOL = this.typeStore.lookupAbstractDataType("TypeSymbol");
    uses = values.relationWriter(m3TupleType);
    declarations = values.relationWriter(m3TupleType);
    containment = values.relationWriter(m3TupleType);
    extendsRelations = values.relationWriter(m3TupleType);
View Full Code Here

    }
    return elems;
  }

  private static IList convertToRealList(double[] l, IValueFactory vf) {
    TypeFactory tf = TypeFactory.getInstance();
    IListWriter writer = vf.listWriter(tf.realType());
    for (int i = 0; i < l.length; i++) {
      writer.append(vf.real(l[i]));
    }
    return writer.done();
  }
View Full Code Here

   
    return TypeFactory.getInstance().tupleType(kwTypes, kwLabels);
  }
 
  public void declareConstructor(Data x, Environment env) {
    TypeFactory tf = TypeFactory.getInstance();

    // needs to be done just in case the declaration came
    // from a shell instead of from a module
    Type adt = declareAbstractDataType(x.getUser(), env);

    List<KeywordFormal> common = x.getCommonKeywordParameters().isPresent()
        ? x.getCommonKeywordParameters().getKeywordFormalList()
            : Collections.<KeywordFormal>emptyList();
   
    for (Variant var : x.getVariants()) {
      String altName = Names.name(var.getName());

      Type kwType = tf.voidType();
     
      if (var.isNAryConstructor()) {
        List<KeywordFormal> local = var.getKeywordArguments().hasKeywordFormalList() ? var.getKeywordArguments().getKeywordFormalList() : Collections.<KeywordFormal>emptyList();
        List<KeywordFormal> kws = new ArrayList<>(common.size() + local.size());
       
        if (var.getKeywordArguments().isDefault()) {
          kws.addAll(common);
          kws.addAll(local);
         
          kwType = computeKeywordParametersType(kws, eval);
        }
        else if (!common.isEmpty()) {
          kwType = computeKeywordParametersType(common, eval);
          kws = common;
        }

        List<TypeArg> args = var.getArguments();
        int nAllArgs = args.size();
       
        Type[] fields = new Type[nAllArgs];
        String[] labels = new String[nAllArgs];

        for (int i = 0; i < args.size(); i++) {
          TypeArg arg = args.get(i);
          fields[i] = arg.getType().typeOf(env, true, eval);

          if (fields[i] == null) {
            throw new UndeclaredType(arg.hasName() ? Names.name(arg.getName()) : "?", arg);
          }

          if (arg.hasName()) {
            labels[i] = Names.name(arg.getName());
          } else {
            labels[i] = "arg" + java.lang.Integer.toString(i);
          }
        }
       
        Type children = tf.tupleType(fields, labels);
       
        try {
          ConstructorFunction cons = env.constructorFromTuple(var, eval, adt, altName, children, kwType, kws);
          cons.setPublic(true); // TODO: implement declared visibility
        } catch (org.eclipse.imp.pdb.facts.exceptions.RedeclaredConstructorException e) {
View Full Code Here

TOP

Related Classes of org.eclipse.imp.pdb.facts.type.TypeFactory

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.