diff --git a/package.json b/package.json index 44ce83d27..fe2b22701 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "i18next": "^14.1.1", "iframe-resizer": "^3.6.2", "marked": "^0.3.17", - "nearley": "^2.13.0", + "nearley": "^2.16.0", "ramda": "^0.25.0", "raven-for-redux": "^1.3.1", "raven-js": "^3.26.4", diff --git a/source/engine/grammar.ne b/source/engine/grammar.ne index 487f0fb64..1598a251f 100644 --- a/source/engine/grammar.ne +++ b/source/engine/grammar.ne @@ -1,4 +1,11 @@ -# Pour éditer ou comprendre ce fichier, utilisez l'éditeur web Nearley : https://omrelli.ug/nearley-playground/ +@preprocessor esmodule + +@{% +import maSuperFonction from './maSuperFonction' +import treatVariableTransforms from './treatVariable' +%} + +# To understand or edit this file, use the awesome nearley playground (but save your work, it can crash sometimes) : https://omrelli.ug/nearley-playground/ main -> @@ -18,12 +25,7 @@ P -> "(" _ AS _ ")" {% function(d) {return {category:'parentheses', explanation: | NumericTerminal {% id %} -Comparison -> Comparable _ ComparisonOperator _ Comparable {% d => ({ - category: 'comparison', - type: 'boolean', - operator: d[2][0], - explanation: [d[0], d[4]] -}) %} +Comparison -> Comparable _ ComparisonOperator _ Comparable {% yo => maSuperFonction(yo) %} Comparable -> ( AS | NonNumericTerminal) {% d => d[0][0] %} @@ -81,10 +83,9 @@ Term -> Variable {% id %} | number {% id %} | percentage {% id %} -Variable -> VariableFragment (_ Dot _ VariableFragment {% d => d[3] %}):* {% d => ({ - category: 'variable', - fragments: [d[0], ...d[1]], - type: 'numeric | boolean' +Variable -> VariableFragment (_ Dot _ VariableFragment {% [,,,fragment] => fragment %}):* +{% ([firstFragment, nextFragments]) => treatVariableTransforms({ + fragments: [firstFragment, ...nextFragments], }) %} String -> "'" [ .'a-zA-Z\-\u00C0-\u017F ]:+ "'" {% d => ({ @@ -105,7 +106,7 @@ _ -> [\s] {% d => null %} number -> [0-9]:+ ([\.] [0-9]:+):? {% d => ({category: 'value', nodeValue: parseFloat(d[0].join("")+(d[1]?(d[1][0]+d[1][1].join("")):""))}) %} -percentage -> [0-9]:+ ([\.] [0-9]:+):? [\%] {% d => ({category: 'percentage', nodeValue: parseFloat(d[0].join("")+(d[1]?(d[1][0]+d[1][1].join("")):""))/100}) %} +percentage -> [0-9]:+ ([\.] [0-9]:+):? [\%] {% d => percentage(d)%} Boolean -> "oui" {% d=> ({category: 'boolean', nodeValue: true}) %} | "non" {% d=> ({category: 'boolean', nodeValue: false}) %} diff --git a/source/engine/grammarFunctions.js b/source/engine/grammarFunctions.js new file mode 100644 index 000000000..0f5c75c48 --- /dev/null +++ b/source/engine/grammarFunctions.js @@ -0,0 +1,19 @@ +import React from 'react' + +export let boolean = nodeValue => ({ + category: 'boolean', + nodeValue: nodeValue, + // eslint-disable-next-line + jsx: () => {rawNode} +}) + +export let percentage = d => ({ + // We don't need to handle category == 'value' because YAML then returns it as + // numerical value, not a String: it goes to treatNumber + nodeValue: + parseFloat(d[0].join('') + (d[1] ? d[1][0] + d[1][1].join('') : '')) / 100, + category: 'percentage', + // eslint-disable-next-line + jsx: () => {rawNode.split('%')[0]} % + //on ajoute l'espace nécessaire en français avant le pourcentage +}) diff --git a/source/engine/maSuperFonction.js b/source/engine/maSuperFonction.js new file mode 100644 index 000000000..0039db3d7 --- /dev/null +++ b/source/engine/maSuperFonction.js @@ -0,0 +1,6 @@ +export default d => ({ + category: 'comparison', + type: 'boolean', + operator: d[2][0], + explanation: [d[0], d[4]] +}) diff --git a/source/engine/treat.js b/source/engine/treat.js index 34787f4a5..dac4d6616 100644 --- a/source/engine/treat.js +++ b/source/engine/treat.js @@ -78,21 +78,6 @@ export let treatString = (rules, rule) => rawNode => { ) } - if ( - !contains(parseResult.category)([ - 'variable', - 'calcExpression', - 'filteredVariable', - 'comparison', - 'negatedVariable', - 'percentage', - 'boolean' - ]) - ) - throw new Error( - "Attention ! Erreur de traitement de l'expression : " + rawNode - ) - if (parseResult.category == 'variable') return treatVariableTransforms(rules, rule)(parseResult) if (parseResult.category == 'negatedVariable') @@ -100,25 +85,6 @@ export let treatString = (rules, rule) => rawNode => { treatVariable(rules, rule)(parseResult.variable) ) - if (parseResult.category == 'boolean') { - return { - nodeValue: parseResult.nodeValue, - // eslint-disable-next-line - jsx: () => {rawNode} - } - } - - // We don't need to handle category == 'value' because YAML then returns it as - // numerical value, not a String: it goes to treatNumber - if (parseResult.category == 'percentage') - return { - nodeValue: parseResult.nodeValue, - category: 'percentage', - // eslint-disable-next-line - jsx: () => {rawNode.split('%')[0]} % - //on ajoute l'espace nécessaire en français avant le pourcentage - } - if ( parseResult.category == 'calcExpression' || parseResult.category == 'comparison' @@ -234,6 +200,7 @@ export let treatOther = rawNode => { 'Cette donnée : ' + rawNode + ' doit être un Number, String ou Object' ) } + export let treatObject = (rules, rule, treatOptions) => rawNode => { let mecanisms = intersection(keys(rawNode), keys(knownMecanisms)) diff --git a/test/mécanismes/expressions.yaml b/test/mécanismes/expressions.yaml index e0c5ee3b8..96a1b5f58 100644 --- a/test/mécanismes/expressions.yaml +++ b/test/mécanismes/expressions.yaml @@ -13,6 +13,11 @@ exemples: - valeur attendue: 29.1 +- test: addition de plusieurs nombres + formule: 27 + 1.1 + 0.9 + exemples: + - valeur attendue: 29 + - nom: salaire de base - nom: contrat . salaire de base diff --git a/yarn.lock b/yarn.lock index 06f803d69..280e56907 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6186,7 +6186,7 @@ nearley-loader@^2.0.0: resolved "https://registry.yarnpkg.com/nearley-loader/-/nearley-loader-2.0.0.tgz#8d75fd2ab3ca9f6153ae099b2bf18f8d5605d203" integrity sha512-kkUlMrkLWMoQPlOVmv7a8aHFEiTOShPb1H+CkvJrDMYpMCqnQUpfJgViGFlh0wufMQlGcawPzebcng6KnDJEdg== -nearley@^2.13.0, nearley@^2.7.10: +nearley@^2.16.0, nearley@^2.7.10: version "2.16.0" resolved "https://registry.yarnpkg.com/nearley/-/nearley-2.16.0.tgz#77c297d041941d268290ec84b739d0ee297e83a7" integrity sha512-Tr9XD3Vt/EujXbZBv6UAHYoLUSMQAxSsTnm9K3koXzjzNWY195NqALeyrzLZBKzAkL3gl92BcSogqrHjD8QuUg==