Struct parsit::parser::Parsit

source ·
pub struct Parsit<'a, T>where
    T: Logos<'a, Source = str>,{ /* private fields */ }
Expand description

The base structure of the parser combinator that providers a set of methods to construct a gramma

To construct the parser it takes a token set from Logos typically as an Enum

 use logos::Logos;
 use crate::parsit::parser::Parsit;
 #[derive(Logos,PartialEq)]
    pub enum TFQ {
        #[token("true")]
        True,
        #[token("false")]
        False,

        #[token("?")]
        Question,
    }
 let p:Parsit<TFQ> = Parsit::new("true?").unwrap();

Note: The parser works only with string input

Implementations§

source§

impl<'a, Token> Parsit<'a, Token>where Token: Logos<'a, Source = str> + PartialEq,

source

pub fn new(src: &'a str) -> Result<Self, ParseError<'a>>where Token::Extras: Default,

Creates a parser with aset of tokens from the source Raises an error if lexer raises an error

source

pub fn token(&self, pos: usize) -> Result<(&Token, usize), ParseError<'a>>

obtain a token from the parsing output according to the position number

Arguments
  • pos position number
Examples

typically used in the token! macros:

 let p:Parsit<_> = Parsit::new(...)?;
 token!(p.token(0) => ...)
source

pub fn one_or_more<T, Then>(&self, pos: usize, then: Then) -> Step<'a, Vec<T>>where Then: FnOnce(usize) -> Step<'a, T> + Copy,

executes some rule one or more times shifting the cursor farther. The operator expects at least one occurance of the given function

Arguments
  • pos - starting postion to parse
  • then - parsing function
Examples
 use logos::Logos;
 use crate::parsit::parser::Parsit;
 use crate::parsit::token;
 use crate::parsit::step::Step;
 #[derive(Logos,PartialEq)]
    pub enum TFQ {
        #[token("true")]
        True,
        #[token("false")]
        False,

        #[token("?")]
        Question,
    }
 let parser:Parsit<TFQ> = Parsit::new("true?false").unwrap();
 let parser_fn = |p|{ token!( parser.token(p) =>
                  TFQ::True => Some(true),
                  TFQ::False => Some(false),
                  TFQ::Question => None
            )};

  if let Some(res) = parser.one_or_more(0, parser_fn).ok() {
    assert_eq!(res, vec![Some(true), None, Some(false)]);
  } else { assert!(false) };
source

pub fn zero_or_more<T, Then>(&self, pos: usize, then: Then) -> Step<'a, Vec<T>>where Then: FnOnce(usize) -> Step<'a, T> + Copy,

executes some rule one or more times shifting the cursor farther. The operator expects zero or more occurances of the given function

Arguments
  • pos - starting postion to parse
  • then - parsing function
Examples
 use logos::Logos;
 use crate::parsit::parser::Parsit;
 use crate::parsit::token;
 use crate::parsit::step::Step;
 #[derive(Logos,PartialEq)]
    pub enum TFQ {
        #[token("true")]
        True,
        #[token("false")]
        False,

        #[token("?")]
        Question,
    }
 let parser:Parsit<TFQ> = Parsit::new("").unwrap();
 let parser_fn = |p|{ token!( parser.token(p) =>
                  TFQ::True => Some(true),
                  TFQ::False => Some(false),
                  TFQ::Question => None
            )};

  if let Some(res) = parser.zero_or_more(0, parser_fn).ok() {
    assert_eq!(res, vec![]);
  } else { assert!(false) };
source

pub fn validate_eof<T>(&self, res: Step<'a, T>) -> Step<'a, T>

Validates if the parsing process reaches the end of the input. If so transforms the result to the error UnreachedEOF

It can be used in the end of the parsing

source

pub fn env<T>(&self, step: &Step<'a, T>) -> String

Prints a position and env from the source text. It has a radius of 2 tokens so thus it prints -3-2-10+1+2+3

Auto Trait Implementations§

§

impl<'a, T> RefUnwindSafe for Parsit<'a, T>where T: RefUnwindSafe,

§

impl<'a, T> Send for Parsit<'a, T>where T: Send,

§

impl<'a, T> Sync for Parsit<'a, T>where T: Sync,

§

impl<'a, T> Unpin for Parsit<'a, T>where T: Unpin,

§

impl<'a, T> UnwindSafe for Parsit<'a, T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.