516 lines
18 KiB
Rust
516 lines
18 KiB
Rust
use super::*;
|
|
impl Executor {
|
|
pub(super) fn trace_conditional(
|
|
&mut self,
|
|
command: Primitive,
|
|
skipping: bool,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.state.profile() != Profile::EtexBootstrap
|
|
|| self.read_parameter(Parameter::Tracingifs).width() <= 0
|
|
{
|
|
if !skipping && self.read_parameter(Parameter::Tracingcommands).width() > 1 {
|
|
let inverted = command.conditional()
|
|
&& self.conditionals.last().is_some_and(|frame| frame.inverted);
|
|
let command = self.trace_primitive_name(command, inverted);
|
|
self.emit_command_trace(&command, true, files)?;
|
|
}
|
|
return Ok(());
|
|
}
|
|
let frame = self.conditionals.last().expect("active condition");
|
|
let (condition, inverted, origin) = (frame.primitive, frame.inverted, frame.origin);
|
|
let level = self.conditionals.len();
|
|
let mut record = self.command_trace_prefix();
|
|
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 condition_name = format!("{condition:?}").to_ascii_lowercase();
|
|
let condition_text = if inverted {
|
|
format!("{escape}unless{escape}{condition_name}")
|
|
} else {
|
|
format!("{escape}{condition_name}")
|
|
};
|
|
if command.conditional() {
|
|
record.push_str(&condition_text);
|
|
record.push_str(": ");
|
|
} else {
|
|
let branch = format!("{command:?}").to_ascii_lowercase();
|
|
record.push_str(&format!("{escape}{branch}: {condition_text} "));
|
|
}
|
|
record.push_str(&format!("(level {level})"));
|
|
if let Some((source, line)) = origin {
|
|
self.reads.insert(Dependency::InputPosition(source));
|
|
if line != 0 {
|
|
record.push_str(&format!(" entered on line {line}"));
|
|
}
|
|
}
|
|
let (destination, text) = self.emit_trace_record(&record, files)?;
|
|
self.events.push(Event::ConditionalTrace {
|
|
command,
|
|
condition,
|
|
inverted,
|
|
level,
|
|
origin,
|
|
destination,
|
|
text,
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
fn command_trace_prefix(&mut self) -> String {
|
|
let mode = if self.writing {
|
|
"no mode"
|
|
} else if !self.math.is_empty() {
|
|
if self.display.is_some() && self.math.len() == 1 {
|
|
"display math mode"
|
|
} else {
|
|
"math mode"
|
|
}
|
|
} else if self.paragraph.is_some() {
|
|
if self.restricted() {
|
|
"restricted horizontal mode"
|
|
} else {
|
|
"horizontal mode"
|
|
}
|
|
} else if self.outer_vertical() {
|
|
"vertical mode"
|
|
} else {
|
|
"internal vertical mode"
|
|
};
|
|
let mut record = String::new();
|
|
if self.command_trace_mode != Some(mode) {
|
|
self.command_trace_mode = Some(mode);
|
|
record.push_str(mode);
|
|
record.push_str(": ");
|
|
}
|
|
record
|
|
}
|
|
|
|
fn trace_primitive_name(&mut self, primitive: Primitive, inverted: bool) -> String {
|
|
let name = format!("{primitive:?}").to_ascii_lowercase();
|
|
let kind = TokenKind::Command(CommandName::Control(name));
|
|
let command = self.string_token(&kind);
|
|
if inverted {
|
|
let unless = TokenKind::Command(CommandName::Control("unless".into()));
|
|
return format!("{}{command}", self.string_token(&unless));
|
|
}
|
|
command
|
|
}
|
|
|
|
pub(super) fn trace_command(
|
|
&mut self,
|
|
token: &Token,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let description = match self.meaning(token) {
|
|
Meaning::Character(TokenKind::Character(c, CatCode::Space)) => {
|
|
format!("blank space {c}")
|
|
}
|
|
Meaning::Primitive(Primitive::DiscretionaryHyphen) => "discretionary hyphen".into(),
|
|
_ => self.meaning_string(token)?,
|
|
};
|
|
self.emit_command_trace(&description, true, files)
|
|
}
|
|
|
|
pub(super) fn trace_frozen_command(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let command = self.trace_primitive_name(Primitive::Relax, false);
|
|
self.emit_command_trace(&command, true, files)
|
|
}
|
|
|
|
pub(super) fn trace_command_result(
|
|
&mut self,
|
|
result: &str,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
self.emit_command_trace(result, false, files)
|
|
}
|
|
|
|
fn emit_command_trace(
|
|
&mut self,
|
|
command: &str,
|
|
show_mode: bool,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let mut record = if show_mode {
|
|
self.command_trace_prefix()
|
|
} else {
|
|
String::new()
|
|
};
|
|
record.push_str(command);
|
|
let (destination, text) = self.emit_trace_record(&record, files)?;
|
|
self.events.push(Event::CommandTrace { destination, text });
|
|
Ok(())
|
|
}
|
|
|
|
fn emit_trace_record(
|
|
&mut self,
|
|
record: &str,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(WriteDestination, String), Error> {
|
|
let record = format!("{{{record}}}");
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let mut text = self.print_show_parts(&[&record])?;
|
|
if !text.ends_with('\n') {
|
|
text.push('\n');
|
|
}
|
|
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");
|
|
}
|
|
let destination = if self.read_parameter(Parameter::Tracingonline).width() > 0 {
|
|
WriteDestination::TerminalAndLog
|
|
} else {
|
|
WriteDestination::Log
|
|
};
|
|
files
|
|
.observe_diagnostic(destination, &text)
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
Ok((destination, text))
|
|
}
|
|
|
|
pub(super) fn reserve_diagnostic(
|
|
&mut self,
|
|
output: usize,
|
|
context: &InputContext,
|
|
) -> Result<(), Error> {
|
|
let snapshot = context
|
|
.frames
|
|
.iter()
|
|
.map(|f| {
|
|
f.label.len()
|
|
+ f.before.len()
|
|
+ f.second.len()
|
|
+ match &f.kind {
|
|
ContextKind::Macro(CommandName::Control(name)) => name.len(),
|
|
ContextKind::Source { name, .. } => name.len(),
|
|
_ => 0,
|
|
}
|
|
})
|
|
.sum::<usize>();
|
|
self.diagnostic_bytes = self
|
|
.diagnostic_bytes
|
|
.saturating_add(output)
|
|
.saturating_add(snapshot);
|
|
if self.diagnostic_bytes > self.limits.tokens {
|
|
return self.fail("native diagnostic output limit exceeded");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) fn diagnostic_context(&mut self) -> InputContext {
|
|
self.read_parameter(Parameter::Errorcontextlines);
|
|
self.reads.insert(Dependency::EscapeChar);
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let context = self.input_context();
|
|
self.reads.extend(context.reads.iter().cloned());
|
|
context
|
|
}
|
|
pub(super) fn print_show_parts(&self, parts: &[&str]) -> Result<String, Error> {
|
|
let mut out = String::new();
|
|
let mut column = 0;
|
|
let mut raw = |c: char| -> Result<(), Error> {
|
|
if out.len() + c.len_utf8() + 1 > self.limits.tokens {
|
|
return self.fail("native diagnostic output limit exceeded");
|
|
}
|
|
out.push(c);
|
|
if c == '\n' {
|
|
column = 0;
|
|
} else {
|
|
column += 1;
|
|
if column == 79 {
|
|
out.push('\n');
|
|
column = 0;
|
|
}
|
|
}
|
|
Ok(())
|
|
};
|
|
for (i, text) in parts.iter().enumerate() {
|
|
if i > 0 {
|
|
raw('\n')?;
|
|
}
|
|
for c in text.chars() {
|
|
if c as i32 == self.state.newlinechar {
|
|
raw('\n')?;
|
|
} else {
|
|
for c in super::super::context::encoded_character(c).chars() {
|
|
raw(c)?;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|
|
pub(super) fn show(
|
|
&mut self,
|
|
the: bool,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let text = if the {
|
|
let text = match self.the_value(files)? {
|
|
TheValue::Text(text) => text,
|
|
TheValue::Tokens(tokens) => {
|
|
let mut text = String::new();
|
|
for t in tokens {
|
|
if let TokenKind::Character(c, CatCode::Parameter) = t.kind {
|
|
text.push(c);
|
|
}
|
|
let piece = self.message_token(&t);
|
|
self.append_diagnostic(&mut text, &piece)?;
|
|
}
|
|
text
|
|
}
|
|
};
|
|
self.print_show_parts(&[&format!("> {text}.")])?
|
|
} else {
|
|
let t = self.required_raw(files)?.token;
|
|
let macro_meaning = matches!(self.meaning(&t), Meaning::Macro(_));
|
|
let name = if matches!(t.kind, TokenKind::Command(_)) {
|
|
format!("{}=", self.string_token(&t.kind))
|
|
} else {
|
|
String::new()
|
|
};
|
|
let meaning = self.meaning_string(&t)?;
|
|
if macro_meaning {
|
|
let split = meaning.find("macro:").unwrap() + "macro:".len();
|
|
self.print_show_parts(&[
|
|
&format!("> {name}{}", &meaning[..split]),
|
|
&format!("{}.", &meaning[split..]),
|
|
])?
|
|
} else {
|
|
self.print_show_parts(&[&format!("> {name}{meaning}.")])?
|
|
}
|
|
};
|
|
if text.len() > self.limits.tokens {
|
|
return self.fail("native diagnostic output limit exceeded");
|
|
}
|
|
let context = self.diagnostic_context();
|
|
let separator = if text.ends_with('\n')
|
|
&& !context
|
|
.frames
|
|
.first()
|
|
.is_some_and(|f| matches!(f.kind, ContextKind::Macro(_)))
|
|
{
|
|
""
|
|
} else {
|
|
"\n"
|
|
};
|
|
let output = format!("{text}{separator}{}", context.text);
|
|
let log_output = format!("{output}\n");
|
|
self.reserve_diagnostic(log_output.len(), &context)?;
|
|
files
|
|
.observe_diagnostic_pair(&log_output, &output)
|
|
.map_err(|e| self.error(e, span))?;
|
|
self.events.push(Event::Show(ShowReport {
|
|
output,
|
|
log_output,
|
|
the,
|
|
text,
|
|
context,
|
|
span,
|
|
}));
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) fn show_box(&mut self, files: &mut impl SourceProvider) -> Result<(), Error> {
|
|
let span = self.current;
|
|
self.finish_text(true)?;
|
|
let index = self.register_index(files)?;
|
|
self.reads.insert(Dependency::BoxRegister(index));
|
|
self.reads.insert(Dependency::FontResources);
|
|
self.reads.insert(Dependency::EscapeChar);
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let breadth = self.read_parameter(Parameter::Showboxbreadth).width();
|
|
let depth = self.read_parameter(Parameter::Showboxdepth).width();
|
|
let online = self.read_parameter(Parameter::Tracingonline).width();
|
|
let destination = if online > 0 {
|
|
WriteDestination::TerminalAndLog
|
|
} else {
|
|
WriteDestination::Log
|
|
};
|
|
let (text, reads) = super::super::diagnostics::show_box(
|
|
&self.state,
|
|
index,
|
|
breadth,
|
|
depth,
|
|
self.limits.tokens,
|
|
)
|
|
.map_err(|e| self.error(e, span))?;
|
|
self.reads.extend(reads);
|
|
let context = self.diagnostic_context();
|
|
self.reserve_diagnostic(
|
|
text.len() + context.text.len() + 2 * "! OK (see the transcript file).\n".len(),
|
|
&context,
|
|
)?;
|
|
self.events.push(Event::ShowBox(ShowBoxReport {
|
|
context: context.clone(),
|
|
index,
|
|
breadth,
|
|
depth,
|
|
destination,
|
|
text: text.clone(),
|
|
span,
|
|
}));
|
|
files
|
|
.observe_diagnostic(destination, &text)
|
|
.map_err(|e| self.error(e, span))?;
|
|
let log = format!("{}\n", self.external_print("! OK."));
|
|
let terminal = if online > 0 {
|
|
log.clone()
|
|
} else {
|
|
format!(
|
|
"{}\n",
|
|
self.external_print("! OK (see the transcript file).")
|
|
)
|
|
};
|
|
let log = format!("{log}{}\n", context.text);
|
|
let terminal = format!("{terminal}{}", context.text);
|
|
files
|
|
.observe_diagnostic_pair(&log, &terminal)
|
|
.map_err(|e| self.error(e, span))?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Executor {
|
|
pub(super) fn trace_macro_call(
|
|
&mut self,
|
|
definition: &Macro,
|
|
call: &Token,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.read_parameter(Parameter::Tracingmacros).width() > 0 {
|
|
let command = self.message_token(call);
|
|
let body = self.macro_token_text(definition, call.span)?;
|
|
self.emit_macro_trace(&format!("{command}{body}"), true, files)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) fn trace_macro_argument(
|
|
&mut self,
|
|
definition: &Macro,
|
|
index: usize,
|
|
argument: &[Token],
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.read_parameter(Parameter::Tracingmacros).width() > 0 {
|
|
let mut body = format!("{}{}<-", definition.parameter_chars[index], index + 1);
|
|
let mut tally = 0;
|
|
for token in argument {
|
|
if tally >= 1000 {
|
|
body.push_str(
|
|
&self
|
|
.string_token(&TokenKind::Command(CommandName::Control("ETC.".into()))),
|
|
);
|
|
break;
|
|
}
|
|
let piece = self.message_token(token);
|
|
tally += piece.chars().count();
|
|
if let TokenKind::Character(c, CatCode::Parameter) = token.kind {
|
|
body.push(c);
|
|
tally += 1;
|
|
}
|
|
body.push_str(&piece);
|
|
}
|
|
self.emit_macro_trace(&body, false, files)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn emit_macro_trace(
|
|
&mut self,
|
|
record: &str,
|
|
leading_newline: bool,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
let mut text = self.print_show_parts(&[record])?;
|
|
if leading_newline {
|
|
text.insert(0, '\n');
|
|
}
|
|
if !text.ends_with('\n') {
|
|
text.push('\n');
|
|
}
|
|
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");
|
|
}
|
|
let destination = if self.read_parameter(Parameter::Tracingonline).width() > 0 {
|
|
WriteDestination::TerminalAndLog
|
|
} else {
|
|
WriteDestination::Log
|
|
};
|
|
files
|
|
.observe_raw_diagnostic(destination, &text)
|
|
.map_err(|e| self.error(e, self.current))?;
|
|
self.events.push(Event::MacroTrace { destination, text });
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Executor {
|
|
pub(super) fn trace_shipout(
|
|
&mut self,
|
|
b: &NativeBox,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.read_parameter(Parameter::Tracingoutput).width() <= 0 {
|
|
return Ok(());
|
|
}
|
|
self.reads.insert(Dependency::NewLineChar);
|
|
self.reads.insert(Dependency::EscapeChar);
|
|
self.reads.insert(Dependency::FontResources);
|
|
let breadth = self.read_parameter(Parameter::Showboxbreadth).width();
|
|
let depth = self.read_parameter(Parameter::Showboxdepth).width();
|
|
let mut counters = [0; 10];
|
|
for (index, count) in counters.iter_mut().enumerate() {
|
|
self.reads.insert(Dependency::Count(index as u16));
|
|
*count = self.state.count(index as u16);
|
|
}
|
|
let last = counters.iter().rposition(|count| *count != 0).unwrap_or(0);
|
|
let label = counters[..=last]
|
|
.iter()
|
|
.map(i32::to_string)
|
|
.collect::<Vec<_>>()
|
|
.join(".");
|
|
let (tree, reads) = super::super::diagnostics::shipout_box(
|
|
&self.state,
|
|
b,
|
|
breadth,
|
|
depth,
|
|
self.limits.tokens,
|
|
)
|
|
.map_err(|error| self.error(error, self.current))?;
|
|
self.reads.extend(reads);
|
|
let heading = format!("\nCompleted box being shipped out [{label}]");
|
|
let log = if tree.starts_with(" []") {
|
|
format!("{heading}{tree}")
|
|
} else {
|
|
format!("{heading}\n{tree}")
|
|
};
|
|
let terminal = if self.read_parameter(Parameter::Tracingonline).width() > 0 {
|
|
log.clone()
|
|
} else {
|
|
format!("{heading}\n")
|
|
};
|
|
self.diagnostic_bytes = self
|
|
.diagnostic_bytes
|
|
.saturating_add(log.len())
|
|
.saturating_add(terminal.len());
|
|
if self.diagnostic_bytes > self.limits.tokens {
|
|
return self.fail("native diagnostic output limit exceeded");
|
|
}
|
|
files
|
|
.observe_diagnostic_pair(&log, &terminal)
|
|
.map_err(|error| self.error(error, self.current))?;
|
|
self.events.push(Event::ShipoutTrace { log, terminal });
|
|
Ok(())
|
|
}
|
|
}
|