Grammar for formulas - Show-your-Heart/ShowYourHeart-docs GitHub Wiki

Introduction

Several fields contain expressions need to be computed in runtime. For instance, indicator conditions, formulas, and validations. Here go some examples:

ADD EXAMPLES HERE!

Xtext grammar for formulas

Formula:
	statement=Statement
;

UnaryNumeric:
	unary_numeric_function=UNARY_NUMERIC_FUNCTION
	"(" expression=Expression ")"
;

BinaryNumeric:
	binary_numeric_function=BINARY_NUMERIC_FUNCTION
	"(" expressionl=Expression "," (INT | STRING) ")"
;

Statistical:
	statistical_function=STATISTICAL_FUNCTION
	"(" "["referenceIndicator=[Indicator]"]" ")"
 	//Constraint: only direct indicators should be used
;

Statement:
	expression=Expression | if_statement=If_statement
;

If_statement:
	"IF" expression=Expression "THEN" thenStatement=Statement
	(=>"ELSE" elseStatement=Statement)?
;

Expression:
	simpleExpressionl=Simple_expression ( ("=" | "<>" | "<" | "<=" | ">" | ">=" | "==") 
	simpleExpressionr=Simple_expression)?
;

Simple_expression:
	terml=Term ((("+" | "-") | "OR") termr=Term)*
;

Term:
	factorl=Factor ((("*" | "/") | "AND") factorr=Factor)*
;

Factor:
	basel=Base ("^" baser=Base)?
;

Base:
	( "(" expression=Expression ")" | "["referenceIndicator=[Indicator] "]" | 
	statistic=Statistical | unarynumeric=UnaryNumeric |
	binarynumeric=BinaryNumeric | BOOLEAN | STRING | INT | DOUBLE )
;

enum UNARY_NUMERIC_FUNCTION: absolute="abs" | int="int" ;
enum BINARY_NUMERIC_FUNCTION: roundup="roundUp" | rounddown="roundDown" | 
                              round="round" | count="count" | countif="countIf" ; 
enum STATISTICAL_FUNCTION: minimum="min" | maximum="max" |
                           sum="sum" | mean="avg" | mode="mode" | median="median";
terminal BOOLEAN : ("true"|"false");
terminal DOUBLE: INT "." INT;

Constraints

  • In formulas, indicators should not reference themselves.
  • Statistical functions cannot be used in indicator conditions or in validation rules.

Semantics of some functions

We can interpret the meaning of the functions in the same way as Google Sheets. But note that the syntax is different:

PENDING TO EXPRESS THE SYNTAX AND SEMANTICS OF FUNCTIONS BETTER

⚠️ **GitHub.com Fallback** ⚠️