Documentation
¶
Overview ¶
Package minify rewrites ABNF grammar definitions (RFC 5234, with the RFC 7405 char-val extension) into their smallest equivalent form.
Minify strips the whitespace and comments that carry no meaning, which leaves the grammar itself untouched. Options.Simplify additionally rewrites the expressions of the grammar into shorter equivalent ones, which preserves the matched language but changes the structure of the paths that parsing produces.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpandLists ¶
ExpandLists rewrites the "#" list operators of a grammar into the standard ABNF they stand for, in place. The operator belongs to the HTTP specifications rather than to RFC 5234, so a definition holding one is read only by a parser that knows it; expanding makes the definition portable, at the cost of the length the operator saves.
func Minify ¶
Minify parses an ABNF grammar definition and returns an equivalent definition without the whitespace and comments that carry no meaning. Line endings are normalized to the CRLF endings that the specification requires, so definitions using LF endings are accepted too.
The result is verified: it is parsed back and re-emitted, and the two must agree.
func RenderRule ¶
RenderRule returns the minified definition of a single rule, ending with the CRLF that terminates it. Any transforms given are applied first, to a copy, so that the rule itself is left untouched. Passing a single transform shows what that transform alone would do.
Types ¶
type Options ¶
type Options struct {
// Simplify enables rewriting the expressions of the grammar into
// shorter equivalent ones: dropping redundant parentheses, joining
// adjacent literals, and expressing numeric values as strings.
//
// The matched language is preserved, but the shape of the paths that
// parsing with the resulting grammar produces is not: dropping a group
// removes a level from them. Code that walks those paths by structure,
// rather than by looking up rule names, may need adjusting.
Simplify bool
// ExpandLists writes the "#" list operators of RFC 9110 Section 5.6.1
// out as the standard ABNF they stand for, which makes the definition
// portable to a parser that only reads RFC 5234. It is off by default
// because the expansion is far longer than the operator, which is the
// opposite of what minification is for.
ExpandLists bool
}
Options holds the settings of a minification.
type Transform ¶
type Transform string
Transform identifies a simplification that rewrites the expressions of a grammar into shorter equivalent ones. Every transform preserves the matched language; none of them touches rule names, which are the interface of the grammar.
const ( // TransformGroup drops the parentheses of groups that do not need // them: "a / (b / c)" becomes "a / b / c", and "*(pchar)" becomes // "*pchar". TransformGroup Transform = "group" // TransformNumVal expresses a num-val as a char-val where that is // shorter: "%x4D.6F.6E" becomes %s"Mon". TransformNumVal Transform = "num-val" // TransformLiteral joins adjacent literals that a single element can // express: "%x0D %x0A" becomes "%x0D.0A". TransformLiteral Transform = "literal" )
func AllTransforms ¶
func AllTransforms() []Transform
AllTransforms returns every transform, in the order Minify applies them.