Crates.io | markab_parser |
lib.rs | markab_parser |
version | 0.7.0 |
source | src |
created_at | 2018-12-28 11:27:25.508305 |
updated_at | 2019-01-09 10:07:13.696798 |
description | A simple, copy-less and rich-error-message parser combinator library. |
homepage | |
repository | https://github.com/arkhe634/markab |
max_upload_size | |
id | 104206 |
size | 48,743 |
This crate provides simple, copy-less and rich-error-message parser combinator for parsing string.
You can create simple parser with character
, character_class
and string
.
Each methods returns a class implementing Parser
trait,
CharacterParser
, CharacterClassParser
, StringParser
,
and returns a slice of the source string by calling the parse
method of Parser trait.
use markab_parser::{
string,
Parser,
};
let src = "requirement";
let mut pos = 0;
let parser = string("requirement");
let result = parser.parse(src, &mut pos);
assert!(result.is_ok());
assert_eq!(result.ok().unwrap(), "requirement");
assert_eq!(pos, 11);
Parser
trait has methods for parser combination.
use markab_parser::{
character,
character_class,
Parser,
};
// require "1" and return "1"
let parser = character('1');
// require "12" and return ("1","2")
let seq = parser.and_then(character('2'));
// require "1" or "2" and return Either("1","2")
let ord = character_class(false, &[], &['1'..'2']);
// require `0-9`+ and return as usize
let map = character_class(false, &[], &['0'..'9'])
.one_or_more()
.stringify()
.map(|num| num.parse::<usize>().unwrap());
If the parser has parameters, you should implement Parser
trait.
If the parser does not have parameters, you should implement Parseable
trait.
Parseable trait provides static method Parseable::parse
and
Parseable::get_parser
to get a parser instance for parser combination
AndParser
and NotParser
.Debug
and Display
implementations for types.