1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
//! Tracing system for determining the origin of a token.
//!
//! This module implements a [Tracer] for tokens.
//! When building helpful error messages we need to know the origin of tokens -
//! e.g., the file and line they came from.
//! 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)]
#[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,
// 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 {
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(),
},
);
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,
index: position,
value,
token: Some(token),
}
}
}
}
pub fn trace_end_of_input(&self) -> SourceCodeTrace {
let f = self
.checkpoints
.get(&self.last_external_input.unwrap())
.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,
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);
}
}