SourceLocation

Struct SourceLocation 

Source
pub struct SourceLocation {
    pub input: Arc<str>,
    pub start: usize,
    pub end: usize,
}
Expand description

Represents a source location in a LaTeX/KaTeX mathematical expression.

This struct tracks the position of tokens or expressions within the input string during parsing, enabling precise error reporting and debugging. It stores a reference-counted copy of the input string and byte offsets for the start and end positions.

The struct is immutable once created, ensuring thread safety and preventing accidental modifications during parsing.

§Cross-references

  • Used in ParseError(crate::types::ParseError) for error location reporting.
  • Integrated with ErrorLocationProvider for consistent error handling.

Fields§

§input: Arc<str>

Reference-counted input string that was processed.

This field holds a shared reference to the original LaTeX/KaTeX input string that was tokenized. It’s used for location calculations and error messages.

§start: usize

Zero-based inclusive start offset in the input string.

This represents the byte position where the token or expression begins. For multi-byte UTF-8 characters, this points to the start of the first byte.

§end: usize

Zero-based exclusive end offset in the input string.

This represents the byte position immediately after the token or expression ends. The range [start, end) defines the exact span of the source location.

Implementations§

Source§

impl SourceLocation

Source

pub const fn new(input: Arc<str>, start: usize, end: usize) -> Self

Creates a new SourceLocation with the given input string and position range.

This constructor initializes a source location for tracking a specific span in the input string. The position range is defined by byte offsets relative to the input string.

§Parameters
  • input: Reference-counted input string that was processed
  • start: Zero-based inclusive start offset
  • end: Zero-based exclusive end offset
§Returns

A new SourceLocation instance.

§Examples
use katex::types::SourceLocation;
use std::sync::Arc;

let input = Arc::from("x^2");
let loc = SourceLocation::new(input, 0, 3);
Source

pub fn from_str(input: &str, start: usize, end: usize) -> Self

Creates a new SourceLocation from a string slice and position range.

This is a convenience method that creates a reference-counted string from the provided string slice and initializes a source location.

§Parameters
  • input: String slice containing the input that was processed
  • start: Zero-based inclusive start offset
  • end: Zero-based exclusive end offset
§Returns

A new SourceLocation instance.

§Examples
use katex::types::SourceLocation;

let loc = SourceLocation::from_str("x^2", 0, 3);
Source

pub const fn start(&self) -> usize

Returns the start offset of this source location.

This method provides access to the zero-based inclusive start position within the input string where the tracked element begins.

§Returns

The start offset as a usize.

Source

pub const fn end(&self) -> usize

Returns the end offset of this source location.

This method provides access to the zero-based exclusive end position within the input string where the tracked element ends.

§Returns

The end offset as a usize.

Source

pub fn input(&self) -> &str

Returns a reference to the input string associated with this source location.

This method allows access to the original input string that was processed, which can be used for location calculations and error messages.

§Returns

A string slice containing the input text.

Source

pub fn input_arc(&self) -> Arc<str>

Returns the input string as an Arc<String> for sharing.

This method provides access to the reference-counted input string, allowing it to be shared with other SourceLocation instances.

§Returns

A clone of the Arc<str> containing the input text.

Source

pub fn range(first: Option<Self>, second: Option<Self>) -> Option<Self>

Merges two SourceLocations into a single range.

This method combines two source locations into one that spans from the start of the first to the end of the second. Both locations must use the same input string. If either location is None, the other is returned. If the input strings differ, None is returned.

§Parameters
  • first: Optional first source location
  • second: Optional second source location
§Returns

An Option containing a SourceLocation representing the merged range, or None if merging is not possible.

§Error Handling

Returns None if:

  • Both locations are None
  • The locations use different input strings

Trait Implementations§

Source§

impl Clone for SourceLocation

Source§

fn clone(&self) -> SourceLocation

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourceLocation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ErrorLocationProvider for SourceLocation

Implementation of ErrorLocationProvider for SourceLocation.

This implementation allows SourceLocation to be used as an error location provider in the KaTeX parsing pipeline. It simply returns a reference to itself, as SourceLocation already contains the necessary location information.

§Cross-references

  • Part of the error reporting system in ParseError(crate::types::ParseError).
  • Used by parsers to provide location context for syntax errors.
Source§

fn loc(&self) -> Option<&SourceLocation>

Get the source location if available
Source§

impl PartialEq for SourceLocation

Source§

fn eq(&self, other: &SourceLocation) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SourceLocation

Source§

impl StructuralPartialEq for SourceLocation

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.