Decode XeTeX input files and streams with original byte spans
This commit is contained in:
1 parent
22e3ba993d
commit
d11ac0cba1
18 files changed
+1040
-109
No files matched your search
@@ -1,11 +1,15 @@
|
||||
use super::{CatCode, CommandName, Dependency, Span, State, Token, TokenKind};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Character {
|
||||
value: char,
|
||||
start: usize,
|
||||
end: usize,
|
||||
use super::xe_encoding::{Character, Encoding};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct XePhysical {
|
||||
encoding: Encoding,
|
||||
autodetected: Option<(Encoding, usize)>,
|
||||
skip_lf: bool,
|
||||
terminated: bool,
|
||||
line_start: usize,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -184,6 +188,36 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xetex_utf16_lookahead_preserves_physical_spans_and_rebinding() {
|
||||
use super::super::InputProfile;
|
||||
let utf16 = |text: &str| {
|
||||
[0xff, 0xfe].into_iter()
|
||||
.chain(text.encode_utf16().flat_map(u16::to_le_bytes))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let state = State::default().with_input_profile(InputProfile::XeTeX);
|
||||
let mut reads = BTreeSet::new();
|
||||
let mut mouth = Mouth::new(8, utf16("A\r\n😀\r\n"), InputProfile::XeTeX);
|
||||
let first = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert_eq!(first.span, Span { source: 8, start: 2, end: 4 });
|
||||
assert_eq!(mouth.physical_frontier(), Some(6));
|
||||
assert!(!mouth.matches_loaded_prefix(&utf16("B\r\n😀\r\n")));
|
||||
let edited = utf16("A\r\nΩ\r\n");
|
||||
assert!(mouth.matches_loaded_prefix(&edited));
|
||||
mouth.rebind_loaded_prefix(edited);
|
||||
let white = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert!(white.kind.is_space());
|
||||
let next = mouth.next(&state, &mut reads).unwrap().unwrap();
|
||||
assert_eq!(next.kind, TokenKind::Character('Ω', state.catcode('Ω')));
|
||||
assert_eq!(next.span, Span { source: 8, start: 8, end: 10 });
|
||||
|
||||
// With only CR loaded, the second byte still affects Auto detection.
|
||||
let mut first_line = Mouth::new(0, b"\rA".to_vec(), InputProfile::XeTeX);
|
||||
first_line.load_line(&state, &mut reads).unwrap();
|
||||
assert!(!first_line.matches_loaded_prefix(b"\r\0A\0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn physical_line_endings_and_utf8_byte_offsets_are_preserved() {
|
||||
let result = tokens("é界 \r\nB\rC");
|
||||
@@ -224,6 +258,8 @@ pub(super) struct Mouth {
|
||||
end_after_line: bool,
|
||||
line_breaks: LineBreaks,
|
||||
line_number: usize,
|
||||
xe: Option<XePhysical>,
|
||||
invalid_utf8: Vec<(usize, usize)>,
|
||||
}
|
||||
|
||||
impl Mouth {
|
||||
@@ -236,6 +272,11 @@ impl Mouth {
|
||||
return false;
|
||||
};
|
||||
let loaded = &self.data[..offset];
|
||||
if let Some(xe) = &self.xe {
|
||||
return (xe.terminated || offset == data.len())
|
||||
&& xe.autodetected.as_ref().is_none_or(|old| *old == Encoding::Auto.resolve(data))
|
||||
&& data.starts_with(loaded);
|
||||
}
|
||||
// A CR looks ahead for LF. For an unterminated line, EOF is part
|
||||
// of what has already been observed.
|
||||
let same_boundary = match loaded.last() {
|
||||
@@ -253,17 +294,41 @@ impl Mouth {
|
||||
self.data.len()
|
||||
}
|
||||
pub fn new(source: usize, data: Vec<u8>, profile: super::InputProfile) -> Self {
|
||||
Self::with_encoding(source, data, profile, &Encoding::Auto)
|
||||
}
|
||||
|
||||
pub(super) fn with_encoding(
|
||||
source: usize,
|
||||
data: Vec<u8>,
|
||||
profile: super::InputProfile,
|
||||
encoding: &Encoding,
|
||||
) -> Self {
|
||||
let (xe, offset) = if profile == super::InputProfile::XeTeX {
|
||||
let (resolved, bom) = encoding.resolve(&data);
|
||||
let detected = (*encoding == Encoding::Auto).then(|| (resolved.clone(), bom));
|
||||
(Some(XePhysical {
|
||||
encoding: resolved,
|
||||
autodetected: detected,
|
||||
skip_lf: false,
|
||||
terminated: false,
|
||||
line_start: bom,
|
||||
}), bom)
|
||||
} else {
|
||||
(None, 0)
|
||||
};
|
||||
Self {
|
||||
source,
|
||||
data,
|
||||
profile,
|
||||
offset: 0,
|
||||
offset,
|
||||
line: Vec::new(),
|
||||
cursor: 0,
|
||||
state: LineState::New,
|
||||
end_after_line: false,
|
||||
line_breaks: LineBreaks::Physical,
|
||||
line_number: 0,
|
||||
xe,
|
||||
invalid_utf8: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub(super) fn generated(
|
||||
@@ -274,6 +339,8 @@ impl Mouth {
|
||||
) -> Self {
|
||||
let mut mouth = Self::new(source, data, profile);
|
||||
mouth.line_breaks = LineBreaks::Generated(separator);
|
||||
mouth.xe = None;
|
||||
mouth.offset = 0;
|
||||
mouth
|
||||
}
|
||||
pub(super) fn forced_eof(&self) -> bool {
|
||||
@@ -291,6 +358,24 @@ impl Mouth {
|
||||
pub fn end_input(&mut self) {
|
||||
self.end_after_line = true;
|
||||
}
|
||||
pub(super) fn change_encoding(&mut self, encoding: Encoding) -> bool {
|
||||
if let Some(xe) = &mut self.xe {
|
||||
xe.encoding = encoding;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn xe_location(&self, offset: usize) -> Option<(usize, usize)> {
|
||||
let xe = self.xe.as_ref()?;
|
||||
(xe.line_start <= offset && offset <= self.offset && self.line_number > 0)
|
||||
.then(|| (self.line_number, 1 + self.line.partition_point(|c| c.end <= offset && c.start < offset)))
|
||||
}
|
||||
|
||||
pub(super) fn take_utf8_warnings(&mut self) -> Vec<(usize, usize)> {
|
||||
std::mem::take(&mut self.invalid_utf8)
|
||||
}
|
||||
pub fn line_number(&self) -> usize {
|
||||
self.line_number
|
||||
}
|
||||
@@ -322,6 +407,9 @@ impl Mouth {
|
||||
if self.end_after_line || self.offset == self.data.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
if self.xe.is_some() {
|
||||
return self.load_xe_line(state, reads);
|
||||
}
|
||||
let start = self.offset;
|
||||
self.line_number += 1;
|
||||
let remaining = &self.data[start..];
|
||||
@@ -399,6 +487,57 @@ impl Mouth {
|
||||
));
|
||||
}
|
||||
};
|
||||
self.finish_line(content_end, end, state, reads);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn load_xe_line(
|
||||
&mut self,
|
||||
state: &State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) -> Result<bool, (Span, String)> {
|
||||
let xe = self.xe.as_mut().unwrap();
|
||||
let framing = xe.encoding.framing();
|
||||
let width = framing.unit_width();
|
||||
if std::mem::take(&mut xe.skip_lf)
|
||||
&& framing.code_unit(&self.data[self.offset..]) == Some(10)
|
||||
{
|
||||
self.offset += width;
|
||||
}
|
||||
let start = self.offset;
|
||||
if start == self.data.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
let (length, terminator) = framing.line_boundary(&self.data[start..]);
|
||||
let content_end = start + length;
|
||||
let end = content_end + if terminator.is_some() { width } else { 0 };
|
||||
xe.line_start = start;
|
||||
xe.skip_lf = terminator == Some(13);
|
||||
xe.terminated = terminator.is_some();
|
||||
self.offset = end;
|
||||
self.line_number += 1;
|
||||
self.line.clear();
|
||||
self.cursor = 0;
|
||||
let (mut chars, invalid_utf8) = xe.encoding.decode(&self.data[start..content_end], start)
|
||||
.map_err(|message| (Span { source: self.source, start, end: content_end }, message))?;
|
||||
while chars.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
|
||||
chars.pop();
|
||||
}
|
||||
self.line = chars;
|
||||
if invalid_utf8 > 0 {
|
||||
self.invalid_utf8.push((self.line_number, invalid_utf8));
|
||||
}
|
||||
self.finish_line(content_end, end, state, reads);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn finish_line(
|
||||
&mut self,
|
||||
content_end: usize,
|
||||
end: usize,
|
||||
state: &State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) {
|
||||
reads.insert(Dependency::EndLineChar);
|
||||
if let Ok(value) = u32::try_from(state.endlinechar)
|
||||
&& value <= 255
|
||||
@@ -412,7 +551,6 @@ impl Mouth {
|
||||
}
|
||||
self.cursor = 0;
|
||||
self.state = LineState::New;
|
||||
Ok(true)
|
||||
}
|
||||
fn catcode(state: &State, reads: &mut BTreeSet<Dependency>, c: char) -> CatCode {
|
||||
reads.insert(Dependency::CatCode(c));
|
||||
|
||||
Reference in new issue
Block a user