Exemplo n.º 1
0
  /**
   * Returns a reconstructed Cetus expression from the simple expression.
   *
   * @return the Cetus expression.
   */
  protected Expression getExpression() {
    Expression ret = null;

    if (children.isEmpty()) ret = (Expression) expr.clone();
    else if (sop == TREE) {
      ret = (Expression) expr.clone();
      for (int i = 0; i < children.size(); ++i) ret.setChild(i, getChild(i).getExpression());
    } else if (cop.get(sop) instanceof UnaryOperator) {
      UnaryOperator uop = (UnaryOperator) cop.get(sop);
      ret = new UnaryExpression(uop, getChild(0).getExpression());
    } else if (cop.get(sop) instanceof BinaryOperator) {
      BinaryOperator bop = (BinaryOperator) cop.get(sop);
      Iterator<SimpleExpression> iter = children.iterator();
      if (iter.hasNext()) ret = iter.next().getExpression();
      while (iter.hasNext()) ret = new BinaryExpression(ret, bop, iter.next().getExpression());
    } else Tools.exit("[SimpleExpression] unknown simple expression");

    return ret;
  }
  protected List<EnumItemResult> getEnumValuesAndCommentsByName(
      Enum e, Signatures signatures, Identifier libraryClassName) {
    List<EnumItemResult> ret = new ArrayList<EnumItemResult>();
    Integer lastAdditiveValue = null;
    Expression lastRefValue = null;
    boolean failedOnceForThisEnum = false;
    for (com.ochafik.lang.jnaerator.parser.Enum.EnumItem item : e.getItems()) {
      EnumItemResult res = new EnumItemResult();
      res.originalItem = item;
      try {
        if (item.getArguments().isEmpty()) {
          // no explicit value
          if (lastRefValue == null) {
            if (lastAdditiveValue != null) {
              lastAdditiveValue++;
              res.unconvertedValue = expr(lastAdditiveValue);
            } else {
              if (item == e.getItems().get(0)) {
                lastAdditiveValue = 0;
                res.unconvertedValue = expr(lastAdditiveValue);
              } else res.unconvertedValue = null;
            }
          } else {
            // has a last reference value
            if (lastAdditiveValue != null) lastAdditiveValue++;
            else lastAdditiveValue = 1;

            res.unconvertedValue =
                expr(lastRefValue.clone(), Expression.BinaryOperator.Plus, expr(lastAdditiveValue));
          }
        } else {
          // has an explicit value
          failedOnceForThisEnum = false; // reset skipping
          lastAdditiveValue = null;
          lastRefValue = item.getArguments().get(0);
          res.unconvertedValue = lastRefValue;
          if (lastRefValue instanceof Expression.Constant) {
            try {
              lastAdditiveValue = ((Expression.Constant) lastRefValue).asInteger();
              lastRefValue = null;
            } catch (Exception ex) {
            }
          }
        }

        res.convertedValue =
            result
                .typeConverter
                .convertExpressionToJava(res.unconvertedValue, libraryClassName, true)
                .getFirst();
      } catch (Exception ex) {
        failedOnceForThisEnum = true;
        res.exceptionMessage = ex.toString();
      }
      failedOnceForThisEnum = failedOnceForThisEnum || res.errorElement != null;
      if (failedOnceForThisEnum) res.errorElement = skipDeclaration(item);

      ret.add(res);
    }
    return ret;
  }