
aster
One grammar, two outputs: a RegExp for validation and a parser that returns a typed AST.
Define a grammar once, as named rules rather than regex syntax. Use the regex where you need a fast yes or no, such as a JSON Schema pattern, and the parser where you need the structure. Both accept exactly the same strings.
import { , , , , } from "@ghostry/aster";
const { , } = ();
const = ()
.("digit", () => ("0", "9"))
.("year", ({ }) => ([, , , ]))
.("month", ({ }) => ([, ]))
.("date", ({ , }) => ([, ("-"), ]))
.();
(, "date"); // "^[0-9][0-9][0-9][0-9]-[0-9][0-9]$"
const = (, "date")("2026-11");
if (.) {
const [year, , ] = ..;
.; // "2026"
}toPattern writes an ECMA-262 pattern, and toParser builds a parser. They come from the same rules, so they never disagree about what matches.
Write a format as named rules built from plain functions. Aster does the escaping, grouping, and quantifiers, and TypeScript checks every rule name.
Every rule becomes a node with its name, its text, and a source span. Its children are typed from the rule's body, and every loop iteration is kept.
toPattern refuses a pattern that a backtracking engine can take superlinear time on, and analyze shows an input that proves it.
The parser runs in time linear in the input for every grammar, so it is safe on untrusted input, and a failed parse says where, what could have come next, and the line and column.
Patterns are anchored, capture-free, and mean the same under the u and v flags, so they work as a JSON Schema pattern as written.