![]() |
The DRD data model | ![]() |
Declared in
Data/Drd/drd_info.hpp
, with three data members:
A name used only for printing ("tm" for the standard DRD, "none" for style DRDs, the buffer title for editor DRDs, "preamble" for document DRDs).
The properties of each tag.
The environment (typically, all macro definitions of the style) from which the heuristic properties were computed, set by set_environment. It is also used as a fallback by get_syntax.
drd_info is the usual reference counted handle (CONCRETE), so copying a drd_info shares the object.
drd_info (string name)
The second constructor creates a DRD which inherits from base. The member info is a relative hash map (
Kernel/Containers/rel_hashmap.hpp
): a list of hash maps in which a lookup info[l] returns the entry of the first map that contains l, and an access for writing info(l) first copies the entry from the base into the local map. Hence modifications never leak into the base DRD. All DRDs are directly or indirectly built on top of std_drd.
For a label which is not described anywhere in the chain, info[l] returns the default value of the hash map, tag_info (): arity zero (ARITY_NORMAL), no child records, type TYPE_REGULAR, border BORDER_YES. Code which needs to know whether a label is described at all uses drd_info_rep::contains (string l), which looks through the whole chain. For instance, correct_node (
Data/Tree/tree_modify.cpp
) and edit_dynamic_rep::insert_argument only enforce arities of described labels.
All setters follow the same pattern: return immediately if the property is frozen (see below), make a local copy of the record if necessary, and modify it. Setters for children take a physical index nr into the array of child records (see the child layout), and silently do nothing if nr is out of range. The high-level queries (get_type_child, is_accessible_child, get_writability_child, get_env_child, get_child_name) take a tree and a logical child number instead, and do the translation themselves.
A tag_info (
Data/Drd/tag_info.hpp
) consists of
the properties of the tag itself;
the properties of the children, in a compressed layout;
either the empty string, or an attr tree with further named attributes (names and syntax).
Creates a record with the given arity fields, arity mode am and child mode cm. The array ci receives \(0\) entries if arity+extra is zero, one entry for CHILD_UNIFORM, two for CHILD_BIFORM and arity+extra for CHILD_DETAILED. With frozen set, the arity, border, block, with-like and var-type properties and all child properties are frozen (but see the pitfalls about freeze_type).
The member functions inner_border, outer_border, with_like, var_parameter, var_macro_parameter, type, accessible, hidden, disable_writable, enable_writable, locals, name and long_name modify the record and return it again as a tag_info, so that they can be chained in the declarative style of
drd_std.cpp
:
fixed (2) -> name ("fraction") ->
accessible (0) -> locals (0, "math-display", "false") |
Note that accessible (i) and hidden (i) also set the type of the child to TYPE_REGULAR, so a type declaration must come after them to take effect.
parent_info is a 32-bit bit field. In the order of the fields (which is also the order of the bits in the serialized form, starting from the least significant bit):
|
|||||||||||||||||||||||||||||||||
The field type is the type of the value of the tag: regular content for most tags, but TYPE_NUMERIC for plus, TYPE_LENGTH for cm-length, TYPE_GRAPHICAL for line, TYPE_COLOR for rgb-color, and so on. Getters: get_type (tree_label) and get_type (tree); setter set_type (tree_label, int). The type of a variable (see var_type) is the type of its value. The list of types is given below.
The three fields arity_mode, arity_base and arity_extra describe the set of admissible arities \(n\):
\(n =\operatorname{base}+\operatorname{extra}\) (fixed arity).
\(\operatorname{base} \leqslant n \leqslant \operatorname{base}+\operatorname{extra}\) (optional trailing arguments; the comment in
tag_info.hpp
says \(<\) but the code uses \(\leqslant\)).
\(n =\operatorname{base}+ k \cdot \operatorname{extra}\) for \(k \geqslant 0\): a fixed prefix followed by repeated groups of \(\operatorname{extra}\) children.
As ARITY_REPEAT, but the repeated groups come first and the \(\operatorname{base}\) fixed children come last, as in with (pairs of variables and values, followed by the body).
For ARITY_VAR_REPEAT, set_arity (l, arity, extra, am, cm) stores its arguments swapped (arity_base= extra, arity_extra= arity), so that arity is the length of the repeated group and extra the number of trailing children, as for the helper var_repeat of
drd_std.cpp
. The derived queries are:
int get_minimal_arity (tree_label l)
\(\operatorname{base}+\operatorname{extra}\) for a fixed arity; \(\operatorname{base}\) otherwise for the minimum; \(\operatorname{base}+\operatorname{extra}\) for ARITY_OPTIONS and 0x7fffffff for the two repeat modes for the maximum.
Used to validate trees (drd_correct, correct_node) and to find the arity of a new tag (edit_dynamic_rep::make_compound takes the smallest admissible positive arity, or zero for fixed arity \(0\)).
For a tag of arity \(n\): never for a fixed arity; for ARITY_OPTIONS at any position \(\geqslant \operatorname{base}\) as long as \(n <\operatorname{base}+\operatorname{extra}\); for ARITY_REPEAT at positions inside the prefix or at the start of a group; for ARITY_VAR_REPEAT at the start of a group or inside the trailing part. Used by insert_argument and remove_empty_argument (
Edit/Modify/edit_dynamic.cpp
) to implement structured insertion of arguments.
True if the arity mode is not ARITY_NORMAL, except for document, para, concat, table and row. With hack set, all non-primitive tags are considered dynamic (marked FIXME in the source). The editor (edit_dynamic_rep::find_dynamic) uses the default, the glue tree-is-dynamic? passes false.
get_old_arity returns the arity for a fixed arity and \(- 1\) otherwise; get_arity_mode, get_arity_base, get_arity_extra, get_child_mode give direct access to the fields, and get_nr_indices returns the number of child records.
The field child_mode determines how a logical child number \(i\) of a tree with \(n\) children is mapped to an index into the array ci; this is implemented by tag_info_rep::get_index (int child, int n):
All children share ci[0].
Two records. For all arity modes except ARITY_VAR_REPEAT, ci[0] describes the first \(\operatorname{base}\) children and ci[1] the others; for ARITY_VAR_REPEAT, ci[0] describes the repeated part and ci[1] the \(\operatorname{base}\) trailing children. So for with, ci[0] is the record of the variable/value pairs and ci[1] that of the body.
One record per child of the “period”: for fixed and optional arities, \(i\) itself; for ARITY_REPEAT, the prefix children have their own records and the children of a group cycle through the next \(\operatorname{extra}\) records; for ARITY_VAR_REPEAT the group comes first.
If the computed index falls outside ci (for instance for a tree with more children than its declared arity), the high-level queries answer conservatively: not accessible (except in source mode), type TYPE_INVALID, writability WRITABILITY_DISABLE, empty name, and an empty environment "" from get_env_child.
The field border_mode says whether the cursor may stand at the boundaries of the tag. It is interpreted bitwise (BORDER_NO is BORDER_INNER|BORDER_OUTER):
The cursor may not be positioned on the tag itself (paths ending with 0 or 1 relative to t); it has to go inside a child. This is the case for document, concat, table, row, cell, hidden, …
The cursor may not be positioned at the start of the first accessible child or the end of the last one; the positions outside the tag are used instead (see is_accessible_cursor in
Data/Tree/tree_cursor.cpp
).
var_without_border (tree_label l) is true for non-primitive tags with the BORDER_INNER bit; the editor treats such macros like concat when selecting (edit_select_rep::select_enlarge in
Edit/Replace/edit_select.cpp
, selection_adjust_border in
Data/Tree/tree_select.cpp
) and when removing structure (edit_text_rep::remove_structure_upwards in
Edit/Modify/edit_delete.cpp
). The heuristics never set the border; it is only changed by drd_std.cpp and by drd-props.
Meant to tell whether the tag is block or inline content (BLOCK_OR: a block if one of its children which admit both is a block). The getters and setters are declared in
drd_info.hpp
but not implemented, and no code reads the field: it is currently unused.
The tag only modifies the environment of its last child, like with, with-package or a macro such as strong. Queried by is_with_like (tree t) (which also requires N(t)>0); used by the correction routines of
Data/Tree/tree_analyze.cpp
and
tree_correct.cpp
(with-correct and friends), by the heuristics (heuristic_with_like) and by make_compound, which delegates the insertion of with-like tags to the Scheme routine with-like-check-insert.
VAR_MACRO for tags (the default), VAR_PARAMETER for environment variables and VAR_MACRO_PARAMETER for macros which are used as parameters (a zero-argument macro holding a length or a localized string). The free functions is_macro and is_parameter (
Data/Tree/tree_traverse.cpp
, glue tree-label-macro? and tree-label-parameter?) test var_type != VAR_PARAMETER, resp. var_type != VAR_MACRO; they are used to build the focus menus of style parameters in
generic/generic-menu.scm
.
Each property has a freeze_* bit. A frozen property is never changed by the corresponding set_* method, which simply returns. The methods freeze_type, freeze_arity, freeze_border, freeze_with_like, freeze_var_type (for the tag) and freeze_type, freeze_accessible, freeze_writability, freeze_env (for a child) set them. Freezing is how explicit declarations win over heuristics: drd-props freezes every property it sets, and init_std_drd freezes the arity and border of all primitives, so that a style cannot accidentally change the arity of frac by defining a macro frac.
child_info is also a bit field:
|
|||||||||||||||||||||
accessible says whether the cursor may enter the child. ACCESSIBLE_ALWAYS children are ordinary editable content (the numerator of a fraction, the body of a with); ACCESSIBLE_NEVER children are only editable in source mode or through the focus bar (the URL of a hyperlink, the width of an image); ACCESSIBLE_HIDDEN children are accessible only when the global access mode allows hidden content (the body of hidden, which search must still traverse). The decision is taken by
Translates i with get_index and compares the stored value with the global access mode (
Data/Drd/drd_mode.hpp
): DRD_ACCESS_NORMAL accepts only ACCESSIBLE_ALWAYS, DRD_ACCESS_HIDDEN also ACCESSIBLE_HIDDEN, and DRD_ACCESS_SOURCE accepts every existing child. For an extern tree whose first child is a string f, the record of the pseudo-label extern:f is used, which allows style files to declare the accessibility of the arguments of individual Scheme functions with drd-props (see
packages/customize/math/math-check.ts
).
Related queries: is_accessible_path (t, p) (all children along a path), all_accessible (l) (all records are ACCESSIBLE_ALWAYS, and there is at least one) and none_accessible (l). The low-level get_accessible (l, nr) and set_accessible (l, nr, mode) work on physical indices. How the access modes are switched is explained in style and document DRDs.
The accessibility in the DRD must agree with the boxes produced by the typesetter: a child declared accessible but typeset as a decoration (or conversely) leads to erratic cursor movement. See the macro expansion chapter for details.
writability implements read-only regions: WRITABILITY_DISABLE makes the whole subtree of the child read-only, except for descendants whose writability is re-enabled with WRITABILITY_ENABLE. The routine is_accessible_cursor (
Data/Tree/tree_cursor.cpp
) switches the global writable mode (set_writable_mode) to DRD_WRITABLE_INPUT when it enters a disabled child and back to DRD_WRITABLE_NORMAL when it enters an enabled one; in DRD_WRITABLE_INPUT mode no cursor position in a string is accessible (outside source mode). The macros disable-writability and enable-writability of
packages/gui/gui-widget.ts
are declared this way. Query: get_writability_child (t, i).
block (BLOCK_REQUIRE_*) was meant to say whether a child must contain block or inline content. Like the corresponding parent field it is never set nor read. Note that its numerical default is \(0\), i.e. BLOCK_REQUIRE_BLOCK.
env describes how the environment changes when going from the tag into the child, for instance mode=math for the body of equation or math-display=false for scripts. The value is an attr tree (attr var-1 val-1 … var-n val-n) (the default is the empty with tree, meaning no change). To keep child_info small, these trees are stored in a global table and the record contains only their index:
int drd_encode (tree t)
drd_encode returns the index of t in a global table, adding it if necessary (the table is never shrunk; more than \(2^{16}\) different environments trigger an assertion).
A value (arg i) with an integer i refers to the \(i\)-th child of the same tree: the heuristics produce such values when an argument is used in a with around another argument, for example for
<assign|my-code|<macro|lang|body|<with|prog-language|lang|body>>> |
the environment of child \(1\) is (attr prog-language (arg 0)).
Returns env (an attr tree) updated with the changes for child i: for with the variables of the tree itself are merged in; otherwise the stored environment is decoded, references (arg j) are replaced by copies of t[j], and the result is merged with drd_env_merge. Values are not evaluated: the caller decides whether to evaluate them (the editor does so in typeset_exec_until, most other callers only compare strings). The result is the empty string if i is outside the layout.
get_env_child (t, i, var, val) returns the value of a single variable (with default val), and get_env_descendant (t, p, env) and get_env_descendant (t, p, var, val) follow a whole path. The free functions drd_env_write, drd_env_merge and drd_env_read manipulate the attr trees (drd_env_write keeps the variables sorted). The low-level access is get_env (l, nr), set_env (l, nr, env) and freeze_env (l, nr).
The tree extra stores further attributes as an attr list, through set_attribute and get_attribute:
The name of the tag shown to the user (footer, focus bar, menus). get_name (l) defaults to the label itself and get_long_name (l) to the name.
The names of the children, by physical index. The heuristics use the parameter names of the macro. Queried by get_child_name (t, i) and get_child_long_name (t, i), which translate the logical index.
A tree describing how the tag should be understood by the mathematical parser (packrat grammars). It may be a symbol (<int>) or a macro which is applied to the arguments of the tag. get_syntax (l) falls back on the definition of the tag in env, i.e. a macro is by default “parsed as its expansion”; get_syntax (tree t, path p) performs the substitution of the arguments (wrapping them in quasi trees with their paths if p is given). Consumers:
System/Language/packrat_serializer.cpp
and symbol_type in
Data/Tree/tree_analyze.cpp
.
The constants TYPE_* are used both for the value of a tag and for its children. drd_decode_type and drd_encode_type convert between the constants and the names used by drd-props and by the glue (tree-child-type, tree-label-type).
Ordinary content.
Content without a well-defined type; the default for children.
Raw binary data (child of raw-data); has no name, so drd_decode_type returns unknown.
Names of environment variables and macro arguments.
Alternating variables and values, as in with and attr. get_type_child resolves it to TYPE_VARIABLE for even and TYPE_REGULAR for odd children; it has no name.
Scalars (boolean, integer, string, length, numeric).
Program code, identifiers (labels, keys), URLs and colors (code, identifier, url, color).
Graphical objects, points, constraints and graphical identifiers.
Graphical effects, animations, durations and font sizes.
Special values; TYPE_UNKNOWN is the initial value used by the heuristics. TYPE_INVALID (\(- 1\)) is returned by get_type_child for a child outside the layout and is never stored.
The child types are used for syntax coloring in source mode (highlight in
Typeset/Env/env_inactive.cpp
maps them to src-regular, src-var, src-length, …), by the focus bar to choose an input field for inaccessible children (type->format in
generic/generic-menu.scm
), by the correction routine is_correctable_child, and by the heuristics to infer the types of macro arguments.
A DRD can be converted to a tree and back, which is how style DRDs are stored in the disk cache:
parent_info::operator tree packs the bit field into an integer, written as a decimal string.
child_info::operator tree does the same with all fields but env (16 bits); if the environment is not empty, the result is the environment tree with the integer appended as last child.
tag_info::operator tree returns (tuple pi (tuple ci …)), followed by extra if it is not empty.
drd_info_rep::get_locals returns (collection (associate label info) …) for the local entries only (those which differ from the base), and set_locals installs such a collection.