add rule definition with minimal dsl

This commit is contained in:
JOLIMAITRE Matthieu 2024-08-07 11:59:58 +02:00
parent fa524a5976
commit 084c5a2574
5 changed files with 48 additions and 15 deletions

View file

@ -1,6 +1,7 @@
import { Rule } from "./rule.ts";
import { PairElts, StructOfArr } from "./types.ts";
import { Extractor } from "./extractor.ts"
import { split_once } from "./utils.ts";
export function extr<A extends string[]>(...args: A): Extractor<StructOfArr<PairElts<A>, string>> {
const seps = [] as string[];
@ -18,7 +19,7 @@ export function extr<A extends string[]>(...args: A): Extractor<StructOfArr<Pair
return new Extractor(text => rule.get(text))
}
Deno.test("types", async () => {
Deno.test("extr_types", async () => {
const { assertEquals } = await import("https://deno.land/std@0.224.0/assert/assert_equals.ts");
const vars = extr()
.or(extr("bonjour, ", 'animal', ", poilu"))
@ -26,3 +27,34 @@ Deno.test("types", async () => {
.get("bonjour, le chêne, feuillu");
assertEquals({ vars }, { vars: { "arbre": "le chêne" } })
})
type ArrForDef<D extends string> =
D extends "" ? [] :
D extends `${infer W extends string}{{${infer V extends string}}}${infer R extends string}` ? [W, V, ...ArrForDef<R>] :
D extends `${infer W extends string}` ? [W] :
never;
export function def<D extends string>(def: D): Extractor<StructOfArr<PairElts<ArrForDef<D>>, string>> {
const result = [] as string[];
let rest = def as string;
while (true) {
const [word, var_rest] = split_once(rest, "{{");
if (var_rest === null) break;
const [var_, rest_] = split_once(var_rest, "}}");
if (rest_ === null) throw new Error(`Invalid rule near '${rest}'`);
result.push(word, var_);
rest = rest_;
}
if (rest.length > 0) result.push(rest);
const args = result as ArrForDef<D>;
return extr(...args);
}
Deno.test("def_types", async () => {
const { assertEquals } = await import("https://deno.land/std@0.224.0/assert/assert_equals.ts");
const vars = extr()
.or(def("bonjour, {{animal}}, poilu"))
.or(def("bonjour, {{arbre}}, feuillu"))
.get("bonjour, le chêne, feuillu");
assertEquals({ vars }, { vars: { "arbre": "le chêne" } })
})

View file

@ -1,5 +1,5 @@
import { StructOfArr } from "./types.ts";
import { zip } from "./utils.ts";
import { split_once, zip } from "./utils.ts";
export class Rule<V extends string[]> {
private separators: ({ kind: "word", word: string } | { kind: "end" })[];
@ -32,11 +32,3 @@ export class Rule<V extends string[]> {
return result as StructOfArr<V, string>;
}
}
function split_once(text: string, separator: string) {
const cursor = text.indexOf(separator);
if (cursor === -1) return [text, null] as const;
const left = text.slice(0, cursor);
const right = text.slice(cursor + separator.length);
return [left, right] as const;
}

View file

@ -12,3 +12,11 @@ export function next<T>(iterator: Iterator<T>) {
if (result === undefined) return null;
else return result as T;
}
export function split_once(text: string, separator: string) {
const cursor = text.indexOf(separator);
if (cursor === -1) return [text, null] as const;
const left = text.slice(0, cursor);
const right = text.slice(cursor + separator.length);
return [left, right] as const;
}