[][src]Struct string_parser::Parser

pub struct Parser {
    pub keywords: Vec<String>,
    pub end_filters: HashMap<String, Rc<Box<dyn Fn(Vec<char>) -> bool>>>,
    pub callbacks: HashMap<String, Rc<Box<dyn Fn(String, usize, &str)>>>,
}

Main struct of the crate

Example

./text being "...'foo'..."

use std::rc::Rc;
extern crate string_parser;
use string_parser::Parser; 
 
fn end_filter(c : Vec<char>) -> bool{            
    if c.last().unwrap()== &'\'' {
        return true;
        }
    else {
        return false;
        }   
}
//can also use closures
let callback = |s : String, line : usize, file : &str| {
    assert_eq!(String::from("foo"), s); 
};
let mut string_parser = Parser::new();
string_parser.add(String::from("'"), Rc::new(Box::from(end_filter)), Rc::new(Box::from(callback)));
string_parser.parse("./text");

Fields

keywords: Vec<String>

The list of keyword to parse

end_filters: HashMap<String, Rc<Box<dyn Fn(Vec<char>) -> bool>>>

Filter funtions for each keyword (indexed by keyword)

callbacks: HashMap<String, Rc<Box<dyn Fn(String, usize, &str)>>>

Callback funtions for each keyword (indexed by keyword)

Implementations

impl Parser[src]

pub fn new() -> Parser[src]

Instanciante a new empty Parser

pub fn add(
    &mut self,
    keyword: String,
    end_filter: Rc<Box<dyn Fn(Vec<char>) -> bool>>,
    callback: Rc<Box<dyn Fn(String, usize, &str)>>
) -> &Parser
[src]

Add a new token to parse

Arguments

  • text - the text to search
  • end_filter - the function called at each character to check if we're still within the token. Sould return true when out of the token.
    It receives the vector of characters inside the token
    • Should return true when at the end of the token

Example

fn end_filter(c : Vec<char>) -> bool{
    if c.last().unwrap() == &'\'' {
        println!("end filter");
        return true;
    }
    else {
        return false;
    }
}
  • callback - the function called when the text is exited. take the inside of the token as argument
    it should takes 3 arguments
    • The text inside the token
    • The line Number of the token
    • The file

Example

let callback = |s : String, line : usize, file : String| {
    println!("{} {} : {}", file, line, s);
};

pub fn get_keywords(self) -> Vec<String>[src]

pub fn get_end_filters(
    self
) -> HashMap<String, Rc<Box<dyn Fn(Vec<char>) -> bool>>>
[src]

pub fn get_callbacks(
    self
) -> HashMap<String, Rc<Box<dyn Fn(String, usize, &str)>>>
[src]

pub fn parse(&self, path: &str) -> Result<(), Error>[src]

Open the file and parse every token of the Parser

Trait Implementations

impl Clone for Parser[src]

Auto Trait Implementations

impl !RefUnwindSafe for Parser

impl !Send for Parser

impl !Sync for Parser

impl Unpin for Parser

impl !UnwindSafe for Parser

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.