⚙️ Grammaire d'expressions plus complexe

Elle permet des opérations chainées.
On peut maintenant faire formule: 3 + 5 + 6
Ou 3 * (ma variable + 4)
pull/529/head
Mael 2019-05-02 15:58:39 +02:00
parent 528c714e26
commit ced2a37490
2 changed files with 43 additions and 16 deletions

View File

@ -2,14 +2,21 @@
main ->
CalcExpression {% id %}
| Boolean {% id %}
| Variable {% id %}
| NegatedVariable {% id %}
AS {% id %}
| Comparison {% id %}
| NonNumericTerminal {% id %}
NumericTerminal ->
Variable {% id %}
| TemporalVariable {% id %}
| FilteredVariable {% id %}
| percentage {% id %}
| Comparison {% id %}
| number {% id %}
# Parentheses
P -> "(" _ AS _ ")" {% function(d) {return {category:'parentheses', explanation: d[2]}} %}
| NumericTerminal {% id %}
Comparison -> Comparable _ ComparisonOperator _ Comparable {% d => ({
category: 'comparison',
@ -18,7 +25,13 @@ Comparison -> Comparable _ ComparisonOperator _ Comparable {% d => ({
explanation: [d[0], d[4]]
}) %}
Comparable -> (number | percentage | CalcExpression | Variable | TemporalVariable | Constant) {% d => d[0][0] %}
Comparable -> ( AS | NonNumericTerminal) {% d => d[0][0] %}
NonNumericTerminal ->
Boolean {% id %}
| String {% id %}
| NegatedVariable {% id %}
ComparisonOperator -> ">" | "<" | ">=" | "<=" | "=" | "!="
@ -36,33 +49,45 @@ Temporalities -> "annuel" | "mensuel" {% id %}
#-----
CalcExpression -> Term _ ArithmeticOperator _ Term {% d => ({
# Addition and subtraction
AS -> AS _ ASOperator _ MD {% d => ({
category: 'calcExpression',
operator: d[2],
explanation: [d[0], d[4]],
type: 'numeric'
}) %}
| MD {% id %}
ASOperator -> "+" {% id %}
| "-" {% id %}
MDOperator -> "*" {% id %}
| "/" {% id %}
# Multiplication and division
MD -> MD _ MDOperator _ P {% d => ({
category: 'calcExpression',
operator: d[2],
explanation: [d[0], d[4]],
type: 'numeric'
}) %}
| P {% id %}
Term -> Variable {% id %}
| FilteredVariable {% id %}
| number {% id %}
| percentage {% id %}
ArithmeticOperator -> "+" {% id %}
| "-" {% id %}
| "*" {% id %}
| "/" {% id %}
Variable -> VariableFragment (_ Dot _ VariableFragment {% d => d[3] %}):* {% d => ({
category: 'variable',
fragments: [d[0], ...d[1]],
type: 'numeric | boolean'
}) %}
Constant -> "'" [ .'a-zA-Z\-\u00C0-\u017F ]:+ "'" {% d => ({
String -> "'" [ .'a-zA-Z\-\u00C0-\u017F ]:+ "'" {% d => ({
category: 'value',
type: 'string',
nodeValue: d[1].join('')
@ -84,3 +109,4 @@ percentage -> [0-9]:+ ([\.] [0-9]:+):? [\%] {% d => ({category: 'percenta
Boolean -> "oui" {% d=> ({category: 'boolean', nodeValue: true}) %}
| "non" {% d=> ({category: 'boolean', nodeValue: false}) %}

View File

@ -211,6 +211,7 @@ export let treatString = (rules, rule) => rawNode => {
return {
evaluate,
jsx,
operator,
text: rawNode,
category: parseResult.category,