pub fn keyword<'kw>(kw: &'kw str) -> impl Fn(&str) -> IResult<&str, &str> + 'kw
Expand description

Parser builder for making keyword parsers

keyword is not a parser on its own. Instead it can be called to create and return parsers that can be used to match keywords.

Example:

let function_kw_parser = keyword("function");
let while_kw_parser = keyword("while");

assert!(function_kw_parser("function f(x) ...").is_ok()); // succeeds, "function" is matched
assert!(while_kw_parser("function f(x) ...").is_err()); // fails, "while" not matched
assert!(function_kw_parser("x + 1").is_err()); // fails, "function" not matched

Lifetime:

Note: The lifetime of the resulting parsers are bound by the 'kw lifetime of the input string.

let test_parser = {
  let test_kw: String = "test".to_owned(); // test_kw is dropped at the end of this block
  keyword(&test_kw) // Borrow check error here: the returned parser would outlive `test_kw`
};
test_parser("test");