2019-09-10 16:24:05 +00:00
|
|
|
/* Those are postprocessor functions for the Nearley grammar.ne.
|
2019-05-16 13:58:30 +00:00
|
|
|
The advantage of putting them here is to get prettier's JS formatting, since Nealrey doesn't support it https://github.com/kach/nearley/issues/310 */
|
2020-05-08 10:04:00 +00:00
|
|
|
import { normalizeDateString } from './date'
|
|
|
|
import { parseUnit } from './units'
|
2020-03-18 14:04:49 +00:00
|
|
|
import { parsePeriod } from './temporal'
|
2019-05-02 14:24:22 +00:00
|
|
|
|
2019-09-19 09:45:16 +00:00
|
|
|
export let binaryOperation = operationType => ([A, , operator, , B]) => ({
|
2019-05-16 13:58:30 +00:00
|
|
|
[operator]: {
|
2019-06-04 14:37:20 +00:00
|
|
|
operationType,
|
2019-05-16 13:58:30 +00:00
|
|
|
explanation: [A, B]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-19 09:45:16 +00:00
|
|
|
export let unaryOperation = operationType => ([operator, , A]) => ({
|
|
|
|
[operator]: {
|
|
|
|
operationType,
|
|
|
|
explanation: [number([{ value: '0' }]), A]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-28 11:03:23 +00:00
|
|
|
export let filteredVariable = ([{ variable }, , { value: filter }]) => ({
|
|
|
|
filter: { filter, explanation: variable }
|
|
|
|
})
|
2019-05-16 13:58:30 +00:00
|
|
|
|
2019-11-28 11:03:23 +00:00
|
|
|
export let variableWithConversion = ([{ variable }, , unit]) => ({
|
|
|
|
unitConversion: { explanation: variable, unit: parseUnit(unit.value) }
|
2019-05-16 13:58:30 +00:00
|
|
|
})
|
|
|
|
|
2020-02-04 17:33:03 +00:00
|
|
|
export let temporalNumericValue = (variable, word, date) => ({
|
|
|
|
temporalValue: {
|
|
|
|
explanation: variable,
|
|
|
|
period: parsePeriod(word.value.slice(2), date)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
export let variable = ([firstFragment, nextFragment], _, reject) => {
|
|
|
|
const fragments = [firstFragment, ...nextFragment].map(({ value }) => value)
|
|
|
|
if (!nextFragment.length && ['oui', 'non'].includes(firstFragment)) {
|
2019-05-16 13:58:30 +00:00
|
|
|
return reject
|
2019-09-10 16:24:05 +00:00
|
|
|
}
|
2019-05-16 13:58:30 +00:00
|
|
|
return {
|
2020-11-05 16:48:19 +00:00
|
|
|
variable: fragments.join(' . ')
|
2019-05-16 13:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
export let number = ([{ value }]) => ({
|
2019-05-16 13:58:30 +00:00
|
|
|
constant: {
|
2019-09-10 16:24:05 +00:00
|
|
|
nodeValue: parseFloat(value)
|
2019-05-16 13:58:30 +00:00
|
|
|
}
|
2019-05-02 14:24:22 +00:00
|
|
|
})
|
|
|
|
|
2019-10-17 12:51:53 +00:00
|
|
|
export let numberWithUnit = ([number, , unit]) => ({
|
|
|
|
constant: {
|
|
|
|
nodeValue: parseFloat(number.value),
|
|
|
|
unit: parseUnit(unit.value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-28 11:03:23 +00:00
|
|
|
export let date = ([{ value }]) => {
|
2019-11-26 18:14:57 +00:00
|
|
|
return {
|
|
|
|
constant: {
|
|
|
|
type: 'date',
|
2020-01-06 18:20:58 +00:00
|
|
|
nodeValue: normalizeDateString(value)
|
2019-11-26 18:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 16:56:15 +00:00
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
export let boolean = nodeValue => () => ({
|
2019-05-16 13:58:30 +00:00
|
|
|
constant: {
|
|
|
|
type: 'boolean',
|
2019-09-10 16:24:05 +00:00
|
|
|
nodeValue
|
2019-05-16 13:58:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
export let string = ([{ value }]) => ({
|
2019-05-16 13:58:30 +00:00
|
|
|
constant: {
|
|
|
|
type: 'string',
|
2019-09-10 16:24:05 +00:00
|
|
|
nodeValue: value.slice(1, -1)
|
2019-05-16 13:58:30 +00:00
|
|
|
}
|
2019-05-02 14:24:22 +00:00
|
|
|
})
|