Invoke frozen original primitives in native TeX
This commit is contained in:
1 parent
2007ac5627
commit
ba548ef05d
10 files changed
+219
-33
No files matched your search
@@ -405,6 +405,14 @@ impl Printer<'_> {
|
||||
self.command("relax");
|
||||
self.literal(" ");
|
||||
}
|
||||
TokenKind::Command(CommandName::FrozenPrimitivePrefix) => {
|
||||
self.command(self.state.primitive_prefix_name());
|
||||
self.literal(" ");
|
||||
}
|
||||
TokenKind::Command(CommandName::FrozenPrimitive(name)) => {
|
||||
self.command(name);
|
||||
self.literal(" ");
|
||||
}
|
||||
TokenKind::Command(CommandName::Font(font)) => {
|
||||
let name = self.state.fonts.font(*font)?.identifier_text();
|
||||
self.command(&name);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -18,7 +18,8 @@ impl Executor {
|
||||
let c = match &mut token.kind {
|
||||
TokenKind::Character(c, _) | TokenKind::Command(CommandName::Active(c)) => c,
|
||||
TokenKind::Command(
|
||||
CommandName::Control(_) | CommandName::Font(_) | CommandName::FrozenRelax,
|
||||
CommandName::Control(_) | CommandName::Font(_) | CommandName::FrozenRelax
|
||||
| CommandName::FrozenPrimitivePrefix | CommandName::FrozenPrimitive(_),
|
||||
) => continue,
|
||||
};
|
||||
self.reads.insert(Dependency::CaseCode(kind, *c));
|
||||
|
||||
@@ -387,11 +387,19 @@ impl Executor {
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) {
|
||||
match name {
|
||||
CommandName::FrozenRelax => {
|
||||
CommandName::FrozenRelax | CommandName::FrozenPrimitivePrefix
|
||||
| CommandName::FrozenPrimitive(_) => {
|
||||
if let Ok(escape) = u8::try_from(self.state.escapechar) {
|
||||
out.character(char::from(escape));
|
||||
}
|
||||
out.text("relax ");
|
||||
let name = match name {
|
||||
CommandName::FrozenRelax => "relax",
|
||||
CommandName::FrozenPrimitivePrefix => self.state.primitive_prefix_name(),
|
||||
CommandName::FrozenPrimitive(name) => name,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
out.text(name);
|
||||
out.push(' ');
|
||||
}
|
||||
CommandName::Font(font) => {
|
||||
reads.insert(Dependency::FontResources);
|
||||
@@ -569,7 +577,8 @@ impl Executor {
|
||||
.unwrap_or_default();
|
||||
format!("{escape}{} ", self.font_token_name(*font))
|
||||
}
|
||||
CommandName::FrozenRelax => unreachable!("frozen relax is not a macro"),
|
||||
CommandName::FrozenRelax | CommandName::FrozenPrimitivePrefix
|
||||
| CommandName::FrozenPrimitive(_) => unreachable!("frozen primitive is not a macro"),
|
||||
CommandName::Active(c) => c.to_string(),
|
||||
CommandName::Control(name) => {
|
||||
if name.chars().count() == 1 {
|
||||
|
||||
@@ -882,7 +882,7 @@ impl Executor {
|
||||
| Font | Fontdimen | Hyphenchar | Skewchar | Lpcode | Rpcode | Efcode | Pdffontexpand
|
||||
| Knbccode | Knaccode | Letterspacefont | Textfont | Scriptfont
|
||||
| Scriptscriptfont | Endlinechar | Newlinechar | Escapechar | Globaldefs | Time
|
||||
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
|
||||
| Day | Month | Year | BeginGroup | EndGroup | Relax | DeferredPrimitive | Message | Errmessage
|
||||
| Endinput | Showbox | Show | Showthe | Showtokens | Showlists | Showifs | Showgroups
|
||||
| Discretionary | DiscretionaryHyphen
|
||||
| Parshape | LinePenalties(_) | Partokenname => {
|
||||
|
||||
@@ -100,7 +100,8 @@ impl FontInstance {
|
||||
CommandName::Control(name) if name.is_empty() => "FONT".into(),
|
||||
CommandName::Control(name) => name.clone(),
|
||||
CommandName::Active(c) => format!("FONT{c}"),
|
||||
CommandName::Font(_) | CommandName::FrozenRelax => {
|
||||
CommandName::Font(_) | CommandName::FrozenRelax
|
||||
| CommandName::FrozenPrimitivePrefix | CommandName::FrozenPrimitive(_) => {
|
||||
unreachable!("a frozen id is not a definition selector")
|
||||
}
|
||||
}
|
||||
@@ -334,7 +335,7 @@ impl FontCatalog {
|
||||
defaults: FontIntegers,
|
||||
optional: bool,
|
||||
) -> Result<FontId, FontLoadError> {
|
||||
if matches!(selector, CommandName::Font(_) | CommandName::FrozenRelax) {
|
||||
if !matches!(selector, CommandName::Control(_) | CommandName::Active(_)) {
|
||||
return Err(FontLoadError::Fatal("frozen font identifiers cannot name font definitions".into()));
|
||||
}
|
||||
size.validate()?;
|
||||
|
||||
@@ -16,9 +16,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 101;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 67] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, SCHEMA,
|
||||
const SCHEMA: u32 = 102;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 68] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
@@ -217,8 +217,8 @@ impl FormatImage {
|
||||
if image.schema < 100 {
|
||||
image.state.install_line_penalty_primitives();
|
||||
}
|
||||
if image.schema < 101 {
|
||||
image.state.install_primitive_inspection();
|
||||
if image.schema < 102 {
|
||||
image.state.install_primitive_commands();
|
||||
}
|
||||
let pdf_objects = image.state.pdf_objects.as_deref();
|
||||
let mut numbered_images = 0;
|
||||
|
||||
@@ -16,12 +16,18 @@ pub enum CommandName {
|
||||
/// It retains the loaded font even when the spelling selector is redefined.
|
||||
Font(FontId),
|
||||
FrozenRelax,
|
||||
/// Preserved in token lists before the ordinary token naming the operand.
|
||||
FrozenPrimitivePrefix,
|
||||
/// The selected primitive meaning, which survives putting a token back.
|
||||
FrozenPrimitive(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum Primitive {
|
||||
Relax,
|
||||
Ignorespaces,
|
||||
UsePrimitive,
|
||||
DeferredPrimitive,
|
||||
Explicitspace,
|
||||
Par,
|
||||
BeginGroup,
|
||||
@@ -495,6 +501,7 @@ impl Primitive {
|
||||
| Firstmarks
|
||||
| Botmarks
|
||||
| Noexpand
|
||||
| UsePrimitive
|
||||
| Expandafter
|
||||
| Number
|
||||
| Uchar
|
||||
@@ -1357,7 +1364,7 @@ impl Default for State {
|
||||
state.parameters.insert(p, v);
|
||||
}
|
||||
state.install_pdftex_primitives();
|
||||
state.install_primitive_inspection();
|
||||
state.install_primitive_commands();
|
||||
state.install_unicode_commands();
|
||||
state
|
||||
}
|
||||
@@ -1596,7 +1603,7 @@ impl State {
|
||||
use Primitive::*;
|
||||
self.profile = Profile::EtexBootstrap;
|
||||
self.install_line_penalty_primitives();
|
||||
self.install_primitive_inspection();
|
||||
self.install_primitive_commands();
|
||||
let hook = TokenParameter::Everyeof;
|
||||
self.commands.insert(
|
||||
CommandName::Control(hook.name()),
|
||||
@@ -1942,14 +1949,16 @@ impl State {
|
||||
self.install_unicode_commands();
|
||||
self.install_xetex_etex_commands();
|
||||
self.install_direction_commands();
|
||||
self.install_primitive_inspection();
|
||||
self.install_primitive_commands();
|
||||
self
|
||||
}
|
||||
|
||||
pub(super) fn install_primitive_inspection(&mut self) {
|
||||
pub(super) fn install_primitive_commands(&mut self) {
|
||||
let xe = self.input_profile == super::InputProfile::XeTeX;
|
||||
self.update_profile_command("ifprimitive", Meaning::Primitive(Primitive::Ifprimitive), xe);
|
||||
self.update_profile_command("ifpdfprimitive", Meaning::Primitive(Primitive::Ifpdfprimitive), !xe);
|
||||
self.update_profile_command("primitive", Meaning::Primitive(Primitive::UsePrimitive), xe);
|
||||
self.update_profile_command("pdfprimitive", Meaning::Primitive(Primitive::UsePrimitive), !xe);
|
||||
self.update_profile_command(
|
||||
"showtokens",
|
||||
Meaning::Primitive(Primitive::Showtokens),
|
||||
@@ -2049,7 +2058,14 @@ impl State {
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn primitive_prefix_name(&self) -> &'static str {
|
||||
if self.input_profile == super::InputProfile::XeTeX { "primitive" } else { "pdfprimitive" }
|
||||
}
|
||||
|
||||
pub(super) fn profile_primitive_name(&self, primitive: Primitive) -> Option<&'static str> {
|
||||
if matches!(primitive, Primitive::UsePrimitive | Primitive::DeferredPrimitive) {
|
||||
return Some(self.primitive_prefix_name());
|
||||
}
|
||||
if self.input_profile == super::InputProfile::XeTeX {
|
||||
return XETEX_PRIMITIVE_ALIASES
|
||||
.iter()
|
||||
@@ -2122,10 +2138,13 @@ impl State {
|
||||
})
|
||||
}
|
||||
/// Built-in meanings before any macro, alias or format assignments.
|
||||
pub(super) fn original_primitive(&self, name: &CommandName) -> Option<&'static Meaning> {
|
||||
pub(super) fn original_primitive<'a>(
|
||||
&self,
|
||||
name: &'a CommandName,
|
||||
) -> Option<(&'a str, &'static Meaning)> {
|
||||
use super::InputProfile;
|
||||
use std::sync::OnceLock;
|
||||
type CommandCatalogue = OnceLock<BTreeMap<CommandName, Meaning>>;
|
||||
type CommandCatalogue = OnceLock<BTreeMap<String, Meaning>>;
|
||||
|
||||
static CATALOGUES: [[CommandCatalogue; 2]; 3] =
|
||||
[const { [const { OnceLock::new() }; 2] }; 3];
|
||||
@@ -2134,27 +2153,36 @@ impl State {
|
||||
InputProfile::UnicodeScalar => 1,
|
||||
InputProfile::XeTeX => 2,
|
||||
};
|
||||
let name = match name {
|
||||
CommandName::Control(name) | CommandName::FrozenPrimitive(name) => name.as_str(),
|
||||
CommandName::FrozenPrimitivePrefix => self.primitive_prefix_name(),
|
||||
CommandName::FrozenRelax => "relax",
|
||||
CommandName::Font(FontId::NULL) => "nullfont",
|
||||
_ => return None,
|
||||
};
|
||||
let extended = self.profile == Profile::EtexBootstrap;
|
||||
CATALOGUES[input][usize::from(extended)]
|
||||
.get_or_init(|| {
|
||||
let state = Self::initex().with_input_profile(self.input_profile);
|
||||
let state = if extended { state.with_etex() } else { state };
|
||||
state.commands
|
||||
state.commands.into_iter().filter_map(|(name, meaning)| match name {
|
||||
CommandName::Control(name) => Some((name, meaning)),
|
||||
_ => None,
|
||||
}).collect()
|
||||
})
|
||||
.get(name)
|
||||
.map(|meaning| (name, meaning))
|
||||
}
|
||||
|
||||
pub fn meaning(&self, name: &CommandName) -> Meaning {
|
||||
if *name == CommandName::FrozenRelax {
|
||||
return Meaning::Primitive(Primitive::Relax);
|
||||
match name {
|
||||
CommandName::FrozenRelax => Meaning::Primitive(Primitive::Relax),
|
||||
CommandName::FrozenPrimitivePrefix => Meaning::Primitive(Primitive::DeferredPrimitive),
|
||||
CommandName::FrozenPrimitive(_) => self.original_primitive(name)
|
||||
.map(|(_, meaning)| meaning.clone()).unwrap_or(Meaning::Undefined),
|
||||
CommandName::Font(font) => Meaning::Font(*font),
|
||||
_ => self.commands.get(name).cloned().unwrap_or(Meaning::Undefined),
|
||||
}
|
||||
if let CommandName::Font(font) = name {
|
||||
return Meaning::Font(*font);
|
||||
}
|
||||
self.commands
|
||||
.get(name)
|
||||
.cloned()
|
||||
.unwrap_or(Meaning::Undefined)
|
||||
}
|
||||
/// Classic projection for byte-table consumers. A code outside the
|
||||
/// representable classic range has TeX's recovery value of zero.
|
||||
|
||||
Reference in new issue
Block a user