Package org.eclipse.imp.pdb.facts

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


    return result;
  }

  private IValue traverseADTOnce(IValue subject, CaseBlockList casesOrRules,
      DIRECTION direction, PROGRESS progress, FIXEDPOINT fixedpoint, TraverseResult tr) {
    IConstructor cons = (IConstructor)subject;
   
    Map<String, IValue> kwParams = null;
    if (cons.mayHaveKeywordParameters() && cons.asWithKeywordParameters().hasParameters()) {
      kwParams = new HashMap<>();
    }
    if (cons.arity() == 0 && kwParams == null) {
      return subject; // constants have no children to traverse into
    }

    if (casesOrRules.hasAllConcretePatternCases() && TreeAdapter.isChar(cons)) {
        return subject; // we dont traverse into the structure of literals and characters
    }

    IValue args[] = new IValue[cons.arity()];

    if (casesOrRules.hasAllConcretePatternCases() && TreeAdapter.isAppl(cons)){
      // Constructor is "appl": we are dealing with a syntax tree
      // - Lexical or literal are returned immediately

      if (TreeAdapter.isLexical(cons)|| TreeAdapter.isLiteral(cons)){
        return subject; // we dont traverse into the structure of literals, lexicals, and characters
      }
     
      // Otherwise:
      // - Copy prod node verbatim to result
      // - Only visit non-layout nodes in argument list
      args[0] = cons.get(0);
      IList list = (IList) cons.get(1);
      int len = list.length();

      if (len > 0) {
        IListWriter w = eval.getValueFactory().listWriter(list.getType().getElementType());
        boolean hasChanged = false;
        boolean hasMatched = false;

        for (int i = 0; i < len; i++){
          IValue elem = list.get(i);
          if (i % 2 == 0) { // Recursion to all non-layout elements
            tr.changed = false;
            tr.matched = false;
            w.append(traverseOnce(elem, casesOrRules, direction, progress, fixedpoint, tr));
            hasChanged |= tr.changed;
            hasMatched |= tr.matched;
          } else { // Just copy layout elements
            w.append(list.get(i));
          }
        }
        tr.changed = hasChanged;
        tr.matched = hasMatched;
        args[1] = w.done();
      } else {
        args[1] = list;
      }
    } else {
      // Constructor is not "appl", or at least one of the patterns is not a concrete pattern
      boolean hasChanged = false;
      boolean hasMatched = false;
      for (int i = 0; i < cons.arity(); i++){
        IValue child = cons.get(i);
        tr.matched = false;
        tr.changed = false;
        args[i] = traverseOnce(child, casesOrRules, direction, progress, fixedpoint, tr);
        hasChanged |= tr.changed;
        hasMatched |= tr.matched;
      }
      if (kwParams != null) {
        IWithKeywordParameters<? extends INode> consKw = cons.asWithKeywordParameters();
        for (String kwName : consKw.getParameterNames()) {
          IValue val = consKw.getParameter(kwName);
          tr.changed = false;
          tr.matched = false;
          IValue newVal = traverseOnce(val, casesOrRules, direction, progress, fixedpoint, tr);
          kwParams.put(kwName, newVal);
          hasChanged |= tr.changed;
          hasMatched |= tr.matched;
        }
      }
      tr.matched = hasMatched;
      tr.changed = hasChanged;

     
    }

    if (tr.changed) {
      IConstructor rcons;
     
      try {
        QualifiedName n = Names.toQualifiedName(cons.getType().getName(), cons.getName(), null);
        rcons = (IConstructor) eval.call(n, kwParams != null ? kwParams : Collections.<String,IValue>emptyMap(), args);
      }
      catch (UndeclaredFunction | UndeclaredModule | ArgumentsMismatch e) {
        // This may happen when visiting data constructors dynamically which are not
        // defined in the current scope. For example, when data was serialized and the format
        // has changed in the meantime. We issue a warning, because it is indicative of a bug
        // and normalizing "rewrite rules" will not trigger at all, but we can gracefully continue
        // because we know what the tree looked like before we started visiting.
        eval.warning("In visit: " + e.getMessage(), eval.getCurrentAST().getLocation());
        if (kwParams != null) {
          rcons = (IConstructor) eval.getValueFactory().constructor(cons.getConstructorType(),  args, kwParams);
        }
        else {
          rcons = (IConstructor) eval.getValueFactory().constructor(cons.getConstructorType(),  args);
        }
      }
     
      if (!cons.mayHaveKeywordParameters() && cons.asAnnotatable().hasAnnotations()) {
        rcons = rcons.asAnnotatable().setAnnotations(cons.asAnnotatable().getAnnotations());
      }

      return rcons;
    }
    else {
View Full Code Here


      return arg;
    }
   
    @Override
    public IConstructor visitTreeAppl(IConstructor arg) throws E {
      IConstructor prod = TreeAdapter.getProduction(arg);
     
      if (TreeAdapter.isAppl(arg)
          && !ProductionAdapter.isLayout(prod)
          && !ProductionAdapter.isLiteral(prod)
          && !ProductionAdapter.isCILiteral(prod)
View Full Code Here

      String[] result = {};
      for (IValue attributeValue : attributes) {
       
        if (attributeValue.getType().isAbstractData() && !attributeValue.getType().isBottom()) {
          IConstructor attributeConstructor = (IConstructor)attributeValue;

          if (attributeConstructor.getName().equals("tag")) {
       
            for (IValue childValue : attributeConstructor.getChildren()) {
              INode childNode = (INode)childValue;

              // non-empty breakable tag?
              if (childNode.getName().equals("breakable")
                  && childNode.getChildren().iterator().hasNext()) {
View Full Code Here

    return new StandardTextReader().read(ValueFactoryFactory.getValueFactory(), Factory.uptr, Factory.Tree, new StringReader(expectedInput));
  }

  public static void main(String[] args){
    LeftRecursion lr = new LeftRecursion();
    IConstructor result = lr.executeParser();
    System.out.println(result);
   
    System.out.println("S(A(A(A(a),a),a)) <- good");
  }
View Full Code Here

        for (Integer pos = 0; iter.hasNext(); pos++) {
 
          IValue kidValue = iter.next();
 
          if (kidValue.getType().isAbstractData() && !kidValue.getType().isBottom()) {
            IConstructor kidConstructor = (IConstructor)kidValue;
           
            if (kidConstructor.getName().equals("label")) {
             
              String labelName = ((IString) kidConstructor.get(0)).getValue();
             
              if (pushDownProductionNames.contains(labelName)) {
                result.add(pos);
              }
            }
View Full Code Here

    return new StandardTextReader().read(ValueFactoryFactory.getValueFactory(), Factory.uptr, Factory.Tree, new StringReader(expectedInput));
  }

  public static void main(String[] args){
    SplitAndMerge3 ms3 = new SplitAndMerge3();
    IConstructor result = ms3.executeParser();
    System.out.println(result);
   
    System.out.println("[S(C(B(A(B(a),a),a))),S(A(B(A(a),a),a))] <- good");
  }
View Full Code Here

   
    public IConstructor visitTreeAmb(IConstructor arg) throws IOException {
      Iterator<IValue> alternatives = ((ISet) arg.get("alternatives")).iterator();
      // do not try to print the alternative with the cycle in it.
      // so lets try to find the tree without the cycle
      IConstructor tree = (IConstructor)alternatives.next();
      while (alternatives.hasNext() && CycleDetector.detect(tree) ) {
        tree = (IConstructor)alternatives.next();
      }
      tree.accept(this);
      return arg;
    }
View Full Code Here

//      throw new ImplementationError("Cannot handle ambiguous subject");
    }
     
    NonTerminalType ctype = (NonTerminalType) RascalTypeFactory.getInstance().nonTerminalType(tree);
    if(debug)System.err.println("ctype.getSymbol=" + ctype.getSymbol());
    IConstructor sym = ctype.getSymbol();
        if(SymbolAdapter.isAnyList(sym)){
          sym = SymbolAdapter.getSymbol(sym);
         
          int delta = 1;          // distance between "real" list elements, e.g. non-layout and non-separator
          IList listElems = (IList) tree.get(1);
View Full Code Here

  @Override
  public IConstructor constructor(Type cons, Map<String, IValue> annotations, IValue... children)
      throws FactTypeUseException {
    try {
      IConstructor result = (IConstructor) ctx.getEvaluator().call(cons.getAbstractDataType().getName(), cons.getName(), children);
      return result.asAnnotatable().setAnnotations(annotations);
    }
    catch (UndeclaredFunction | ArgumentsMismatch e) {
      // TODO this makes this very robust, but also may hide issues. Not sure what is best here yet.
      return adapted.constructor(cons, annotations, children);
    }
View Full Code Here

  /**
   * @return a constructor name if present or null otherwise
   */
  public static String getConstructorName(IConstructor tree) {
    IConstructor def = getDefined(tree);
   
    if (SymbolAdapter.isLabel(def)) {
      return SymbolAdapter.getLabel(def);
    }
   
View Full Code Here

TOP

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

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.