Skip to main content

flm_core/stdlib/features/xnotes/
pointer.rs

1//! The content pointer of a list of xnotes: the element
2//! [`CONTENT_POINTER`](super::CONTENT_POINTER), whose payload is the lowering
3//! feature's [`ContentPointer`](crate::stdlib::backends::lowering::ContentPointer),
4//! and its general lowering rule [`PointerRule`].
5//!
6//! A content pointer is the payload of a synthesized node that shows, where
7//! the node is placed, content written elsewhere in the tree without copying
8//! it (see [content
9//! pointers](crate::guide::concepts_overview#content-pointers)). A list of
10//! xnotes shows each xnote's content in this way. Each entry of the list holds
11//! one node of the pointer element, whose payload refers to the node carrying
12//! the xnote and to the region of that node holding the content. Lowering the
13//! pointer lowers that region where the pointer is placed, through
14//! [`lower_content_pointer`], as content of the tree that holds the xnote.
15//! Everything inside the content is therefore lowered as the document's own
16//! content (a numbered list inside a footnote is numbered as the document
17//! numbered it, a labeled equation is an ordinary labeled reference point),
18//! and lowered once, in the list: the xnote's own rule writes its mark where
19//! the xnote is written, and never its content.
20//!
21//! The pointer is checked where it is lowered: a pointer that refers to a
22//! region that is not the content of an xnote of a kind the table of xnotes
23//! records (where the table is absent, an xnote of any kind is accepted) is
24//! refused ([`PointerRefused`]), since what it would show is not
25//! an xnote's content, and the reader would see that content twice.
26
27use std::sync::Arc;
28
29use techy::core::node::{BuildId, NodeRef};
30
31use crate::core::lowering::{
32    DefaultMark, Lower, LowerCx, LowerError, Lowerer, Lowering, LoweringDecl, LoweringRule,
33    NodeRegion, RegionShape,
34};
35use crate::lang::Flm;
36use crate::model::aspects::{BlockRole, BlockRoleAspect, FixedBlockRole};
37use crate::model::element::ElementSpec;
38use crate::stdlib::backends::lowering::{lower_content_pointer, lower_region_content};
39
40use super::diagnostics::PointerRefused;
41use super::stages::XNOTES;
42use super::{XnoteSource, CONTENT_POINTER, POINTER_RULE};
43
44/// The element value of a content pointer: a block of its own in its entry,
45/// so that the block-structure pass places no paragraph around it.
46pub(super) fn pointer_element() -> Arc<ElementSpec> {
47    Arc::new(
48        ElementSpec::new(CONTENT_POINTER)
49            .docs("The content of an xnote, shown where this node is placed.")
50            .with::<dyn BlockRoleAspect>(Arc::new(FixedBlockRole(BlockRole::Block))),
51    )
52}
53
54/// The general rule of the element [`CONTENT_POINTER`], [`POINTER_RULE`],
55/// marked default on every backend; the recomposed LaTeX, which places no
56/// list, uses a rule of its own that writes nothing
57/// ([`PointerLatexRule`](super::latex::PointerLatexRule)).
58pub(super) fn pointer_rule() -> LoweringRule {
59    LoweringRule::new(
60        POINTER_RULE,
61        CONTENT_POINTER,
62        LoweringDecl::new().reads_opt(XNOTES),
63        Arc::new(PointerRule),
64        DefaultMark::Everywhere,
65    )
66    .docs(
67        "a content pointer lowers to the content of the xnote it refers to, lowered where the \
68         pointer is placed; a pointer that refers to no xnote's content is refused and writes \
69         nothing",
70    )
71}
72
73/// The general lowering rule of the element
74/// [`CONTENT_POINTER`](super::CONTENT_POINTER),
75/// [`POINTER_RULE`](super::POINTER_RULE): the content that the pointer refers
76/// to ([`ContentPointer`](crate::stdlib::backends::lowering::ContentPointer)),
77/// lowered where the pointer is placed, as it is lowered inside the xnote,
78/// through [`lower_content_pointer`].
79///
80/// A pointer that refers to a region that is not the content of an xnote of a
81/// kind the table of xnotes records (where the table is absent, an xnote of
82/// any kind is accepted) is refused ([`PointerRefused`]) and writes nothing. A pointer that refers to a node the lowering cannot reach, or to
83/// a region the node does not have, is reported at the pointer as the
84/// lowering stage's `flm.lowering.region-not-found` and writes nothing.
85///
86/// The list of an xnote kind lowers the pointer of each entry itself, through
87/// the same check, to place the link back to the mark at the end of the
88/// content's last paragraph ([`ListRule`](super::ListRule)); this rule lowers
89/// a pointer placed anywhere else.
90#[derive(Debug, Default, Clone, Copy)]
91pub struct PointerRule;
92
93impl Lower for PointerRule {
94    fn lower(
95        &self,
96        node: NodeRef<'_, Flm>,
97        cx: &mut LowerCx<'_>,
98        lw: &mut dyn Lowerer,
99    ) -> Result<Lowering, LowerError> {
100        let staged = lower_pointed(cx, lw, &node, &mut lower_region_content)?;
101        Ok(Lowering::Emit(staged))
102    }
103}
104
105/// Lowers the content that the content pointer `pointer` refers to, shaped by
106/// `shape` ([`RegionShape`]), through [`lower_content_pointer`], once the
107/// check has found that the node it refers to carries an xnote whose content
108/// is that region. A pointer that the check refuses is reported
109/// ([`PointerRefused`]) and writes nothing. A pointer that refers to a region
110/// that cannot be found is reported at the pointer as the lowering stage's
111/// `flm.lowering.region-not-found`, and writes nothing either.
112///
113/// The calling rule must declare an optional read of [`XNOTES`], from which
114/// the check takes the xnote kinds the table records.
115///
116/// # Errors
117///
118/// [`LowerError`] when `pointer` carries no
119/// [`ContentPointer`](crate::stdlib::backends::lowering::ContentPointer),
120/// when the calling rule did not declare its read of [`XNOTES`], and any
121/// error that `shape` returns.
122pub(super) fn lower_pointed(
123    cx: &mut LowerCx<'_>,
124    lw: &mut dyn Lowerer,
125    pointer: &NodeRef<'_, Flm>,
126    shape: &mut RegionShape<'_>,
127) -> Result<Vec<BuildId>, LowerError> {
128    let span = pointer.span().clone();
129    let mut check = |lw: &mut dyn Lowerer, node: &NodeRef<'_, Flm>, region: NodeRegion| {
130        if is_xnote_content(lw, node, region)? {
131            return Ok(true);
132        }
133        let element = node.ext().element.name.to_string();
134        lw.error(PointerRefused { element, region: region.to_string() }, span.clone());
135        Ok(false)
136    };
137    lower_content_pointer(cx, lw, pointer, &mut check, shape)
138}
139
140/// Whether `node` carries an xnote whose content is `region`, of an xnote
141/// kind for which the table of xnotes has an entry (any kind, where the table
142/// is absent).
143fn is_xnote_content(
144    lw: &dyn Lowerer,
145    node: &NodeRef<'_, Flm>,
146    region: NodeRegion,
147) -> Result<bool, LowerError> {
148    let Some(source) = node.ext().element.aspect::<dyn XnoteSource>() else { return Ok(false) };
149    let xnotes = lw.get(XNOTES)?;
150    Ok(source.xnotes(node).iter().any(|xnote| {
151        xnote.content == region && xnotes.is_none_or(|table| table.by_kind.contains_key(&xnote.kind))
152    }))
153}