2019-06-04 14:01:21 +00:00
|
|
|
# This grammar is inspired by the "fancier grammar" tab of the nearley playground : https://omrelli.ug/nearley-playground
|
|
|
|
|
2019-09-05 13:48:22 +00:00
|
|
|
# Look for the PEMDAS system : Parentheses, Exponents (omitted here), Multiplication, and you should guess the rest :)
|
2019-06-04 14:01:21 +00:00
|
|
|
|
2019-05-16 13:58:30 +00:00
|
|
|
@preprocessor esmodule
|
|
|
|
|
2019-09-05 13:48:22 +00:00
|
|
|
@{%
|
2019-05-16 13:58:30 +00:00
|
|
|
import {string, filteredVariable, variable, temporalVariable, operation, boolean, number, percentage } from './grammarFunctions'
|
2019-09-10 16:24:05 +00:00
|
|
|
|
|
|
|
const moo = require("moo");
|
|
|
|
|
|
|
|
const letter = '[a-zA-Z\u00C0-\u017F]';
|
|
|
|
const letterOrNumber = '[a-zA-Z\u00C0-\u017F0-9\']';
|
|
|
|
const word = `${letter}(?:[\-']?${letterOrNumber}+)*`;
|
|
|
|
const words = `${word}(?: ${word}|${letterOrNumber}*)*`
|
|
|
|
const numberRegExp = '-?(?:[1-9][0-9]+|[0-9])(?:\.[0-9]+)?';
|
|
|
|
const percentageRegExp = numberRegExp + '\\%'
|
|
|
|
|
|
|
|
const lexer = moo.compile({
|
|
|
|
percentage: new RegExp(percentageRegExp),
|
|
|
|
number: new RegExp(numberRegExp),
|
|
|
|
'(': '(',
|
|
|
|
')': ')',
|
|
|
|
'[': '[',
|
|
|
|
']': ']',
|
|
|
|
comparisonOperator: ['>','<','>=','<=','=','!='],
|
|
|
|
additionSubstractionOperator: /[\+-]/,
|
|
|
|
multiplicationDivisionOperator: ['*','/'],
|
|
|
|
temporality: ['annuel' , 'mensuel'],
|
|
|
|
words: new RegExp(words),
|
|
|
|
string: /'[ \t\.'a-zA-Z\-\u00C0-\u017F0-9 ]+'/,
|
|
|
|
dot: ' . ',
|
|
|
|
_: { match: /[\s]/, lineBreaks: true }
|
|
|
|
});
|
2019-05-16 13:58:30 +00:00
|
|
|
%}
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
@lexer lexer
|
|
|
|
|
2017-03-07 17:25:25 +00:00
|
|
|
main ->
|
2019-09-10 16:24:05 +00:00
|
|
|
AdditionSubstraction {% id %}
|
|
|
|
| Comparison {% id %}
|
|
|
|
| NonNumericTerminal {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
|
|
|
NumericTerminal ->
|
2019-09-10 16:24:05 +00:00
|
|
|
Variable {% id %}
|
|
|
|
| TemporalVariable {% id %}
|
|
|
|
| FilteredVariable {% id %}
|
|
|
|
| number {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
Parentheses ->
|
|
|
|
"(" AdditionSubstraction ")" {% ([,e]) => e %}
|
|
|
|
| NumericTerminal {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
Comparison -> Comparable %_ %comparisonOperator %_ Comparable {% operation('comparison')%}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2019-06-04 14:01:21 +00:00
|
|
|
Comparable -> ( AdditionSubstraction | NonNumericTerminal) {% ([[e]]) => e %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2019-09-05 13:48:22 +00:00
|
|
|
NonNumericTerminal ->
|
2019-09-10 16:24:05 +00:00
|
|
|
boolean {% id %}
|
|
|
|
| string {% id %}
|
|
|
|
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2019-09-11 17:09:05 +00:00
|
|
|
Variable -> %words (%dot %words {% ([,words]) => words %}):* {% variable %}
|
2017-06-22 16:03:12 +00:00
|
|
|
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
Filter -> "[" %words "]" {% ([,filter]) => filter %}
|
|
|
|
FilteredVariable -> Variable %_ Filter {% filteredVariable %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
TemporalTransform -> "[" %temporality "]" {% ([,temporality]) => temporality %}
|
|
|
|
TemporalVariable -> Variable %_ TemporalTransform {% temporalVariable %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2017-05-09 13:39:42 +00:00
|
|
|
#-----
|
|
|
|
|
2019-05-02 13:58:39 +00:00
|
|
|
# Addition and subtraction
|
2019-09-10 16:24:05 +00:00
|
|
|
AdditionSubstraction ->
|
|
|
|
AdditionSubstraction %_ %additionSubstractionOperator %_ MultiplicationDivision {% operation('calculation') %}
|
|
|
|
| MultiplicationDivision {% id %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
|
|
|
|
2019-05-02 13:58:39 +00:00
|
|
|
# Multiplication and division
|
2019-09-10 16:24:05 +00:00
|
|
|
MultiplicationDivision ->
|
|
|
|
MultiplicationDivision %_ %multiplicationDivisionOperator %_ Parentheses {% operation('calculation') %}
|
|
|
|
| Parentheses {% id %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2017-09-19 11:17:43 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
boolean ->
|
|
|
|
"oui" {% boolean(true) %}
|
|
|
|
| "non" {% boolean(false) %}
|
2018-08-07 18:46:26 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
number ->
|
|
|
|
%number {% number %}
|
|
|
|
| %percentage {% percentage %}
|
|
|
|
string -> %string {% string %}
|