Expand native Unicode character primitives and definitions
This commit is contained in:
1 parent
d17680fcd8
commit
651fab42f1
7 files changed
+227
-31
No files matched your search
@@ -674,6 +674,64 @@ impl Executor {
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
// XeTeX scans a 16-bit code for \char in math and a full Unicode
|
||||
// character number for shorthand definitions and text character commands.
|
||||
fn character_number(
|
||||
&mut self,
|
||||
command: &str,
|
||||
unicode_maximum: i32,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<u32, Error> {
|
||||
let number = self.integer(files)?;
|
||||
let maximum = if self.state.input_profile() == super::InputProfile::UnicodeScalar {
|
||||
unicode_maximum
|
||||
} else {
|
||||
255
|
||||
};
|
||||
if !(0..=maximum).contains(&number) {
|
||||
return self.fail(format!("{command} must be in 0..{maximum}"));
|
||||
}
|
||||
Ok(number as u32)
|
||||
}
|
||||
|
||||
fn scalar_character(&self, number: u32, span: Span) -> Result<char, Error> {
|
||||
char::from_u32(number)
|
||||
.ok_or_else(|| self.error(format!("character U+{number:04X} is not a Unicode scalar"), span))
|
||||
}
|
||||
|
||||
fn expand_unicode_character(
|
||||
&mut self,
|
||||
primitive: Primitive,
|
||||
token: &Token,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
use CatCode::*;
|
||||
let c = self.character_number("Unicode character", 0x10FFFF, files)?;
|
||||
let c = self.scalar_character(c, token.span)?;
|
||||
let cat = if primitive == Primitive::Ucharcat {
|
||||
let category = self.integer(files)?;
|
||||
if !matches!(category, 1..=4 | 6..=8 | 10..=13) {
|
||||
return self.fail(format!("invalid category code for Ucharcat: {category}"));
|
||||
}
|
||||
CatCode::from_number(category).expect("validated category")
|
||||
} else if c == ' ' {
|
||||
Space
|
||||
} else {
|
||||
Other
|
||||
};
|
||||
let kind = if cat == Active {
|
||||
TokenKind::Command(CommandName::Active(c))
|
||||
} else {
|
||||
TokenKind::Character(c, cat)
|
||||
};
|
||||
self.push(vec![Token {
|
||||
kind,
|
||||
span: token.span,
|
||||
invocation: token.invocation,
|
||||
}
|
||||
.into()])
|
||||
}
|
||||
|
||||
fn roman_numeral(&self, mut number: usize) -> Result<String, Error> {
|
||||
let mut text = String::new();
|
||||
for (value, symbol) in [
|
||||
@@ -802,6 +860,7 @@ impl Executor {
|
||||
let value = self.integer(files)?;
|
||||
self.print_tokens(&value.to_string(), token)?;
|
||||
}
|
||||
Uchar | Ucharcat => self.expand_unicode_character(primitive, token, files)?,
|
||||
Romannumeral => {
|
||||
let value = self.integer(files)?;
|
||||
if value > 0 {
|
||||
@@ -1908,10 +1967,7 @@ impl Executor {
|
||||
self.define_traced(name.clone(), Meaning::Primitive(Relax), global, files)?;
|
||||
self.optional_equals(files)?;
|
||||
let value = match primitive {
|
||||
Chardef => Meaning::CharGiven(
|
||||
u8::try_from(self.integer(files)?)
|
||||
.map_err(|_| self.error("chardef must be in 0..255", self.current))?,
|
||||
),
|
||||
Chardef => Meaning::CharGiven(self.character_number("chardef", 0x10FFFF, files)?),
|
||||
Mathchardef => {
|
||||
let code = self.integer(files)?;
|
||||
if !(0..32768).contains(&code) {
|
||||
@@ -3071,6 +3127,8 @@ impl Executor {
|
||||
Primitive::ItalicCorrection => "/".into(),
|
||||
Primitive::Etexversion => "eTeXversion".into(),
|
||||
Primitive::Etexrevision => "eTeXrevision".into(),
|
||||
Primitive::Uchar => "Uchar".into(),
|
||||
Primitive::Ucharcat => "Ucharcat".into(),
|
||||
Primitive::Umathcode => "Umathcode".into(),
|
||||
Primitive::Umathcodenum => "Umathcodenum".into(),
|
||||
Primitive::Umathchar => "Umathchar".into(),
|
||||
@@ -3831,14 +3889,8 @@ impl Executor {
|
||||
Patterns => self.patterns(files)?,
|
||||
Setlanguage => self.set_language(files)?,
|
||||
Char => {
|
||||
let n = self.integer(files)?;
|
||||
let c = if self.state.input_profile() == super::InputProfile::UnicodeScalar {
|
||||
u32::try_from(n).ok().and_then(char::from_u32)
|
||||
.ok_or_else(|| self.error("char requires a Unicode scalar", token.span))?
|
||||
} else {
|
||||
char::from(u8::try_from(n)
|
||||
.map_err(|_| self.error("char must be in 0..255", token.span))?)
|
||||
};
|
||||
let c = self.character_number("char", 0x10FFFF, files)?;
|
||||
let c = self.scalar_character(c, token.span)?;
|
||||
self.text_character(c, CatCode::Other, &token, files)?;
|
||||
self.events.push(Event::Character(Token {
|
||||
kind: TokenKind::Character(c, CatCode::Other),
|
||||
@@ -3902,9 +3954,10 @@ impl Executor {
|
||||
self.finish_assignment()?;
|
||||
}
|
||||
Meaning::CharGiven(c) => {
|
||||
self.text_character(c as char, CatCode::Other, &token, files)?;
|
||||
let c = self.scalar_character(c, token.span)?;
|
||||
self.text_character(c, CatCode::Other, &token, files)?;
|
||||
self.events.push(Event::Character(Token {
|
||||
kind: TokenKind::Character(c as char, CatCode::Other),
|
||||
kind: TokenKind::Character(c, CatCode::Other),
|
||||
..token
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user