UNPKG

unified-query

Version:

Composable search input with autocompletion and a rich query-language parser for the Unified Data System

37 lines (36 loc) 1.44 kB
// src/lib/parsers/index.ts import { registry } from '../analyzers/registry.js'; import { tokenizeBody } from './parse.js'; import { scan } from './scanner.js'; /* --------------------------------------------------------- */ export function parse(input) { /* 1. low-level scan ---------------------------------------------------- */ const { segments, errors: scanErrors } = scan(input); const keywords = []; const allErrors = [...scanErrors]; /* 2. tokenise + semantic analyse for each segment ---------------------- */ for (const seg of segments) { if (seg.ignored) continue; // duplicates skipped seg.tokens = tokenizeBody(seg); // fills seg.tokens[] const kw = seg.keyword; // 'head' or real if (kw != 'head') { const analyse = registry[kw]; if (!analyse) throw new Error('Unknown keywords should be ignored (seg.ignored)'); const parsed = analyse(seg); keywords.push(parsed); } else { keywords.push({ ...seg, keyword: 'head', parsed: seg.raw }); } } /* collect semantic errors from every segment */ for (const seg of segments) allErrors.push(...seg.errors); return { segments, // now include tokens[] & segment.errors keywords, // high-level ParsedKeyword list errors: allErrors // flat array for CodeMirror linter }; }