public String getErrorMessage() { assert !valid; if(errors.size() == 1) { return errors.get(0).getMessage(); } else { return I18N.CONSTANTS.calculationExpressionIsInvalid(); } }
private void applyMarks(ParsedFormula formula) { if(!formula.isValid()) { for (FormulaError error : formula.getErrors()) { if(error.hasSourceRange()) { SourceRange range = error.getSourceRange(); MarkOptions options = MarkOptions.create(); options.setClassName("CodeMirror-lint-mark-error"); options.setTitle(error.getMessage()); TextMarker marker = editor.getDoc().markText(pos(range.getStart()), pos(range.getEnd()), options); markers.add(marker); } } } else { for (FieldReference fieldReference : formula.getReferences()) { SourceRange range = fieldReference.getSourceRange(); MarkOptions options = MarkOptions.create(); options.setClassName(FormulaResources.INSTANCE.styles().fieldAnnotation()); options.setTitle(fieldReference.getDescription()); TextMarker marker = editor.getDoc().markText(pos(range.getStart()), pos(range.getEnd()), options); markers.add(marker); } } }
private FormulaValidator validate(String formula) { Survey surveyForm = catalog.getSurvey(); FormulaValidator validator = new FormulaValidator(catalog.getFormTree(surveyForm.getFormId())); validator.validate(FormulaParser.parse(formula)); for (FormulaError error : validator.getErrors()) { System.out.println("Error at " + error.getSourceRange() + ": " + error.getMessage()); } return validator; }
@Test public void badNumberOfArguments() { FormulaValidator validator = validate("IF(1)"); assertThat(validator.getErrors(), hasSize(1)); assertThat(validator.getErrors().get(0).getSourceRange(), equalTo(new SourceRange(new SourcePos(0, 0), 5))); }
public ParsedFormula(FormTree tree, String formula) { this.formula = formula; valid = true; if (Strings.isNullOrEmpty(formula)) { errors.add(new FormulaError("The formula is empty.")); valid = false; } try { rootNode = FormulaParser.parse(formula); } catch (FormulaException e) { errors.add(new FormulaError(e.getSourceRange(), e.getMessage())); valid = false; } if(valid) { FormulaValidator validator = new FormulaValidator(tree); validator.validate(rootNode); valid = validator.isValid(); errors.addAll(validator.getErrors()); this.resultType = validator.getResultType(); this.references = validator.getReferences(); this.simpleReference = validator.isSimpleReference(); } }
private FieldType validateFunctionCall(FunctionCallNode call) { // If function calls are involved, this is no longer // a simple field reference this.simpleReference = false; List<FormulaNode> arguments = call.getArguments(); List<FieldType> argumentTypes = new ArrayList<>(); boolean validationFailed = false; for (FormulaNode formulaNode : arguments) { try { argumentTypes.add(validateExpr(formulaNode)); } catch (ValidationFailed e) { // Continue validating the other arguments. validationFailed = true; } } if(validationFailed) { throw new ValidationFailed(); } try { return call.getFunction().resolveResultType(argumentTypes); } catch (ArgumentException e) { errors.add(new FormulaError(arguments.get(e.getArgumentIndex()).getSourceRange(), e.getMessage())); throw new ValidationFailed(); } catch(FormulaException e) { errors.add(new FormulaError(call, e.getMessage())); throw new ValidationFailed(); } }
errors.add(new FormulaError(formulaNode, e.getMessage())); throw new ValidationFailed(); errors.add(new FormulaError(formulaNode, "Invalid field reference")); throw new ValidationFailed();