Decode XeTeX input files and streams with original byte spans

This commit is contained in:
gloamxun committed 2026-11-21 17:10:46 +08:00
1 parent 22e3ba993d
commit d11ac0cba1
18 files changed
+1040 -109

No files matched your search

+74 -36
View File
@@ -513,6 +513,15 @@ impl Executor {
}
}
}
if let Some((line, column)) = self.inputs.iter().rev().find_map(|frame| match frame {
InputFrame::Source { source, mouth, .. } if *source == span.source => {
mouth.xe_location(span.start)
}
_ => None,
}) {
error.line = line;
error.column = column;
}
error.context = self.input_context();
error.scanner_status = self.scanner_status.clone();
error
@@ -561,7 +570,13 @@ impl Executor {
return self.fail("native input stack limit exceeded");
}
let source = self.sources.len();
let mouth = Mouth::new(source, file.data.clone(), self.state.input_profile());
let profile = self.state.input_profile();
if profile == InputProfile::XeTeX {
self.reads.insert(Dependency::XeDefaultInputEncoding);
}
let mouth = Mouth::with_encoding(
source, file.data.clone(), profile, &self.state.xe_default_encoding,
);
self.sources.push(file);
self.reads.insert(Dependency::Source(source));
self.inputs.push(InputFrame::Source {
@@ -2728,7 +2743,11 @@ impl Executor {
data: Vec::new(),
});
self.stream_sources.insert(source);
self.io.inputs.insert(index, InputStream { source });
let xe_encoding = (self.state.input_profile() == InputProfile::XeTeX).then(|| {
self.reads.insert(Dependency::XeDefaultInputEncoding);
self.state.xe_default_encoding.clone()
});
self.io.inputs.insert(index, InputStream { source, xe_encoding, xe_skip_lf: false });
self.reads.insert(Dependency::Source(source));
}
}
@@ -2774,9 +2793,7 @@ impl Executor {
let mut depth = 0i32;
loop {
self.tick()?;
let line = files
.read_line(index)
.map_err(|e| self.error(e, self.current))?;
let (line, encoding) = self.read_physical_stream_line(index, source, files)?;
let eof = line.is_none();
if eof {
files
@@ -2793,42 +2810,52 @@ impl Executor {
let end = self.sources[source].data.len();
self.reads.insert(Dependency::EndLineChar);
if readline {
let mut length = line.len();
while length > 0 && matches!(line[length - 1], b'\r' | b'\n' | b' ') {
length -= 1;
}
let characters = self
.state
.input_profile()
.characters(&line[..length])
.map_err(|e| {
self.error(
let chars: Vec<xe_encoding::Character> = if let Some(encoding) = &encoding {
let (resolved, bom) = encoding.resolve(&line);
let (length, _) = resolved.framing().line_boundary(&line[bom..]);
let (mut chars, warnings) = resolved.decode(&line[bom..bom + length], base + bom)
.map_err(|e| self.error(e, Span { source, start: base + bom, end }))?;
while chars.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
chars.pop();
}
if warnings > 0 {
self.observe_xe_utf8_warnings(
vec![(self.input_position.map_or(1, |(_, line)| line), warnings)],
files,
)?;
}
chars
} else {
let mut length = line.len();
while length > 0 && matches!(line[length - 1], b'\r' | b'\n' | b' ') {
length -= 1;
}
let profile = self.state.input_profile();
profile.characters(&line[..length])
.map_err(|e| self.error(
"invalid UTF-8 input for Unicode scalar profile",
Span {
source,
start: base + e.valid_up_to(),
end: base
+ e.valid_up_to()
end: base + e.valid_up_to()
+ e.error_len().unwrap_or(length - e.valid_up_to()),
},
)
})?;
for (offset, c) in characters {
))?
.map(|(offset, value)| xe_encoding::Character {
value,
start: base + offset,
end: base + offset + profile.width(value),
})
.collect()
};
for c in chars {
self.tick()?;
body.push(Token::new(
TokenKind::Character(
c,
if c == ' ' {
CatCode::Space
} else {
CatCode::Other
},
c.value,
if c.value == ' ' { CatCode::Space } else { CatCode::Other },
),
Span {
source,
start: base + offset,
end: base + offset + self.state.input_profile().width(c),
},
Span { source, start: c.start, end: c.end },
));
if body.len() > self.limits.tokens {
return self.fail("native read token limit exceeded");
@@ -2855,11 +2882,14 @@ impl Executor {
}
// Mouth reads exactly one physical line with the current categories;
// the virtual EOF line is real TeX read semantics, not host data.
let mut mouth = Mouth::new(
source,
if eof { b"\n".to_vec() } else { line },
self.state.input_profile(),
);
let profile = self.state.input_profile();
let mut mouth = if eof {
Mouth::generated(source, b"\n".to_vec(), profile, Some('\n'))
} else {
Mouth::with_encoding(
source, line, profile, encoding.as_ref().unwrap_or(&xe_encoding::Encoding::Auto),
)
};
while let Some(mut token) =
mouth
.next(&self.state, &mut self.reads)
@@ -2888,6 +2918,7 @@ impl Executor {
return self.fail("native read token limit exceeded");
}
}
self.observe_xe_utf8_warnings(mouth.take_utf8_warnings(), files)?;
if depth == 0 {
break;
}
@@ -3188,6 +3219,8 @@ impl Executor {
Primitive::XeTeXrevision => "XeTeXrevision".into(),
Primitive::XeTeXpdffile => "XeTeXpdffile".into(),
Primitive::XeTeXpicfile => "XeTeXpicfile".into(),
Primitive::XeTeXinputencoding => "XeTeXinputencoding".into(),
Primitive::XeTeXdefaultencoding => "XeTeXdefaultencoding".into(),
Primitive::BeginL => "beginL".into(),
Primitive::EndL => "endL".into(),
Primitive::BeginR => "beginR".into(),
@@ -3645,6 +3678,8 @@ impl Executor {
let line = mouth
.load_line(&self.state, &mut self.reads)
.map(|_| (*source, mouth.line_number()));
let warnings = mouth.take_utf8_warnings();
self.observe_xe_utf8_warnings(warnings, files)?;
self.input_position =
Some(line.map_err(|(span, message)| self.error(message, span))?);
self.push_mode_hook(TokenParameter::Everyjob)?;
@@ -3872,6 +3907,9 @@ impl Executor {
self.request_page();
self.events.push(Event::Paragraph(token.span));
}
XeTeXinputencoding | XeTeXdefaultencoding => {
self.change_xe_encoding(primitive, files)?;
}
BeginL | EndL | BeginR | EndR => {
let boundary = match primitive {
BeginL => DirectionBoundary::BeginL,