[][src]Crate rcombinators

rcombinators is a parser combinator library without special magic. It aims to be both easy to use and reasonably fast, without using too much special syntax or macros.

You will notice two kinds of parsers that however differ only in minor aspects:

  • Ones starting with a capital letter are structs (such as Int, Sequence). You can create them using ParserName::new(), or a specialized constructor method.
  • Ones starting with a lower case letter (and in snake case, such as string_of). Those are functions returning Parser objects combined from one or more elementary parsers.

The resulting objects implementing the Parser trait are identical to use.

Note that not all primitives and combinators are exported at the crate level! Only "important" ones are.

Here's a short example of how to use it:

use rcombinators::combinators;
use rcombinators::primitives;
use rcombinators::ParseState;
use rcombinators::Parser;

// Goal: Parse the string between the parentheses, and then the float.
let mut ps = ParseState::new("(a1b3c4) -1.25e-1");

let mut some_string = combinators::Alternative::new(
    (primitives::StringParser::new("xyz"),
     primitives::string_of("abcde12345",
     combinators::RepeatSpec::Min(1))));
let mut in_parens = combinators::Sequence::new(
    (primitives::StringParser::new("("),
     some_string,
     primitives::StringParser::new(")")));
assert_eq!(Ok(
    ("(".to_string(),
     "a1b3c4".to_string(),
     ")".to_string())), in_parens.parse(&mut ps));

// You can continue using a ParseState, for example when implementing your own parsers.
let _ = primitives::whitespace().parse(&mut ps);
// Parsers returned by functions such as float() should be cached when used more frequently.
// This saves time due to not needing to construct the parsers repeatedly.
assert_eq!(Ok(-0.125), primitives::float().parse(&mut ps));

Re-exports

pub use combinators::Alternative;
pub use combinators::Maybe;
pub use combinators::PartialSequence;
pub use combinators::Repeat;
pub use combinators::Sequence;
pub use combinators::Then;
pub use combinators::Transform;
pub use parser::execerr;
pub use parser::ParseResult;
pub use parser::Parser;
pub use primitives::float;
pub use primitives::string_none_of;
pub use primitives::string_of;
pub use primitives::whitespace;
pub use primitives::Int;
pub use primitives::StringParser;

Modules

combinators
parser
primitives

Structs

ParseState

ParseState encapsulates a stream of chars.