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
Vendored
+1246
File diff suppressed because it is too large.
Load diff
Vendored
+539
@@ -0,0 +1,539 @@
|
||||
//! TeX tokens and category codes.
|
||||
|
||||
pub mod lexer;
|
||||
pub mod trace;
|
||||
use crate::types::CatCode;
|
||||
use std::{fmt::Display, num};
|
||||
use texcraft_stdext::collections::interner;
|
||||
|
||||
/// String type used to represent control sequence names in Texlang.
|
||||
///
|
||||
/// The implementation of this type is opaque so that it can be performance optimized
|
||||
/// without worrying about downstream consumers.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct CsName(num::NonZeroU32);
|
||||
|
||||
impl CsName {
|
||||
#[inline]
|
||||
pub fn to_usize(&self) -> usize {
|
||||
self.0.get() as usize
|
||||
}
|
||||
|
||||
pub fn try_from_usize(u: usize) -> Option<CsName> {
|
||||
let u = match u32::try_from(u) {
|
||||
Ok(u) => u,
|
||||
Err(_) => return None,
|
||||
};
|
||||
num::NonZeroU32::new(u).map(CsName)
|
||||
}
|
||||
}
|
||||
|
||||
/// String interner for control sequence names.
|
||||
pub type CsNameInterner = interner::Interner<CsName>;
|
||||
|
||||
impl interner::Key for CsName {
|
||||
fn try_from_usize(index: usize) -> Option<Self> {
|
||||
num::NonZeroU32::try_from_usize(index).map(CsName)
|
||||
}
|
||||
|
||||
fn into_usize(self) -> usize {
|
||||
self.0.into_usize()
|
||||
}
|
||||
}
|
||||
|
||||
/// The value of a token.
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum Value {
|
||||
BeginGroup(char),
|
||||
EndGroup(char),
|
||||
MathShift(char),
|
||||
AlignmentTab(char),
|
||||
Parameter(char),
|
||||
Superscript(char),
|
||||
Subscript(char),
|
||||
Space(char),
|
||||
Letter(char),
|
||||
Other(char),
|
||||
CommandRef(CommandRef),
|
||||
}
|
||||
|
||||
/// The value of a token that references a command
|
||||
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum CommandRef {
|
||||
ControlSequence(CsName),
|
||||
ActiveCharacter(char),
|
||||
}
|
||||
|
||||
impl CommandRef {
|
||||
pub fn to_string(&self, cs_name_interner: &CsNameInterner) -> String {
|
||||
match self {
|
||||
CommandRef::ControlSequence(cs_name) => {
|
||||
format!("\\{}", cs_name_interner.resolve(*cs_name).unwrap())
|
||||
}
|
||||
CommandRef::ActiveCharacter(c) => format!("{c}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn new(c: char, cat_code: CatCode) -> Value {
|
||||
match cat_code {
|
||||
CatCode::BeginGroup => Value::BeginGroup(c),
|
||||
CatCode::EndGroup => Value::EndGroup(c),
|
||||
CatCode::MathShift => Value::MathShift(c),
|
||||
CatCode::AlignmentTab => Value::AlignmentTab(c),
|
||||
CatCode::Parameter => Value::Parameter(c),
|
||||
CatCode::Superscript => Value::Superscript(c),
|
||||
CatCode::Subscript => Value::Subscript(c),
|
||||
CatCode::Space => Value::Space(c),
|
||||
CatCode::Letter => Value::Letter(c),
|
||||
CatCode::Other => Value::Other(c),
|
||||
CatCode::Active => Value::CommandRef(CommandRef::ActiveCharacter(c)),
|
||||
_ => panic!("raw cat code not allowed"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn char(&self) -> Option<char> {
|
||||
Some(self.char_and_cat_code()?.0)
|
||||
}
|
||||
|
||||
pub fn cat_code(&self) -> Option<CatCode> {
|
||||
Some(self.char_and_cat_code()?.1)
|
||||
}
|
||||
|
||||
pub fn char_and_cat_code(&self) -> Option<(char, CatCode)> {
|
||||
let (c, code) = match self {
|
||||
Value::BeginGroup(c) => (c, CatCode::BeginGroup),
|
||||
Value::EndGroup(c) => (c, CatCode::EndGroup),
|
||||
Value::MathShift(c) => (c, CatCode::MathShift),
|
||||
Value::AlignmentTab(c) => (c, CatCode::AlignmentTab),
|
||||
Value::Parameter(c) => (c, CatCode::Parameter),
|
||||
Value::Superscript(c) => (c, CatCode::Superscript),
|
||||
Value::Subscript(c) => (c, CatCode::Subscript),
|
||||
Value::Space(c) => (c, CatCode::Space),
|
||||
Value::Letter(c) => (c, CatCode::Letter),
|
||||
Value::Other(c) => (c, CatCode::Other),
|
||||
Value::CommandRef(command_ref) => match command_ref {
|
||||
CommandRef::ControlSequence(_) => return None,
|
||||
CommandRef::ActiveCharacter(c) => (c, CatCode::Active),
|
||||
},
|
||||
};
|
||||
Some((*c, code))
|
||||
}
|
||||
}
|
||||
|
||||
/// A TeX token.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Token {
|
||||
value: Value,
|
||||
trace_key: trace::Key,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Token {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.value {
|
||||
Value::CommandRef(_) => {
|
||||
write![f, "todo"] // TODO
|
||||
}
|
||||
_ => {
|
||||
write![f, "{}", self.char().unwrap()]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! token_constructor {
|
||||
($name: ident, $value: expr) => {
|
||||
pub fn $name(c: char, trace_key: trace::Key) -> Token {
|
||||
Token {
|
||||
value: $value(c),
|
||||
trace_key,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl Token {
|
||||
token_constructor!(new_begin_group, Value::BeginGroup);
|
||||
token_constructor!(new_end_group, Value::EndGroup);
|
||||
token_constructor!(new_math_shift, Value::MathShift);
|
||||
token_constructor!(new_alignment_tab, Value::AlignmentTab);
|
||||
token_constructor!(new_parameter, Value::Parameter);
|
||||
token_constructor!(new_superscript, Value::Superscript);
|
||||
token_constructor!(new_subscript, Value::Subscript);
|
||||
token_constructor!(new_space, Value::Space);
|
||||
token_constructor!(new_letter, Value::Letter);
|
||||
token_constructor!(new_other, Value::Other);
|
||||
|
||||
pub fn new_command_ref(command_ref: CommandRef, trace_key: trace::Key) -> Token {
|
||||
Token {
|
||||
value: Value::CommandRef(command_ref),
|
||||
trace_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_active_character(c: char, trace_key: trace::Key) -> Token {
|
||||
Token {
|
||||
value: Value::CommandRef(CommandRef::ActiveCharacter(c)),
|
||||
trace_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_control_sequence(name: CsName, trace_key: trace::Key) -> Token {
|
||||
Token {
|
||||
value: Value::CommandRef(CommandRef::ControlSequence(name)),
|
||||
trace_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_value(value: Value, trace_key: trace::Key) -> Token {
|
||||
Token { value, trace_key }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn value(&self) -> Value {
|
||||
self.value
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn trace_key(&self) -> trace::Key {
|
||||
self.trace_key
|
||||
}
|
||||
|
||||
pub fn char(&self) -> Option<char> {
|
||||
self.value.char()
|
||||
}
|
||||
|
||||
pub fn cat_code(&self) -> Option<CatCode> {
|
||||
self.value.cat_code()
|
||||
}
|
||||
pub fn char_and_cat_code(&self) -> Option<(char, CatCode)> {
|
||||
self.value.char_and_cat_code()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
enum PendingWhitespace {
|
||||
#[default]
|
||||
NotStarted,
|
||||
None,
|
||||
Space,
|
||||
Newlines(usize),
|
||||
}
|
||||
|
||||
impl PendingWhitespace {
|
||||
fn reset(&mut self) {
|
||||
*self = PendingWhitespace::None;
|
||||
}
|
||||
|
||||
fn add_space(&mut self) {
|
||||
*self = match self {
|
||||
PendingWhitespace::NotStarted => PendingWhitespace::NotStarted,
|
||||
PendingWhitespace::None => PendingWhitespace::Space,
|
||||
PendingWhitespace::Space => PendingWhitespace::Space,
|
||||
PendingWhitespace::Newlines(n) => PendingWhitespace::Newlines(*n),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_newline(&mut self) {
|
||||
*self = match self {
|
||||
PendingWhitespace::NotStarted => PendingWhitespace::NotStarted,
|
||||
PendingWhitespace::None => PendingWhitespace::Newlines(1),
|
||||
PendingWhitespace::Space => PendingWhitespace::Newlines(1),
|
||||
PendingWhitespace::Newlines(n) => PendingWhitespace::Newlines(*n + 1),
|
||||
}
|
||||
}
|
||||
|
||||
fn new_paragraph(&mut self) {
|
||||
*self = match self {
|
||||
PendingWhitespace::NotStarted => PendingWhitespace::NotStarted,
|
||||
PendingWhitespace::None | PendingWhitespace::Space | PendingWhitespace::Newlines(1) => {
|
||||
PendingWhitespace::Newlines(2)
|
||||
}
|
||||
PendingWhitespace::Newlines(n) => PendingWhitespace::Newlines(*n),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PendingWhitespace {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
PendingWhitespace::NotStarted | PendingWhitespace::None => Ok(()),
|
||||
PendingWhitespace::Space => {
|
||||
write!(f, " ")
|
||||
}
|
||||
PendingWhitespace::Newlines(n) => {
|
||||
for _ in 0..*n {
|
||||
writeln!(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Data structure for writing tokens
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Writer {
|
||||
pending_whitespace: PendingWhitespace,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
/// Write a token.
|
||||
pub fn write(
|
||||
&mut self,
|
||||
io_writer: &mut dyn std::io::Write,
|
||||
interner: &CsNameInterner,
|
||||
value: Value,
|
||||
) -> Result<(), std::io::Error> {
|
||||
match &value {
|
||||
Value::CommandRef(CommandRef::ControlSequence(s)) => {
|
||||
write!(
|
||||
io_writer,
|
||||
"{}\\{}",
|
||||
self.pending_whitespace,
|
||||
interner.resolve(*s).unwrap()
|
||||
)?;
|
||||
self.pending_whitespace.reset();
|
||||
}
|
||||
Value::Space(_) => self.pending_whitespace.add_space(),
|
||||
_ => {
|
||||
write!(
|
||||
io_writer,
|
||||
"{}{}",
|
||||
self.pending_whitespace,
|
||||
value.char().unwrap()
|
||||
)?;
|
||||
self.pending_whitespace.reset();
|
||||
}
|
||||
}
|
||||
io_writer.flush()
|
||||
}
|
||||
|
||||
pub fn add_newline(&mut self) {
|
||||
self.pending_whitespace.add_newline();
|
||||
}
|
||||
|
||||
pub fn start_paragraph(&mut self) {
|
||||
self.pending_whitespace.new_paragraph();
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a collection of tokens to a string.
|
||||
pub fn write_tokens<'a, T>(tokens: T, interner: &CsNameInterner) -> String
|
||||
where
|
||||
T: IntoIterator<Item = &'a Token>,
|
||||
{
|
||||
let mut buffer: Vec<u8> = Default::default();
|
||||
let mut writer: Writer = Default::default();
|
||||
for token in tokens.into_iter() {
|
||||
writer.write(&mut buffer, interner, token.value()).unwrap();
|
||||
}
|
||||
std::str::from_utf8(&buffer).unwrap().into()
|
||||
}
|
||||
|
||||
/// Write a collection of token values to a string.
|
||||
pub fn write_token_values<'a, T>(values: T, interner: &CsNameInterner) -> String
|
||||
where
|
||||
T: IntoIterator<Item = &'a Value>,
|
||||
{
|
||||
let mut buffer: Vec<u8> = Default::default();
|
||||
let mut writer: Writer = Default::default();
|
||||
for value in values.into_iter() {
|
||||
writer.write(&mut buffer, interner, *value).unwrap();
|
||||
}
|
||||
std::str::from_utf8(&buffer).unwrap().into()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
enum Instruction {
|
||||
ControlSequence(&'static str),
|
||||
Character(char, CatCode),
|
||||
Newline,
|
||||
NewParagraph,
|
||||
}
|
||||
|
||||
fn writer_test(input: Vec<Instruction>, want: &str) {
|
||||
let mut buffer: Vec<u8> = Default::default();
|
||||
let mut writer: Writer = Default::default();
|
||||
let mut interner = CsNameInterner::default();
|
||||
for pre_interned_token in input {
|
||||
match pre_interned_token {
|
||||
Instruction::ControlSequence(name) => {
|
||||
let cs_name = interner.get_or_intern(name);
|
||||
let token = Token::new_control_sequence(cs_name, trace::Key::dummy());
|
||||
writer.write(&mut buffer, &interner, token.value()).unwrap();
|
||||
}
|
||||
Instruction::Character(c, code) => {
|
||||
let token = Token::new_from_value(Value::new(c, code), trace::Key::dummy());
|
||||
writer.write(&mut buffer, &interner, token.value()).unwrap();
|
||||
}
|
||||
Instruction::Newline => {
|
||||
writer.add_newline();
|
||||
}
|
||||
Instruction::NewParagraph => {
|
||||
writer.start_paragraph();
|
||||
}
|
||||
};
|
||||
}
|
||||
let got: String = std::str::from_utf8(&buffer).unwrap().into();
|
||||
let want = want.to_string();
|
||||
|
||||
if got != want {
|
||||
println!("Output is different:");
|
||||
println!("------[got]-------");
|
||||
println!("{}", got);
|
||||
println!("------[want]------");
|
||||
println!("{}", want);
|
||||
println!("-----------------");
|
||||
panic!("write_tokens test failed");
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! write_tokens_tests {
|
||||
($( ($name: ident, $input: expr, $want: expr), )+) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
writer_test($input, $want);
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
write_tokens_tests!(
|
||||
(blank, vec!(), ""),
|
||||
(
|
||||
trim_whitespace_from_start,
|
||||
vec![
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
],
|
||||
"H"
|
||||
),
|
||||
(
|
||||
trim_whitespace_from_end,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
],
|
||||
"H"
|
||||
),
|
||||
(
|
||||
trim_whitespace_from_middle_1,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H W"
|
||||
),
|
||||
(
|
||||
trim_whitespace_from_middle_2,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H W"
|
||||
),
|
||||
(
|
||||
trim_whitespace_from_middle_3,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('\n', CatCode::Space),
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H W"
|
||||
),
|
||||
(
|
||||
control_sequence,
|
||||
vec![Instruction::ControlSequence("HelloWorld"),],
|
||||
"\\HelloWorld"
|
||||
),
|
||||
(
|
||||
newline_1,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Newline,
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H\nW"
|
||||
),
|
||||
(
|
||||
newline_2,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Newline,
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Newline,
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H\n\nW"
|
||||
),
|
||||
(
|
||||
newline_3,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::Newline,
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Newline,
|
||||
Instruction::Character(' ', CatCode::Space),
|
||||
Instruction::Newline,
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H\n\n\nW"
|
||||
),
|
||||
(
|
||||
par_1,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::NewParagraph,
|
||||
Instruction::NewParagraph,
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H\n\nW"
|
||||
),
|
||||
(
|
||||
par_2,
|
||||
vec![
|
||||
Instruction::Character('H', CatCode::Letter),
|
||||
Instruction::NewParagraph,
|
||||
Instruction::NewParagraph,
|
||||
Instruction::NewParagraph,
|
||||
Instruction::Character('W', CatCode::Letter),
|
||||
],
|
||||
"H\n\nW"
|
||||
),
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn token_size() {
|
||||
assert_eq!(std::mem::size_of::<CommandRef>(), 8);
|
||||
assert_eq!(std::mem::size_of::<Value>(), 8);
|
||||
assert_eq!(std::mem::size_of::<Token>(), 12);
|
||||
assert_eq!(std::mem::size_of::<Result<Token, ()>>(), 12);
|
||||
assert_eq!(std::mem::size_of::<Result<Option<Token>, ()>>(), 12);
|
||||
assert_eq!(std::mem::size_of::<crate::prelude::Result<Token>>(), 12);
|
||||
assert_eq!(
|
||||
std::mem::size_of::<crate::prelude::Result<Option<Token>>>(),
|
||||
12
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+508
@@ -0,0 +1,508 @@
|
||||
//! Token tracing system
|
||||
//!
|
||||
//! This module implements a [Tracer] that determines the origins of tokens.
|
||||
//! When building helpful error messages we need to know the token came from -
|
||||
//! e.g., the file and line number.
|
||||
//! The tracing functionality here enables obtaining this information in the form of a [SourceCodeTrace].
|
||||
//!
|
||||
//! Rather than the custom system here,
|
||||
//! a simpler solution would be to include this information on the token itself.
|
||||
//! The token could include a line position number and a reference
|
||||
//! counting pointer to some data structure containing information about the line.
|
||||
//! The problem with this solution is that it makes the [Token] type very large, and
|
||||
//! this causes unacceptably poor performance in Texlang's tight inner loops.
|
||||
//! With the tracer here, each token only needs to hold onto a 32-bit [Key] which is enough
|
||||
//! to perform a full trace.
|
||||
//!
|
||||
//! # How the tracer works
|
||||
//!
|
||||
//! When adding source code to the input, the tracer is informed using the
|
||||
//! [register_source_code](Tracer::register_source_code) method.
|
||||
//! The tracer allocates a contiguous range of [Keys](Key) that is large enough
|
||||
//! to give each UTF-8 character in the input a unique key.
|
||||
//! These keys are returned using the opaque [KeyRange] type, which enables the caller to retrieve
|
||||
//! these keys.
|
||||
//! It is assumed that the caller will assign keys in order to each UTF-8 character in the source code.
|
||||
//!
|
||||
//! In addition to returning the range, the tracer associates the key range with the source code in an
|
||||
//! internal data structure.
|
||||
//!
|
||||
//! When tracing a token (using [trace](Tracer::trace)), the token's key is used to identify
|
||||
//! which key range the key came from.
|
||||
//! This key range is then used to identify the source code associated to the token.
|
||||
//! The difference between the token's key and the first key for the source code is the UTF-8 offset
|
||||
//! into the source code.
|
||||
//! Thus we can uniquely identify the UTF-8 character the token is a associated to.
|
||||
use crate::token::{CsNameInterner, Token, Value};
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops::Bound::Included;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::CommandRef;
|
||||
|
||||
/// Key attached to tokens to enable tracing them.
|
||||
///
|
||||
/// This type is 32 bits.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Key(u32);
|
||||
|
||||
impl Key {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn dummy() -> Key {
|
||||
Key(u32::MAX)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn as_u32(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Range of free keys that may be assigned to tokens.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct KeyRange {
|
||||
next: u32,
|
||||
limit: u32,
|
||||
}
|
||||
|
||||
impl KeyRange {
|
||||
/// Get the next trace [Key].
|
||||
///
|
||||
/// Panics if all of the keys in this range have been used.
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn next(&mut self) -> Key {
|
||||
if self.next >= self.limit {
|
||||
panic!["requested more trace keys than are in the range"]
|
||||
}
|
||||
let n = self.next;
|
||||
self.next += 1;
|
||||
Key(n)
|
||||
}
|
||||
|
||||
/// Peek at the next trace [Key].
|
||||
///
|
||||
/// Panics if all of the keys in this range have been used.
|
||||
pub fn peek(&mut self) -> Key {
|
||||
if self.next >= self.limit {
|
||||
panic!["requested more trace keys than are in the range"]
|
||||
}
|
||||
Key(self.next)
|
||||
}
|
||||
|
||||
/// Advances the range forward by the provided offset.
|
||||
///
|
||||
/// Panics if the provided offset cannot be cast to u32.
|
||||
pub fn advance_by(&mut self, u: usize) {
|
||||
self.next = self.next.checked_add(u.try_into().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl KeyRange {
|
||||
pub fn empty() -> KeyRange {
|
||||
KeyRange { next: 0, limit: 0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn for_testing() -> KeyRange {
|
||||
KeyRange {
|
||||
next: 0,
|
||||
limit: u32::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A token trace
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct SourceCodeTrace {
|
||||
/// Origin of the source code this token came from.
|
||||
pub origin: Origin,
|
||||
/// Content of the line this token came from.
|
||||
pub line_content: String,
|
||||
/// Number of the line within the file, starting at 1.
|
||||
pub line_number: usize,
|
||||
/// Index within the line that the token starts.
|
||||
pub index: usize,
|
||||
/// Value of the token.
|
||||
pub value: String,
|
||||
/// If this is for a token, the value of the token.
|
||||
/// Otherwise this is an end of input snippet.
|
||||
pub token: Option<Token>,
|
||||
}
|
||||
|
||||
/// Data structure that records information for token tracing
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Tracer {
|
||||
checkpoints: BTreeMap<u32, Checkpoint>,
|
||||
next_key: u32,
|
||||
line_number_offsets: BTreeMap<u32, usize>,
|
||||
|
||||
// A key use to get the last file that was inputted manually; i.e., not via an \input
|
||||
// or other command in a TeX file
|
||||
last_external_input: Option<u32>,
|
||||
}
|
||||
|
||||
/// Enum describing the possible origins of source code
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Origin {
|
||||
File(PathBuf),
|
||||
Terminal,
|
||||
}
|
||||
|
||||
impl Tracer {
|
||||
/// Registers source code with the tracer.
|
||||
///
|
||||
/// The returned [KeyRange] should be used to assign [Keys](Key) to the tokens that
|
||||
/// are lexed from the referenced source code.
|
||||
/// The tracer assumes that the first key is assigned to token corresponding to the
|
||||
/// first UTF-8 character in their source code,
|
||||
/// the second key to the second UTF-8 character, and so on.
|
||||
pub fn register_source_code(
|
||||
&mut self,
|
||||
token: Option<Token>,
|
||||
origin: Origin,
|
||||
source_code: &str,
|
||||
) -> KeyRange {
|
||||
self.register_source_code_with_line_offset(token, origin, source_code, 0)
|
||||
}
|
||||
|
||||
/// Registers source code whose first line follows `line_number_offset` lines in its file.
|
||||
pub fn register_source_code_with_line_offset(
|
||||
&mut self,
|
||||
token: Option<Token>,
|
||||
origin: Origin,
|
||||
source_code: &str,
|
||||
line_number_offset: usize,
|
||||
) -> KeyRange {
|
||||
let first_key = self.next_key;
|
||||
let len = match u32::try_from(source_code.len()) {
|
||||
Err(_) => {
|
||||
panic!(
|
||||
"source code too big ({} bytes); max is 2^32={} bytes",
|
||||
source_code.len(),
|
||||
u32::MAX
|
||||
)
|
||||
}
|
||||
// For empty files, we still assign one key because this allows us to trace end of input errors.
|
||||
Ok(0) => 1_u32,
|
||||
// We add 1 to handle the case when the file does not end in a newline character. In this case
|
||||
// the extra key will be used to refer to a character added using the \endlinechar mechanism.
|
||||
Ok(limit) => limit + 1,
|
||||
};
|
||||
let range = KeyRange {
|
||||
next: self.next_key,
|
||||
limit: self.next_key + len,
|
||||
};
|
||||
self.checkpoints.insert(
|
||||
range.next,
|
||||
Checkpoint::SourceCode {
|
||||
origin,
|
||||
content: source_code.to_string(),
|
||||
},
|
||||
);
|
||||
self.line_number_offsets
|
||||
.insert(first_key, line_number_offset);
|
||||
if token.is_none() {
|
||||
self.last_external_input = Some(self.next_key);
|
||||
}
|
||||
self.next_key = range.limit;
|
||||
range
|
||||
}
|
||||
|
||||
/// Return a trace for the provided token.
|
||||
pub fn trace(&self, token: Token, cs_name_interner: &CsNameInterner) -> SourceCodeTrace {
|
||||
let value = match token.value() {
|
||||
Value::CommandRef(CommandRef::ControlSequence(cs_name)) => {
|
||||
format!["\\{}", cs_name_interner.resolve(cs_name).unwrap()]
|
||||
}
|
||||
// TODO: maybe have a cs or char method?
|
||||
_ => token.char().unwrap().to_string(),
|
||||
};
|
||||
|
||||
let (&first_key, checkpoint) = self
|
||||
.checkpoints
|
||||
.range((Included(&0), Included(&token.trace_key.0)))
|
||||
.next_back()
|
||||
.unwrap();
|
||||
|
||||
match checkpoint {
|
||||
Checkpoint::SourceCode { origin, content } => {
|
||||
let char_offset = (token.trace_key().0 - first_key) as usize;
|
||||
let mut line_number = 1;
|
||||
let mut byte_line_start = 0;
|
||||
let mut char_line_start = 0;
|
||||
for (char_index, (byte_index, c)) in content.char_indices().enumerate() {
|
||||
if char_index == char_offset {
|
||||
break;
|
||||
}
|
||||
if c == '\n' {
|
||||
byte_line_start = byte_index + 1;
|
||||
char_line_start = char_index + 1;
|
||||
line_number += 1;
|
||||
}
|
||||
}
|
||||
let position = char_offset - char_line_start;
|
||||
let tail = &content[byte_line_start..];
|
||||
let line_content = match tail.split_once('\n') {
|
||||
None => tail.to_string(),
|
||||
Some((a, _)) => a.to_string(),
|
||||
};
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content,
|
||||
line_number: line_number
|
||||
+ self.line_number_offsets.get(&first_key).copied().unwrap_or(0),
|
||||
index: position,
|
||||
value,
|
||||
token: Some(token),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trace_end_of_input(&self) -> SourceCodeTrace {
|
||||
let first_key = self.last_external_input.unwrap();
|
||||
let line_number_offset = self
|
||||
.line_number_offsets
|
||||
.get(&first_key)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
let f = self.checkpoints.get(&first_key).unwrap();
|
||||
match f {
|
||||
Checkpoint::SourceCode { origin, content } => {
|
||||
// (line index, byte index of first character)
|
||||
let mut last_line: (usize, usize) = (0, 0);
|
||||
let mut last_non_empty_line: (usize, usize) = (0, 0);
|
||||
for (i, c) in content.char_indices() {
|
||||
if !c.is_whitespace() {
|
||||
last_non_empty_line = last_line;
|
||||
} else if c == '\n' {
|
||||
last_line.0 += 1;
|
||||
last_line.1 = i + 1;
|
||||
}
|
||||
}
|
||||
let last_line = content[last_non_empty_line.1..].trim_end();
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: last_line.to_string(),
|
||||
line_number: last_non_empty_line.0 + 1 + line_number_offset,
|
||||
index: last_line.len(),
|
||||
value: " ".to_string(),
|
||||
token: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
enum Checkpoint {
|
||||
SourceCode {
|
||||
origin: Origin,
|
||||
content: String, // TODO: should be rc::Rc<str>?
|
||||
},
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn one_source_code() {
|
||||
let file_name: PathBuf = "input.tex".into();
|
||||
let origin = Origin::File(file_name);
|
||||
let line_1 = "hël".to_string();
|
||||
let line_2 = "wor\\cömmand".to_string();
|
||||
let line_3 = "hël".to_string();
|
||||
let source_code = format!("{}\n{}\n{}", line_1, line_2, line_3);
|
||||
|
||||
let mut tracer: Tracer = Default::default();
|
||||
let mut interner: CsNameInterner = Default::default();
|
||||
let command = interner.get_or_intern("command");
|
||||
let mut range = tracer.register_source_code(None, origin.clone(), &source_code);
|
||||
let mut tokens = vec![
|
||||
Token::new_letter('h', range.next()),
|
||||
Token::new_letter('e', range.next()),
|
||||
Token::new_letter('l', range.next()),
|
||||
Token::new_space('\n', range.next()),
|
||||
Token::new_letter('w', range.next()),
|
||||
Token::new_letter('o', range.next()),
|
||||
Token::new_letter('r', range.next()),
|
||||
Token::new_control_sequence(command, range.next()),
|
||||
];
|
||||
for _ in 0.."command".len() {
|
||||
range.next();
|
||||
}
|
||||
let mut extra_tokens = vec![
|
||||
Token::new_space('\n', range.next()),
|
||||
Token::new_letter('h', range.next()),
|
||||
Token::new_letter('e', range.next()),
|
||||
Token::new_letter('l', range.next()),
|
||||
];
|
||||
tokens.append(&mut extra_tokens);
|
||||
|
||||
let got_traces: Vec<SourceCodeTrace> = tokens
|
||||
.iter()
|
||||
.map(|token| tracer.trace(*token, &interner))
|
||||
.collect();
|
||||
|
||||
let want_traces = vec![
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_1.clone(),
|
||||
line_number: 1,
|
||||
index: 0,
|
||||
value: "h".to_string(),
|
||||
token: Some(tokens[0]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_1.clone(),
|
||||
line_number: 1,
|
||||
index: 1,
|
||||
value: "e".to_string(),
|
||||
token: Some(tokens[1]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_1.clone(),
|
||||
line_number: 1,
|
||||
index: 2,
|
||||
value: "l".to_string(),
|
||||
token: Some(tokens[2]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_1.clone(),
|
||||
line_number: 1,
|
||||
index: 3,
|
||||
value: "\n".to_string(),
|
||||
token: Some(tokens[3]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_2.clone(),
|
||||
line_number: 2,
|
||||
index: 0,
|
||||
value: "w".to_string(),
|
||||
token: Some(tokens[4]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_2.clone(),
|
||||
line_number: 2,
|
||||
index: 1,
|
||||
value: "o".to_string(),
|
||||
token: Some(tokens[5]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_2.clone(),
|
||||
line_number: 2,
|
||||
index: 2,
|
||||
value: "r".to_string(),
|
||||
token: Some(tokens[6]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_2.clone(),
|
||||
line_number: 2,
|
||||
index: 3,
|
||||
value: "\\command".to_string(),
|
||||
token: Some(tokens[7]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_2.clone(),
|
||||
line_number: 2,
|
||||
index: 11,
|
||||
value: "\n".to_string(),
|
||||
token: Some(tokens[8]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_3.clone(),
|
||||
line_number: 3,
|
||||
index: 0,
|
||||
value: "h".to_string(),
|
||||
token: Some(tokens[9]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_3.clone(),
|
||||
line_number: 3,
|
||||
index: 1,
|
||||
value: "e".to_string(),
|
||||
token: Some(tokens[10]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: origin.clone(),
|
||||
line_content: line_3.clone(),
|
||||
line_number: 3,
|
||||
index: 2,
|
||||
value: "l".to_string(),
|
||||
token: Some(tokens[11]),
|
||||
},
|
||||
];
|
||||
assert_eq!(want_traces, got_traces);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_source_code() {
|
||||
let mut tokens = Vec::new();
|
||||
let mut tracer: Tracer = Default::default();
|
||||
let interner: CsNameInterner = Default::default();
|
||||
|
||||
let file_1 = Origin::File("a.tex".into());
|
||||
let file_1_content = "a".to_string();
|
||||
let mut range = tracer.register_source_code(None, file_1.clone(), &file_1_content);
|
||||
tokens.push(Token::new_letter('a', range.next()));
|
||||
|
||||
let file_2 = Origin::File("b.tex".into());
|
||||
let file_2_content = "b".to_string();
|
||||
let mut range = tracer.register_source_code(None, file_2.clone(), &file_2_content);
|
||||
tokens.push(Token::new_letter('b', range.next()));
|
||||
|
||||
let file_3 = Origin::Terminal;
|
||||
let file_3_content = "c".to_string();
|
||||
let mut range = tracer.register_source_code(None, file_3.clone(), &file_3_content);
|
||||
tokens.push(Token::new_letter('c', range.next()));
|
||||
|
||||
let got_traces: Vec<SourceCodeTrace> = tokens
|
||||
.iter()
|
||||
.map(|token| tracer.trace(*token, &interner))
|
||||
.collect();
|
||||
|
||||
let want_traces = vec![
|
||||
SourceCodeTrace {
|
||||
origin: file_1,
|
||||
line_content: file_1_content,
|
||||
line_number: 1,
|
||||
index: 0,
|
||||
value: "a".to_string(),
|
||||
token: Some(tokens[0]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: file_2,
|
||||
line_content: file_2_content,
|
||||
line_number: 1,
|
||||
index: 0,
|
||||
value: "b".to_string(),
|
||||
token: Some(tokens[1]),
|
||||
},
|
||||
SourceCodeTrace {
|
||||
origin: file_3,
|
||||
line_content: file_3_content,
|
||||
line_number: 1,
|
||||
index: 0,
|
||||
value: "c".to_string(),
|
||||
token: Some(tokens[2]),
|
||||
},
|
||||
];
|
||||
assert_eq!(want_traces, got_traces);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user