Struct pest::inputs::Position [] [src]

pub struct Position<I: Input> { /* fields omitted */ }

A struct containing a position that is tied to an Input which provides useful methods to manually parse it. This leads to an API largely based on the standard Result.

Methods

impl<I: Input> Position<I>
[src]

Creates starting Position from an Rc<Input>.

Examples

let input = Rc::new(StringInput::new("".to_owned()));

Position::from_start(input);

Returns the current position as a usize.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(start.pos(), 0);
assert_eq!(start.match_string("ab").unwrap().pos(), 2);

Creates a Span from two Positions.

Panics

Panics when the positions come from different inputs.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);
let end = start.clone().match_string("ab").unwrap();
let span = start.span(end);

assert_eq!(span.start(), 0);
assert_eq!(span.end(), 2);

Returns the line - and column number pair of the current Position.

Examples

let input = Rc::new(StringInput::new("\na".to_owned()));
let start = Position::from_start(input);
let pos = start.match_string("\na").unwrap();

assert_eq!(pos.line_col(), (2, 2));

Returns the actual line of the current Position.

Examples

let input = Rc::new(StringInput::new("\na".to_owned()));
let start = Position::from_start(input);
let pos = start.match_string("\na").unwrap();

assert_eq!(pos.line_of(), "a");

Returns Ok with the current Position if it is at the start of its Input or Err of the same Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);
let end = start.clone().match_string("ab").unwrap();

assert_eq!(start.clone().at_start(), Ok(start));
assert_eq!(end.clone().at_start(), Err(end));

Returns Ok with the current Position if it is at the end of its Input or Err of the same Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);
let end = start.clone().match_string("ab").unwrap();

assert_eq!(start.clone().at_end(), Err(start));
assert_eq!(end.clone().at_end(), Ok(end));

Skips n chars from the Position and returns Ok with the new Position if the skip was possible or Err with the current Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(start.clone().skip(2).unwrap().pos(), 2);
assert_eq!(start.clone().skip(3), Err(start));

Matches string from the Position and returns Ok with the new Position if a match was made or Err with the current Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(start.clone().match_string("ab").unwrap().pos(), 2);
assert_eq!(start.clone().match_string("ac"), Err(start));

Case-insensitively matches string from the Position and returns Ok with the new Position if a match was made or Err with the current Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(start.clone().match_insensitive("AB").unwrap().pos(), 2);
assert_eq!(start.clone().match_insensitive("AC"), Err(start));

Matches char range from the Position and returns Ok with the new Position if a match was made or Err with the current Position otherwise.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(start.clone().match_range('a'..'z').unwrap().pos(), 1);
assert_eq!(start.clone().match_range('A'..'Z'), Err(start));

Starts a sequence of transformations provided by f from the Position. It returns the same Result returned by f in the case of an Ok or Err with the current Position otherwise.

This method is useful to parse sequences that only match together which usually come in the form of chained Results with Result::and_then. Such chains should always be wrapped up in ParserState::sequence if they can create Tokens before being wrapped in Position::sequence.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(
    start.clone().sequence(|p| {
        p.match_string("a").and_then(|p| {
            p.match_string("b")
        })
    }).unwrap().pos(),
    2
);
assert_eq!(
    start.clone().sequence(|p| {
        p.match_string("a").and_then(|p| {
            p.match_string("c")
        })
    }),
    Err(start)
);

Starts a lookahead transformation provided by f from the Position. It returns Ok with the current position if f also returns an Ok or Err with the current Position otherwise.

If is_positive is false, it swaps the Ok and Err together, negating the Result. It should always be wrapped up in ParserState::lookahead if it can create Tokens before being wrapped in Position::lookahead.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(
    start.clone().lookahead(true, |p| {
        p.match_string("ab")
    }),
    Ok(start.clone())
);
assert_eq!(
    start.clone().lookahead(true, |p| {
        p.match_string("ac")
    }),
    Err(start.clone())
);
assert_eq!(
    start.clone().lookahead(false, |p| {
        p.match_string("ac")
    }),
    Ok(start)
);

Optionally applies the transformation provided by f from the Position. It returns Ok with the Position returned by f regardless of the Result.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(
    start.clone().optional(|p| {
        p.match_string("a").and_then(|p| {
            p.match_string("b")
        })
    }).unwrap().pos(),
    2
);
assert_eq!(
    start.clone().sequence(|p| {
        p.match_string("a").and_then(|p| {
            p.match_string("c")
        })
    }),
    Err(start)
);

Repeatedly applies the transformation provided by f from the Position. It returns Ok with the first Position returned by f which is wrapped up in an Err.

Examples

let input = Rc::new(StringInput::new("ab".to_owned()));
let start = Position::from_start(input);

assert_eq!(
    start.clone().repeat(|p| {
        p.match_string("a")
    }).unwrap().pos(),
    1
);
assert_eq!(
    start.repeat(|p| {
        p.match_string("b")
    }).unwrap().pos(),
    0
);

Trait Implementations

impl<I: Input> Debug for Position<I>
[src]

Formats the value using the given formatter.

impl<I: Input> Clone for Position<I>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<I: Input> PartialEq for Position<I>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<I: Input> Eq for Position<I>
[src]

impl<I: Input> PartialOrd for Position<I>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<I: Input> Ord for Position<I>
[src]

This method returns an Ordering between self and other. Read more

🔬 This is a nightly-only experimental API. (ord_max_min)

Compares and returns the maximum of two values. Read more

🔬 This is a nightly-only experimental API. (ord_max_min)

Compares and returns the minimum of two values. Read more

impl<I: Input> Hash for Position<I>
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more