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
impl SourceLocation
Sourcepub const fn new(input: Arc<str>, start: usize, end: usize) -> Self
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 processedstart
: Zero-based inclusive start offsetend
: 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);
Sourcepub fn from_str(input: &str, start: usize, end: usize) -> Self
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 processedstart
: Zero-based inclusive start offsetend
: Zero-based exclusive end offset
§Returns
A new SourceLocation
instance.
§Examples
use katex::types::SourceLocation;
let loc = SourceLocation::from_str("x^2", 0, 3);
Sourcepub const fn start(&self) -> usize
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
.
Sourcepub const fn end(&self) -> usize
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
.
Sourcepub fn input(&self) -> &str
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.
Sourcepub fn input_arc(&self) -> Arc<str>
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.
Sourcepub fn range(first: Option<Self>, second: Option<Self>) -> Option<Self>
pub fn range(first: Option<Self>, second: Option<Self>) -> Option<Self>
Merges two SourceLocation
s 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 locationsecond
: 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
impl Clone for SourceLocation
Source§fn clone(&self) -> SourceLocation
fn clone(&self) -> SourceLocation
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SourceLocation
impl Debug for SourceLocation
Source§impl ErrorLocationProvider for SourceLocation
Implementation of ErrorLocationProvider
for SourceLocation
.
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.