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,
impl<'a, Token> Parsit<'a, Token>where Token: Logos<'a, Source = str> + PartialEq,
sourcepub fn new(src: &'a str) -> Result<Self, ParseError<'a>>where
Token::Extras: Default,
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
sourcepub fn one_or_more<T, Then>(&self, pos: usize, then: Then) -> Step<'a, Vec<T>>where
Then: FnOnce(usize) -> Step<'a, T> + Copy,
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 parsethen
- 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) };
sourcepub fn zero_or_more<T, Then>(&self, pos: usize, then: Then) -> Step<'a, Vec<T>>where
Then: FnOnce(usize) -> Step<'a, T> + Copy,
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 parsethen
- 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) };
sourcepub fn validate_eof<T>(&self, res: Step<'a, T>) -> Step<'a, T>
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
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more