use std::sync::Arc;
use techy::core::node::NodeRef;
use crate::lang::mint::{ArgDef, ArgLayout, ArgStdKind, BodyDef, ElementDef, ScopedPackageDef, SyntaxDef};
use crate::lang::Flm;
use crate::model::aspects::{BlockRole, BlockRoleAspect, FixedBlockRole};
use crate::model::element::{ElementSpec, element_of};
use crate::model::names::{FeatureName, Name};
use crate::stdlib::backends::lowering::rules::owning_callable;
use super::parser::CellStylesParser;
use super::{
BODY_PACKAGE, CELL, COLNAMES, COLUMN, CONTENT_ARG, DATA, DATA_ARG, DATA_PACKAGE, ENVIRONMENT,
ENVIRONMENT_PACKAGE, MAPPING_ARG, MERGE, MERGE_PACKAGE, NAMES_ARG, PLACEMENT_ARG, RANGE_ARG, ROW,
ROWNAMES, STYLES_ARG,
};
pub fn stands_in_body(node: &NodeRef<'_, Flm>) -> bool {
owning_callable(node).is_some_and(|owner| element_of(&owner).name.as_str() == ENVIRONMENT)
}
#[derive(Debug, Default, Clone, Copy)]
pub struct InBody;
impl BlockRoleAspect for InBody {
fn block_role(&self, node: &NodeRef<'_, Flm>) -> BlockRole {
if stands_in_body(node) { BlockRole::Block } else { BlockRole::DEFAULT }
}
}
fn by_position(name: &str, docs: &str) -> Arc<ElementSpec> {
Arc::new(ElementSpec::new(name).docs(docs).with::<dyn BlockRoleAspect>(Arc::new(InBody)))
}
fn block(name: &str, docs: &str) -> Arc<ElementSpec> {
Arc::new(
ElementSpec::new(name)
.docs(docs)
.with::<dyn BlockRoleAspect>(Arc::new(FixedBlockRole(BlockRole::Block))),
)
}
fn inline(name: &str, docs: &str) -> Arc<ElementSpec> {
Arc::new(ElementSpec::new(name).docs(docs))
}
pub(super) fn environment_spec() -> Arc<ElementSpec> {
block(ENVIRONMENT, "`\\begin{cells}…\\end{cells}`: a table of cells.")
}
pub(super) fn environment_def(owner: &FeatureName, spec: &Arc<ElementSpec>) -> ElementDef {
ElementDef::new(
owner.clone(),
["cells"],
SyntaxDef::Environment { args: Vec::new(), body: BodyDef::new().blocks().scoped(BODY_PACKAGE) },
Arc::clone(spec),
)
.docs(
"Writes a table of cells, filled with `\\cell`, `\\celldata` and `\\\\`, its rows and \
columns optionally named with `\\rownames` and `\\colnames`.",
)
}
fn styles_arg() -> ArgDef {
ArgDef::named(STYLES_ARG, CellStylesParser::new())
.docs("Style tokens, separated by whitespace: a name, or a name with a value in braces.")
}
fn body_commands(owner: &FeatureName) -> Vec<ElementDef> {
let cell = ElementDef::new(
owner.clone(),
["cell"],
SyntaxDef::Macro {
args: vec![
styles_arg(),
ArgDef::named(PLACEMENT_ARG, ArgStdKind::Optional).scoped(MERGE_PACKAGE).docs(
"Where the cell goes: `COL` or `ROW;COL`, each a number, a name, `.` or empty for \
the current one, or `\\merge{RANGE}` for a span.",
),
ArgDef::named(CONTENT_ARG, ArgStdKind::Mandatory)
.layout(ArgLayout::Blocks)
.docs("The cell's content: paragraphs and blocks."),
],
},
by_position(CELL, "`\\cell<styles>[placement]{content}`: one cell of a table of cells."),
)
.docs("Adds a cell to the table, in the next column of the current row by default.");
let data = ElementDef::new(
owner.clone(),
["celldata"],
SyntaxDef::Macro {
args: vec![
styles_arg(),
ArgDef::named(MAPPING_ARG, ArgStdKind::Optional).scoped(MERGE_PACKAGE).docs(
"Where the data goes: `COLS` or `ROWS;COLS`, each a comma-separated list of \
indices, ranges and `\\merge{RANGE}`.",
),
ArgDef::named(DATA_ARG, ArgStdKind::Mandatory)
.layout(ArgLayout::Inline)
.scoped(DATA_PACKAGE)
.docs("Rows ended by `\\\\`, entries separated by `&`; each entry inline content."),
],
},
block(DATA, "`\\celldata<styles>[mapping]{data}`: several cells of a table of cells, by rows."),
)
.docs("Adds a cell per entry of the data, row by row.");
let row = ElementDef::new(
owner.clone(),
["\\"],
SyntaxDef::Macro { args: Vec::new() },
by_position(ROW, "`\\\\` in a table of cells: the next cell starts a new row."),
)
.docs("Starts a new row: the next cell goes into it.");
let names = |command: &str, element: &str, what: &str| {
ElementDef::new(
owner.clone(),
[command],
SyntaxDef::Macro {
args: vec![ArgDef::named(NAMES_ARG, ArgStdKind::CharsGroup).docs(format!(
"Comma-separated names; the k-th is the name of {what} k, and an empty entry \
gives no name."
))],
},
by_position(element, &format!("`\\{command}{{names}}`: names for the {what}s of a table of cells.")),
)
.docs(format!("Names the {what}s, so that a name can be written wherever a {what} number can."))
};
vec![cell, data, row, names("rownames", ROWNAMES, "row"), names("colnames", COLNAMES, "column")]
}
pub(super) fn packages(owner: &FeatureName, environment: &Arc<ElementSpec>) -> Vec<ScopedPackageDef> {
let merge = ElementDef::new(
owner.clone(),
["merge"],
SyntaxDef::Macro {
args: vec![ArgDef::named(RANGE_ARG, ArgStdKind::CharsGroup).docs(
"The rows or columns the cell spans: `2-4`, `2+3`, `1,2,3`, a start or an end left out.",
)],
},
inline(MERGE, "`\\merge{range}`: a span of rows or of columns, in a cell's placement."),
)
.docs("Gives the rows or the columns a cell spans.");
let column = ElementDef::new(
owner.clone(),
Vec::<Name>::new(),
SyntaxDef::Specials { trigger: String::from("&"), args: Vec::new() },
inline(COLUMN, "`&` in the data of `\\celldata`: the end of an entry."),
)
.docs("Ends an entry of the data.");
vec![
ScopedPackageDef {
owner: owner.clone(),
name: Name::from(ENVIRONMENT_PACKAGE),
elements: vec![environment_def(owner, environment)],
docs: String::from("The `cells` environment, in the bodies that accept a table of cells."),
},
ScopedPackageDef {
owner: owner.clone(),
name: Name::from(BODY_PACKAGE),
elements: body_commands(owner),
docs: String::from(
"The instructions of a table of cells: `\\cell`, `\\celldata`, `\\\\`, `\\rownames` and \
`\\colnames`.",
),
},
ScopedPackageDef {
owner: owner.clone(),
name: Name::from(MERGE_PACKAGE),
elements: vec![merge],
docs: String::from("`\\merge`, in the placement of a cell and the mapping of `\\celldata`."),
},
ScopedPackageDef {
owner: owner.clone(),
name: Name::from(DATA_PACKAGE),
elements: vec![column],
docs: String::from("`&`, in the data of `\\celldata`."),
},
]
}