Implement native paragraph token naming and execution contexts
This commit is contained in:
1 parent
862801ee5f
commit
13ab32247d
12 files changed
+118
-24
No files matched your search
@@ -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)?;
|
||||
|
||||
@@ -169,7 +169,16 @@ impl Executor {
|
||||
) -> Result<(), Error> {
|
||||
match boundary {
|
||||
Boundary::Prefix => unreachable!("prefix boundary is handled by raw"),
|
||||
Boundary::Entry => self.finish_alignment_column(files),
|
||||
Boundary::Entry => {
|
||||
if self.alignments.last().is_some_and(|frame| !frame.horizontal)
|
||||
&& self.in_unrestricted_horizontal_mode()
|
||||
&& self.read_parameter(Parameter::Partokencontext).width() > 1
|
||||
{
|
||||
self.alignment_marker(Boundary::Entry)?;
|
||||
return self.insert_paragraph_token(self.current);
|
||||
}
|
||||
self.finish_alignment_column(files)
|
||||
}
|
||||
Boundary::Peek => self.peek_alignment(files),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -788,7 +788,7 @@ impl Executor {
|
||||
| Scriptscriptfont | Endlinechar | Newlinechar | Escapechar | Globaldefs | Time
|
||||
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
|
||||
| Endinput | Showbox | Show | Showthe | Discretionary | DiscretionaryHyphen
|
||||
| Parshape => {
|
||||
| Parshape | Partokenname => {
|
||||
return Ok(false);
|
||||
}
|
||||
_ => return self.fail(format!("unsupported native math primitive: {p:?}")),
|
||||
|
||||
@@ -8,8 +8,8 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 36;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 8] = [27, 28, 31, 32, 33, 34, 35, SCHEMA];
|
||||
const SCHEMA: u32 = 37;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 9] = [27, 28, 31, 32, 33, 34, 35, 36, SCHEMA];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
|
||||
@@ -423,6 +423,7 @@ pub enum Dependency {
|
||||
InputStream(u8),
|
||||
InputPosition(usize),
|
||||
OutputStream(u8),
|
||||
ParagraphToken,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
|
||||
@@ -499,7 +499,10 @@ impl Mouth {
|
||||
CatCode::EndLine => {
|
||||
self.cursor = self.line.len();
|
||||
let kind = match self.state {
|
||||
LineState::New => TokenKind::Command(CommandName::Control("par".into())),
|
||||
LineState::New => {
|
||||
reads.insert(Dependency::ParagraphToken);
|
||||
state.paragraph_token()
|
||||
}
|
||||
LineState::Middle => TokenKind::Character(' ', CatCode::Space),
|
||||
LineState::Skip => continue,
|
||||
};
|
||||
|
||||
@@ -113,6 +113,7 @@ pub enum Parameter {
|
||||
Tabskip,
|
||||
Pdfprotrudechars,
|
||||
Pdfadjustspacing,
|
||||
Partokencontext,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum ParameterValue {
|
||||
@@ -229,6 +230,7 @@ impl Parameter {
|
||||
Self::Tabskip,
|
||||
Self::Pdfprotrudechars,
|
||||
Self::Pdfadjustspacing,
|
||||
Self::Partokencontext,
|
||||
];
|
||||
pub fn name(self) -> String {
|
||||
format!("{self:?}").to_ascii_lowercase()
|
||||
|
||||
@@ -327,6 +327,7 @@ pub enum Primitive {
|
||||
Pdfuniformdeviate,
|
||||
Pdfnormaldeviate,
|
||||
Pdfpageref,
|
||||
Partokenname,
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
@@ -594,6 +595,8 @@ pub struct State {
|
||||
pub newlinechar: i32,
|
||||
pub escapechar: i32,
|
||||
pub globaldefs: i32,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
paragraph_token: Option<CommandName>,
|
||||
groups: Vec<Group>,
|
||||
job_parameters: BTreeMap<JobParameter, i32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -709,6 +712,7 @@ impl Default for State {
|
||||
escapechar: 92,
|
||||
globaldefs: 0,
|
||||
groups: Vec::new(),
|
||||
paragraph_token: None,
|
||||
job_parameters: BTreeMap::new(),
|
||||
pdf_creation_date: None,
|
||||
registers: BTreeMap::new(),
|
||||
@@ -965,6 +969,7 @@ impl Default for State {
|
||||
("pdfuniformdeviate", Pdfuniformdeviate),
|
||||
("pdfnormaldeviate", Pdfnormaldeviate),
|
||||
("pdfpageref", Pdfpageref),
|
||||
("partokenname", Partokenname),
|
||||
("pdfcolorstack", Pdfcolorstack),
|
||||
("pdfsave", Pdfsave),
|
||||
("pdfstartlink", Pdfstartlink),
|
||||
@@ -1334,8 +1339,11 @@ impl State {
|
||||
pub fn tokens(&self, index: u16) -> Vec<Token> {
|
||||
self.toks.get(&index).cloned().unwrap_or_default()
|
||||
}
|
||||
pub(super) fn group_kind(&self) -> Option<GroupKind> {
|
||||
self.groups.last().map(|group| group.kind)
|
||||
}
|
||||
pub(super) fn group_type(&self) -> i32 {
|
||||
match self.groups.last().map(|g| g.kind) {
|
||||
match self.group_kind() {
|
||||
None => 0,
|
||||
Some(GroupKind::Simple) => 1,
|
||||
Some(GroupKind::SemiSimple) => 14,
|
||||
@@ -1571,6 +1579,26 @@ impl State {
|
||||
pub fn count(&self, index: u16) -> i32 {
|
||||
self.counts.get(&index).copied().unwrap_or(0)
|
||||
}
|
||||
pub(super) fn set_paragraph_token(&mut self, name: CommandName) {
|
||||
self.paragraph_token = match &name {
|
||||
CommandName::Control(value) if value == "par" => None,
|
||||
_ => Some(name),
|
||||
};
|
||||
}
|
||||
pub(super) fn paragraph_token(&self) -> TokenKind {
|
||||
let name = self
|
||||
.paragraph_token
|
||||
.clone()
|
||||
.unwrap_or_else(|| CommandName::Control("par".into()));
|
||||
TokenKind::Command(name)
|
||||
}
|
||||
pub(super) fn is_paragraph_token(&self, token: &TokenKind) -> bool {
|
||||
match (token, &self.paragraph_token) {
|
||||
(TokenKind::Command(name), Some(paragraph)) => name == paragraph,
|
||||
(TokenKind::Command(CommandName::Control(name)), None) => name == "par",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
pub fn group_depth(&self) -> usize {
|
||||
self.groups.len()
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ fn genuine_fraction_primitives_and_retained_boxes_survive_format_roundtrip() {
|
||||
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
||||
let payload: serde_json::Value =
|
||||
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(payload["schema"], 36);
|
||||
assert_eq!(payload["schema"], 37);
|
||||
let state = State::initex().with_etex();
|
||||
let loaded = FormatImage::decode(&bytes)
|
||||
.unwrap()
|
||||
|
||||
@@ -334,7 +334,7 @@ fn old_schema_and_missing_unicode_state_are_rejected_even_with_valid_checksums()
|
||||
let envelope: serde_json::Value = serde_json::from_slice(&encoded).unwrap();
|
||||
let original: serde_json::Value =
|
||||
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(original["schema"], 36);
|
||||
assert_eq!(original["schema"], 37);
|
||||
for (schema, expected) in [
|
||||
(1, "incompatible native format"),
|
||||
(2, "incompatible native format"),
|
||||
|
||||
@@ -164,7 +164,7 @@ fn new_format_owns_vcenter_and_boxed_math_without_reinjecting_old_meanings() {
|
||||
let envelope: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
|
||||
let payload: serde_json::Value =
|
||||
serde_json::from_str(envelope["payload"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(payload["schema"], 36);
|
||||
assert_eq!(payload["schema"], 37);
|
||||
let executor = FormatImage::decode(&bytes)
|
||||
.unwrap()
|
||||
.new_job(
|
||||
|
||||
Reference in new issue
Block a user