Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,546 @@
|
||||
use super::super::context::Pseudo;
|
||||
use super::*;
|
||||
|
||||
impl InputFrame {
|
||||
fn exhausted_tokens(&self) -> bool {
|
||||
match self {
|
||||
Self::Tokens { tokens, cursor, .. } => *cursor == tokens.len(),
|
||||
Self::Hook { tokens, cursor, .. } | Self::Argument { tokens, cursor, .. } => {
|
||||
*cursor == tokens.len()
|
||||
}
|
||||
Self::Macro {
|
||||
definition, cursor, ..
|
||||
} => *cursor == definition.replacement.len(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Executor {
|
||||
pub(super) fn discard_exhausted_tokens(&mut self) {
|
||||
while self.inputs.last().is_some_and(InputFrame::exhausted_tokens) {
|
||||
self.inputs.pop();
|
||||
}
|
||||
}
|
||||
pub(super) fn raw(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
enum Next {
|
||||
Token(Option<InputToken>),
|
||||
Argument(Arc<[Token]>, Span),
|
||||
Error(Span, String),
|
||||
Boundary,
|
||||
EndSource { source: usize, trace: Option<bool> },
|
||||
EofHook,
|
||||
}
|
||||
loop {
|
||||
self.observe_resource_stacks();
|
||||
let Some(input) = self.inputs.last_mut() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let next = match input {
|
||||
InputFrame::Tokens { tokens, cursor, .. } => {
|
||||
let t = tokens.get(*cursor).cloned();
|
||||
if t.is_some() {
|
||||
*cursor += 1;
|
||||
}
|
||||
Next::Token(t)
|
||||
}
|
||||
InputFrame::Macro {
|
||||
definition,
|
||||
arguments,
|
||||
cursor,
|
||||
call,
|
||||
..
|
||||
} => match definition.replacement.get(*cursor) {
|
||||
Some(Replacement::Token(t)) => {
|
||||
let mut t = t.clone();
|
||||
t.invocation = Some(*call);
|
||||
*cursor += 1;
|
||||
Next::Token(Some(t.into()))
|
||||
}
|
||||
Some(Replacement::Parameter(n)) => {
|
||||
*cursor += 1;
|
||||
Next::Argument(arguments[*n].clone(), *call)
|
||||
}
|
||||
None => Next::Token(None),
|
||||
},
|
||||
InputFrame::Argument {
|
||||
tokens,
|
||||
cursor,
|
||||
call,
|
||||
} => {
|
||||
let t = tokens.get(*cursor).cloned().map(|mut t| {
|
||||
t.invocation = Some(*call);
|
||||
t.into()
|
||||
});
|
||||
if t.is_some() {
|
||||
*cursor += 1;
|
||||
}
|
||||
Next::Token(t)
|
||||
}
|
||||
InputFrame::Source {
|
||||
source,
|
||||
mouth,
|
||||
eof_seen,
|
||||
pseudo_trace,
|
||||
..
|
||||
} => match mouth.next(&self.state, &mut self.reads) {
|
||||
Ok(Some(token)) => {
|
||||
self.input_position = Some((*source, mouth.line_number()));
|
||||
Next::Token(Some(token.into()))
|
||||
}
|
||||
Ok(None) => {
|
||||
if self.state.profile() == Profile::EtexBootstrap
|
||||
&& !*eof_seen
|
||||
&& !mouth.forced_eof()
|
||||
{
|
||||
self.reads
|
||||
.insert(Dependency::TokenParameter(TokenParameter::Everyeof));
|
||||
if self
|
||||
.state
|
||||
.token_parameter(TokenParameter::Everyeof)
|
||||
.is_some()
|
||||
{
|
||||
*eof_seen = true;
|
||||
self.input_position = Some((*source, mouth.enter_eof_line()));
|
||||
Next::EofHook
|
||||
} else {
|
||||
Next::EndSource {
|
||||
source: *source,
|
||||
trace: *pseudo_trace,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Next::EndSource {
|
||||
source: *source,
|
||||
trace: *pseudo_trace,
|
||||
}
|
||||
}
|
||||
}
|
||||
Err((span, text)) => Next::Error(span, text),
|
||||
},
|
||||
InputFrame::Hook { tokens, cursor, .. } => {
|
||||
let t = tokens.get(*cursor).cloned().map(InputToken::from);
|
||||
if t.is_some() {
|
||||
*cursor += 1;
|
||||
}
|
||||
Next::Token(t)
|
||||
}
|
||||
InputFrame::Boundary => Next::Boundary,
|
||||
};
|
||||
match next {
|
||||
Next::Token(Some(token)) => {
|
||||
self.current = token.token.invocation.unwrap_or(token.token.span);
|
||||
self.tick()?;
|
||||
if self.alignment_raw(&token)? {
|
||||
continue;
|
||||
}
|
||||
self.reject_outer(&token.token, token.frozen)?;
|
||||
return Ok(Some(token));
|
||||
}
|
||||
Next::EofHook => self.push_mode_hook(TokenParameter::Everyeof)?,
|
||||
Next::EndSource { source, trace } => {
|
||||
self.warn_input_eof(files)?;
|
||||
if let Some(traced) = trace {
|
||||
self.pseudo_input_boundary(source, false, traced, files)?;
|
||||
}
|
||||
self.inputs.pop();
|
||||
self.restore_input_position();
|
||||
}
|
||||
Next::Token(None) => {
|
||||
self.inputs.pop();
|
||||
self.restore_input_position();
|
||||
}
|
||||
Next::Argument(tokens, call) => {
|
||||
if !tokens.is_empty() {
|
||||
if self.inputs.len() >= self.limits.input_depth {
|
||||
return self.fail("native input stack limit exceeded");
|
||||
}
|
||||
self.inputs.push(InputFrame::Argument {
|
||||
tokens,
|
||||
cursor: 0,
|
||||
call,
|
||||
});
|
||||
}
|
||||
}
|
||||
Next::Error(span, text) => return Err(self.error(text, span)),
|
||||
Next::Boundary => return Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn restore_input_position(&mut self) {
|
||||
if let Some(position) = self.inputs.iter().rev().find_map(|frame| match frame {
|
||||
InputFrame::Source { source, mouth, .. } => Some((*source, mouth.line_number())),
|
||||
_ => None,
|
||||
}) {
|
||||
self.input_position = Some(position);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn pseudo_input_boundary(
|
||||
&mut self,
|
||||
source: usize,
|
||||
opening: bool,
|
||||
traced: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
if traced {
|
||||
self.diagnostic_bytes =
|
||||
self.diagnostic_bytes
|
||||
.saturating_add(if opening { 3 } else { 1 });
|
||||
if self.diagnostic_bytes > self.limits.tokens {
|
||||
return self.fail("native diagnostic output limit exceeded");
|
||||
}
|
||||
files
|
||||
.observe_pseudo_input(opening)
|
||||
.map_err(|error| self.error(error, self.current))?;
|
||||
}
|
||||
self.events.push(Event::PseudoInput {
|
||||
source,
|
||||
opening,
|
||||
traced,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn scan_tokens_input(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
let origin = self.current;
|
||||
let tokens = self.scan_group(files)?;
|
||||
let rendered = self.token_list_text(&tokens);
|
||||
let profile = self.state.input_profile();
|
||||
let bytes = match profile {
|
||||
InputProfile::UnicodeScalar => rendered.into_bytes(),
|
||||
InputProfile::TeX8Bit => rendered
|
||||
.chars()
|
||||
.map(|c| {
|
||||
u8::try_from(c as u32).map_err(|_| {
|
||||
self.error("scantokens character exceeds byte profile", origin)
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
};
|
||||
if bytes.len() > self.limits.tokens || self.inputs.len() >= self.limits.input_depth {
|
||||
return self.fail("native scantokens input capacity exceeded");
|
||||
}
|
||||
self.reads.insert(Dependency::NewLineChar);
|
||||
let separator = u8::try_from(self.state.newlinechar).ok().map(char::from);
|
||||
let traced = self.read_parameter(Parameter::Tracingscantokens).width() > 0;
|
||||
let source = self.sources.len();
|
||||
let mouth = Mouth::generated(source, bytes.clone(), profile, separator);
|
||||
self.sources.push(SourceFile {
|
||||
name: "<scantokens>".into(),
|
||||
data: bytes,
|
||||
});
|
||||
self.generated_inputs
|
||||
.insert(source, GeneratedInput { origin, separator });
|
||||
self.reads.insert(Dependency::Source(source));
|
||||
self.inputs.push(InputFrame::Source {
|
||||
source,
|
||||
mouth,
|
||||
eof_seen: false,
|
||||
pseudo_trace: Some(traced),
|
||||
group_boundary: self.state.group_depth(),
|
||||
conditional_boundary: self.conditionals.len(),
|
||||
});
|
||||
self.input_position = Some((source, 0));
|
||||
self.pseudo_input_boundary(source, true, traced, files)
|
||||
}
|
||||
|
||||
fn context_token(&self, out: &mut Pseudo, token: &TokenKind, reads: &mut BTreeSet<Dependency>) {
|
||||
match token {
|
||||
TokenKind::Character(c, code) => {
|
||||
out.character(*c);
|
||||
if *code == CatCode::Parameter {
|
||||
out.character(*c);
|
||||
}
|
||||
}
|
||||
TokenKind::Command(name) => self.context_command(out, name, reads),
|
||||
}
|
||||
}
|
||||
fn context_command(
|
||||
&self,
|
||||
out: &mut Pseudo,
|
||||
name: &CommandName,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) {
|
||||
match name {
|
||||
CommandName::FrozenRelax => {
|
||||
if let Ok(escape) = u8::try_from(self.state.escapechar) {
|
||||
out.character(char::from(escape));
|
||||
}
|
||||
out.text("relax ");
|
||||
}
|
||||
CommandName::Font(font) => {
|
||||
reads.insert(Dependency::FontResources);
|
||||
if let Ok(escape) = u8::try_from(self.state.escapechar) {
|
||||
out.character(char::from(escape));
|
||||
}
|
||||
out.text(&self.font_token_name(*font));
|
||||
out.push(' ');
|
||||
}
|
||||
CommandName::Active(c) => out.character(*c),
|
||||
CommandName::Control(name) => {
|
||||
if let Ok(escape) = u8::try_from(self.state.escapechar) {
|
||||
out.character(char::from(escape));
|
||||
}
|
||||
if name.is_empty() {
|
||||
out.text("csname");
|
||||
if let Ok(escape) = u8::try_from(self.state.escapechar) {
|
||||
out.character(char::from(escape));
|
||||
}
|
||||
out.text("endcsname ");
|
||||
} else {
|
||||
if name.chars().count() == 1 {
|
||||
reads.insert(Dependency::CatCode(name.chars().next().unwrap()));
|
||||
}
|
||||
out.text(name);
|
||||
if name.chars().count() > 1
|
||||
|| name
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|c| self.state.catcode(c) == CatCode::Letter)
|
||||
{
|
||||
out.push(' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) fn input_context(&self) -> InputContext {
|
||||
let mut frames = Vec::new();
|
||||
let mut reads = BTreeSet::from([
|
||||
Dependency::Parameter(Parameter::Errorcontextlines),
|
||||
Dependency::EscapeChar,
|
||||
Dependency::NewLineChar,
|
||||
Dependency::EndLineChar,
|
||||
]);
|
||||
let mut top = true;
|
||||
for frame in self.inputs.iter().rev() {
|
||||
let mut out = Pseudo::new();
|
||||
let (kind, label) = match frame {
|
||||
InputFrame::Boundary => continue,
|
||||
InputFrame::Hook {
|
||||
parameter,
|
||||
tokens,
|
||||
cursor,
|
||||
} => {
|
||||
reads.insert(Dependency::TokenParameter(*parameter));
|
||||
for (i, t) in tokens.iter().enumerate() {
|
||||
if i == *cursor {
|
||||
out.split();
|
||||
}
|
||||
self.context_token(&mut out, &t.kind, &mut reads);
|
||||
}
|
||||
if *cursor == tokens.len() {
|
||||
out.split();
|
||||
}
|
||||
(
|
||||
ContextKind::Hook(*parameter),
|
||||
format!("<{}> ", parameter.name()),
|
||||
)
|
||||
}
|
||||
InputFrame::Source { source, mouth, .. } => {
|
||||
mouth.context(&mut out, self.state.endlinechar);
|
||||
reads.insert(Dependency::Source(*source));
|
||||
(
|
||||
ContextKind::Source {
|
||||
source: *source,
|
||||
name: self.sources[*source].name.clone(),
|
||||
line: mouth.line_number(),
|
||||
},
|
||||
format!("l.{} ", mouth.line_number()),
|
||||
)
|
||||
}
|
||||
InputFrame::Tokens {
|
||||
tokens,
|
||||
cursor,
|
||||
kind,
|
||||
} => {
|
||||
let exhausted = *cursor == tokens.len();
|
||||
if matches!(kind, TokenInputKind::BackedUp) && exhausted && !top {
|
||||
continue;
|
||||
}
|
||||
for (i, t) in tokens.iter().enumerate() {
|
||||
if i == *cursor {
|
||||
out.split();
|
||||
}
|
||||
self.context_token(&mut out, &t.token.kind, &mut reads);
|
||||
}
|
||||
if exhausted {
|
||||
out.split();
|
||||
}
|
||||
match kind {
|
||||
TokenInputKind::Inserted => {
|
||||
(ContextKind::Inserted, "<inserted text> ".into())
|
||||
}
|
||||
TokenInputKind::Write => (ContextKind::Write, "<write> ".into()),
|
||||
TokenInputKind::Mark => (ContextKind::Mark, "<mark> ".into()),
|
||||
TokenInputKind::Output => (ContextKind::Output, "<output> ".into()),
|
||||
TokenInputKind::BackedUp => (
|
||||
ContextKind::BackedUp { exhausted },
|
||||
if exhausted {
|
||||
"<recently read> "
|
||||
} else {
|
||||
"<to be read again> "
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
}
|
||||
}
|
||||
InputFrame::Argument { tokens, cursor, .. } => {
|
||||
for (i, t) in tokens.iter().enumerate() {
|
||||
if i == *cursor {
|
||||
out.split();
|
||||
}
|
||||
self.context_token(&mut out, &t.kind, &mut reads);
|
||||
}
|
||||
if *cursor == tokens.len() {
|
||||
out.split();
|
||||
}
|
||||
(ContextKind::Argument, "<argument> ".into())
|
||||
}
|
||||
InputFrame::Macro {
|
||||
definition,
|
||||
name,
|
||||
cursor,
|
||||
..
|
||||
} => {
|
||||
for t in &definition.prefix {
|
||||
self.context_token(&mut out, t, &mut reads);
|
||||
}
|
||||
for (n, delimiter) in definition.parameters.iter().enumerate() {
|
||||
out.text(&format!("{}{}", definition.parameter_chars[n], n + 1));
|
||||
for t in delimiter {
|
||||
self.context_token(&mut out, t, &mut reads);
|
||||
}
|
||||
}
|
||||
out.text("->");
|
||||
for (i, t) in definition.replacement.iter().enumerate() {
|
||||
if i == *cursor {
|
||||
out.split();
|
||||
}
|
||||
match t {
|
||||
Replacement::Token(t) => {
|
||||
self.context_token(&mut out, &t.kind, &mut reads)
|
||||
}
|
||||
Replacement::Parameter(n) => out.text(&format!(
|
||||
"{}{}",
|
||||
definition.parameter_chars.last().copied().unwrap_or('#'),
|
||||
n + 1
|
||||
)),
|
||||
}
|
||||
}
|
||||
if *cursor == definition.replacement.len() {
|
||||
out.split();
|
||||
}
|
||||
let label = match name {
|
||||
CommandName::Font(font) => {
|
||||
reads.insert(Dependency::FontResources);
|
||||
let escape = u8::try_from(self.state.escapechar)
|
||||
.ok()
|
||||
.map(char::from)
|
||||
.map(|c| c.to_string())
|
||||
.unwrap_or_default();
|
||||
format!("{escape}{} ", self.font_token_name(*font))
|
||||
}
|
||||
CommandName::FrozenRelax => unreachable!("frozen relax is not a macro"),
|
||||
CommandName::Active(c) => c.to_string(),
|
||||
CommandName::Control(name) => {
|
||||
if name.chars().count() == 1 {
|
||||
reads.insert(Dependency::CatCode(name.chars().next().unwrap()));
|
||||
}
|
||||
let escape = u8::try_from(self.state.escapechar)
|
||||
.ok()
|
||||
.map(char::from)
|
||||
.map(|c| c.to_string())
|
||||
.unwrap_or_default();
|
||||
let space = name.chars().count() > 1
|
||||
|| name
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|c| self.state.catcode(c) == CatCode::Letter);
|
||||
if name.is_empty() {
|
||||
format!("{escape}csname{escape}endcsname ")
|
||||
} else {
|
||||
format!("{escape}{name}{}", if space { " " } else { "" })
|
||||
}
|
||||
}
|
||||
};
|
||||
(ContextKind::Macro(name.clone()), label)
|
||||
}
|
||||
};
|
||||
let bottom = matches!(kind, ContextKind::Source { .. });
|
||||
frames.push(out.finish(&label, kind, self.state.newlinechar));
|
||||
top = false;
|
||||
if bottom {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let limit = self.state.parameter(Parameter::Errorcontextlines).width();
|
||||
let mut text = String::new();
|
||||
let mut column = 0;
|
||||
let mut shown = -1;
|
||||
for (i, frame) in frames.iter().enumerate() {
|
||||
if i == 0 || matches!(frame.kind, ContextKind::Source { .. }) || shown < limit {
|
||||
if i != 0 && (column != 0 || matches!(frame.kind, ContextKind::Macro(_))) {
|
||||
text.push('\n');
|
||||
column = 0;
|
||||
}
|
||||
for c in frame.label.chars() {
|
||||
if c as i32 == self.state.newlinechar {
|
||||
text.push('\n');
|
||||
column = 0;
|
||||
} else {
|
||||
for c in super::super::context::encoded_character(c).chars() {
|
||||
context_char(&mut text, &mut column, c, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
for c in frame.before.chars() {
|
||||
context_char(&mut text, &mut column, c, self.state.newlinechar);
|
||||
}
|
||||
text.push('\n');
|
||||
column = 0;
|
||||
for c in frame.second.chars() {
|
||||
context_char(&mut text, &mut column, c, self.state.newlinechar);
|
||||
}
|
||||
shown += 1;
|
||||
} else if shown == limit {
|
||||
if column != 0 {
|
||||
text.push('\n');
|
||||
column = 0;
|
||||
}
|
||||
for c in "...".chars() {
|
||||
context_char(&mut text, &mut column, c, self.state.newlinechar);
|
||||
}
|
||||
shown += 1;
|
||||
}
|
||||
}
|
||||
if !frames.is_empty() {
|
||||
text.push('\n');
|
||||
}
|
||||
InputContext {
|
||||
frames,
|
||||
text,
|
||||
reads,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn context_char(out: &mut String, column: &mut usize, c: char, newline: i32) {
|
||||
if c as i32 == newline {
|
||||
out.push('\n');
|
||||
*column = 0;
|
||||
} else {
|
||||
out.push(c);
|
||||
*column += 1;
|
||||
if *column == 79 {
|
||||
out.push('\n');
|
||||
*column = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user