Struct nom_locate::LocatedSpan

source ·
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: AsBytes> LocatedSpan<T, ()>

source

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: AsBytes, X> LocatedSpan<T, X>

source

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");
source

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.

source

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.

source

pub fn location_line(&self) -> u32

The line number of the fragment relatively to the input of the parser. It starts at line 1.

source

pub fn fragment(&self) -> &T

The fragment that is spanned. The fragment represents a part of the input of the parser.

source

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(),
);
source

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);
source

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);
source

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>

source§

fn as_bytes(&self) -> &[u8]

casts the input type to a byte slice
source§

impl<T: Clone, X: Clone> Clone for LocatedSpan<T, X>

source§

fn clone(&self) -> LocatedSpan<T, X>

Returns a copy 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<A: Compare<B>, B: Into<LocatedSpan<B>>, X> Compare<B> for LocatedSpan<A, X>

source§

fn compare(&self, t: B) -> CompareResult

compares self to another value for equality
source§

fn compare_no_case(&self, t: B) -> CompareResult

compares self to another value for equality independently of the case. Read more
source§

impl<T: Debug, X: Debug> Debug for LocatedSpan<T, X>

source§

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

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

impl<T, X> Deref for LocatedSpan<T, X>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: ToString, X> Display for LocatedSpan<T, X>

source§

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

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

impl<'a, X> ExtendInto for LocatedSpan<&'a [u8], X>

§

type Item = u8

the current input type is a sequence of that Item type. Read more
§

type Extender = Vec<u8>

the type that will be produced
source§

fn new_builder(&self) -> Self::Extender

create a new Extend of the correct type
source§

fn extend_into(&self, acc: &mut Self::Extender)

accumulate the input into an accumulator
source§

impl<'a, X> ExtendInto for LocatedSpan<&'a str, X>

§

type Item = char

the current input type is a sequence of that Item type. Read more
§

type Extender = String

the type that will be produced
source§

fn new_builder(&self) -> Self::Extender

create a new Extend of the correct type
source§

fn extend_into(&self, acc: &mut Self::Extender)

accumulate the input into an accumulator
source§

impl<'a, T, X> FindSubstring<&'a str> for LocatedSpan<T, X>
where T: FindSubstring<&'a str>,

source§

fn find_substring(&self, substr: &'a str) -> Option<usize>

returns the byte position of the substring if it is found
source§

impl<Fragment: FindToken<Token>, Token, X> FindToken<Token> for LocatedSpan<Fragment, X>

source§

fn find_token(&self, token: Token) -> bool

returns true if self contains the token
source§

impl<T: AsBytes, X: Default> From<T> for LocatedSpan<T, X>

source§

fn from(i: T) -> Self

Converts to this type from the input type.
source§

impl<'a, X> InputIter for LocatedSpan<&'a [u8], X>

§

type Item = u8

the current input type is a sequence of that Item type. Read more
§

type Iter = Enumerate<<LocatedSpan<&'a [u8], X> as InputIter>::IterElem>

an iterator over the input type, producing the item and its position for use with Slice. If we’re iterating over &str, the position corresponds to the byte index of the character
§

type IterElem = Map<Iter<'a, <LocatedSpan<&'a [u8], X> as InputIter>::Item>, fn(_: &u8) -> u8>

an iterator over the input type, producing the item
source§

fn iter_indices(&self) -> Self::Iter

returns an iterator over the elements and their byte offsets
source§

fn iter_elements(&self) -> Self::IterElem

returns an iterator over the elements
source§

fn position<P>(&self, predicate: P) -> Option<usize>
where P: Fn(Self::Item) -> bool,

finds the byte position of the element
source§

fn slice_index(&self, count: usize) -> Option<usize>

get the byte offset from the element’s position in the stream
source§

impl<'a, X> InputIter for LocatedSpan<&'a str, X>

§

type Item = char

the current input type is a sequence of that Item type. Read more
§

type Iter = CharIndices<'a>

an iterator over the input type, producing the item and its position for use with Slice. If we’re iterating over &str, the position corresponds to the byte index of the character
§

type IterElem = Chars<'a>

an iterator over the input type, producing the item
source§

fn iter_indices(&self) -> Self::Iter

returns an iterator over the elements and their byte offsets
source§

fn iter_elements(&self) -> Self::IterElem

returns an iterator over the elements
source§

fn position<P>(&self, predicate: P) -> Option<usize>
where P: Fn(Self::Item) -> bool,

finds the byte position of the element
source§

fn slice_index(&self, count: usize) -> Option<usize>

get the byte offset from the element’s position in the stream
source§

impl<T: InputLength, X> InputLength for LocatedSpan<T, X>

source§

fn input_len(&self) -> usize

calculates the input length, as indicated by its name, and the name of the trait itself
source§

impl<T, X> InputTake for LocatedSpan<T, X>
where Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,

source§

fn take(&self, count: usize) -> Self

returns a slice of count bytes. panics if count > length
source§

fn take_split(&self, count: usize) -> (Self, Self)

split the stream at the count byte offset. panics if count > length
source§

impl<T, X> InputTakeAtPosition for LocatedSpan<T, X>

§

type Item = <T as InputIter>::Item

the current input type is a sequence of that Item type. Read more
source§

fn split_at_position_complete<P, E: ParseError<Self>>( &self, predicate: P ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

looks for the first element of the input type for which the condition returns true, and returns the input up to this position Read more
source§

fn split_at_position<P, E: ParseError<Self>>( &self, predicate: P ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

looks for the first element of the input type for which the condition returns true, and returns the input up to this position Read more
source§

fn split_at_position1<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

looks for the first element of the input type for which the condition returns true and returns the input up to this position Read more
source§

fn split_at_position1_complete<P, E: ParseError<Self>>( &self, predicate: P, e: ErrorKind ) -> IResult<Self, Self, E>
where P: Fn(Self::Item) -> bool,

looks for the first element of the input type for which the condition returns true and returns the input up to this position Read more
source§

impl<T, X> Offset for LocatedSpan<T, X>

source§

fn offset(&self, second: &Self) -> usize

offset between the first byte of self and the first byte of the argument
source§

impl<R: FromStr, T, X> ParseTo<R> for LocatedSpan<T, X>
where T: ParseTo<R>,

source§

fn parse_to(&self) -> Option<R>

succeeds if parse() succeeded. The byte slice implementation will first convert it to a &str, then apply the parse() function
source§

impl<T: AsBytes + PartialEq, X> PartialEq for LocatedSpan<T, X>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, X: Clone> Slice<Range<usize>> for LocatedSpan<&'a [u8], X>

source§

fn slice(&self, range: Range<usize>) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<Range<usize>> for LocatedSpan<&'a str, X>

source§

fn slice(&self, range: Range<usize>) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeFrom<usize>> for LocatedSpan<&'a [u8], X>

source§

fn slice(&self, range: RangeFrom<usize>) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeFrom<usize>> for LocatedSpan<&'a str, X>

source§

fn slice(&self, range: RangeFrom<usize>) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeFull> for LocatedSpan<&'a [u8], X>

source§

fn slice(&self, range: RangeFull) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeFull> for LocatedSpan<&'a str, X>

source§

fn slice(&self, range: RangeFull) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeTo<usize>> for LocatedSpan<&'a [u8], X>

source§

fn slice(&self, range: RangeTo<usize>) -> Self

slices self according to the range argument
source§

impl<'a, X: Clone> Slice<RangeTo<usize>> for LocatedSpan<&'a str, X>

source§

fn slice(&self, range: RangeTo<usize>) -> Self

slices self according to the range argument
source§

impl<T: Copy, X: Copy> Copy for LocatedSpan<T, X>

source§

impl<T: AsBytes + Eq, X> Eq for LocatedSpan<T, X>

Auto Trait Implementations§

§

impl<T, X> Freeze for LocatedSpan<T, X>
where T: Freeze, X: Freeze,

§

impl<T, X> RefUnwindSafe for LocatedSpan<T, X>

§

impl<T, X> Send for LocatedSpan<T, X>
where T: Send, X: Send,

§

impl<T, X> Sync for LocatedSpan<T, X>
where T: Sync, X: Sync,

§

impl<T, X> Unpin for LocatedSpan<T, X>
where T: Unpin, X: Unpin,

§

impl<T, X> UnwindSafe for LocatedSpan<T, X>
where T: UnwindSafe, X: UnwindSafe,

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> 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,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.