Rebasely Beta: data may be purged

justintsus-experiment Public

Text · 223 lines · 9.4 KiB · at 8e57616

// Reading a query (ast.tsus) from what a person typed: the limits first, then each term in input// order (lex.tsus splits them), then the one cross-term rule (a second, different sort: is a// conflict) and the removal of duplicates. A bad query is data: every error comes back, with the span// of its term, and nothing throws.import { Ok, Err, Some, None, tag, type Option, type Result, type Tagged } from "@tsus/runtime";import { fromNullable } from "@tsus/runtime/boundary";import { LIMITS, type ParseFailure, type QueryError, type QueryErrorCode, type SearchQuery, type Target, type Term } from "./ast.tsus";import { lex, type Raw } from "./lex.tsus";import { serializeTerm } from "./serialize.tsus";import { dateValue, isValue, labelValue, LABEL_MAX, noValue, sortValue, userValue } from "./values.tsus";type Bad = Tagged<"Bad", QueryError>;type Span = { readonly start: number; readonly end: number };const bad = (code: QueryErrorCode, span: Span, message: string): Result<Term, Bad> =>  Err(tag("Bad", { code, message, start: span.start, end: span.end }));type Key = "is" | "label" | "no" | "author" | "review-requested" | "sort" | "created" | "updated";const keyOf = (k: string): Option<Key> =>  match (k) {    "is" => Some<Key>("is"),    "label" => Some<Key>("label"),    "no" => Some<Key>("no"),    "author" => Some<Key>("author"),    "review-requested" => Some<Key>("review-requested"),    "sort" => Some<Key>("sort"),    "created" => Some<Key>("created"),    "updated" => Some<Key>("updated"),    _ => None,  };// Which list a key works on.const keyTargets = (k: Key): "both" | Target =>  match (k) {    "label" => "issues",    "no" => "issues",    "review-requested" => "pulls",    "is" => "both",    "author" => "both",    "sort" => "both",    "created" => "both",    "updated" => "both",  };const negatable = (k: Key): boolean =>  match (k) {    "is" => true,    "label" => true,    "author" => true,    "review-requested" => true,    "no" => false,    "sort" => false,    "created" => false,    "updated" => false,  };const listName = (t: Target): string =>  match (t) {    "issues" => "issues",    "pulls" => "pull requests",  };const keysFor = (t: Target): string =>  match (t) {    "issues" => "is:, label:, no:, author:, sort:, created: or updated:",    "pulls" => "is:, author:, review-requested:, sort:, created: or updated:",  };const DATE_HELP = "Use a day (2026-10-01), a comparison (>=2026-10-01), a range (2026-10-01..2026-10-31, either end may be *) or an age with a comparison (<7d, >=2w, <12h).";const badValueMessage = (k: Key, t: Target): string =>  match (k) {    "is" => t === "pulls" ? "is: takes open, closed, merged or draft." : "is: takes open or closed.",    "label" => `A label is lowercase letters, digits, spaces, dots, dashes or underscores, starting with a letter or digit, at most ${LABEL_MAX} characters.`,    "no" => "no: takes label, as in no:label.",    "author" => "author: takes @me, a user id or the name of one of this organization's agents.",    "review-requested" => "review-requested: takes @me, a user id or the name of one of this organization's agents.",    "sort" => t === "pulls"      ? "sort: takes created-desc, created-asc, updated-desc or updated-asc."      : "sort: takes created-desc, created-asc, updated-desc, updated-asc, comments-desc or comments-asc.",    "created" => `That is not a date filter. ${DATE_HELP}`,    "updated" => `That is not a date filter. ${DATE_HELP}`,  };const example = (k: Key): string =>  match (k) {    "is" => "is:open",    "label" => "label:bug",    "no" => "no:label",    "author" => "author:@me",    "review-requested" => "review-requested:@me",    "sort" => "sort:updated-desc",    "created" => "created:>=2026-10-01",    "updated" => "updated:<7d",  };const isTerm = (raw: string, t: Target, negated: boolean): Option<Term> =>  isValue(raw, t).map((state): Term => ({ tag: "Is", negated, state }));const valueOf = (k: Key, raw: string, t: Target, negated: boolean): Option<Term> =>  match (k) {    "is" => isTerm(raw, t, negated),    "label" => labelValue(raw).map((label): Term => ({ tag: "Label", negated, label })),    "no" => noValue(raw) ? Some<Term>({ tag: "NoLabel" }) : None,    "author" => userValue(raw).map((user): Term => ({ tag: "Author", negated, user })),    "review-requested" => userValue(raw).map((user): Term => ({ tag: "ReviewRequested", negated, user })),    "sort" => sortValue(raw, t).map((sort): Term => ({ tag: "Sort", sort })),    "created" => dateValue(raw).map((filter): Term => ({ tag: "Date", field: "created", filter })),    "updated" => dateValue(raw).map((filter): Term => ({ tag: "Date", field: "updated", filter })),  };const checkValue = (k: Key, raw: string, t: Target, negated: boolean, span: Span): Result<Term, Bad> =>  match (valueOf(k, raw, t, negated)) {    Some(term) => Ok(term),    None => bad(k === "created" || k === "updated" ? "bad_date" : "bad_value", span, badValueMessage(k, t)),  };const checkKey = (k: Key, raw: string, t: Target, negated: boolean, span: Span): Result<Term, Bad> => {  const on = keyTargets(k);  if (on !== "both" && on !== t) return bad("unsupported_qualifier", span, `${k}: works on ${listName(on)}, not on ${listName(t)}.`);  if (raw === "") return bad("empty_value", span, `${k}: needs a value, as in ${example(k)}.`);  if (negated && !negatable(k)) return bad("negation_not_allowed", span, `${k}: cannot be negated. Remove the "-" in front of it.`);  return checkValue(k, raw, t, negated, span);};const qualifier = (written: string, raw: string, t: Target, negated: boolean, span: Span): Result<Term, Bad> => {  const name = written.toLowerCase();  return match (keyOf(name)) {    Some(k) => checkKey(k, raw, t, negated, span),    None => bad("unknown_qualifier", span, `There is no ${name}: qualifier for ${listName(t)}. Use ${keysFor(t)}, or put the text in quotes to search for it.`),  };};const QUALIFIER = /^[A-Za-z]+(-[A-Za-z]+)*:/;const NUMBER = /^#[1-9][0-9]{0,8}$/;const NON_WS = /\S/;const text = (s: string, negated: boolean): Result<Term, Bad> => Ok({ tag: "Text", negated, text: s });const word = (w: Extract<Raw, { tag: "Word" }>, t: Target): Result<Term, Bad> => {  if (QUALIFIER.test(w.text)) {    const colon = w.text.indexOf(":");    return qualifier(w.text.slice(0, colon), w.text.slice(colon + 1), t, w.negated, w);  }  if (NUMBER.test(w.text)) {    return w.negated ? bad("negation_not_allowed", w, "A #number cannot be negated. Remove the \"-\" in front of it.") : Ok({ tag: "Number", number: Number(w.text.slice(1)) });  }  return text(w.text, w.negated);};const phrase = (p: Extract<Raw, { tag: "Phrase" }>): Result<Term, Bad> =>  NON_WS.test(p.text) ? text(p.text, p.negated) : bad("empty_value", p, "These quotes hold nothing to search for. Put a word or phrase between them.");const QUOTE_HELP = "A quote starts a phrase (\"needs review\") or a qualifier's value (label:\"needs review\"), and the term ends after the closing quote.";const classify = (r: Raw, t: Target): Result<Term, Bad> =>  match (r) {    Word(w) => word(w, t),    Phrase(p) => phrase(p),    Keyed(k) => qualifier(k.prefix, k.text, t, k.negated, k),    Broken(b) => b.code === "unterminated_quote"      ? bad("unterminated_quote", b, "This quote is never closed. Add the closing \" or remove the opening one.")      : bad("unexpected_quote", b, QUOTE_HELP),  };const failure = (errors: readonly QueryError[]): Result<SearchQuery, ParseFailure> => Err({ tag: "Invalid", errors });// The limits, checked before anything else and reported alone.const limitError = (input: string, raws: readonly Raw[]): Option<QueryError> => {  if (input.length > LIMITS.length) {    return Some({ code: "too_long", message: `This search is ${input.length} characters long; the most is ${LIMITS.length}. Shorten it.`, start: 0, end: input.length });  }  return fromNullable(raws.at(LIMITS.terms)).map((r): QueryError => ({    code: "too_many_terms", message: `This search has ${raws.length} terms; the most is ${LIMITS.terms}. Remove some from here on.`, start: r.start, end: input.length,  }));};const sortOf = (t: Term): Option<string> =>  match (t) {    Sort(s) => Some(s.sort),    _ => None,  };const conflict = (r: Raw): QueryError => ({  code: "conflict", message: "A search has one sort: order. Remove this one or the earlier one.", start: r.start, end: r.end,});// Terms in order: errors collected, a second different sort: a conflict, duplicates dropped.const collect = (raws: readonly Raw[], t: Target): Result<SearchQuery, ParseFailure> => {  const errors: QueryError[] = [];  const terms: Term[] = [];  const seen = new Set<string>();  let sort: Option<string> = None;  for (const r of raws) {    const c = classify(r, t);    if (c.isErr()) {      errors.push(c.error.value);    } else {      const s = sortOf(c.value);      if (s.isSome() && sort.isSome() && s.value !== sort.value) {        errors.push(conflict(r));      } else {        if (s.isSome()) sort = s;        const key = serializeTerm(c.value);        if (!seen.has(key)) {          seen.add(key);          terms.push(c.value);        }      }    }  }  return errors.length > 0 ? failure(errors) : Ok({ target: t, terms });};export const parseQuery = (input: string, target: Target): Result<SearchQuery, ParseFailure> => {  const raws = input.length > LIMITS.length ? [] : lex(input);  return match (limitError(input, raws)) {    Some(e) => failure([e]),    None => collect(raws, target),  };};