Ejemplo n.º 1
0
  /**
   * Given a production or application returns the constructor name attached to the production, or
   * null if there is no constructor.
   *
   * @author Martin Bravenboer
   * @author Lennart Kats
   */
  public String getConstructor(ATerm arg) {
    ATermAppl appl = assertAppl(arg, applFun);

    ATermAppl prod;
    if (appl.getAFun() == prodFun) {
      prod = appl;
    } else if (appl.getAFun() == applFun) {
      prod = assertAppl(appl.getArgument(APPL_PROD), prodFun);
    } else {
      throw new IllegalArgumentException("Expected prod or appl: " + arg);
    }

    ATermAppl attrs = assertAppl(prod.getArgument(PROD_ATTRS));
    if (attrs.getAFun() == noattrsFun) {
      return null;
    } else {
      for (ATerm attr : (ATermList) attrs.getChildAt(ATTRS_LIST)) {
        if (attr instanceof ATermAppl) {
          ATermAppl namedAttr = (ATermAppl) attr;
          if (namedAttr.getAFun() == termFun) {
            namedAttr = (ATermAppl) namedAttr.getArgument(TERM_CONS);
            if (namedAttr.getAFun() == consFun) {
              namedAttr = (ATermAppl) namedAttr.getArgument(CONS_NAME);
              return namedAttr.getName();
            }
          }
        }
      }
    }

    return null;
  }
Ejemplo n.º 2
0
  private static ATermAppl assertAppl(ATerm t, AFun fun) {
    ATermAppl result = assertAppl(t);
    if (result.getAFun() != fun) {
      throw new IllegalArgumentException(
          "Expected application of function " + fun + ": " + result.getAFun());
    }

    return result;
  }
Ejemplo n.º 3
0
  /**
   * Yields a parse tree (parsetree or appl) to a string builder.
   *
   * @author Martin Bravenboer
   */
  public void yield(ATerm parsetree, StringBuilder builder) {
    ATermAppl appl = assertAppl(parsetree);
    if (appl.getAFun() == parsetreeFun) {
      appl = assertAppl(appl.getArgument(PARSE_TREE));
    }

    yieldAppl(appl, builder);
  }
Ejemplo n.º 4
0
 /**
  * Apply a conversion on the ATerm contained in the String and returns a gram.i.types.DefTipo from
  * it
  *
  * @param trm ATerm to convert into a Gom term
  * @param atConv ATerm Converter used to convert the ATerm
  * @return the Gom term
  */
 public static gram.i.types.DefTipo fromTerm(
     aterm.ATerm trm, tom.library.utils.ATermConverter atConv) {
   trm = atConv.convert(trm);
   if (trm instanceof aterm.ATermAppl) {
     aterm.ATermAppl appl = (aterm.ATermAppl) trm;
     if (symbolName.equals(appl.getName()) && !appl.getAFun().isQuoted()) {
       return make();
     }
   }
   return null;
 }
Ejemplo n.º 5
0
 /**
  * Apply a conversion on the ATerm contained in the String and returns a parser.rec.types.RelExp
  * from it
  *
  * @param trm ATerm to convert into a Gom term
  * @param atConv ATerm Converter used to convert the ATerm
  * @return the Gom term
  */
 public static parser.rec.types.RelExp fromTerm(
     aterm.ATerm trm, tom.library.utils.ATermConverter atConv) {
   trm = atConv.convert(trm);
   if (trm instanceof aterm.ATermAppl) {
     aterm.ATermAppl appl = (aterm.ATermAppl) trm;
     if (symbolName.equals(appl.getName()) && !appl.getAFun().isQuoted()) {
       return make(parser.rec.types.AritExp.fromTerm(appl.getArgument(0), atConv));
     }
   }
   return null;
 }
Ejemplo n.º 6
0
  @Override
  public Visitable visitAppl(ATermAppl arg) throws VisitFailure {

    AFun fun = (AFun) arg.getAFun().accept(this);

    ATerm[] arguments = arg.getArgumentArray();

    if (fun != arg.getAFun()) {
      arg = arg.getFactory().makeAppl(fun, arguments);
    }

    for (int i = 0; i < arguments.length; i++) {
      Visitable v = arguments[i].accept(this);

      if (v != arguments[i]) {
        arg = (ATermAppl) arg.setChildAt(i, v);
      }
    }

    return arg;
  }
Ejemplo n.º 7
0
 /**
  * Apply a conversion on the ATerm contained in the String and returns a gram.i.types.Expressao
  * from it
  *
  * @param trm ATerm to convert into a Gom term
  * @param atConv ATerm Converter used to convert the ATerm
  * @return the Gom term
  */
 public static gram.i.types.Expressao fromTerm(
     aterm.ATerm trm, tom.library.utils.ATermConverter atConv) {
   trm = atConv.convert(trm);
   if (trm instanceof aterm.ATermAppl) {
     aterm.ATermAppl appl = (aterm.ATermAppl) trm;
     if (symbolName.equals(appl.getName()) && !appl.getAFun().isQuoted()) {
       return make(
           gram.i.types.Expressao.fromTerm(appl.getArgument(0), atConv),
           gram.i.types.LComentarios.fromTerm(appl.getArgument(1), atConv),
           gram.i.types.LComentarios.fromTerm(appl.getArgument(2), atConv),
           gram.i.types.Expressao.fromTerm(appl.getArgument(3), atConv));
     }
   }
   return null;
 }
Ejemplo n.º 8
0
 /** Private helper for the yield method. */
 private void yieldAppl(ATermAppl appl, StringBuilder builder) {
   for (ATerm t : (ATermList) appl.getArgument(APPL_ARGS)) {
     if (t instanceof ATermAppl) {
       ATermAppl arg = (ATermAppl) t;
       if (arg.getAFun() == applFun) {
         yieldAppl(arg, builder);
       } else {
         throw new IllegalArgumentException("Don't know how to yield " + arg);
       }
     } else if (t instanceof ATermInt) {
       ATermInt arg = (ATermInt) t;
       builder.append((char) arg.getInt());
     } else {
       throw new IllegalArgumentException("Don't know how to yield " + t);
     }
   }
 }
Ejemplo n.º 9
0
  protected boolean match(ATerm pattern, List<Object> list) {
    if (equals(pattern)) {
      return true;
    }

    if (pattern.getType() == ATerm.PLACEHOLDER) {
      ATerm type = ((ATermPlaceholder) pattern).getPlaceholder();
      if (type.getType() == ATerm.APPL) {
        ATermAppl appl = (ATermAppl) type;
        AFun afun = appl.getAFun();
        if (afun.getName().equals("real") && afun.getArity() == 0 && !afun.isQuoted()) {
          list.add(new Double(value));
          return true;
        }
      }
    }

    return super.match(pattern, list);
  }