unified-query
Version:
Composable search input with autocompletion and a rich query-language parser for the Unified Data System
44 lines (43 loc) • 1.33 kB
JavaScript
// lib/parsers/parse.ts
import { dateParser, datetimeParser, timeParser } from './date.js';
import { booleanParser } from "./boolean.js";
import { stringParser } from "./string.js";
import { uuidParser } from "./id.js";
import { utilityDateParser } from "./utility.js";
const defaultParsers = [
dateParser, datetimeParser, timeParser,
utilityDateParser, booleanParser, uuidParser
];
const STRING_ONLY = []; // falls through to stringParser
const ID_ONLY = [uuidParser]; // used by @id, @in
export const lexerProfiles = {
head: STRING_ONLY,
name: STRING_ONLY,
content: STRING_ONLY,
kind: STRING_ONLY,
id: ID_ONLY,
in: ID_ONLY,
sort: STRING_ONLY,
// every other keyword → defaultParsers (inherits dates etc.)
};
export function tokenizeBody(seg) {
const profile = lexerProfiles[seg.keyword]
?? defaultParsers;
const tokens = [];
let cur = seg.from + seg.keyword.length + 2;
for (const word of seg.body.split(/\s+/)) {
if (!word)
continue;
tokens.push(lexWord(word, cur, profile));
cur += word.length + 1;
}
return tokens;
}
function lexWord(word, pos, parsers) {
for (const fn of parsers) {
const tok = fn(word, pos);
if (tok)
return tok;
}
return stringParser(word, pos); // fallback
}