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.
282 lines
7.1 KiB
Rust
282 lines
7.1 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use crate::{
|
|
EngineToken,
|
|
mouth::{CatCodeTable, InvalidCharacter, Mouth, Token},
|
|
};
|
|
|
|
/// Token access required to scan a TeX input file name.
|
|
pub trait FileNameTokenStream {
|
|
type Token;
|
|
type Error;
|
|
|
|
fn next_token(&mut self) -> Result<Option<Self::Token>, Self::Error>;
|
|
fn back_token(&mut self, token: Self::Token);
|
|
fn is_space(&self, token: &Self::Token) -> bool;
|
|
fn character(&self, token: &Self::Token) -> Option<char>;
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct InputFileLocation {
|
|
pub path: String,
|
|
pub extension: Option<String>,
|
|
pub area: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum InputPathError {
|
|
UnsupportedArea(String),
|
|
MissingWorkingDirectory,
|
|
}
|
|
|
|
impl std::fmt::Display for InputPathError {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Self::UnsupportedArea(area) => {
|
|
write!(formatter, "input file area `{area}` is not supported")
|
|
}
|
|
Self::MissingWorkingDirectory => formatter
|
|
.write_str("cannot resolve a relative input path without a working directory"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl InputFileLocation {
|
|
pub fn resolve_path(
|
|
&self,
|
|
working_directory: Option<&Path>,
|
|
default_extension: &str,
|
|
) -> Result<PathBuf, InputPathError> {
|
|
if let Some(area) = &self.area {
|
|
return Err(InputPathError::UnsupportedArea(area.clone()));
|
|
}
|
|
|
|
let mut path = working_directory.map(Path::to_path_buf).unwrap_or_default();
|
|
path.push(&self.path);
|
|
path.set_extension(self.extension.as_deref().unwrap_or(default_extension));
|
|
|
|
if !path.is_absolute() {
|
|
return Err(InputPathError::MissingWorkingDirectory);
|
|
}
|
|
Ok(path)
|
|
}
|
|
}
|
|
|
|
/// Scan and split an expanded TeX file name into its area, name, and extension.
|
|
pub fn scan_file_location<S: FileNameTokenStream>(
|
|
stream: &mut S,
|
|
) -> Result<InputFileLocation, S::Error> {
|
|
let mut raw = String::new();
|
|
let mut area_end = None;
|
|
let mut extension_start = None;
|
|
|
|
loop {
|
|
let Some(token) = stream.next_token()? else {
|
|
break;
|
|
};
|
|
if stream.is_space(&token) {
|
|
if raw.is_empty() {
|
|
continue;
|
|
}
|
|
stream.back_token(token);
|
|
break;
|
|
}
|
|
let Some(character) = stream.character(&token) else {
|
|
stream.back_token(token);
|
|
break;
|
|
};
|
|
if character == ' ' {
|
|
break;
|
|
}
|
|
|
|
if matches!(character, '>' | ':') {
|
|
area_end = Some(raw.len() + character.len_utf8());
|
|
extension_start = None;
|
|
} else if character == '.' && extension_start.is_none() {
|
|
extension_start = Some(raw.len());
|
|
}
|
|
raw.push(character);
|
|
}
|
|
|
|
let name_start = area_end.unwrap_or(0);
|
|
let name_end = extension_start.unwrap_or(raw.len());
|
|
Ok(InputFileLocation {
|
|
path: raw[name_start..name_end].to_string(),
|
|
extension: extension_start.map(|index| raw[index + 1..].to_string()),
|
|
area: area_end.map(|end| raw[..end].to_string()),
|
|
})
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
pub struct InputId(u64);
|
|
|
|
pub struct InputFrame {
|
|
id: InputId,
|
|
file_name: PathBuf,
|
|
source: String,
|
|
line_offset: usize,
|
|
priority: bool,
|
|
ended: bool,
|
|
mouth: Option<Mouth>,
|
|
expansion_tokens: Option<Vec<EngineToken>>,
|
|
}
|
|
|
|
impl InputFrame {
|
|
fn new(
|
|
id: InputId,
|
|
file_name: impl Into<PathBuf>,
|
|
source: &str,
|
|
line_offset: usize,
|
|
priority: bool,
|
|
) -> Self {
|
|
Self {
|
|
id,
|
|
file_name: file_name.into(),
|
|
source: source.to_string(),
|
|
line_offset,
|
|
priority,
|
|
ended: false,
|
|
mouth: Some(Mouth::new(source)),
|
|
expansion_tokens: None,
|
|
}
|
|
}
|
|
|
|
fn new_expansion(id: InputId, tokens: Vec<EngineToken>) -> Self {
|
|
Self {
|
|
id,
|
|
file_name: PathBuf::from("<macro expansion>"),
|
|
source: String::new(),
|
|
line_offset: 0,
|
|
priority: true,
|
|
ended: false,
|
|
mouth: None,
|
|
expansion_tokens: Some(tokens.into_iter().rev().collect()),
|
|
}
|
|
}
|
|
|
|
pub fn id(&self) -> InputId {
|
|
self.id
|
|
}
|
|
|
|
pub fn file_name(&self) -> &Path {
|
|
&self.file_name
|
|
}
|
|
|
|
pub fn source(&self) -> &str {
|
|
&self.source
|
|
}
|
|
|
|
pub fn line_offset(&self) -> usize {
|
|
self.line_offset
|
|
}
|
|
|
|
pub fn is_priority(&self) -> bool {
|
|
self.priority
|
|
}
|
|
|
|
pub fn is_expansion(&self) -> bool {
|
|
self.expansion_tokens.is_some()
|
|
}
|
|
|
|
pub fn next_expansion_token(&mut self) -> Option<EngineToken> {
|
|
self.expansion_tokens.as_mut()?.pop()
|
|
}
|
|
|
|
pub fn has_expansion_tokens(&self) -> bool {
|
|
self.expansion_tokens
|
|
.as_ref()
|
|
.is_some_and(|tokens| !tokens.is_empty())
|
|
}
|
|
|
|
pub fn is_source(&self) -> bool {
|
|
self.mouth.is_some()
|
|
}
|
|
|
|
pub fn is_ended(&self) -> bool {
|
|
self.ended
|
|
}
|
|
|
|
pub fn end(&mut self) {
|
|
self.ended = true;
|
|
}
|
|
|
|
pub fn next_token(
|
|
&mut self,
|
|
catcodes: &CatCodeTable,
|
|
) -> Result<Option<Token>, InvalidCharacter> {
|
|
self.mouth
|
|
.as_mut()
|
|
.expect("token expansion frames do not own a mouth")
|
|
.next_token(catcodes)
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct InputStack {
|
|
frames: Vec<InputFrame>,
|
|
next_id: u64,
|
|
}
|
|
|
|
impl InputStack {
|
|
pub fn clear(&mut self) {
|
|
self.frames.clear();
|
|
}
|
|
|
|
pub fn push(
|
|
&mut self,
|
|
file_name: impl Into<PathBuf>,
|
|
source: &str,
|
|
line_offset: usize,
|
|
priority: bool,
|
|
) -> InputId {
|
|
let id = InputId(self.next_id);
|
|
self.next_id = self.next_id.wrapping_add(1);
|
|
self.frames.push(InputFrame::new(
|
|
id,
|
|
file_name,
|
|
source,
|
|
line_offset,
|
|
priority,
|
|
));
|
|
id
|
|
}
|
|
|
|
/// Push an already-tokenized macro replacement before the current input.
|
|
/// Tokens are supplied in the order in which TeX should read them.
|
|
pub fn push_expansion_tokens(&mut self, tokens: Vec<EngineToken>) -> InputId {
|
|
let id = InputId(self.next_id);
|
|
self.next_id = self.next_id.wrapping_add(1);
|
|
if !tokens.is_empty() {
|
|
self.frames.push(InputFrame::new_expansion(id, tokens));
|
|
}
|
|
id
|
|
}
|
|
|
|
pub fn pop(&mut self) -> Option<InputFrame> {
|
|
self.frames.pop()
|
|
}
|
|
|
|
pub fn push_frame(&mut self, frame: InputFrame) {
|
|
self.frames.push(frame);
|
|
}
|
|
|
|
pub fn last(&self) -> Option<&InputFrame> {
|
|
self.frames.last()
|
|
}
|
|
|
|
pub fn last_mut(&mut self) -> Option<&mut InputFrame> {
|
|
self.frames.last_mut()
|
|
}
|
|
|
|
pub fn last_source_mut(&mut self) -> Option<&mut InputFrame> {
|
|
self.frames.iter_mut().rev().find(|frame| frame.is_source())
|
|
}
|
|
|
|
pub fn external_depth(&self) -> usize {
|
|
self.frames
|
|
.iter()
|
|
.filter(|frame| !frame.is_priority())
|
|
.count()
|
|
}
|
|
}
|