Implement native paragraph token naming and execution contexts

This commit is contained in:
gloamxun committed 2026-11-21 01:49:10 +08:00
1 parent 862801ee5f
commit 13ab32247d
12 files changed
+118 -24

No files matched your search

+36 -15
View File
@@ -1666,7 +1666,7 @@ impl Executor {
Ok(replacement)
}
fn paragraph_argument(&self, token: &Token, long: bool) -> Result<(), Error> {
if !long && token.kind == TokenKind::Command(CommandName::Control("par".into())) {
if !long && self.state.is_paragraph_token(&token.kind) {
return Err(self.error(
"paragraph ended before non-long macro argument was complete",
token.span,
@@ -1737,6 +1737,9 @@ impl Executor {
}
self.consume_macro_delimiter(expected);
}
if !definition.long && !definition.parameters.is_empty() {
self.reads.insert(Dependency::ParagraphToken);
}
let mut arguments = Vec::new();
for (index, delimiter) in definition.parameters.iter().enumerate() {
let mut argument = Vec::new();
@@ -3397,6 +3400,24 @@ impl Executor {
}
Ok(initial_depth)
}
fn insert_paragraph_token(&mut self, span: Span) -> Result<(), Error> {
self.reads.insert(Dependency::ParagraphToken);
self.push(vec![Token::new(self.state.paragraph_token(), span).into()])
}
fn in_unrestricted_horizontal_mode(&self) -> bool {
self.paragraph.is_some() && self.math.is_empty() && !self.restricted()
}
fn group_end_needs_paragraph_token(&mut self) -> bool {
let minimum = match self.state.group_kind() {
Some(GroupKind::Vbox | GroupKind::Vtop | GroupKind::Vcenter) => 1,
Some(GroupKind::Insertion | GroupKind::Output | GroupKind::NoAlign) => 2,
_ => return false,
};
self.read_parameter(Parameter::Partokencontext).width() >= minimum
}
fn run_active_input<F: SourceProvider>(
&mut self,
initial_depth: usize,
@@ -3425,11 +3446,8 @@ impl Executor {
self.trace_command(&token, files)?;
}
}
if self.native_page.is_some()
&& self.paragraph.is_some()
&& self.math.is_empty()
&& !self.restricted()
&& matches!(
if self.in_unrestricted_horizontal_mode()
&& (matches!(
&meaning,
Meaning::Primitive(
Primitive::End
@@ -3446,21 +3464,19 @@ impl Executor {
| Primitive::Splitdiscards
| Primitive::Halign
)
)
) || (matches!(
&meaning,
Meaning::Character(TokenKind::Character(_, CatCode::EndGroup))
) && self.group_end_needs_paragraph_token()))
{
let span = token.span;
self.unread(InputToken {
token: token.clone(),
token,
frozen: input.frozen,
end_write: input.end_write,
alignment: None,
})?;
self.push(vec![
Token::new(
TokenKind::Command(CommandName::Control("par".into())),
token.span,
)
.into(),
])?;
self.insert_paragraph_token(span)?;
continue;
}
if frozen_expandable {
@@ -3586,6 +3602,11 @@ impl Executor {
token.invocation.unwrap_or(token.span),
files,
)?,
Partokenname => {
if let TokenKind::Command(name) = self.required_raw(files)?.token.kind {
self.state.set_paragraph_token(name);
}
}
Par => {
if self.paragraph.is_none() {
self.normal_paragraph(files)?;