Search in sources :

Example 6 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class DateComponentFunction method apply.

@Override
public final FieldValue apply(List<FieldValue> arguments) {
    checkArity(arguments, 1);
    FieldValue argument = arguments.get(0);
    if (!(argument instanceof LocalDate)) {
        throw new FormulaSyntaxException("Expected date argument");
    }
    LocalDate date = (LocalDate) argument;
    return new Quantity(apply(date));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 7 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class ComparisonOperator method columnApply.

@Override
public ColumnView columnApply(int numRows, List<ColumnView> arguments) {
    checkArity(arguments, 2);
    ColumnView a = arguments.get(0);
    ColumnView b = arguments.get(1);
    if (a.numRows() != b.numRows()) {
        throw new FormulaSyntaxException("Arguments must have the same number of rows");
    }
    if (a.getType() == ColumnType.NUMBER && b.getType() == ColumnType.NUMBER) {
        return columnApplyNumber(a, b);
    } else if (a.getType() == ColumnType.STRING && b.getType() == ColumnType.STRING) {
        if (a instanceof EnumColumnView) {
            return columnApplyEnum((EnumColumnView) a, b);
        } else if (b instanceof EnumColumnView) {
            return columnApplyEnum((EnumColumnView) b, a);
        } else {
            return columnApplyString(a, b);
        }
    } else {
        throw new FormulaSyntaxException("Comparsion between incompatible types: " + a.getType() + ", " + b.getType());
    }
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) EnumColumnView(org.activityinfo.model.query.EnumColumnView) ColumnView(org.activityinfo.model.query.ColumnView) BooleanColumnView(org.activityinfo.model.query.BooleanColumnView) EnumColumnView(org.activityinfo.model.query.EnumColumnView)

Example 8 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class FormulaParser method unary.

private FormulaNode unary() {
    if (!lexer.hasNext()) {
        throw new FormulaSyntaxException("Unexpected end of formula");
    }
    Token token = lexer.peek();
    if (token.getType() == TokenType.OPERATOR) {
        if (token.getString().equals("-") || token.getString().equals("+")) {
            Token opToken = lexer.next();
            FormulaFunction op = function(opToken);
            FormulaNode operand = unary();
            SourceRange sourceRange = new SourceRange(opToken.getStart(), operand.getSourceRange().getEnd());
            return new FunctionCallNode(op, singletonList(operand), sourceRange);
        }
    }
    return unary2();
}
Also used : FormulaFunction(org.activityinfo.model.formula.functions.FormulaFunction) FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)

Example 9 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class FormulaParser method unary2.

private FormulaNode unary2() {
    if (!lexer.hasNext()) {
        throw new FormulaSyntaxException("Unexpected end of formula");
    }
    Token token = lexer.peek();
    if (token.getType() == TokenType.OPERATOR) {
        if (token.getString().equals("!")) {
            Token opToken = lexer.next();
            FormulaFunction op = function(opToken);
            FormulaNode operand = primary();
            SourceRange sourceRange = new SourceRange(opToken.getStart(), operand.getSourceRange().getEnd());
            return new FunctionCallNode(op, singletonList(operand), sourceRange);
        }
    }
    return primary();
}
Also used : FormulaFunction(org.activityinfo.model.formula.functions.FormulaFunction) FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)

Aggregations

FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)9 FormulaFunction (org.activityinfo.model.formula.functions.FormulaFunction)3 ColumnView (org.activityinfo.model.query.ColumnView)3 BooleanColumnView (org.activityinfo.model.query.BooleanColumnView)2 Quantity (org.activityinfo.model.type.number.Quantity)2 LocalDate (org.activityinfo.model.type.time.LocalDate)2 ArrayList (java.util.ArrayList)1 DoubleArrayColumnView (org.activityinfo.model.query.DoubleArrayColumnView)1 EnumColumnView (org.activityinfo.model.query.EnumColumnView)1 FieldValue (org.activityinfo.model.type.FieldValue)1