use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.
the class FormulaParser method number.
private FormulaNode number() {
Token token = lexer.next();
double value;
try {
value = Double.parseDouble(token.getString());
} catch (NumberFormatException e) {
throw new FormulaSyntaxException(new SourceRange(token), "Invalid number '" + token.getString() + "': " + e.getMessage());
}
return new ConstantNode(value, new SourceRange(token));
}
use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.
the class FormulaParser method call.
private FormulaNode call(Token functionToken) {
FormulaFunction function = function(functionToken);
expect(TokenType.PAREN_START);
List<FormulaNode> arguments = new ArrayList<>();
while (true) {
if (!lexer.hasNext()) {
throw new FormulaSyntaxException("Unexpected end of formula");
}
TokenType nextToken = lexer.peek().getType();
if (nextToken == TokenType.COMMA) {
// Consume comma and parse next argument
lexer.next();
continue;
}
if (nextToken == TokenType.PAREN_END) {
// consume paren and complete argument list
Token closingParen = lexer.next();
return new FunctionCallNode(function, arguments, new SourceRange(functionToken, closingParen));
}
// Otherwise parse the next argument
arguments.add(expression());
}
}
use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.
the class BinaryBooleanOperator 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");
}
int[] result = new int[a.numRows()];
for (int i = 0; i < result.length; i++) {
int ai = a.getBoolean(i);
int bi = b.getBoolean(i);
result[i] = apply(ai, bi);
}
return new BooleanColumnView(result);
}
use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.
the class YearFracFunction method apply.
@Override
public FieldValue apply(List<FieldValue> arguments) {
if (arguments.size() != 2) {
throw new FormulaSyntaxException("YEARFRAC() requires two arguments");
}
LocalDate startDate = (LocalDate) arguments.get(0);
LocalDate endDate = (LocalDate) arguments.get(1);
if (startDate == null || endDate == null) {
return null;
}
return new Quantity(compute(startDate, endDate));
}
use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.
the class YearFracFunction method columnApply.
@Override
public ColumnView columnApply(int numRows, List<ColumnView> arguments) {
if (arguments.size() != 2) {
throw new FormulaSyntaxException("YEARFRAC() requires two arguments");
}
ColumnView startView = arguments.get(0);
ColumnView endView = arguments.get(1);
double[] result = new double[numRows];
for (int i = 0; i < numRows; i++) {
String start = startView.getString(i);
String end = endView.getString(i);
if (start == null || end == null) {
result[i] = Double.NaN;
} else {
result[i] = compute(LocalDate.parse(start), LocalDate.parse(end));
}
}
return new DoubleArrayColumnView(result);
}
Aggregations