Files
texrra-engine/crates/texrra-tex-core/src/execution/engine/nesting.rs
T

183 lines
6.1 KiB
Rust

use super::*;
impl Executor {
// Advance saved boundaries as their stack frames close, even with tracing off.
// A new frame at the same depth then still counts as opened in this file.
fn cross_input_boundary(&mut self, depth: usize, group: bool) -> bool {
let mut crossed = false;
for frame in self.inputs.iter_mut().rev() {
if let InputFrame::Source {
group_boundary,
conditional_boundary,
..
} = frame
{
let boundary = if group {
group_boundary
} else {
conditional_boundary
};
if *boundary != depth {
break;
}
*boundary -= 1;
crossed = true;
}
}
crossed
}
pub(super) fn warn_group_close(
&mut self,
kind: GroupKind,
level: usize,
origin: Option<(usize, usize)>,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if self.cross_input_boundary(level, true) {
let tracing = self.read_parameter(Parameter::Tracingnesting).width();
if tracing > 0 {
let name = self.nesting_group(kind, level, origin);
self.emit_nesting_warning(
format!("Warning: end of {name} of a different file"),
tracing,
files,
)?;
}
}
Ok(())
}
pub(super) fn finish_conditional(
&mut self,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let depth = self.conditionals.len();
if self.cross_input_boundary(depth, false) {
let tracing = self.read_parameter(Parameter::Tracingnesting).width();
if tracing > 0 {
let name = self.nesting_condition(depth - 1, false);
self.emit_nesting_warning(
format!("Warning: end of {name} of a different file"),
tracing,
files,
)?;
}
}
self.conditionals.pop();
Ok(())
}
pub(super) fn warn_input_eof(&mut self, files: &mut impl SourceProvider) -> Result<(), Error> {
let Some(InputFrame::Source {
group_boundary,
conditional_boundary,
..
}) = self.inputs.last()
else {
return Ok(());
};
let (groups, conditions) = (*group_boundary, *conditional_boundary);
if groups == self.state.group_depth() && conditions == self.conditionals.len() {
return Ok(());
}
let tracing = self.read_parameter(Parameter::Tracingnesting).width();
if tracing <= 0 {
return Ok(());
}
let open_groups: Vec<_> = self
.state
.open_groups()
.enumerate()
.skip(groups)
.rev()
.collect();
let mut warnings = String::new();
for (index, (kind, origin)) in open_groups {
let name = self.nesting_group(kind, index + 1, origin);
warnings.push_str(&format!("Warning: end of file when {name} is incomplete\n"));
}
for index in (conditions..self.conditionals.len()).rev() {
let name = self.nesting_condition(index, true);
warnings.push_str(&format!("Warning: end of file when {name} is incomplete\n"));
}
if tracing > 1 {
if let Some(InputFrame::Source { mouth, .. }) = self.inputs.last_mut() {
mouth.advance_eof_context();
}
}
self.emit_nesting_warning(warnings, tracing, files)
}
fn nesting_group(
&mut self,
kind: GroupKind,
level: usize,
origin: Option<(usize, usize)>,
) -> String {
let mut name = format!(
"{} group (level {level})",
super::scheduling::group_name(kind)
);
if let Some((source, line)) = origin {
self.reads.insert(Dependency::InputPosition(source));
if line != 0 {
name.push_str(&format!(" entered at line {line}"));
}
}
name
}
fn nesting_condition(&mut self, index: usize, eof: bool) -> String {
let frame = &self.conditionals[index];
self.reads.insert(Dependency::EscapeChar);
let escape = u8::try_from(self.state.escapechar)
.ok()
.map(|c| char::from(c).to_string())
.unwrap_or_default();
let mut name = String::new();
if frame.inverted {
name.push_str(&format!("{escape}unless"));
}
name.push_str(&escape);
name.push_str(&format!("{:?}", frame.primitive).to_ascii_lowercase());
if eof && frame.limit == ConditionalLimit::Fi {
name.push_str(&format!("{escape}else"));
}
if let Some((source, line)) = frame.origin {
self.reads.insert(Dependency::InputPosition(source));
if line != 0 {
name.push_str(&format!(" entered on line {line}"));
}
}
name
}
fn emit_nesting_warning(
&mut self,
warning: String,
tracing: i32,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
self.reads.insert(Dependency::NewLineChar);
let lines: Vec<_> = warning.lines().collect();
let mut text = self.print_show_parts(&lines)?;
if !text.ends_with('\n') {
text.push('\n');
}
if tracing > 1 {
let context = self.diagnostic_context();
text.push_str(&context.text);
self.reserve_diagnostic(text.len(), &context)?;
} else {
self.diagnostic_bytes = self.diagnostic_bytes.saturating_add(text.len());
if self.diagnostic_bytes > self.limits.tokens {
return self.fail("native diagnostic output limit exceeded");
}
}
files
.observe_diagnostic(WriteDestination::TerminalAndLog, &text)
.map_err(|e| self.error(e, self.current))
}
}