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-10-18 17:15:58 +00:00
|
|
|
# This preprocessor was disabled because it doesn't work with Jest
|
|
|
|
# @preprocessor esmodule
|
2019-05-16 13:58:30 +00:00
|
|
|
|
2019-09-05 13:48:22 +00:00
|
|
|
@{%
|
2020-02-04 17:33:03 +00:00
|
|
|
const {
|
|
|
|
string, filteredVariable, date, variable, variableWithConversion,
|
|
|
|
temporalNumericValue, binaryOperation, unaryOperation, boolean, number,
|
2020-06-08 15:43:55 +00:00
|
|
|
numberWithUnit
|
2020-02-04 17:33:03 +00:00
|
|
|
} = require('./grammarFunctions')
|
2019-09-10 16:24:05 +00:00
|
|
|
|
|
|
|
const moo = require("moo");
|
|
|
|
|
2020-02-04 17:33:03 +00:00
|
|
|
const dateRegexp = `(?:(?:0?[1-9]|[12][0-9]|3[01])\\/)?(?:0?[1-9]|1[012])\\/\\d{4}`
|
2019-11-28 11:03:23 +00:00
|
|
|
const letter = '[a-zA-Z\u00C0-\u017F€$%]';
|
2020-06-25 17:41:58 +00:00
|
|
|
const letterOrNumber = '[a-zA-Z\u00C0-\u017F0-9\'°]';
|
2020-02-04 17:33:03 +00:00
|
|
|
const word = `${letter}(?:[-']?${letterOrNumber}+)*`;
|
2019-11-04 16:44:03 +00:00
|
|
|
const wordOrNumber = `(?:${word}|${letterOrNumber}+)`
|
|
|
|
const words = `${word}(?:[\\s]?${wordOrNumber}+)*`
|
2020-02-04 17:33:03 +00:00
|
|
|
const periodWord = `\\| ${word}(?:[\\s]${word})*`
|
|
|
|
|
2019-11-04 16:44:03 +00:00
|
|
|
const numberRegExp = '-?(?:[1-9][0-9]+|[0-9])(?:\\.[0-9]+)?';
|
2019-09-10 16:24:05 +00:00
|
|
|
const lexer = moo.compile({
|
|
|
|
'(': '(',
|
|
|
|
')': ')',
|
|
|
|
'[': '[',
|
|
|
|
']': ']',
|
2019-09-19 09:45:16 +00:00
|
|
|
comparison: ['>','<','>=','<=','=','!='],
|
2020-02-19 16:47:21 +00:00
|
|
|
infinity: 'Infinity',
|
2020-02-04 17:33:03 +00:00
|
|
|
colon: " : ",
|
|
|
|
date: new RegExp(dateRegexp),
|
|
|
|
periodWord: new RegExp(periodWord),
|
2019-11-04 16:44:03 +00:00
|
|
|
words: new RegExp(words),
|
|
|
|
number: new RegExp(numberRegExp),
|
2020-06-08 15:43:55 +00:00
|
|
|
string: /'.*'/,
|
2019-11-05 15:02:08 +00:00
|
|
|
additionSubstraction: /[\+-]/,
|
|
|
|
multiplicationDivision: ['*','/'],
|
2019-09-10 16:24:05 +00:00
|
|
|
dot: ' . ',
|
2019-11-28 11:03:23 +00:00
|
|
|
'.': '.',
|
2019-10-23 18:00:01 +00:00
|
|
|
letterOrNumber: new RegExp(letterOrNumber),
|
2020-02-04 17:33:03 +00:00
|
|
|
space: { match: /[\s]+/, lineBreaks: true },
|
2019-09-10 16:24:05 +00:00
|
|
|
});
|
2019-10-23 18:00:01 +00:00
|
|
|
|
|
|
|
const join = (args) => ({value: (args.map(x => x && x.value).join(""))})
|
|
|
|
const flattenJoin = ([a, b]) => Array.isArray(b) ? join([a, ...b]) : a
|
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 ->
|
2020-02-04 17:33:03 +00:00
|
|
|
Comparison {% id %}
|
|
|
|
| NumericValue {% id %}
|
|
|
|
| Date {% id %}
|
2019-09-10 16:24:05 +00:00
|
|
|
| NonNumericTerminal {% id %}
|
2020-02-04 17:33:03 +00:00
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
NumericValue ->
|
2020-02-04 17:33:03 +00:00
|
|
|
AdditionSubstraction {% id %}
|
2019-09-19 09:45:16 +00:00
|
|
|
| Negation {% id %}
|
2020-02-04 17:33:03 +00:00
|
|
|
| TemporalNumericValue {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
TemporalNumericValue ->
|
2020-02-04 17:33:03 +00:00
|
|
|
NumericValue %space %periodWord %space %date {% ([value,,word,,dateString]) => temporalNumericValue(value, word, date([dateString])) %}
|
|
|
|
| NumericValue %space %periodWord %colon Date {% ([value,,word,,date]) => temporalNumericValue(value, word, date) %}
|
2020-06-08 15:43:55 +00:00
|
|
|
|
2019-05-02 13:58:39 +00:00
|
|
|
NumericTerminal ->
|
2019-09-10 16:24:05 +00:00
|
|
|
Variable {% id %}
|
2019-11-28 11:03:23 +00:00
|
|
|
| VariableWithUnitConversion {% id %}
|
2019-09-10 16:24:05 +00:00
|
|
|
| FilteredVariable {% id %}
|
|
|
|
| number {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2019-09-19 09:45:16 +00:00
|
|
|
Negation ->
|
|
|
|
"-" %space Parentheses {% unaryOperation('calculation') %}
|
2019-11-26 18:24:41 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
Parentheses ->
|
2020-02-16 18:56:07 +00:00
|
|
|
"(" NumericValue ")" {% ([,e]) => e %}
|
2019-09-10 16:24:05 +00:00
|
|
|
| NumericTerminal {% id %}
|
2019-05-02 13:58:39 +00:00
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
Date ->
|
2019-11-26 18:24:41 +00:00
|
|
|
Variable {% id %}
|
|
|
|
| %date {% date %}
|
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
Comparison ->
|
2019-11-26 18:24:41 +00:00
|
|
|
Comparable %space %comparison %space Comparable {% binaryOperation('comparison')%}
|
|
|
|
| Date %space %comparison %space Date {% binaryOperation('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-11-04 16:44:03 +00:00
|
|
|
Variable -> %words (%dot %words {% ([,words]) => words %}):* {% variable %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2019-11-28 11:03:23 +00:00
|
|
|
UnitDenominator ->
|
|
|
|
(%space):? "/" %words {% join %}
|
|
|
|
UnitNumerator -> %words ("." %words):? {% flattenJoin %}
|
2017-06-22 16:03:12 +00:00
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
Unit -> UnitNumerator:? UnitDenominator:* {% flattenJoin %}
|
2019-11-28 11:03:23 +00:00
|
|
|
UnitConversion -> "[" Unit "]" {% ([,unit]) => unit %}
|
2020-06-08 15:43:55 +00:00
|
|
|
VariableWithUnitConversion ->
|
2019-11-28 11:03:23 +00:00
|
|
|
Variable %space UnitConversion {% variableWithConversion %}
|
|
|
|
# | FilteredVariable %space UnitConversion {% variableWithConversion %} TODO
|
2017-06-22 16:03:12 +00:00
|
|
|
|
2019-11-28 11:03:23 +00:00
|
|
|
Filter -> "." %words {% ([,filter]) => filter %}
|
2019-09-19 09:45:16 +00:00
|
|
|
FilteredVariable -> Variable %space Filter {% filteredVariable %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2020-02-04 17:33:03 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
AdditionSubstraction ->
|
2019-09-19 09:45:16 +00:00
|
|
|
AdditionSubstraction %space %additionSubstraction %space MultiplicationDivision {% binaryOperation('calculation') %}
|
2019-09-10 16:24:05 +00:00
|
|
|
| MultiplicationDivision {% id %}
|
2017-03-06 16:35:30 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
MultiplicationDivision ->
|
2019-09-19 09:45:16 +00:00
|
|
|
MultiplicationDivision %space %multiplicationDivision %space Parentheses {% binaryOperation('calculation') %}
|
2019-09-10 16:24:05 +00:00
|
|
|
| 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 %}
|
2020-02-19 16:47:21 +00:00
|
|
|
| %infinity {% number %}
|
2019-11-28 11:03:23 +00:00
|
|
|
| %number (%space):? Unit {% numberWithUnit %}
|
2019-09-19 09:45:16 +00:00
|
|
|
|
2020-06-08 15:43:55 +00:00
|
|
|
string -> %string {% string %}
|