Invoke frozen original primitives in native TeX

This commit is contained in:
gloamxun committed 2026-11-28 14:19:26 +08:00
1 parent 2007ac5627
commit ba548ef05d
10 files changed
+219 -33

No files matched your search

+81 -5
View File
@@ -655,6 +655,9 @@ impl Executor {
TokenKind::Command(CommandName::FrozenRelax) => {
self.fail("frozen relax cannot be redefined")
}
TokenKind::Command(
CommandName::FrozenPrimitivePrefix | CommandName::FrozenPrimitive(_),
) => self.fail("frozen primitive cannot be redefined"),
TokenKind::Command(CommandName::Font(_)) => {
self.fail("frozen font identifier cannot be redefined")
}
@@ -662,6 +665,34 @@ impl Executor {
_ => self.fail("expected a control sequence or active character"),
}
}
fn frozen_primitive(&self, token: &Token) -> Option<(InputToken, bool)> {
let TokenKind::Command(name) = &token.kind else {
return None;
};
let (name, meaning) = self.state.original_primitive(name)?;
let expandable = matches!(meaning, Meaning::Primitive(p) if p.expandable());
let token = Token {
kind: TokenKind::Command(CommandName::FrozenPrimitive(name.into())),
..token.clone()
};
Some((token.into(), expandable))
}
fn deferred_internal_primitive(
&mut self,
prefix: InputToken,
files: &mut impl SourceProvider,
) -> Result<InputToken, Error> {
let operand = self.required_raw(files)?;
Ok(self.frozen_primitive(&operand.token).map_or_else(
|| Token {
kind: TokenKind::Command(CommandName::FrozenRelax),
..prefix.token
}.into(),
|(operand, _)| operand,
))
}
fn expanded(&mut self, files: &mut impl SourceProvider) -> Result<Option<InputToken>, Error> {
self.expanded_context(files, false)
}
@@ -830,12 +861,26 @@ impl Executor {
break Some(input);
}
let meaning = self.meaning(&input.token);
if !matches!(meaning, Meaning::Primitive(Primitive::Expandafter)) {
let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else {
break self.expand_one_inner(input, meaning, files, definition_context)?;
}
};
if self.read_parameter(Parameter::Tracingcommands).width() > 1 {
self.trace_command(&input.token, files)?;
}
if command == Primitive::UsePrimitive {
let operand = self.normal_raw(files)?;
let Some((original, expandable)) = self.frozen_primitive(&operand.token) else {
break None;
};
if expandable {
input = original;
definition_context = false;
continue;
}
self.unread(operand)?;
input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix);
break Some(input);
}
// Expanding the second token is the same dispatch with deferred
// input. Only a re-entry from a scanner needs a new expansion frame.
if let Some(outer) = first.replace(self.required_raw(files)?) {
@@ -1222,7 +1267,10 @@ impl Executor {
}
fn the_value(&mut self, files: &mut impl SourceProvider) -> Result<TheValue, Error> {
use Primitive::*;
let next = self.expanded_non_space(files)?;
let mut next = self.expanded_non_space(files)?;
if self.primitive(&next.token) == Some(DeferredPrimitive) {
next = self.deferred_internal_primitive(next, files)?;
}
let text = match self.meaning(&next.token) {
Meaning::Font(font) => {
let mut token = next.token;
@@ -1527,7 +1575,8 @@ impl Executor {
&operand.token.kind,
TokenKind::Command(name)
if !Self::suppresses_meaning(&operand, &live)
&& self.state.original_primitive(name) == Some(&live)
&& self.state.original_primitive(name)
.is_some_and(|(_, meaning)| *meaning == live)
)
}
Ifcsname => {
@@ -2620,6 +2669,11 @@ impl Executor {
_ => break token,
}
};
let first = if first.token.kind == TokenKind::Command(CommandName::FrozenPrimitivePrefix) {
self.deferred_internal_primitive(first, files)?
} else {
first
};
if let TokenKind::Character('\u{0060}', CatCode::Other) = first.token.kind {
let next = self.required_raw(files)?;
let character = match &next.token.kind {
@@ -3272,6 +3326,10 @@ impl Executor {
}
TokenKind::Command(CommandName::Control(name)) => format!("\\{name}"),
TokenKind::Command(CommandName::FrozenRelax) => "\\relax".into(),
TokenKind::Command(CommandName::FrozenPrimitive(name)) => format!("\\{name}"),
TokenKind::Command(CommandName::FrozenPrimitivePrefix) => {
format!("\\{}", self.state.primitive_prefix_name())
}
TokenKind::Command(CommandName::Font(font)) => {
format!("\\{}", self.font_token_name(*font))
}
@@ -3474,6 +3532,10 @@ impl Executor {
}
TokenKind::Command(CommandName::Control(name)) => format!("{escape}{name}"),
TokenKind::Command(CommandName::FrozenRelax) => format!("{escape}relax"),
TokenKind::Command(CommandName::FrozenPrimitive(name)) => format!("{escape}{name}"),
TokenKind::Command(CommandName::FrozenPrimitivePrefix) => {
format!("{escape}{}", self.state.primitive_prefix_name())
}
TokenKind::Command(CommandName::Font(font)) => {
self.reads.insert(Dependency::FontResources);
format!("{escape}{}", self.font_token_name(*font))
@@ -3494,7 +3556,9 @@ impl Executor {
text
}
fn message_token(&mut self, token: &Token) -> std::string::String {
if matches!(token.kind, TokenKind::Command(CommandName::Font(_))) {
if matches!(token.kind, TokenKind::Command(
CommandName::Font(_) | CommandName::FrozenPrimitivePrefix | CommandName::FrozenPrimitive(_)
)) {
return format!("{} ", self.string_token(&token.kind));
}
@@ -4030,6 +4094,18 @@ impl Executor {
self.paragraph.as_mut().unwrap().nodes.push(space);
}
}
DeferredPrimitive => {
let operand = self.normal_raw(files)?;
if let Some((original, expandable)) = self.frozen_primitive(&operand.token) {
if expandable {
return self.fail(format!(
"misplaced {}",
self.token_string(&original.token.kind)
));
}
self.unread(original)?;
}
}
Ignorespaces => {
while let Some(next) = self.expanded(files)? {
if !self.is_spacer(&next.token) {