Example #1
0
  public static Expr parse(Tokens tokens) {
    int pos = tokens.getPosition();
    Expr firstExpr = MultiplyExpr.parse(tokens);

    if (firstExpr == null) {
      tokens.setPosition(pos);
      return null;
    }

    List<Expr> multiplyExprs = new LinkedList<Expr>();
    multiplyExprs.add(firstExpr);

    BitSet operators = new BitSet();

    for (int i = 0; tokens.hasNext(); i++) {
      char operator = tokens.nextChar();

      if (operator == '+' || operator == '-') {
        Expr nextExpr = MultiplyExpr.parse(tokens);

        if (nextExpr == null) {
          throw new QuerySyntaxException(tokens);
        }

        multiplyExprs.add(nextExpr);

        if (operator == '+') {
          operators.set(i);
        }
      } else {
        tokens.pushback();
        break;
      }
    }

    return multiplyExprs.size() == 1 ? firstExpr : new AdditiveExpr(multiplyExprs, operators);
  }
Example #2
0
 AdditiveExpr(List<Expr> multiplyExprs, BitSet operators) {
   this.multiplyExprs = multiplyExprs.toArray(new Expr[multiplyExprs.size()]);
   this.operators = operators;
 }