Initial texrra engine
Rename the crate, binary, core crate, font families, env vars, and internal commands from texcraft/tex-engine to texrra. Serve the separately versioned texrra-web frontend from TEXRRA_WEB_DIR, ../web, or ./web instead of embedding index.html. Add starred section heading support and an arXiv template conformance fixture.
This commit is contained in:
commit
bb1d9e0561
147 files changed
+66719
No files matched your search
@@ -0,0 +1,477 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{CatCode as EngineCatCode, EngineCommandName, EngineToken, EngineTokenValue};
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MacroDefinition {
|
||||
pub prefix: Vec<EngineTokenValue>,
|
||||
pub parameters: Vec<MacroParameter>,
|
||||
pub optional_first_default: Option<Vec<EngineToken>>,
|
||||
pub replacement: Vec<Replacement>,
|
||||
#[serde(default)]
|
||||
pub is_long: bool,
|
||||
#[serde(default)]
|
||||
pub is_outer: bool,
|
||||
}
|
||||
|
||||
impl MacroDefinition {
|
||||
pub fn instantiate(&self, arguments: &[Vec<EngineToken>]) -> Vec<EngineToken> {
|
||||
let mut expansion = Vec::new();
|
||||
for item in &self.replacement {
|
||||
match item {
|
||||
Replacement::Tokens(tokens) => {
|
||||
expansion.extend(tokens.iter().cloned());
|
||||
}
|
||||
Replacement::Parameter(index) => {
|
||||
expansion.extend(arguments[*index].iter().cloned());
|
||||
}
|
||||
}
|
||||
}
|
||||
expansion
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MacroParameter {
|
||||
pub delimiter: Vec<EngineTokenValue>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub enum Replacement {
|
||||
Tokens(Vec<EngineToken>),
|
||||
Parameter(usize),
|
||||
}
|
||||
|
||||
pub trait MacroDefinitionTokenStream {
|
||||
type Error;
|
||||
|
||||
fn next_unexpanded(&mut self) -> Result<Option<EngineToken>, Self::Error>;
|
||||
fn is_outer_macro(&self, token: &EngineToken) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum MacroDefinitionErrorStream {
|
||||
Input,
|
||||
Unexpanded,
|
||||
}
|
||||
|
||||
pub enum MacroDefinitionScanError<E> {
|
||||
Stream(E),
|
||||
Macro {
|
||||
token: EngineToken,
|
||||
message: String,
|
||||
stream: MacroDefinitionErrorStream,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct ScannedMacroDefinition {
|
||||
pub target: EngineToken,
|
||||
pub parameter_text: Vec<EngineToken>,
|
||||
pub replacement_text: Vec<EngineToken>,
|
||||
}
|
||||
|
||||
pub fn scan_tex_macro_definition<S: MacroDefinitionTokenStream>(
|
||||
stream: &mut S,
|
||||
command: &EngineToken,
|
||||
) -> Result<ScannedMacroDefinition, MacroDefinitionScanError<S::Error>> {
|
||||
let target = loop {
|
||||
let Some(token) = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroDefinitionScanError::Stream)?
|
||||
else {
|
||||
return Err(macro_definition_error(
|
||||
command,
|
||||
"macro definition expects a control sequence",
|
||||
MacroDefinitionErrorStream::Input,
|
||||
));
|
||||
};
|
||||
if is_space(&token) {
|
||||
continue;
|
||||
}
|
||||
break token;
|
||||
};
|
||||
|
||||
if !is_command(&target) {
|
||||
return Err(macro_definition_error(
|
||||
&target,
|
||||
"macro definition expects a control sequence or active character",
|
||||
MacroDefinitionErrorStream::Input,
|
||||
));
|
||||
}
|
||||
|
||||
let mut parameter_text = Vec::new();
|
||||
loop {
|
||||
let Some(next) = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroDefinitionScanError::Stream)?
|
||||
else {
|
||||
return Err(macro_definition_error(
|
||||
command,
|
||||
"unterminated macro definition",
|
||||
MacroDefinitionErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
reject_outer_macro(stream, &next)?;
|
||||
|
||||
if is_begin_group(&next) {
|
||||
let replacement_text = read_braced_replacement(stream, command)?;
|
||||
return Ok(ScannedMacroDefinition {
|
||||
target,
|
||||
parameter_text,
|
||||
replacement_text,
|
||||
});
|
||||
}
|
||||
if is_end_group(&next) {
|
||||
return Err(macro_definition_error(
|
||||
&next,
|
||||
"unexpected end group while reading macro parameters",
|
||||
MacroDefinitionErrorStream::Input,
|
||||
));
|
||||
}
|
||||
if is_parameter(&next) {
|
||||
let Some(parameter_part) = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroDefinitionScanError::Stream)?
|
||||
else {
|
||||
return Err(macro_definition_error(
|
||||
command,
|
||||
"unterminated macro parameter text",
|
||||
MacroDefinitionErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
reject_outer_macro(stream, ¶meter_part)?;
|
||||
if is_begin_group(¶meter_part) {
|
||||
parameter_text.push(parameter_part.clone());
|
||||
let mut replacement_text =
|
||||
read_replacement_after_special_open(stream, command, ¶meter_part)?;
|
||||
replacement_text.push(parameter_part);
|
||||
return Ok(ScannedMacroDefinition {
|
||||
target,
|
||||
parameter_text,
|
||||
replacement_text,
|
||||
});
|
||||
}
|
||||
parameter_text.push(next);
|
||||
parameter_text.push(parameter_part);
|
||||
continue;
|
||||
}
|
||||
parameter_text.push(next);
|
||||
}
|
||||
}
|
||||
|
||||
fn read_braced_replacement<S: MacroDefinitionTokenStream>(
|
||||
stream: &mut S,
|
||||
command: &EngineToken,
|
||||
) -> Result<Vec<EngineToken>, MacroDefinitionScanError<S::Error>> {
|
||||
let mut depth = 1usize;
|
||||
let mut replacement = Vec::new();
|
||||
loop {
|
||||
let Some(next) = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroDefinitionScanError::Stream)?
|
||||
else {
|
||||
return Err(macro_definition_error(
|
||||
command,
|
||||
"unterminated braced command argument",
|
||||
MacroDefinitionErrorStream::Input,
|
||||
));
|
||||
};
|
||||
reject_outer_macro(stream, &next)?;
|
||||
if is_begin_group(&next) {
|
||||
depth += 1;
|
||||
replacement.push(next);
|
||||
} else if is_end_group(&next) {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
return Ok(replacement);
|
||||
}
|
||||
replacement.push(next);
|
||||
} else {
|
||||
replacement.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_replacement_after_special_open<S: MacroDefinitionTokenStream>(
|
||||
stream: &mut S,
|
||||
command: &EngineToken,
|
||||
opening: &EngineToken,
|
||||
) -> Result<Vec<EngineToken>, MacroDefinitionScanError<S::Error>> {
|
||||
let mut depth = 0usize;
|
||||
let mut replacement = Vec::new();
|
||||
loop {
|
||||
let Some(next) = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroDefinitionScanError::Stream)?
|
||||
else {
|
||||
let delimiter = match opening.value {
|
||||
EngineTokenValue::Character { value, .. } => value,
|
||||
_ => '{',
|
||||
};
|
||||
return Err(macro_definition_error(
|
||||
command,
|
||||
&format!("unterminated macro replacement after special delimiter `{delimiter}`"),
|
||||
MacroDefinitionErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
reject_outer_macro(stream, &next)?;
|
||||
if is_begin_group(&next) {
|
||||
depth += 1;
|
||||
replacement.push(next);
|
||||
} else if is_end_group(&next) {
|
||||
if depth == 0 {
|
||||
return Ok(replacement);
|
||||
}
|
||||
depth -= 1;
|
||||
replacement.push(next);
|
||||
} else {
|
||||
replacement.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reject_outer_macro<S: MacroDefinitionTokenStream>(
|
||||
stream: &S,
|
||||
token: &EngineToken,
|
||||
) -> Result<(), MacroDefinitionScanError<S::Error>> {
|
||||
if stream.is_outer_macro(token) {
|
||||
return Err(macro_definition_error(
|
||||
token,
|
||||
"outer macro is forbidden while scanning a definition or argument",
|
||||
MacroDefinitionErrorStream::Input,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn macro_definition_error<E>(
|
||||
token: &EngineToken,
|
||||
message: impl Into<String>,
|
||||
stream: MacroDefinitionErrorStream,
|
||||
) -> MacroDefinitionScanError<E> {
|
||||
MacroDefinitionScanError::Macro {
|
||||
token: token.clone(),
|
||||
message: message.into(),
|
||||
stream,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_space(token: &EngineToken) -> bool {
|
||||
matches!(
|
||||
token.value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::Space,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_begin_group(token: &EngineToken) -> bool {
|
||||
matches!(
|
||||
token.value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_end_group(token: &EngineToken) -> bool {
|
||||
matches!(
|
||||
token.value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_parameter(token: &EngineToken) -> bool {
|
||||
matches!(
|
||||
token.value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::Parameter,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_command(token: &EngineToken) -> bool {
|
||||
matches!(
|
||||
token.value,
|
||||
EngineTokenValue::ControlSequence(_) | EngineTokenValue::Active(_)
|
||||
)
|
||||
}
|
||||
|
||||
pub struct MacroDefinitionParseError {
|
||||
pub token_index: usize,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub fn parse_tex_macro_parameters(
|
||||
parameter_text: &[EngineTokenValue],
|
||||
) -> Result<(Vec<EngineTokenValue>, Vec<MacroParameter>), MacroDefinitionParseError> {
|
||||
let mut prefix = Vec::new();
|
||||
let mut parameters: Vec<MacroParameter> = Vec::new();
|
||||
let mut between = Vec::new();
|
||||
let mut cursor = 0;
|
||||
while cursor < parameter_text.len() {
|
||||
let current = ¶meter_text[cursor];
|
||||
if !is_parameter_value(current) {
|
||||
between.push(current.clone());
|
||||
cursor += 1;
|
||||
continue;
|
||||
}
|
||||
let Some(number_token) = parameter_text.get(cursor + 1) else {
|
||||
return Err(MacroDefinitionParseError {
|
||||
token_index: cursor,
|
||||
message: "parameter marker must be followed by a number from 1 to 9".into(),
|
||||
});
|
||||
};
|
||||
let expected_number = parameters.len() + 1;
|
||||
let valid_number = token_character(number_token)
|
||||
.filter(char::is_ascii_digit)
|
||||
.and_then(|character| character.to_digit(10))
|
||||
.is_some_and(|number| number as usize == expected_number);
|
||||
if expected_number > 9 || !valid_number {
|
||||
return Err(MacroDefinitionParseError {
|
||||
token_index: cursor + 1,
|
||||
message: format!("expected macro parameter #{expected_number}"),
|
||||
});
|
||||
}
|
||||
if let Some(previous) = parameters.last_mut() {
|
||||
previous.delimiter = std::mem::take(&mut between);
|
||||
} else {
|
||||
prefix = std::mem::take(&mut between);
|
||||
}
|
||||
parameters.push(MacroParameter {
|
||||
delimiter: Vec::new(),
|
||||
});
|
||||
cursor += 2;
|
||||
}
|
||||
if let Some(last) = parameters.last_mut() {
|
||||
last.delimiter = between;
|
||||
} else {
|
||||
prefix = between;
|
||||
}
|
||||
Ok((prefix, parameters))
|
||||
}
|
||||
|
||||
pub fn parse_macro_replacement(
|
||||
replacement: &[EngineToken],
|
||||
arity: u8,
|
||||
) -> Result<Vec<Replacement>, MacroDefinitionParseError> {
|
||||
let mut result = Vec::new();
|
||||
let mut literal_tokens = Vec::new();
|
||||
let mut cursor = 0;
|
||||
while cursor < replacement.len() {
|
||||
let current = &replacement[cursor];
|
||||
if !is_parameter_value(¤t.value) {
|
||||
literal_tokens.push(current.clone());
|
||||
cursor += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let Some(next) = replacement.get(cursor + 1) else {
|
||||
return Err(MacroDefinitionParseError {
|
||||
token_index: cursor,
|
||||
message: "parameter marker must be followed by `#` or a parameter number".into(),
|
||||
});
|
||||
};
|
||||
if is_parameter_value(&next.value) {
|
||||
literal_tokens.push(next.clone());
|
||||
cursor += 2;
|
||||
continue;
|
||||
}
|
||||
let parameter_number = token_character(&next.value)
|
||||
.filter(char::is_ascii_digit)
|
||||
.and_then(|character| character.to_digit(10))
|
||||
.and_then(|number| u8::try_from(number).ok())
|
||||
.filter(|number| *number > 0 && *number <= arity)
|
||||
.ok_or_else(|| MacroDefinitionParseError {
|
||||
token_index: cursor + 1,
|
||||
message: format!("parameter number must be between 1 and {arity}"),
|
||||
})?;
|
||||
|
||||
if !literal_tokens.is_empty() {
|
||||
result.push(Replacement::Tokens(std::mem::take(&mut literal_tokens)));
|
||||
}
|
||||
result.push(Replacement::Parameter(usize::from(parameter_number - 1)));
|
||||
cursor += 2;
|
||||
}
|
||||
if !literal_tokens.is_empty() {
|
||||
result.push(Replacement::Tokens(literal_tokens));
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn is_parameter_value(value: &EngineTokenValue) -> bool {
|
||||
matches!(
|
||||
value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::Parameter,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn token_character(value: &EngineTokenValue) -> Option<char> {
|
||||
match value {
|
||||
EngineTokenValue::Character { value, .. } => Some(*value),
|
||||
EngineTokenValue::ControlSequence(_) | EngineTokenValue::Active(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MacroTable {
|
||||
definitions: HashMap<EngineCommandName, MacroDefinition>,
|
||||
groups: Vec<HashMap<EngineCommandName, Option<MacroDefinition>>>,
|
||||
}
|
||||
|
||||
impl MacroTable {
|
||||
pub fn get(&self, name: &EngineCommandName) -> Option<&MacroDefinition> {
|
||||
self.definitions.get(name)
|
||||
}
|
||||
|
||||
pub fn define(&mut self, name: EngineCommandName, definition: MacroDefinition, global: bool) {
|
||||
self.prepare_assignment(&name, global);
|
||||
self.definitions.insert(name, definition);
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, name: EngineCommandName, global: bool) {
|
||||
self.prepare_assignment(&name, global);
|
||||
self.definitions.remove(&name);
|
||||
}
|
||||
|
||||
fn prepare_assignment(&mut self, name: &EngineCommandName, global: bool) {
|
||||
if global {
|
||||
for group in &mut self.groups {
|
||||
group.remove(name);
|
||||
}
|
||||
} else if let Some(group) = self.groups.last_mut() {
|
||||
group
|
||||
.entry(name.clone())
|
||||
.or_insert_with(|| self.definitions.get(name).cloned());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn begin_group(&mut self) {
|
||||
self.groups.push(HashMap::new());
|
||||
}
|
||||
|
||||
pub fn end_group(&mut self) {
|
||||
let Some(group) = self.groups.pop() else {
|
||||
return;
|
||||
};
|
||||
for (name, previous) in group {
|
||||
match previous {
|
||||
Some(definition) => {
|
||||
self.definitions.insert(name, definition);
|
||||
}
|
||||
None => {
|
||||
self.definitions.remove(&name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user