pub struct LocatedSpan<T, X = ()> {
pub extra: X,
/* private fields */
}
Expand description
A LocatedSpan is a set of meta information about the location of a token, including extra information.
The LocatedSpan
structure can be used as an input of the nom parsers.
It implements all the necessary traits for LocatedSpan<&str,X>
and LocatedSpan<&[u8],X>
Fields§
§extra: X
Extra information that can be embedded by the user. Example: the parsed file name
Implementations§
Source§impl<T> LocatedSpan<T, ()>
impl<T> LocatedSpan<T, ()>
Sourcepub fn new(program: T) -> LocatedSpan<T, ()>
pub fn new(program: T) -> LocatedSpan<T, ()>
Create a span for a particular input with default offset
and
line
values and empty extra data.
You can compute the column through the get_column
or get_utf8_column
methods.
offset
starts at 0, line
starts at 1, and column
starts at 1.
Do not use this constructor in parser functions; nom
and
nom_locate
assume span offsets are relative to the beginning of the
same input. In these cases, you probably want to use the
nom::traits::Slice
trait instead.
§Example of use
use nom_locate::LocatedSpan;
let span = LocatedSpan::new(b"foobar");
assert_eq!(span.location_offset(), 0);
assert_eq!(span.location_line(), 1);
assert_eq!(span.get_column(), 1);
assert_eq!(span.fragment(), &&b"foobar"[..]);
Source§impl<T, X> LocatedSpan<T, X>
impl<T, X> LocatedSpan<T, X>
Sourcepub fn new_extra(program: T, extra: X) -> LocatedSpan<T, X>
pub fn new_extra(program: T, extra: X) -> LocatedSpan<T, X>
Create a span for a particular input with default offset
and
line
values. You can compute the column through the get_column
or get_utf8_column
methods.
offset
starts at 0, line
starts at 1, and column
starts at 1.
Do not use this constructor in parser functions; nom
and
nom_locate
assume span offsets are relative to the beginning of the
same input. In these cases, you probably want to use the
nom::traits::Slice
trait instead.
§Example of use
use nom_locate::LocatedSpan;
let span = LocatedSpan::new_extra(b"foobar", "extra");
assert_eq!(span.location_offset(), 0);
assert_eq!(span.location_line(), 1);
assert_eq!(span.get_column(), 1);
assert_eq!(span.fragment(), &&b"foobar"[..]);
assert_eq!(span.extra, "extra");
Sourcepub unsafe fn new_from_raw_offset(
offset: usize,
line: u32,
fragment: T,
extra: X,
) -> LocatedSpan<T, X>
pub unsafe fn new_from_raw_offset( offset: usize, line: u32, fragment: T, extra: X, ) -> LocatedSpan<T, X>
Similar to new_extra
, but allows overriding offset and line.
This is unsafe, because giving an offset too large may result in
undefined behavior, as some methods move back along the fragment
assuming any negative index within the offset is valid.
Sourcepub fn location_offset(&self) -> usize
pub fn location_offset(&self) -> usize
The offset represents the position of the fragment relatively to the input of the parser. It starts at offset 0.
Sourcepub fn location_line(&self) -> u32
pub fn location_line(&self) -> u32
The line number of the fragment relatively to the input of the parser. It starts at line 1.
Sourcepub fn fragment(&self) -> &T
pub fn fragment(&self) -> &T
The fragment that is spanned. The fragment represents a part of the input of the parser.
Sourcepub fn map_extra<U, F: FnOnce(X) -> U>(self, f: F) -> LocatedSpan<T, U>
pub fn map_extra<U, F: FnOnce(X) -> U>(self, f: F) -> LocatedSpan<T, U>
Transform the extra inside into another type
§Example of use
use nom::{
IResult,
combinator::{recognize, map_res},
sequence::{terminated, tuple},
character::{complete::{char, one_of}, is_digit},
bytes::complete::{tag, take_while1}
};
fn decimal(input: LocatedSpan<&str>) -> IResult<LocatedSpan<&str>, LocatedSpan<&str>> {
recognize(
take_while1(|c| is_digit(c as u8) || c == '_')
)(input)
}
fn main() {
let span = LocatedSpan::new("$10");
// matches the $ and then matches the decimal number afterwards,
// converting it into a `u8` and putting that value in the span
let (_, (_, n)) = tuple((
tag("$"),
map_res(
decimal,
|x| x.fragment().parse::<u8>().map(|n| x.map_extra(|_| n))
)
))(span).unwrap();
assert_eq!(n.extra, 10);
}
Sourcepub fn into_fragment(self) -> T
pub fn into_fragment(self) -> T
Takes ownership of the fragment without (re)borrowing it.
§Example of use
use nom::{
IResult,
bytes::complete::{take_till, tag},
combinator::rest,
};
fn parse_pair<'a>(input: LocatedSpan<&'a str>) -> IResult<LocatedSpan<&'a str>, (&'a str, &'a str)> {
let (input, key) = take_till(|c| c == '=')(input)?;
let (input, _) = tag("=")(input)?;
let (input, value) = rest(input)?;
Ok((input, (key.into_fragment(), value.into_fragment())))
}
fn main() {
let span = LocatedSpan::new("key=value");
let (_, pair) = parse_pair(span).unwrap();
assert_eq!(pair, ("key", "value"));
}
Sourcepub fn into_fragment_and_extra(self) -> (T, X)
pub fn into_fragment_and_extra(self) -> (T, X)
Takes ownership of the fragment and extra data without (re)borrowing them.
Source§impl<T: AsBytes, X> LocatedSpan<T, X>
impl<T: AsBytes, X> LocatedSpan<T, X>
Sourcepub fn get_line_beginning(&self) -> &[u8] ⓘ
pub fn get_line_beginning(&self) -> &[u8] ⓘ
Return the line that contains this LocatedSpan.
The get_column
and get_utf8_column
functions returns
indexes that corresponds to the line returned by this function.
Note that if this LocatedSpan ends before the end of the
original data, the result of calling get_line_beginning()
will not include any data from after the LocatedSpan.
let program = LocatedSpan::new(
"Hello World!\
\nThis is a multi-line input\
\nthat ends after this line.\n");
let multi = program.find_substring("multi").unwrap();
assert_eq!(
program.slice(multi..).get_line_beginning(),
"This is a multi-line input".as_bytes(),
);
Sourcepub fn get_column(&self) -> usize
pub fn get_column(&self) -> usize
Return the column index, assuming 1 byte = 1 column.
Use it for ascii text, or use get_utf8_column for UTF8.
§Example of use
let span = LocatedSpan::new("foobar");
assert_eq!(span.slice(3..).get_column(), 4);
Sourcepub fn get_utf8_column(&self) -> usize
pub fn get_utf8_column(&self) -> usize
Return the column index for UTF8 text. Return value is unspecified for non-utf8 text.
This version uses bytecount’s hyper algorithm to count characters. This is much faster
for long lines, but is non-negligibly slower for short slices (below around 100 bytes).
This is also sped up significantly more depending on architecture and enabling the simd
feature gates. If you expect primarily short lines, you may get a noticeable speedup in
parsing by using naive_get_utf8_column
instead. Benchmark your specific use case!
§Example of use
let span = LocatedSpan::new("メカジキ");
let indexOf3dKanji = span.find_substring("ジ").unwrap();
assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7);
assert_eq!(span.slice(indexOf3dKanji..).get_utf8_column(), 3);
Sourcepub fn naive_get_utf8_column(&self) -> usize
pub fn naive_get_utf8_column(&self) -> usize
Return the column index for UTF8 text. Return value is unspecified for non-utf8 text.
A simpler implementation of get_utf8_column
that may be faster on shorter lines.
If benchmarking shows that this is faster, you can use it instead of get_utf8_column
.
Prefer defaulting to get_utf8_column
unless this legitimately is a performance bottleneck.
§Example of use
let span = LocatedSpan::new("メカジキ");
let indexOf3dKanji = span.find_substring("ジ").unwrap();
assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7);
assert_eq!(span.slice(indexOf3dKanji..).naive_get_utf8_column(), 3);
Trait Implementations§
Source§impl<T: AsBytes, X> AsBytes for LocatedSpan<T, X>
impl<T: AsBytes, X> AsBytes for LocatedSpan<T, X>
Source§impl<T, U, X> AsRef<U> for LocatedSpan<&T, X>
impl<T, U, X> AsRef<U> for LocatedSpan<&T, X>
Source§impl<T: Clone, X: Clone> Clone for LocatedSpan<T, X>
impl<T: Clone, X: Clone> Clone for LocatedSpan<T, X>
Source§fn clone(&self) -> LocatedSpan<T, X>
fn clone(&self) -> LocatedSpan<T, X>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<A: Compare<B>, B: Into<LocatedSpan<B>>, X> Compare<B> for LocatedSpan<A, X>
impl<A: Compare<B>, B: Into<LocatedSpan<B>>, X> Compare<B> for LocatedSpan<A, X>
Source§fn compare(&self, t: B) -> CompareResult
fn compare(&self, t: B) -> CompareResult
Source§fn compare_no_case(&self, t: B) -> CompareResult
fn compare_no_case(&self, t: B) -> CompareResult
Source§impl<T, X> Deref for LocatedSpan<T, X>
impl<T, X> Deref for LocatedSpan<T, X>
Source§impl<T: ToString, X> Display for LocatedSpan<T, X>
impl<T: ToString, X> Display for LocatedSpan<T, X>
Source§impl<'a, T, X> ExtendInto for LocatedSpan<T, X>where
T: ExtendInto,
impl<'a, T, X> ExtendInto for LocatedSpan<T, X>where
T: ExtendInto,
Source§type Item = <T as ExtendInto>::Item
type Item = <T as ExtendInto>::Item
Item
type. Read moreSource§type Extender = <T as ExtendInto>::Extender
type Extender = <T as ExtendInto>::Extender
Source§fn new_builder(&self) -> Self::Extender
fn new_builder(&self) -> Self::Extender
Extend
of the correct typeSource§fn extend_into(&self, acc: &mut Self::Extender)
fn extend_into(&self, acc: &mut Self::Extender)
Source§impl<T, U, X> FindSubstring<U> for LocatedSpan<T, X>where
T: FindSubstring<U>,
impl<T, U, X> FindSubstring<U> for LocatedSpan<T, X>where
T: FindSubstring<U>,
Source§fn find_substring(&self, substr: U) -> Option<usize>
fn find_substring(&self, substr: U) -> Option<usize>
Source§impl<Fragment: FindToken<Token>, Token, X> FindToken<Token> for LocatedSpan<Fragment, X>
impl<Fragment: FindToken<Token>, Token, X> FindToken<Token> for LocatedSpan<Fragment, X>
Source§fn find_token(&self, token: Token) -> bool
fn find_token(&self, token: Token) -> bool
Source§impl<T: Hash, X> Hash for LocatedSpan<T, X>
impl<T: Hash, X> Hash for LocatedSpan<T, X>
Source§impl<'a, T, X> InputIter for LocatedSpan<T, X>where
T: InputIter,
impl<'a, T, X> InputIter for LocatedSpan<T, X>where
T: InputIter,
Source§type Item = <T as InputIter>::Item
type Item = <T as InputIter>::Item
Item
type. Read moreSource§type Iter = <T as InputIter>::Iter
type Iter = <T as InputIter>::Iter
&str
, the position
corresponds to the byte index of the characterSource§type IterElem = <T as InputIter>::IterElem
type IterElem = <T as InputIter>::IterElem
Source§fn iter_indices(&self) -> Self::Iter
fn iter_indices(&self) -> Self::Iter
Source§fn iter_elements(&self) -> Self::IterElem
fn iter_elements(&self) -> Self::IterElem
Source§impl<T: InputLength, X> InputLength for LocatedSpan<T, X>
impl<T: InputLength, X> InputLength for LocatedSpan<T, X>
Source§impl<T, X> InputTake for LocatedSpan<T, X>
impl<T, X> InputTake for LocatedSpan<T, X>
Source§impl<T, X> InputTakeAtPosition for LocatedSpan<T, X>where
T: InputTakeAtPosition + InputLength + InputIter,
Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Clone,
impl<T, X> InputTakeAtPosition for LocatedSpan<T, X>where
T: InputTakeAtPosition + InputLength + InputIter,
Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Clone,
Source§type Item = <T as InputIter>::Item
type Item = <T as InputIter>::Item
Item
type. Read moreSource§fn split_at_position_complete<P, E: ParseError<Self>>(
&self,
predicate: P,
) -> IResult<Self, Self, E>
fn split_at_position_complete<P, E: ParseError<Self>>( &self, predicate: P, ) -> IResult<Self, Self, E>
Source§fn split_at_position<P, E: ParseError<Self>>(
&self,
predicate: P,
) -> IResult<Self, Self, E>
fn split_at_position<P, E: ParseError<Self>>( &self, predicate: P, ) -> IResult<Self, Self, E>
Source§fn split_at_position1<P, E: ParseError<Self>>(
&self,
predicate: P,
e: ErrorKind,
) -> IResult<Self, Self, E>
fn split_at_position1<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind, ) -> IResult<Self, Self, E>
Source§fn split_at_position1_complete<P, E: ParseError<Self>>(
&self,
predicate: P,
e: ErrorKind,
) -> IResult<Self, Self, E>
fn split_at_position1_complete<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind, ) -> IResult<Self, Self, E>
Source§impl<T, X> Offset for LocatedSpan<T, X>
impl<T, X> Offset for LocatedSpan<T, X>
Source§impl<'a, T, R, X: Clone> Slice<R> for LocatedSpan<T, X>
impl<'a, T, R, X: Clone> Slice<R> for LocatedSpan<T, X>
impl<T: Copy, X: Copy> Copy for LocatedSpan<T, X>
impl<T: AsBytes + Eq, X> Eq for LocatedSpan<T, X>
Auto Trait Implementations§
impl<T, X> Freeze for LocatedSpan<T, X>
impl<T, X> RefUnwindSafe for LocatedSpan<T, X>where
T: RefUnwindSafe,
X: RefUnwindSafe,
impl<T, X> Send for LocatedSpan<T, X>
impl<T, X> Sync for LocatedSpan<T, X>
impl<T, X> Unpin for LocatedSpan<T, X>
impl<T, X> UnwindSafe for LocatedSpan<T, X>where
T: UnwindSafe,
X: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)