Skip to content
Logo
ghostry

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.

npm install @ghostry/aster
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 [yearArrow
, , ] = ..;
.; // "2026" }