unified-query
Version:
Composable search input with autocompletion and a rich query-language parser for the Unified Data System
37 lines (36 loc) • 1.08 kB
JavaScript
/**
* Semantic analyser for the `@date` keyword.
* Accepts only tokens produced by `dateParser` (kind === "date").
* Everything else is reported as an invalid token.
*/
export function analyzeDate(seg) {
const dates = [];
const errors = [];
for (const tok of seg.tokens) {
if (tok.kind === 'date') {
dates.push(tok.value);
continue;
}
errors.push(invalid(tok));
}
// surface the semantic errors in the segment (for CM linter)
seg.errors.push(...errors);
return {
keyword: 'date',
parsed: dates,
from: seg.from,
to: seg.to,
raw: seg.raw,
};
}
/* -------------------------------------------------------------------------- */
/* helpers */
/* -------------------------------------------------------------------------- */
function invalid(tok) {
return {
message: `invalid token "${tok.raw}" for @date`,
token: tok.raw,
from: tok.from,
to: tok.to,
};
}