Gobble is a simple parser combinator system for parsing strings.
*Note:It works well but it is currently still under heavy development, so the API may change significantly between versions. if the 'b' changes in "0.b.c" there will be breaking changes. Though I do believe right now I'm close to setting on the API
I'm very open to recieving feedback on github*
Creating Parsers in rust should be quite straight forward. For example parsing a function call
use *;
parser!
parser!
let = FSig.parse_s.unwrap;
assert_eq!;
assert_eq!;
//Idents can't begin with numbers
assert!;
If you'd prefer not to use macros, you don't have to:
use *;
let ident = ;
let fsig = ;
let = fsig.parse_s.unwrap;
assert_eq!;
assert_eq!;
//identifiers cant start with numbers,
assert!;
But the macros guarantee of Zero-Sized types which is nice when combining them
To work this library depends the following:
// In the OK Case the value mean
// LCChars = copy of original, but moved forward,
// V = The resulting type
// Option<ParserError> 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> = ;
//implements Iterator and can be cloned relatively cheaply
Parser is automatically implemented for:
Fn<'a>(&LCChars<'a>)->ParseRes<'a,String>
&'static str
which will return itself if it matcheschar
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
use *;
// map can be used to convert one result to another
// keyval is now a function that returns a parser
let keyval = ;
//this can also be written as below for better type safety
// or as a macro KeyVal is now a struct like:
// pub struct KeyVal;
parser!
//parse_s is a helper on Parsers
let = keyval.parse_s.unwrap;
assert_eq!;
assert_eq!;
//this can now be combined with other parsers.
// 'ig_then' combines 2 parsers and drops the result of the first
// 'then_ig' drops the result of the second
// 'sep_until will repeat the first term into a Vec, separated by the second
// until the final term.
let obj = ;
let obs = obj.parse_s.unwrap;
assert_eq!;
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 characterplus(self)
'+' requires at least 1 matches and ruturns a stringmin_n(self,n:usize)
requires at least n matches and ruturns a stringstar(self)
'*' matches any number of chars returning a stringexact(self,n:usize)
'*' matches exactly n chars returning a stringiplus(self)
'+' requires at least 1 matches and ruturns a ()istar(self)
'*' matches any number of chars returning a ()iexact(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
use *;
let s = ;
let xv = s.one.parse_s.unwrap;
assert_eq!;
let id = .min_n.parse_s.unwrap;
assert_eq!;
// not enough matches
assert!;
// any succeeds even with no matches equivilent to min_n(0) but "Zero Size"
assert_eq!;
assert_eq!;
White Space
White space is pretty straight forward to handle
use *;
let my_ws = ;
// middle takes three parsers and returns the result of the middle
// this could also be done easily with 'map' or 'then_ig'
let my_s = ;
let sp_id = my_s;
let v = sp_id.parse_s.unwrap;
assert_eq!;
That said gobble already provides WS
and s_(p)
use *;
//eoi = end of input
let p = repeat_until_ig;
let r = p.parse_s.unwrap;
assert_eq!;
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' or with a macro which will return a zero sized struct for certain
use *;
// using the full fn def we avoid the recursive structure
let r = expr.parse_s.unwrap;
//recursive structures are never fun to write manually
assert_eq!;
Changelog:
v 0.5.3
- Added Hash to error type
v 0.5.2
- Added catch for repeats on zero length parse results
- StrungErr now implements PartialEq
v 0.5.1
- Added auto docs to the macros, hope to improve these soon
v 0.5.0
- Added macros -- Unexpectedly, Somewhat unexpectedly. see the Docs
- swapped skip_star to istar etc for charbools
- Added StrungError and StrError, which can print themselves with great information
v 0.4.4:
- Added
skip_star(p)
- Added
skip_plus(p)
- Added
skip_exact(p,n)
v 0.4.3:
- Added
string<A:Parser>(a:A)->impl Parser<String>
to create a parser that reads the internal parser but returns the whole string it matched on
v 0.4.2:
- Skip2Star added for skipping 2 parsers of different types
v 0.4.1:
- derives Hash and Eq for Error
v 0.4.0:
- Now uses "star" and "plus" for CharBool Repeats instead of "min_n" and "any"
- Now requires successes to declare if they could have continued with correct input
- Now has clearer errors containing info on how to find them, and what they expected next
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
- read_fs removed - use CharBool.min_n(usize) instead
- Esc removed - see common::common_str for how to handle escapes
v 0.2.1 :
- Added StringRepeat
- added SkipRepeat
- switched LCChars to use CharIndices
- now has index parser
- Added skip and skip_min to CharBool
- Added StrPos Parser str_pos
v 0.2.0 -- Major update:
- created a new trait called CharBool
- removed is_alpha_num
- Added Character readers, that take use the CharBool trait to get what they want
v 0.1.6:
- Added line_col wrappper to get the line and column of the result
- Added
one_char(&str)
Parser to check the next char is a member of that.
v 0.1.5 :
- Added common_float method
- impl Parser for char and &'static str
- made tuples work as combinator parsers
v 0.1.4:
- Added keyword to make sure there are no alpha num characters on the end of the keyword
- Fixed the error display method to make them easier to read.
- added a 'common' module and
common_int
andcommon_bool
parsers
v 0.1.3:
- Added reflect functionality for when you need to count up and down again
v 0.1.2 :
- Added
sep_until(main,sep,close)
- Added
repeat_until(main,close)
- Fixed Or Error to include both errors to make it easier to find the problems in branching iterators
v 0.1.1 :
- Added
eoi
andto_end()
functions for making sure you have the end of the input; - Added
common_str()
for getting the most common form of string