Skip to main content

Parser

Struct Parser 

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

Parses a document as it arrives, a piece at a time, into the tree parse_with would build from the whole: the same for any way of cutting the input up, with the same spans and errors when asked for.

Each Parser::feed tokenizes and builds as far as the input allows, keeping only what it can’t decide yet (an unfinished tag, a character reference cut short, a CR that might be half a CRLF) for the next piece; Parser::finish reads the end of the input and hands back the document. The pieces are &str, so one can’t end inside a character: decoding bytes, and finding the encoding, comes first, and is the encoding crate’s.

use parsec::{Options, Parser};

let mut parser = Parser::new(Options::default());
for piece in ["<p>He", "llo, <b>wor", "ld</b>"] {
    parser.feed(piece);
}
let doc = parser.finish().unwrap();
assert_eq!(doc.text(doc.root()), "Hello, world");

Implementations§

Source§

impl Parser

Source

pub fn new(options: Options) -> Parser

A parser for a whole document, as parse_with parses one.

Source

pub fn fragment(ns: Ns, context: &str, options: Options) -> Parser

A parser for the contents of an element named context in namespace ns, as parse_fragment_with parses them.

Source

pub fn feed(&mut self, chunk: &str)

The next piece of the input.

Source

pub fn finish(self) -> Result<Document, ParseError>

The end of the input: the document, or why there’s none, as parse_with gives them. A budget counts the time spent in Parser::feed and here, not the time between.

Auto Trait Implementations§

§

impl Forget for Parser

§

impl Freeze for Parser

§

impl Move for Parser

§

impl RefUnwindSafe for Parser

§

impl Send for Parser

§

impl Sync for Parser

§

impl Unpin for Parser

§

impl UnsafeUnpin for Parser

§

impl UnwindSafe for Parser

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.