Struct Context

Source
pub struct Context<I: Stream> { /* private fields */ }
Expand description

A high-level wrapper around an input Stream.

It handles errors, and tracks the Locations of the input Tokens that could form part of the next output Token. It also provides an unread() method to pretend that you didn’t read a Token.

Implementations§

Source§

impl<I: Stream> Context<I>

Source

pub fn new(input: I) -> Self

Source

pub fn pop(&mut self) -> Location

Returns the Location of the most recent Token, and forgets it.

Source

pub fn first(&self) -> Location

Returns the Location of the first Token returned by read().

Source

pub fn last(&self) -> Location

Returns the Location of the last Token returned by read().

Source

pub fn locate<T>(&self, value: T) -> Loc<T>

Annotate t with last().

Source

pub fn drain(&mut self) -> Location

Returns a Location containing all Tokens read() so far, and forgets them.

Source

pub fn read_any(&mut self) -> Result<Box<dyn Tree>, String>

Read the next Token and internally record its Location.

  • Ok(tree) - The parse Tree of the next Token.
  • Err(msg) - An error prevented parsing of the next Token.
Source

pub fn read<T: Tree>(&mut self) -> Result<Option<Box<T>>, String>

Read the next Token and internally record its Location, but only if its parse Tree is of type T.

  • Ok(Some(tree)) - The next Token’s parse tree is of type T.
  • Ok(None) - The next Token is not a T. It has been unread().
  • Err(message) - An error prevented parsing of the next Token.
Source

pub fn read_if<T: Tree>( &mut self, is_wanted: impl FnOnce(&T) -> bool, ) -> Result<Option<Box<T>>, String>

Read the next Token and internally record its Location, but only if it is_wanted.

  • Ok(Some(tree)) - If is_wanted(tree).
  • Ok(None) - The next Token’s parse tree is not a T or is unwanted. It has been unread().
  • Err(message) - An error prevented parsing of the next Token.
Source

pub fn unread(&mut self, tree: Box<dyn Tree>)

Pretend we haven’t read the most recent Token.

tree must be the parse Tree of the most recent Token. It will be returned by the next call to read().

Auto Trait Implementations§

§

impl<I> Freeze for Context<I>
where I: Freeze,

§

impl<I> !RefUnwindSafe for Context<I>

§

impl<I> !Send for Context<I>

§

impl<I> !Sync for Context<I>

§

impl<I> Unpin for Context<I>
where I: Unpin,

§

impl<I> !UnwindSafe for Context<I>

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