ABNF
@hyperjump/abnf compiles grammars written in Augmented Backus-Naur Form
(ABNF) into regular expressions. ABNF is the notation that RFCs use for
syntax, so you can paste an RFC’s rules in and get a RegExp that matches
what the RFC describes.
The following example compiles RFC 3339’s full-date:
import { abnf } from "@hyperjump/abnf";
const fullDate = abnf()` full-date = date-fullyear "-" date-month "-" date-mday date-fullyear = 4DIGIT date-month = 2DIGIT date-mday = 2DIGIT`;
fullDate.test("2004-01-15"); // truefullDate.test("2004-1-15"); // falseThe package offers the following:
- RFC 5234 and RFC 7405. Rules, repetition, options, groups,
numeric values, and case-insensitive and case-sensitive strings, with
RFC 5234’s core rules, like
ALPHAandDIGIT, built in. - A regular expression. The result is a
RegExpwith theuflag, as fast as one written by hand, which you can test strings with or compile once at build time. - What you know about the input. A context can supply rules to reference, characters that end a match, and characters the input can’t have. For more information, see Contexts.
- Small. No dependencies. It has TypeScript types and works in Node.js and browsers.
@hyperjump/uri
uses it for the patterns of URI schemes.
Contents
Section titled “Contents”- Install: setup for Node.js, browsers, and TypeScript
- Write grammars: rule lists, expressions, sharing rules, and errors
- Contexts: rules to reference, delimiters, and what the input can have