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,470 @@
|
||||
use crate::{CatCode as EngineCatCode, EngineToken, EngineTokenValue, macros::MacroDefinition};
|
||||
|
||||
pub trait MacroTokenStream {
|
||||
type Error;
|
||||
|
||||
fn next_unexpanded(&mut self) -> Result<Option<EngineToken>, Self::Error>;
|
||||
fn back_unexpanded(&mut self, token: EngineToken);
|
||||
fn macro_definition(&self, token: &EngineToken) -> Option<MacroDefinition>;
|
||||
fn in_expanded_definition(&self) -> bool;
|
||||
fn is_outer_macro(&self, token: &EngineToken) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum ErrorStream {
|
||||
Input,
|
||||
Unexpanded,
|
||||
}
|
||||
|
||||
pub enum MacroScanError<E> {
|
||||
Stream(E),
|
||||
Macro {
|
||||
token: EngineToken,
|
||||
message: String,
|
||||
stream: ErrorStream,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn expand_macro<S: MacroTokenStream>(
|
||||
stream: &mut S,
|
||||
macro_token: EngineToken,
|
||||
macro_name: &str,
|
||||
) -> Result<Vec<EngineToken>, MacroScanError<S::Error>> {
|
||||
let Some(definition) = stream.macro_definition(¯o_token) else {
|
||||
return Err(macro_error(
|
||||
¯o_token,
|
||||
"macro definition is no longer active",
|
||||
ErrorStream::Input,
|
||||
));
|
||||
};
|
||||
if definition.is_outer && stream.in_expanded_definition() {
|
||||
return Err(macro_error(
|
||||
¯o_token,
|
||||
"outer macro cannot be expanded in an expanded definition",
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
let arguments = scan_arguments(
|
||||
stream,
|
||||
macro_token,
|
||||
macro_name,
|
||||
&definition,
|
||||
definition.optional_first_default.as_deref(),
|
||||
)?;
|
||||
Ok(definition.instantiate(&arguments))
|
||||
}
|
||||
|
||||
pub fn scan_arguments<S: MacroTokenStream>(
|
||||
stream: &mut S,
|
||||
macro_token: EngineToken,
|
||||
macro_name: &str,
|
||||
definition: &MacroDefinition,
|
||||
optional_default: Option<&[EngineToken]>,
|
||||
) -> Result<Vec<Vec<EngineToken>>, MacroScanError<S::Error>> {
|
||||
for expected in &definition.prefix {
|
||||
let actual = stream
|
||||
.next_unexpanded()
|
||||
.map_err(MacroScanError::Stream)?
|
||||
.ok_or_else(|| {
|
||||
macro_error(
|
||||
¯o_token,
|
||||
"macro prefix does not match its definition",
|
||||
ErrorStream::Unexpanded,
|
||||
)
|
||||
})?;
|
||||
if actual.value != *expected {
|
||||
return Err(macro_error(
|
||||
&actual,
|
||||
"macro prefix does not match its definition",
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut arguments = Vec::with_capacity(definition.parameters.len());
|
||||
for (index, parameter) in definition.parameters.iter().enumerate() {
|
||||
let parameter_number = (index + 1) as u8;
|
||||
let argument = if index == 0 {
|
||||
if let Some(default) = optional_default {
|
||||
read_optional_argument(
|
||||
stream,
|
||||
¯o_token,
|
||||
macro_name,
|
||||
default,
|
||||
definition.is_long,
|
||||
parameter_number,
|
||||
)?
|
||||
} else if parameter.delimiter.is_empty() {
|
||||
read_argument(
|
||||
stream,
|
||||
¯o_token,
|
||||
macro_name,
|
||||
parameter_number,
|
||||
definition.is_long,
|
||||
)?
|
||||
} else {
|
||||
read_delimited_argument(
|
||||
stream,
|
||||
¯o_token,
|
||||
macro_name,
|
||||
¶meter.delimiter,
|
||||
parameter_number,
|
||||
definition.is_long,
|
||||
)?
|
||||
}
|
||||
} else if parameter.delimiter.is_empty() {
|
||||
read_argument(
|
||||
stream,
|
||||
¯o_token,
|
||||
macro_name,
|
||||
parameter_number,
|
||||
definition.is_long,
|
||||
)?
|
||||
} else {
|
||||
read_delimited_argument(
|
||||
stream,
|
||||
¯o_token,
|
||||
macro_name,
|
||||
¶meter.delimiter,
|
||||
parameter_number,
|
||||
definition.is_long,
|
||||
)?
|
||||
};
|
||||
arguments.push(argument);
|
||||
}
|
||||
Ok(arguments)
|
||||
}
|
||||
|
||||
fn read_argument<S: MacroTokenStream>(
|
||||
stream: &mut S,
|
||||
macro_token: &EngineToken,
|
||||
macro_name: &str,
|
||||
parameter_number: u8,
|
||||
is_long: bool,
|
||||
) -> Result<Vec<EngineToken>, MacroScanError<S::Error>> {
|
||||
let first = loop {
|
||||
match stream.next_unexpanded().map_err(MacroScanError::Stream)? {
|
||||
Some(token) if is_space(&token.value) => continue,
|
||||
Some(token) => break token,
|
||||
None => {
|
||||
return Err(macro_error(
|
||||
macro_token,
|
||||
format!("macro is missing parameter #{parameter_number}"),
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
validate_argument_token(stream, &first, macro_name, parameter_number, is_long)?;
|
||||
if !is_begin_group(&first.value) {
|
||||
return Ok(vec![first]);
|
||||
}
|
||||
|
||||
let mut depth = 1usize;
|
||||
let mut argument = Vec::new();
|
||||
while depth > 0 {
|
||||
let Some(next) = stream.next_unexpanded().map_err(MacroScanError::Stream)? else {
|
||||
return Err(macro_error(
|
||||
macro_token,
|
||||
format!("unterminated braced argument for parameter #{parameter_number}"),
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
validate_argument_token(stream, &next, macro_name, parameter_number, is_long)?;
|
||||
match next.value {
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
} => {
|
||||
depth += 1;
|
||||
argument.push(next);
|
||||
}
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} => {
|
||||
depth -= 1;
|
||||
if depth > 0 {
|
||||
argument.push(next);
|
||||
}
|
||||
}
|
||||
_ => argument.push(next),
|
||||
}
|
||||
}
|
||||
Ok(argument)
|
||||
}
|
||||
|
||||
fn read_delimited_argument<S: MacroTokenStream>(
|
||||
stream: &mut S,
|
||||
macro_token: &EngineToken,
|
||||
macro_name: &str,
|
||||
delimiter: &[EngineTokenValue],
|
||||
parameter_number: u8,
|
||||
is_long: bool,
|
||||
) -> Result<Vec<EngineToken>, MacroScanError<S::Error>> {
|
||||
let closing_scope_depth = usize::from(matches!(
|
||||
delimiter.last(),
|
||||
Some(EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
})
|
||||
));
|
||||
let mut group_depth = 0usize;
|
||||
let mut argument = Vec::new();
|
||||
loop {
|
||||
let Some(next) = stream.next_unexpanded().map_err(MacroScanError::Stream)? else {
|
||||
return Err(macro_error(
|
||||
macro_token,
|
||||
format!("unterminated delimited argument for parameter #{parameter_number}"),
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
if stream.is_outer_macro(&next) {
|
||||
return Err(macro_error(
|
||||
&next,
|
||||
format!("outer macro cannot appear in an argument to {macro_name}"),
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
match next.value {
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
} => group_depth += 1,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} if group_depth > 0 => group_depth -= 1,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} => {
|
||||
return Err(macro_error(
|
||||
&next,
|
||||
format!("unexpected closing group in parameter #{parameter_number}"),
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
argument.push(next);
|
||||
|
||||
if group_depth == closing_scope_depth && argument.len() >= delimiter.len() {
|
||||
let delimiter_start = argument.len() - delimiter.len();
|
||||
if argument[delimiter_start..]
|
||||
.iter()
|
||||
.zip(delimiter)
|
||||
.all(|(token, expected)| token.value == *expected)
|
||||
{
|
||||
argument.truncate(delimiter_start);
|
||||
return Ok(trim_outer_group(argument));
|
||||
}
|
||||
}
|
||||
|
||||
let last = argument.last().expect("the current token was appended");
|
||||
if !is_long
|
||||
&& is_par_token(last)
|
||||
&& (group_depth != closing_scope_depth
|
||||
|| !matches_delimiter_prefix(&argument, delimiter))
|
||||
{
|
||||
return Err(macro_error(
|
||||
last,
|
||||
format!(
|
||||
"paragraph ended before parameter #{parameter_number} of {macro_name} was complete"
|
||||
),
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_optional_argument<S: MacroTokenStream>(
|
||||
stream: &mut S,
|
||||
macro_token: &EngineToken,
|
||||
macro_name: &str,
|
||||
default: &[EngineToken],
|
||||
is_long: bool,
|
||||
parameter_number: u8,
|
||||
) -> Result<Vec<EngineToken>, MacroScanError<S::Error>> {
|
||||
let opening = loop {
|
||||
match stream.next_unexpanded().map_err(MacroScanError::Stream)? {
|
||||
Some(token) if is_space(&token.value) => continue,
|
||||
token => break token,
|
||||
}
|
||||
};
|
||||
let Some(opening) = opening else {
|
||||
return Ok(default.to_vec());
|
||||
};
|
||||
if !matches!(
|
||||
opening.value,
|
||||
EngineTokenValue::Character {
|
||||
value: '[',
|
||||
catcode: EngineCatCode::Other
|
||||
}
|
||||
) {
|
||||
stream.back_unexpanded(opening);
|
||||
return Ok(default.to_vec());
|
||||
}
|
||||
|
||||
let mut group_depth = 0usize;
|
||||
let mut argument = Vec::new();
|
||||
loop {
|
||||
let Some(next) = stream.next_unexpanded().map_err(MacroScanError::Stream)? else {
|
||||
return Err(macro_error(
|
||||
macro_token,
|
||||
"unterminated optional macro argument; expected `]`",
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
};
|
||||
if stream.is_outer_macro(&next) {
|
||||
return Err(macro_error(
|
||||
&next,
|
||||
format!("outer macro cannot appear in an argument to {macro_name}"),
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
validate_long_token(&next, macro_name, parameter_number, is_long)?;
|
||||
match next.value {
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
} => {
|
||||
group_depth += 1;
|
||||
argument.push(next);
|
||||
}
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} if group_depth > 0 => {
|
||||
group_depth -= 1;
|
||||
argument.push(next);
|
||||
}
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} => {
|
||||
return Err(macro_error(
|
||||
&next,
|
||||
"unexpected closing group in optional macro argument",
|
||||
ErrorStream::Unexpanded,
|
||||
));
|
||||
}
|
||||
EngineTokenValue::Character {
|
||||
value: ']',
|
||||
catcode: EngineCatCode::Other,
|
||||
} if group_depth == 0 => return Ok(argument),
|
||||
_ => argument.push(next),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_argument_token<S: MacroTokenStream>(
|
||||
stream: &S,
|
||||
argument_token: &EngineToken,
|
||||
macro_name: &str,
|
||||
parameter_number: u8,
|
||||
is_long: bool,
|
||||
) -> Result<(), MacroScanError<S::Error>> {
|
||||
if stream.is_outer_macro(argument_token) {
|
||||
return Err(macro_error(
|
||||
argument_token,
|
||||
format!("outer macro cannot appear in an argument to {macro_name}"),
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
validate_long_token(argument_token, macro_name, parameter_number, is_long)
|
||||
}
|
||||
|
||||
fn validate_long_token<E>(
|
||||
argument_token: &EngineToken,
|
||||
macro_name: &str,
|
||||
parameter_number: u8,
|
||||
is_long: bool,
|
||||
) -> Result<(), MacroScanError<E>> {
|
||||
if !is_long && is_par_token(argument_token) {
|
||||
return Err(macro_error(
|
||||
argument_token,
|
||||
format!(
|
||||
"paragraph ended before parameter #{parameter_number} of {macro_name} was complete"
|
||||
),
|
||||
ErrorStream::Input,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn matches_delimiter_prefix(argument: &[EngineToken], delimiter: &[EngineTokenValue]) -> bool {
|
||||
let max_prefix = argument.len().min(delimiter.len());
|
||||
(1..=max_prefix).rev().any(|prefix_len| {
|
||||
argument[argument.len() - prefix_len..]
|
||||
.iter()
|
||||
.zip(&delimiter[..prefix_len])
|
||||
.all(|(token, expected)| token.value == *expected)
|
||||
})
|
||||
}
|
||||
|
||||
fn trim_outer_group(mut tokens: Vec<EngineToken>) -> Vec<EngineToken> {
|
||||
if tokens.len() < 2 || !is_begin_group(&tokens[0].value) {
|
||||
return tokens;
|
||||
}
|
||||
let mut depth = 0usize;
|
||||
for (index, token) in tokens.iter().enumerate() {
|
||||
match token.value {
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
} => depth += 1,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::EndGroup,
|
||||
..
|
||||
} => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
if index + 1 == tokens.len() {
|
||||
tokens.remove(0);
|
||||
tokens.pop();
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
tokens
|
||||
}
|
||||
|
||||
fn is_begin_group(value: &EngineTokenValue) -> bool {
|
||||
matches!(
|
||||
value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::BeginGroup,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_space(value: &EngineTokenValue) -> bool {
|
||||
matches!(
|
||||
value,
|
||||
EngineTokenValue::Character {
|
||||
catcode: EngineCatCode::Space,
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn is_par_token(token: &EngineToken) -> bool {
|
||||
matches!(&token.value, EngineTokenValue::ControlSequence(name) if name == "par")
|
||||
}
|
||||
|
||||
fn macro_error<E>(
|
||||
token: &EngineToken,
|
||||
message: impl Into<String>,
|
||||
stream: ErrorStream,
|
||||
) -> MacroScanError<E> {
|
||||
MacroScanError::Macro {
|
||||
token: token.clone(),
|
||||
message: message.into(),
|
||||
stream,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user