Skip to main content

flm_core/stdlib/features/cells/
parser.rs

1//! The parser of a cell's styles argument ([`CellStylesParser`]): an
2//! optional group delimited by `<` and `>`, holding document content.
3//!
4//! `\cell<H l>{…}` and `\celldata<H, l>{…}` write their style tokens in angle
5//! brackets. The styles argument is an ordinary argument declaration that
6//! carries this parser, which reads the group as the language reads an
7//! optional `[…]` group: the opening `<` may follow whitespace or a comment,
8//! a `>` inside a `{…}` group belongs to that group, an argument that is not
9//! written is absent, and an opening `<` without its `>` gets the language's
10//! ordinary report of an unclosed group. The group holds content (the tokens
11//! as characters, a value in braces as a group), which the table's reader
12//! ([`read_grid`](super::read_grid)) reads at lowering.
13
14use std::fmt;
15use std::sync::Arc;
16
17use techy::core::constructs::{
18    ArgumentParser, ConstructParserResult, OptionalGroupArgumentParser, ParseContext, ParsedArgumentNodes,
19};
20use techy::core::specs::ArgumentSpec;
21use techy::core::token::GroupRule;
22
23use crate::lang::mint::{DefinitionShape, FlmArgumentParser};
24use crate::lang::{Flm, FlmGroupType};
25
26/// The identity of the parser in the registry digest, `flm.cells.styles`.
27pub const IDENTITY: &str = "flm.cells.styles";
28
29/// The parser of the styles argument of `\cell` and `\celldata`: an optional
30/// `<…>` group of document content, built on techy's optional group parser
31/// with the delimiters `<` and `>`. See the [module documentation](self).
32///
33/// What it states through [`FlmArgumentParser`]: its identity is
34/// [`IDENTITY`]; it has no shape in a recomposed user definition, since no
35/// user definition declares such an argument; its description is "an
36/// optional `<…>` group of style tokens". An argument that is not written is
37/// absent.
38pub struct CellStylesParser {
39    angles: OptionalGroupArgumentParser<Flm>,
40}
41
42impl CellStylesParser {
43    /// Creates the parser.
44    pub fn new() -> CellStylesParser {
45        let rule = Arc::new(GroupRule {
46            group_type: FlmGroupType::Content,
47            open: String::from("<"),
48            close: String::from(">"),
49        });
50        CellStylesParser { angles: OptionalGroupArgumentParser::new(rule) }
51    }
52}
53
54impl Default for CellStylesParser {
55    fn default() -> CellStylesParser {
56        CellStylesParser::new()
57    }
58}
59
60impl fmt::Debug for CellStylesParser {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        f.write_str("CellStylesParser")
63    }
64}
65
66impl ArgumentParser<Flm> for CellStylesParser {
67    fn parse_argument(
68        &self,
69        cx: &mut ParseContext<'_, '_, Flm>,
70        spec: &ArgumentSpec<Flm>,
71    ) -> ConstructParserResult<Flm, Option<ParsedArgumentNodes<Flm>>> {
72        self.angles.parse_argument(cx, spec)
73    }
74}
75
76impl FlmArgumentParser for CellStylesParser {
77    fn identity(&self) -> Vec<u8> {
78        IDENTITY.as_bytes().to_vec()
79    }
80
81    fn definition_shape(&self) -> DefinitionShape {
82        DefinitionShape::NoShape { token: "cell-styles" }
83    }
84
85    fn description(&self) -> String {
86        String::from("an optional `<…>` group of style tokens")
87    }
88}