Beispiel #1
0
  /*package*/ public NonTerminalType(IConstructor cons) {
    // TODO refactor this into different factory methods in RascalTypeFactory

    if (cons.getConstructorType() == RascalValueFactory.Tree_Appl) {
      // Note that here we go from * to + lists if the list is not empty
      this.symbol = TreeAdapter.getType((ITree) cons);
    } else if (cons.getConstructorType() == RascalValueFactory.Tree_Amb) {
      ISet alts = TreeAdapter.getAlternatives((ITree) cons);

      if (!alts.isEmpty()) {
        ITree first = (ITree) alts.iterator().next();
        this.symbol = TreeAdapter.getType(first);
      } else {
        this.symbol =
            IRascalValueFactory.getInstance().constructor(RascalValueFactory.Symbol_Empty);
      }
    } else if (cons.getConstructorType() == RascalValueFactory.Tree_Cycle) {
      this.symbol = TreeAdapter.getType((ITree) cons);
    } else if (cons.getType() == RascalValueFactory.Symbol) {
      this.symbol = cons;
    } else if (cons.getType() == RascalValueFactory.Production) {
      this.symbol = ProductionAdapter.getType(cons);
    } else {
      throw new ImplementationError("Invalid concrete syntax type constructor:" + cons);
    }
  }
  @Override
  public ITree filterProduction(ITree tree, Object environment) {
    String cons = TreeAdapter.getConstructorName(tree);

    if (cons != null) {
      Environment env = (Environment) environment;
      Result<IValue> var = env.getFrameVariable(cons);

      if (var != null && var instanceof ICallableValue) {
        ICallableValue function = (ICallableValue) var;

        try {
          Result<IValue> result = null;
          if (TreeAdapter.isContextFree(tree)) {
            // For context free trees, try it without layout and literal arguments first.
            result = call(function, TreeAdapter.getASTArgs(tree));
          }

          if (result == null) {
            result = call(function, TreeAdapter.getArgs(tree));
          }

          if (result == null) {
            return tree;
          }

          if (result.getType().isBottom()) {
            return tree;
          }

          if (!(result.getType() instanceof NonTerminalType
              && SymbolAdapter.isEqual(
                  ((NonTerminalType) result.getType()).getSymbol(), TreeAdapter.getType(tree)))) {
            // do not call the function if it does not return the right type
            return tree;
          }

          return (ITree) result.getValue();
        } catch (Filtered f) {
          return null;
        }
      }
    }

    return tree;
  }