oni_comb_parser_rs::prelude

Function take_while1

Source
pub fn take_while1<'a, I, F>(f: F) -> Parser<'a, I, &'a [I]>
where F: Fn(&I) -> bool + 'a, I: Element + Debug + 'a,
Expand description

クロージャの結果が真である間は要素を返すParserを返す。
Returns a Parser that returns elements, while the result of the closure is true.

解析結果の長さは1要素以上必要です。
The length of the analysis result must be at least one element.

§Example

use std::iter::FromIterator;
use oni_comb_parser_rs::prelude::*;

let text: &str = "abcdef";
let input = text.chars().collect::<Vec<_>>();

let parser: Parser<char, String> = take_while1(|e| match *e {
 'a'..='c' => true,
  _ => false
}).map(String::from_iter);

let result: ParseResult<char, String> = parser.parse(&input);

assert!(result.is_success());
assert_eq!(result.success().unwrap(), "abc");
use std::iter::FromIterator;
use oni_comb_parser_rs::prelude::*;

let text: &str = "def";
let input = text.chars().collect::<Vec<_>>();

let parser: Parser<char, String> = take_while1(|e| match *e {
 'a'..='c' => true,
  _ => false
}).map(String::from_iter);

let result: ParseResult<char, String> = parser.parse(&input);

assert!(result.is_failure());
assert_eq!(result.failure().unwrap(), ParseError::of_in_complete());