use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use std::sync::Mutex;
use super::{
parse_declared_arguments, ConstructParser, ConstructParserResult, Invocation, ParseContext,
PlainCharsError,
};
use crate::engine::Language;
use crate::error::{HookFailed, ParseError, Recovery};
use crate::extract::{content_as_chars, split_at_chars_drop_annotations};
use crate::latexlike::{
argument_specs, CallableType, Latexlike, LatexlikeDriver, MacroSpec,
};
use crate::node::{
check_tree_invariants, validate_tree, BuildId, ContentNodes, NodeBuildError, NodeTree,
ParsedArguments, ParsedSlots, StagedNodes,
};
use crate::source::SourceSpan;
use crate::scopes::Package;
use crate::serialize::SerializableObject;
use crate::spec::{ArgumentSpec, CallableSpec, FrameRole};
use crate::state::{ParsingState, ParsingStateDelta};
use crate::token::TokenEdge;
#[derive(Debug)]
struct Probe {
index: usize,
tree: Option<NodeTree<Latexlike, Option<BuildId>>>,
chars: Result<Option<String>, PlainCharsError>,
content_ids: Vec<BuildId>,
subtree_ids: Vec<BuildId>,
}
#[derive(Debug, Default)]
struct Probes {
before: Vec<Probe>,
after: Vec<Probe>,
}
type Log = Arc<Mutex<Probes>>;
#[derive(Debug)]
struct ProbeSpec {
arguments: Vec<Arc<ArgumentSpec<Latexlike>>>,
log: Log,
}
impl SerializableObject<Latexlike> for ProbeSpec {}
impl CallableSpec<Latexlike> for ProbeSpec {
fn arguments(&self) -> &[Arc<ArgumentSpec<Latexlike>>] {
&self.arguments
}
fn stack_frame_title(&self, _role: FrameRole, name: &str) -> String {
alloc::format!("macro \\{name}")
}
fn make_invocation_parser<'a>(
&'a self,
invocation: Invocation<'a, Latexlike>,
) -> Result<
Box<dyn ConstructParser<Latexlike, Output = BuildId> + 'a>,
crate::error::ParseError<Option<String>>,
> {
Ok(Box::new(ProbeParser { invocation, log: Arc::clone(&self.log) }))
}
}
struct ProbeParser<'a> {
invocation: Invocation<'a, Latexlike>,
log: Log,
}
impl ConstructParser<Latexlike> for ProbeParser<'_> {
type Output = BuildId;
fn parse(
&mut self,
cx: &mut ParseContext<'_, '_, Latexlike>,
) -> ConstructParserResult<Latexlike, (BuildId, Option<Box<ParsingStateDelta<Latexlike>>>)>
{
let token = self.invocation.token;
let name = cx.tokens.source_span_between(token, TokenEdge::Start, TokenEdge::End);
let (children, arguments) = parse_declared_arguments(cx, self.invocation.spec, &name)?;
let before = read_all(cx, &arguments, &children)?;
self.log.lock().unwrap().before.extend(before);
let claimed_arguments = arguments.clone();
let claimed_children = children.clone();
let id = cx.stage_invocation(
&self.invocation,
ParsedArguments::from(arguments),
ParsedSlots::empty(),
children,
None,
)?;
let after = read_all(cx, &claimed_arguments, &claimed_children)?;
self.log.lock().unwrap().after.extend(after);
Ok((id, None))
}
}
fn read_all(
cx: &mut ParseContext<'_, '_, Latexlike>,
arguments: &[crate::node::ParsedArgument<Latexlike>],
children: &[BuildId],
) -> ConstructParserResult<Latexlike, Vec<Probe>> {
let mut probes = Vec::with_capacity(arguments.len());
for (index, argument) in arguments.iter().enumerate() {
let tree = cx
.argument_content_as_tree(argument, children)
.map_err(|error| lift(cx, error, cx.here()))?;
let chars = cx.argument_content_as_plain_chars(argument, children);
let content_ids = content_ids(cx, argument, children);
let subtree_ids = subtree_ids(cx, &content_ids);
probes.push(Probe { index, tree, chars, content_ids, subtree_ids });
}
Ok(probes)
}
fn lift(
cx: &ParseContext<'_, '_, Latexlike>,
error: NodeBuildError,
span: SourceSpan<Option<String>>,
) -> ParseError<Option<String>> {
match error {
NodeBuildError::ExtMintFailed { detail } => {
ParseError::new(HookFailed::new(detail, None), span)
.with_frames(cx.session.snapshot_frames())
}
other => cx.implementation_error(other, span),
}
}
fn subtree_ids(cx: &ParseContext<'_, '_, Latexlike>, roots: &[BuildId]) -> Vec<BuildId> {
fn walk(staged: &StagedNodes<'_, Latexlike>, id: BuildId, out: &mut Vec<BuildId>) {
out.push(id);
if let Some(view) = staged.get(id) {
for child in view.children() {
walk(staged, *child, out);
}
}
}
let staged = cx.staged_nodes();
let mut out = Vec::new();
for id in roots {
walk(&staged, *id, &mut out);
}
out
}
fn content_ids(
cx: &ParseContext<'_, '_, Latexlike>,
argument: &crate::node::ParsedArgument<Latexlike>,
children: &[BuildId],
) -> Vec<BuildId> {
let Some(region) = argument.region.as_ref() else {
return Vec::new();
};
let (offsets, content) = region.staged().expect("a staged region during the parse");
match content {
ContentNodes::InRegion(range) => children
[offsets.start as usize..offsets.end as usize][range.start as usize..range.end as usize]
.to_vec(),
ContentNodes::InChildrenOf(parent, range) => cx
.staged_nodes()
.get(*parent)
.expect("a staged content parent")
.children()[range.start as usize..range.end as usize]
.to_vec(),
}
}
fn language(codes: &[&str], recovery: Recovery) -> (Language<Latexlike>, Log) {
let log: Log = Arc::new(Mutex::new(Probes::default()));
let mut package = Package::new("content-reader-tests");
package.insert(
CallableType::Macro,
"probe",
Arc::new(ProbeSpec {
arguments: argument_specs(codes).unwrap(),
log: Arc::clone(&log),
}),
);
package.insert(
CallableType::Macro,
"emph",
Arc::new(MacroSpec::new(argument_specs(["m"]).unwrap())),
);
let language = Language::new(
LatexlikeDriver::new(recovery),
ParsingState::lang_initial_with_packages([package]).expect("seed state"),
)
.with_descent_guard_init(crate::engine::StdDescentGuardInit::depth_limit(64));
(language, log)
}
fn probe_both(codes: &[&str], input: &str) -> Probes {
let (language, log) = language(codes, Recovery::Strict);
let result = language.parse(input).unwrap();
check_tree_invariants(&result.tree);
assert!(result.diagnostics.is_empty(), "unexpected diagnostics: {:?}", result.diagnostics);
let probes = core::mem::take(&mut *log.lock().unwrap());
probes
}
fn probe(codes: &[&str], input: &str) -> Vec<Probe> {
probe_both(codes, input).before
}
fn annotations(tree: &NodeTree<Latexlike, Option<BuildId>>) -> Vec<Option<BuildId>> {
tree.root().children().iter().map(|node| *node.annotation()).collect()
}
fn all_annotations(tree: &NodeTree<Latexlike, Option<BuildId>>) -> Vec<Option<BuildId>> {
tree.descendants().map(|node| *node.annotation()).collect()
}
#[test]
fn a_delimited_argument_reads_as_the_groups_children() {
let probes = probe(&["m"], r"\probe{a \emph{b} c}");
assert_eq!(probes.len(), 1);
let probe = &probes[0];
assert_eq!(probe.index, 0);
let tree = probe.tree.as_ref().expect("a provided argument answers a tree");
check_tree_invariants(tree);
assert!(tree.root().is_list());
assert_eq!(tree.root().span_content(), "a \\emph{b} c");
assert_eq!(tree.root().child_count(), 3);
assert_eq!(tree.root().child(0).unwrap().chars(), Some("a "));
assert_eq!(tree.root().child(1).unwrap().macro_name(), Some("emph"));
assert_eq!(*tree.root().annotation(), None);
assert_eq!(annotations(tree), probe.content_ids.iter().copied().map(Some).collect::<Vec<_>>());
assert_eq!(
all_annotations(tree),
probe.subtree_ids.iter().copied().map(Some).collect::<Vec<_>>()
);
assert!(probe.subtree_ids.len() > probe.content_ids.len(), "the copy has depth");
let emph_argument = tree.root().child(1).unwrap().argument_content_nodes(0).unwrap();
assert_eq!(emph_argument.first().unwrap().chars(), Some("b"));
assert!(matches!(probe.chars, Err(PlainCharsError::NotPlainCharacters { .. })));
}
#[test]
fn a_bare_single_token_argument_reads_as_that_token() {
let probes = probe(&["m"], r"\probe 1x");
let probe = &probes[0];
let tree = probe.tree.as_ref().unwrap();
check_tree_invariants(tree);
assert_eq!(tree.root().child_count(), 1);
assert_eq!(tree.root().span_content(), "1");
assert_eq!(tree.root().child(0).unwrap().chars(), Some("1"));
assert_eq!(annotations(tree), [Some(probe.content_ids[0])]);
assert_eq!(probe.chars.as_ref().unwrap().as_deref(), Some("1"));
}
#[test]
fn empty_content_answers_an_empty_tree_anchored_in_the_group() {
let probes = probe(&["m"], r"\probe{}");
let probe = &probes[0];
let tree = probe.tree.as_ref().expect("a provided argument answers a tree");
assert!(validate_tree(tree).is_ok(), "{:?}", validate_tree(tree));
assert_eq!(tree.root().child_count(), 0);
assert_eq!(tree.root().span_content(), "{}");
assert_eq!(*tree.root().annotation(), None);
assert_eq!(probe.chars.as_ref().unwrap().as_deref(), Some(""));
}
#[test]
fn an_argument_that_was_not_provided_answers_none() {
let probes = probe(&["o", "m"], r"\probe{x}");
assert_eq!(probes.len(), 2);
assert!(probes[0].tree.is_none());
assert_eq!(probes[0].chars.as_ref().unwrap(), &None);
assert!(probes[1].tree.is_some());
assert_eq!(probes[1].chars.as_ref().unwrap().as_deref(), Some("x"));
}
#[test]
fn reading_works_just_as_well_after_the_invocation_is_staged() {
let probes = probe_both(&["o", "m"], r"\probe[opt]{a \emph{b} c}");
assert_eq!(probes.before.len(), 2);
assert_eq!(probes.after.len(), 2);
for (before, after) in probes.before.iter().zip(&probes.after) {
assert_eq!(before.index, after.index);
assert_eq!(before.chars, after.chars);
assert_eq!(before.content_ids, after.content_ids);
assert_eq!(before.subtree_ids, after.subtree_ids);
match (&before.tree, &after.tree) {
(Some(before), Some(after)) => {
assert_eq!(
after.descendants().map(|n| n.span_content()).collect::<Vec<_>>(),
before.descendants().map(|n| n.span_content()).collect::<Vec<_>>()
);
assert_eq!(all_annotations(after), all_annotations(before));
assert_eq!(*after.root().annotation(), None);
assert_eq!(after.root().span_content(), before.root().span_content());
}
(None, None) => {}
other => panic!("the two reads disagree on provision: {other:?}"),
}
}
assert_eq!(probes.after[0].chars.as_ref().unwrap().as_deref(), Some("opt"));
assert!(matches!(probes.after[1].chars, Err(PlainCharsError::NotPlainCharacters { .. })));
assert!(!probes.after[1].subtree_ids.is_empty());
}
#[test]
fn the_copied_tree_feeds_the_extract_helpers() {
let probes = probe(&["m"], r"\probe{key1,key2,my{special,key},key4}");
let tree = probes[0].tree.as_ref().unwrap();
assert_eq!(
content_as_chars(tree.root().children()).unwrap(),
"key1,key2,myspecial,key,key4"
);
let split = split_at_chars_drop_annotations(tree.root().children(), ",").unwrap();
assert_eq!(split.segments().count(), 4);
let texts: Vec<String> = split
.segments()
.map(|segment| content_as_chars(segment).unwrap().to_string())
.collect();
assert_eq!(texts, ["key1", "key2", "myspecial,key", "key4"]);
}
#[test]
fn plain_chars_reads_characters_and_refuses_everything_else() {
assert_eq!(
probe(&["m"], r"\probe{chap.tex}")[0].chars.as_ref().unwrap().as_deref(),
Some("chap.tex")
);
assert_eq!(
probe(&["m"], r"\probe{ chap.tex }")[0].chars.as_ref().unwrap().as_deref(),
Some(" chap.tex ")
);
let probes = probe(&["m"], r"\probe{{chap.tex}}");
let group = probes[0].content_ids[0];
assert_eq!(probes[0].chars, Err(PlainCharsError::NotPlainCharacters { node: group }));
assert!(probes[0].tree.as_ref().unwrap().root().child(0).unwrap().is_group());
let probes = probe(&["m"], "\\probe{chap% a note\n.tex}");
let error = probes[0].chars.as_ref().unwrap_err();
assert!(matches!(error, PlainCharsError::NotPlainCharacters { .. }));
assert_eq!(
error,
&PlainCharsError::NotPlainCharacters { node: probes[0].content_ids[1] }
);
let probes = probe(&["m"], r"\probe{\emph{x}}");
assert_eq!(
probes[0].chars,
Err(PlainCharsError::NotPlainCharacters { node: probes[0].content_ids[0] })
);
}
#[test]
fn the_tree_reader_flattens_where_the_plain_reader_refuses() {
let probes = probe(&["m"], r"\probe{{chap.tex}}");
let tree = probes[0].tree.as_ref().unwrap();
assert_eq!(content_as_chars(tree.root().children()).unwrap(), "chap.tex");
}