Expand description
The userdefs feature: it registers every user definition of its
configuration as an element, the rewrite stages that expand their use
sites, and the lowering rules that write the use sites a plan keeps.
§What the feature provides to an author
A user definition (Definition) is a macro, environment or special
character sequence that a document’s author or the host defines by
substitution: a name, an argument syntax, a template and the parsing modes
it is defined for. Its template is FLM text with placeholders (#1,
#{name}, #{body}) in the template
language. A use site of a
definition is a place in a document where the document uses it, such as
\hi{World} for a macro \hi; the use site is replaced by the definition’s
template, with the arguments written there in place of the placeholders. A
document’s body cannot define a macro: every user definition comes from this
feature’s configuration (UserdefsConfig), which an author writes as
settings (the keys macros, environments and specials,
UserdefsData), for example in a document’s front matter when the flm
program reads it. In settings, a definition gives its arguments under the
key args (DefinitionData::args), each argument’s syntax in one of two
forms. The structured syntax is a map whose key syntax holds the name of
a standard kind of argument and whose other keys hold the kind’s
parameters, beside the argument’s own keys: {syntax: delimited-group, delimiters: ["(", ")"], name: size} declares a group in parentheses named
size. An argument code is a shorthand for a structured syntax, written
under the key code of the map or alone: {code: "r()", name: size}
declares the same argument, and "{" a mandatory group. The language
guide describes definitions for authors:
author definitions and
when a definition is
expanded.
A definition’s expansion policy (ExpandPolicy) says when its use
sites are expanded: always, in every plan, or per-plan (the default),
as each plan decides. A plan for any backend other than
latex-recomposed expands the use sites of every definition, unless its
plan request selects another lowering rule by name. A plan for the
latex-recomposed backend keeps the use sites of a per-plan definition in
the tree and writes the definition once as a LaTeX definition in the style
file and each use site as a call of
it; when the definition cannot be written as one LaTeX definition, or its
template needs the characters of an argument, it expands each use site at
lowering instead (user definitions in the recomposed
LaTeX).
The module kept holds the rules that do this.
§Definitions are elements
Every definition of the configuration becomes an element declaration like
any other (UserDefinition::element_def): an element named
flm.userdefs.<kind>.<name> (element_name) that carries the definition
as its value (a UserDefinition), the
Expand aspect (an
aspect is behavior
attached to an element; this one is what the expand pass calls to expand a
use site) and the block role the definition declares. An extension written
in Rust registers the same things for an expandable element of its own
(element_name_under,
expand_in_tree_rule_for); nothing about user definitions is special to
the registry.
- The kind is part of the element name, because an element is found by its name: a macro and an environment may have the same written name, and they must be two elements with two names.
- A definition is available in the parsing modes it is defined for, and
nowhere else (
DefinitionModes): its element is declared available in those modes alone, so a use of it in another mode is an unknown command, like any name the registry does not define (reported in running text; kept as written without a report inside mathematics, where themathfeature accepts any unknown command). A definition for both modes applies in each mode where its template parses; a use in a mode where the template does not parse is an Error at that use. - One name may be declared twice, once for text mode and once for
mathematics mode, with a different template each time: the two never apply
in the same place, and each becomes an element of its own (the element of
a definition for mathematics mode alone has an extra
mathsegment in its name,element_name). Two definitions of one kind and one name whose modes overlap make the registry build fail, since only one of them could apply where they overlap. - A definition that replaces another feature’s command (marked with
Definition::overriding) also registers an override declaration (FeatureBuilder::override_callable). Without it, two features that define one command in overlapping modes make the registry build fail with an error that gives both, unless the feature order places the feature registered last explicitly after the other (the chainmymacros < userdefslets a user definition replace a command of a featuremymacros; seecrate::core::registry::order). A command that another feature declares weakly (ElementDef::weak), as a math processor declares its notation, needs neither: a user definition of\ketapplies in place of the processor’s\ket.
§Capabilities and stages
A capability (CapabilityDef) is a registered dotted name for a state
of processing that a stage brings about; a stage declares the capabilities
it provides and requires, and a caller demands the capabilities it wants
(stages, capabilities and
plans). The
feature registers:
USERDEFS(flm.userdefs), the parent capability: a decision about theper-plandefinitions has been made. Two capabilities refine it (refinement): each is one way of satisfying it, and a plan that demands both is refused.EXPAND(flm.userdefs.expand), the rewrite stage (RewriteStage: a stage that turns one tree into another before the trees are frozen) that expands the use sites of theper-plandefinitions. It publishes its trees in the trees tableEXPANDED(flm.userdefs.expanded; aTreesTable, the table in which a rewrite stage publishes the trees it produces), which is also the capability it provides, the refinement offlm.userdefsmarked as the default: a plan that demandsflm.userdefsalone expands the use sites.KEEP(flm.userdefs.keep), a marker stage that does nothing and providesKEPT(flm.userdefs.kept), the other refinement: the use sites stay in the tree for the lowering rules.EXPAND_ALWAYS(flm.userdefs.expand-always), the rewrite stage that expands the use sites of thealwaysdefinitions. It publishesALWAYS_EXPANDED(flm.userdefs.always-expanded), a capability that refines nothing, so it goes with either alternative;flm.userdefs.expandis ordered after it whenever both are in a plan.
Both rewrite stages run the expand pass
(expand) under the expansion
depth limit of the configuration; the module stages holds them.
§Lowering rules
Every definition registers its expand-in-tree lowering rule
(expand_in_tree_rule, module lowering), the default on every
backend. The rule requires the trees table of the rewrite stage that expands
the definition (flm.userdefs.expanded for a per-plan definition,
flm.userdefs.always-expanded for an always one) and never lowers a use
site, since the stage has replaced every one before lowering. It has two
purposes: the definition’s element has a rule on every backend, so that a
plan finds no element without a rule; and selecting the rule puts the
expansion into the plan. Every plan that lowers a registry holding an
always definition therefore holds flm.userdefs.expand-always.
A per-plan definition registers two more rules, for its use sites kept in
the tree (module kept): emit-call (emit_call_rule), the default on
the latex-recomposed backend alone, which writes a kept use site as a call
of the definition and the definition itself into the style file, or expands
the use site instead when the definition is not recomposable or its template
needs the characters of an argument; and
instantiate-at-lowering (instantiate_at_lowering_rule), never a
default, which a plan request selects by name to have each kept use site
expanded at lowering on any backend. Both require flm.userdefs.kept, so
selecting either puts the flm.userdefs.keep marker into the plan. The rule
selection order (SelectedRules) puts a
rule marked default on one backend before a rule marked default everywhere,
so the three rules of one definition never compete: latex-recomposed gets
emit-call and every other backend gets expand-in-tree. A plan that
demands flm.userdefs.kept on a backend where a per-plan definition’s
selected rule is expand-in-tree is refused when the plan is built, since
that rule requires the other alternative.
The function kept::register also registers, once, a peer declaration
(PeerName) of the latex-recomposed
backend and of each LaTeX construct element that the emit-call rules
stage (build into the lowered tree), so that a registry without the LaTeX
features still builds; the emit-call rules are then set aside (excluded
from rule selection).
§Templates are compiled when the registry is built
A template is parsed against the finished language, which exists only once
every feature has registered. The feature therefore registers a
late-initialization build hook (LateInit)
that compiles the templates of every definition over one template language
(TemplateLanguage)
built for all of them. Every diagnostic of a compilation (the warnings of a
definition that compiled, the errors of one that was refused) becomes a
build diagnostic of the registry
(FlmRegistry::build_diagnostics),
so a mistake in a template is reported once, when the registry is built. A
refused definition stays registered without compiled templates, and each of
its use sites is replaced by its literal source text and reported with
flm.userdefs.bad-definition. None of this makes the registry build fail.
The same hook then records, on every definition, whether it is
recomposable (whether a backend can write it as one definition of its own
output language; NotRecomposable lists the reasons it may not be) and
the arguments one call of it writes (kept::record_recomposability). The
hook reports nothing about recomposability: a refused definition was
reported when it was refused, and none of the other reasons that
NotRecomposable lists is a mistake in the document.
§Configuration
UserdefsConfig holds the definitions and the expansion depth limit. It is
the feature’s configuration, the plain Rust value the feature is constructed
from (Userdefs::new); it is fixed when the registry is built and cannot
vary per document or per render (settings, options and
configuration),
because the trees a run freezes depend on it. The feature gives its
configuration’s canonical bytes (UserdefsConfig::digest_bytes) as an
input of the registry digest, a number computed
from everything about a registry that can change what processing produces,
so that two registries that differ in a template do not share cached trees.
In settings written as data, the configuration is read as a
UserdefsData.
The feature creates a new UserDefinition for every definition each time
it registers, so one feature value may be registered into several
registries, each of which compiles its own templates against its own
language.
§Interactions with other features
- The
blocksfeature’s structure stage requiresflm.userdefsand is ordered afterflm.userdefs.always-expanded, because a use site that is not yet expanded can hide a paragraph break; its contract check reports a use site that remains in a tree the plan expanded. - The
math-passthroughfeature’s rules for thelatex-recomposedbackend write mathematics from its nodes rather than through the lowering rules, and write a use site of a user definition inside mathematics through this feature’s mathematics writer (kept::write_math_run). - The LaTeX family (
crate::stdlib::backends::latex, the parts every LaTeX backend shares) provides the rule that writes the placeholders of a template written into the style file (placeholder_rule).
§Items
Userdefs, the feature, constructed withUserdefs::newfrom aUserdefsConfig; the configuration’s data formsUserdefsData,NamedDefinition,DefinitionData,DefinitionContentandModeTemplates.element_nameandelement_name_under, the name of a definition’s element.FEATURE,USERDEFS,EXPAND,EXPANDED,KEEP,KEPT,EXPAND_ALWAYSandALWAYS_EXPANDED, the names of the feature, its capabilities, stages and trees tables.lowering: theexpand-in-treerule (ExpandInTree,expand_in_tree_rule,expand_in_tree_rule_for,rule_name).kept: the rules for kept use sites (EmitCall,InstantiateAtLoweringand their constructors and names), the recomposability computation, the conditionKeptTemplateDeclaresPointand the mathematics writer.- Re-exported from
crate::lang::rewrites::userdefs:Definition,DefinitionKind,DefinitionModes,ExpandPolicyandNotRecomposable.
Re-exports§
pub use self::kept::EMIT_CALL;pub use self::kept::EmitCall;pub use self::kept::INSTANTIATE_AT_LOWERING;pub use self::kept::InstantiateAtLowering;pub use self::kept::KeptTemplateDeclaresPoint;pub use self::kept::emit_call_rule;pub use self::kept::emit_call_rule_name;pub use self::kept::instantiate_at_lowering_rule;pub use self::kept::instantiate_rule_name;pub use self::lowering::EXPAND_IN_TREE;pub use self::lowering::ExpandInTree;pub use self::lowering::expand_in_tree_rule;pub use self::lowering::expand_in_tree_rule_for;pub use self::lowering::rule_name;pub use crate::lang::rewrites::userdefs::Definition;pub use crate::lang::rewrites::userdefs::DefinitionKind;pub use crate::lang::rewrites::userdefs::DefinitionModes;pub use crate::lang::rewrites::userdefs::ExpandPolicy;pub use crate::lang::rewrites::userdefs::NotRecomposable;
Modules§
- kept
- The lowering rules for a kept use site of a user definition: what a
lowering rule does with a use site of a
per-plandefinition that a plan keeps in the tree (the capabilityflm.userdefs.kept) instead of expanding it. - lowering
- The
expand-in-treelowering rule that every user definition registers: the rule of the definition’s element on every backend where the plan expands the definition’s use sites before lowering.
Structs§
- Definition
Data - A user definition as data gives it, under its name in the map of its kind
(
UserdefsData): the fields of aDefinitionexcept its name, its kind and its parsing modes, which the entry’s name, the map and the content give, and except its extra placeholders (Definition::extra_placeholder), which only a definition declared in Rust can have. A field not written keeps the value thatDefinition::newgives it. - Mode
Templates - The templates of a content given per mode (
DefinitionContent::ByMode); at least one is given. - Named
Definition - One entry of a map of
UserdefsData: the name the entry is written under, and the definition written there. - Userdefs
- The
userdefsfeature (FlmFeature): it registers one element per user definition with its lowering rules (one for analwaysdefinition, three for aper-planone), the capabilities, the two expansion rewrite stages, the keep marker stage, the peer declarations of the LaTeX constructs, the late-initialization build hook that compiles the templates, and the feature’s settings. See the module documentation. - Userdefs
Config - The configuration of the
userdefsfeature: the user definitions and the expansion depth limit. It is fixed when the registry is built; see the module documentation. - Userdefs
Data - The settings of the
userdefsfeature as data gives them: the user definitions in one map per kind, and the expansion depth limit.
Enums§
- Definition
Content - The
contentkey of aDefinitionData: one template for both parsing modes, or one template per mode.
Constants§
- ALWAYS_
EXPANDED - The trees table
flm.userdefs.always-expanded, in which theEXPAND_ALWAYSstage publishes its trees, and the capability of the same name that the stage provides. It refines nothing, so it goes with either alternative ofUSERDEFS. Theexpand-in-treerule of everyalwaysdefinition requires it. - EXPAND
- The name of the rewrite stage that expands the use sites of the
per-plandefinitions,flm.userdefs.expand. It publishes its trees inEXPANDEDand is ordered afterALWAYS_EXPANDEDwhenever both are in a plan. - EXPANDED
- The trees table
flm.userdefs.expanded, in which theEXPANDstage publishes its trees, and the capability of the same name that the stage provides: the refinement ofUSERDEFSmarked as the default. Theexpand-in-treerule of everyper-plandefinition requires it. - EXPAND_
ALWAYS - The name of the rewrite stage that expands the use sites of the
alwaysdefinitions,flm.userdefs.expand-always. It publishes its trees inALWAYS_EXPANDED. - FEATURE
- The feature’s registered name,
userdefs. It is also the middle segment of the names of the feature’s stages and diagnostics (flm.userdefs.expand,flm.userdefs.recursion-limit). - KEEP
- The name of the marker stage
flm.userdefs.keep: it does nothing and providesKEPT, so that a plan can choose to keep the use sites of theper-plandefinitions in the tree. - KEPT
- The capability
flm.userdefs.keptthat theKEEPstage provides: the refinement ofUSERDEFSunder which the use sites of theper-plandefinitions stay in the tree for the lowering rules. Theemit-callandinstantiate-at-loweringrules require it (kept). - USERDEFS
- The parent capability
flm.userdefs: a decision about the use sites of theper-plandefinitions has been made. Two capabilities refine it,EXPANDED(the default) andKEPT; a plan that demands both is refused. See the module documentation.
Functions§
- element_
name - Returns the name of the element of
definition:flm.userdefs.<kind>.<name>, where the kind ismacro,environmentorspecials, andflm.userdefs.<kind>.math.<name>for a definition that applies in mathematics mode alone. - element_
name_ under - Returns the name of the element of
definitionunder the prefixprefix:<prefix>.<kind>.<name>, and<prefix>.<kind>.math.<name>for a definition that applies in mathematics mode alone.element_nameis this function with the prefixflm.userdefs.