Struct Buffer

Source
pub struct Buffer { /* private fields */ }
Expand description

A growable source file that can be parsed incrementally.

let mut buffer = welly_parser::Buffer::default();
buffer.push_str("hw = \"Hello, world!\\n\";\n");
buffer.push_str("for _ in 10 { print(hw); }");
while let Some(token) = buffer.try_parse() {
    println!("{:#?}", token);
}

Implementations§

Source§

impl Buffer

Source

pub fn remainder(&self) -> &str

Returns the suffix of the source code that has not yet been parsed.

Source

pub fn clear(&mut self)

Discard remainder().

Source

pub fn push_str(&mut self, source: &str)

Append source to the source code. Requires !self.is_complete().

Source

pub fn complete(&mut self)

Inform self that it has all the source code.

This can be important, as in the following example:

let mut buffer = welly_parser::Buffer::default();
buffer.push_str("if c {}"); // Could be followed by `else`.
assert!(buffer.try_parse().is_none());
buffer.complete(); // Exclude `else`.
assert!(buffer.try_parse().is_some());
Source

pub fn is_complete(&self) -> bool

Returns true if more source code can be added with self.push_str().

Source

pub fn try_parse(&mut self) -> Option<(Rc<str>, Token)>

Attempt to parse [self.remainder()]. Hopefully the returned Token is a Stmt. Other possibilities can be found in [welly].

Some((source, token)) indicates that there was enough source code to parse a Token (which might be an error message). Locations are relative to the returned source, which is removed from self.

None indicates that there was not enough source code, either because we need to wait for more, or because there is no more. In this case self is not modified.

Trait Implementations§

Source§

impl Debug for Buffer

Source§

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

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

impl Default for Buffer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Buffer

§

impl !RefUnwindSafe for Buffer

§

impl !Send for Buffer

§

impl !Sync for Buffer

§

impl Unpin for Buffer

§

impl !UnwindSafe for Buffer

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.