Package org.eclipse.imp.pdb.facts

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


       
        @Override
        public IConstructor visitTreeAppl(IConstructor tree)  {
          String cons = TreeAdapter.getConstructorName(tree);
          if (cons == null || !cons.equals("$MetaHole") ) {
            IListWriter w = eval.getValueFactory().listWriter();
            IList args = TreeAdapter.getArgs(tree);
            for (IValue elem : args) {
              w.append(elem.accept(this));
            }
            args = w.done();
           
            return TreeAdapter.setArgs(tree, args);
          }
         
          IConstructor type = retrieveHoleType(tree);
          return antiquotes.get(TreeAdapter.yield(tree)).asAnnotatable().setAnnotation("holeType", type);
        }
       
        private IConstructor retrieveHoleType(IConstructor tree) {
          IConstructor prod = TreeAdapter.getProduction(tree);
          ISet attrs = ProductionAdapter.getAttributes(prod);

          for (IValue attr : attrs) {
            if (((IConstructor) attr).getConstructorType() == Factory.Attr_Tag) {
              IValue arg = ((IConstructor) attr).get(0);
             
              if (arg.getType().isNode() && ((INode) arg).getName().equals("holeType")) {
                return (IConstructor) ((INode) arg).get(0);
              }
            }
          }
         
          throw new ImplementationError("expected to find a holeType, but did not: " + tree);
        }

        @Override
        public IConstructor visitTreeAmb(IConstructor arg)  {
          ISetWriter w = vf.setWriter();
          for (IValue elem : TreeAdapter.getAlternatives(arg)) {
            w.insert(elem.accept(this));
          }
          return arg.set("alternatives", w.done());
        }
      });
  }
View Full Code Here


    IConstructor root = convertElement(doc.getRootElement(), trim);
    return vf.constructor(Factory.Node_document, root);
  }

  private IConstructor convertElement(Element e, boolean trim) {
    IListWriter kids = vf.listWriter(Factory.Node);
    for (Object o: e.getAttributes()) {
      Attribute attr = (Attribute)o;
      IString key = vf.string(attr.getName());
      IString val = vf.string(attr.getValue());

      kids.insert(vf.constructor(Factory.Node_attribute, convertNamespace(attr.getNamespace()), key, val));
    }

    int len = e.getContentSize();
    for (int i = 0; i < len; i++) {
      try {
        kids.append(convertContent(e.getContent(i), trim));
      }
      catch (Skip c) { // Ugh, terrible, but I'm in hurry
        continue;
      }
    }
   
    IString name = vf.string(e.getName());
    return vf.constructor(Factory.Node_element, convertNamespace(e.getNamespace()), name, kids.done());
  }
View Full Code Here

  public <U extends IValue, V extends IValue> Result<U> slice(Result<?> first, Result<?> second, Result<?> end) {
    return super.slice(first, second, end, getValue().length());
  }
 
  public Result<IValue> makeSlice(int first, int second, int end){
    IListWriter w = getValueFactory().listWriter();
    int increment = second - first;
    if (first == end || increment == 0) {
      // nothing to be done
    } else if (first <= end) {
      if (increment == 1) {
        return makeResult(TypeFactory.getInstance().listType(getType().getElementType()), getValue().sublist(first, end - first), ctx);
      }
      else {
        for (int i = first; i >= 0 && i < end; i += increment) {
          w.append(getValue().get(i));
        }
      }
    } else {
      for (int j = first; j >= 0 && j > end && j < getValue().length(); j += increment) {
        w.append(getValue().get(j));
      }
    }
    return makeResult(TypeFactory.getInstance().listType(getType().getElementType()), w.done(), ctx);
  }
View Full Code Here

  public IValue listEntries(ISourceLocation sloc, RascalExecutionContext rex) {
    sloc = rex.resolveSourceLocation(sloc);

    try {
      java.lang.String [] entries = rex.getResolverRegistry().listEntries(sloc.getURI());
      IListWriter w = values.listWriter();
      for(java.lang.String entry : entries){
        w.append(values.string(entry));
      }
      return w.done();
    } catch(FileNotFoundException e){
      throw RuntimeExceptionFactory.pathNotFound(sloc, null, null);
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    }
View Full Code Here

        throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
      }
    }
 
  private IList consumeInputStreamLines(ISourceLocation sloc,  Reader stream, RascalExecutionContext rex ) {
    IListWriter w = values.listWriter();

    BufferedReader in = null;
    try{
      in = new BufferedReader(stream);
      java.lang.String line;

      int i = 0;
      //      int offset = sloc.getOffset();
      int beginLine = sloc.hasLineColumn() ? sloc.getBeginLine() : -1;
      int beginColumn = sloc.hasLineColumn() ? sloc.getBeginColumn() : -1;
      int endLine = sloc.hasLineColumn() ? sloc.getEndLine() : -1;
      int endColumn = sloc.hasLineColumn() ? sloc.getEndColumn() : -1;

      do{
        line = in.readLine();
        i++;
        if(line != null){
          if(!sloc.hasOffsetLength()){
            w.append(values.string(line));
          }else{
            if(!sloc.hasLineColumn()){
              endColumn = line.length();
            }
            if(i == beginLine){
              if(i == endLine){
                w.append(values.string(line.substring(beginColumn, Math.min(endColumn, line.length()))));
              }else{
                w.append(values.string(line.substring(beginColumn)));
              }
            }else if(i > beginLine){
              if(i == endLine){
                w.append(values.string(line.substring(0, Math.min(endColumn, line.length()))));
              }
              else if(i < endLine){
                w.append(values.string(line));
              }
            }
          }
        }
      }while(line != null);
    }catch(IOException e){
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    }finally{
      if(in != null){
        try{
          in.close();
        }catch(IOException ioex){
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }

    return w.done();
  }
View Full Code Here

    return w.done();
  }
 
  public IList readFileBytes(ISourceLocation sloc, RascalExecutionContext rex){
    IListWriter w = values.listWriter();
    sloc = rex.resolveSourceLocation(sloc);
   
    BufferedInputStream in = null;
    try{
      InputStream stream = rex.getResolverRegistry().getInputStream(sloc.getURI());
      in = new BufferedInputStream(stream);
      int read;
      final int size = 256;
      byte bytes[] = new byte[size];
     
      do{
        read = in.read(bytes);
        for (int i = 0; i < read; i++) {
          w.append(values.integer(bytes[i] & 0xff));
        }
      }while(read != -1);
    }catch(FileNotFoundException e){
      throw RuntimeExceptionFactory.pathNotFound(sloc, null, null);
    }catch(IOException e){
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    }finally{
      if(in != null){
        try{
          in.close();
        }catch(IOException ioex){
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }

    return w.done();
  }
View Full Code Here

    INumber zero = diff.subtract(diff); // zero in the type that we're dealing with.

    // Use declared types here
    Type resultType = second.getType().lub(from.getType().lub(to.getType()));
   
    IListWriter w = vf.listWriter();
    if (iFrom.lessEqual(iTo).getValue() && diff.greater(zero).getValue()) {
      do {
        w.append(iFrom);
        iFrom = iFrom.add(diff);
        if (ctx.isInterrupted()) throw new InterruptException(ctx.getStackTrace(), ctx.getCurrentAST().getLocation());
      } while (iFrom.lessEqual(iTo).getValue());
    }
    else if (iFrom.greaterEqual(iTo).getValue() && diff.less(zero).getValue()) {
      do {
        w.append(iFrom);
        iFrom = iFrom.add(diff);
      } while (iFrom.greaterEqual(iTo).getValue());
    }
    return makeResult(tf.listType(resultType), w.done(), ctx)
  }
View Full Code Here

      }
    }
  }
 
  private IList extractPath(IValue start, IValue u){
    IListWriter w = values.listWriter();
   
    if(!start.isEqual(u)){
      w.insert(u);
      while(!pred.get(u).isEqual(start)){
        u = pred.get(u);
        w.insert(u);
      }
      // TODO Check if a path was found at all; it could be that we just hit the root of the graph.
    }
    w.insert(start);
    return w.done();
  }
View Full Code Here

    }
  }

  @Deprecated
  public IValue readFile(IString filename){
    IListWriter w = values.listWriter(types.stringType());
   
    BufferedReader in = null;
    try{
      in = new BufferedReader(new FileReader(filename.getValue()));
      java.lang.String line;

      do {
        line = in.readLine();
        if(line != null){
          w.append(values.string(line));
        }
      } while (line != null);
    }catch(FileNotFoundException fnfex){
      throw RuntimeExceptionFactory.pathNotFound(values.sourceLocation(filename.getValue()), null, null);
    }catch(IOException ioex){
      throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
    }finally{
      if(in != null){
        try{
          in.close();
        }catch(IOException ioex){
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
    return w.done();
  }
View Full Code Here

       
        // 2: push-down deferred production names.
        Set<Integer> pushdownPositions = getChildProductionPositionsForPushdown(prod);       
                     
        // update children by recursively applying this visitor.
        IListWriter writer = VF.listWriter(Factory.Args.getElementType());

        Iterator<IValue> iter = TreeAdapter.getArgs(arg).iterator();
        for (Integer pos = 0; iter.hasNext(); pos++) {
         
          boolean isDeferred = pushdownPositions.contains(pos) || addBreakable && isList;
         
          IValue oldKid = iter.next();
          IValue newKid = oldKid.accept(new PushDownTreeVisitor<E>(isDeferred));
         
          writer.append(newKid);
        }

        IList children = writer.done();

        // update current tree with processed children
        arg = TreeAdapter.setArgs(arg, children);
      }
   
View Full Code Here

TOP

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

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.