Gobble is a simple parser combinator system for parsing strings.

For example parsing a function call

```rust use gobble::*; let ident = || string2parts(Alpha.minn(1),(Alpha,NumDigit,'').any());

let fsig = (ident().thenig("("),sep(ident(),",").thenig(")"));

let (nm, args) = fsig.parses("loadFile1(fname,ref)").unwrap(); asserteq!(nm, "loadFile1"); assert_eq!(args, vec!["fname", "ref"]);

//identifiers cant start with numbers, assert!(fsig.parses("23file(fname,ref)").iserr());

```

To work this library depends the following:

```rust pub enum ParseError { //... }

// In the OK Case the value mean // LCChars = copy of original, but moved forward, // V = The resulting type // Option Only "Some" if the parser could have contined with more data // --This is useful for tracking what values would have been expected at a certain point // pub type ParseRes<'a, V> = Result<(LCChars<'a>, V,Option), ParseError>;

//implements Iterator and can be cloned relatively cheaply pub struct LCChars<'a>{ it:std::str::Chars<'a>, line:usize, col:usize, }

pub trait Parser { // Takes a non-mut pointer to the iterator, so that the caller // may try something else if this doesn't work // clone it before reading next fn parse<'a>(&self,it:&LCChars<'a>)->ParseRes<'a,V>;

//...helper methods

} pub trait CharBool { fn char_bool(&self,c:char)->bool; //....helper methods } ```

Parser is automatically implemented for: * Fn<'a>(&LCChars<'a>)->ParseRes<'a,String> * &'static str which will return itself if it matches * char which will return itself if it matched the next char * Tuples of up to 6 parsers. Returning a tuple of all the parsers matched one after the other.

Most of the time a parser can be built simply by combining other parsers ```rust use gobble::*;

// map can be used to convert one result to another // keyval is now a function that returns a parser let keyval = || (commonident,":",commonstr).map(|(a,_,c)|(a,c));

//this can also be written as below for better type safety fn keyval2()->impl Parserident,":",commonstr).map(|(a,_,c)|(a,c)) }

//parses is a helper on Parsers let (k,v) = keyval().parses(r#"car:"mini""#).unwrap(); asserteq!(k,"car"); asserteq!(v,"mini");

//this can now be combined with other parsers. // 'igthen' combines 2 parsers and drops the result of the first // 'thenig' drops the result of the second // 'sepuntil will repeat the first term into a Vec, separated by the second // until the final term. let obj = || "{".igthen(sep_until(keyval(),",","}"));

let obs = obj().parses(r#"{cat:"Tiddles",dog:"Spot"}"#).unwrap(); asserteq!(obs[0],("cat".tostring(),"Tiddles".tostring()));

``` ## CharBool

CharBool is the trait for boolean char checks. It is auto implemented for: * Fn(char)->bool * char -- Returns true if the input matches the char * &'static str -- returns true if the str contains the input * several zero size types - Alpha,NumDigit,HexDigit,WS,WSL,Any * Tuples of up to 6 CharBools -- returning true if any of the members succeed

This means you can combine them in tuples (Alpha,NumDigit,"_").char_bool(c) will be true if any of them match

CharBool also provides several helper methods which each return a parser * one(self) matches and returns exactly 1 character * plus(self) '+' requires at least 1 matches and ruturns a string * min_n(self,n:usize) requires at least n matches and ruturns a string * star(self) '' matches any number of chars returning a string * exact(self,n:usize) '' matches exactly n chars returning a string * skip_plus(self) '+' requires at least 1 matches and ruturns a () * skip_star(self) '*' matches any number of chars returning a () * skip_exact(self,n:usize) matches exactly n chars returning a ()

And a helper that returns a CharBool * except(self,cb:CharBool) Passes if self does, and cb doesnt ```rust use gobble::*; let s = |c| c > 'w' || c == 'z'; let xv = s.one().parses("xhello").unwrap(); asserteq!(xv,'x');

let id = (Alpha,"*").minn(4).parses("sm*shinggame+you").unwrap(); asserteq!(id,"sm*shinggame");

// not enough matches assert!((NumDigit,"abc").minn(4).parses("23fflr").is_err());

// any succeeds even with no matches equivilent to minn(0) but "Zero Size" asserteq!((NumDigit,"abc").star().parses("23fflr"),Ok("23".tostring())); asserteq!((NumDigit,"abc").star().parses("fflr"),Ok("".to_string()));

```

White Space

White space is pretty straight forward to handle

```rust use gobble::*; let myws = || " \t".any(); // middle takes three parsers and returns the result of the middle // this could also be done easily with 'map' or 'thenig' let mys = |p| middle(myws(),p,my_ws());

let spid = mys(commonident); let v = spid.parses(" \t doggo ").unwrap(); asserteq!(v,"doggo"); That said gobble already providesws()ands_(p)```

rust use gobble::*; //eoi = end of input let p = repeat_until_ig(s_("abc".min_n(1)),eoi); let r = p.parse_s("aaa \tbbb bab").unwrap(); assert_eq!(r,vec!["aaa","bbb","bab"]);

Recursive Structures

Some structures like Json, or programming languages need to be able to handle recursion. However with the techniques we have used so far this would lead to infinitely sized structures.

The way to handle this is to make sure one member of the loop is not
build into the structure. Instead to create it using the 'Fn'

```rust use gobble::*;

[derive(Debug,PartialEq)]

enum Expr { Val(isize), Add(Box,Box), Paren(Box), }

fn exprl()->impl Parser{ or( middle("(",s(expr),")").map(|e|Expr::Paren(Box::new(e))), common_int.map(|v|Expr::Val(v)) ) }

// using the full fn def we avoid the recursive structure fn expr<'a>(it:&LCChars<'a>)->ParseRes<'a,Expr> { //note that exprl has brackets but expr doesnt. //expr is a reference to a static function let p = (exprl(),maybe(s("+").igthen(expr))) .map(|(l,opr)|match opr{ Some(r)=>Expr::Add(Box::new(l),Box::new(r)), None=>l, });

p.parse(it)

}

let r = expr.parse_s("45 + (34+3 )").unwrap();

//recursive structures are never fun to write manually assert_eq!(r,Expr::Add( Box::new(Expr::Val(45)), Box::new(Expr::Paren(Box::new(Expr::Add( Box::new(Expr::Val(34)), Box::new(Expr::Val(3)) )))) ));

```

Changelog:

v 0.4.1:

v 0.4.0:

v 0.3.0: Breaking Changes

*Now Parser output is a trait associated type (Out) use impl Parser<Out=V> instead of impl Parser<V> and most things should work * readfs removed - use CharBool.minn(usize) instead * Esc removed - see common::common_str for how to handle escapes

v 0.2.1 :

v 0.2.0 -- Major update:

v 0.1.6:

v 0.1.5 :

v 0.1.4:

v 0.1.3:

v 0.1.2 :

v 0.1.1 :