温馨提示:本文翻译自stackoverflow.com,查看原文请点击:parsing - Match a slug with Nom
parsing rust slug nom

parsing - 与Nom匹配a

发布于 2020-04-09 23:16:56

我已经尝试了一段时间,为Nom找到一个不错的解决方案,以便将Nolu识别为一个插件alpha1所以我可以解析这样的东西

fn parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
  delimited(char(':'), slug, char(':'))(text)
}

assert!(
  parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:"),
  "hello-world-i-only-accept-alpha-numeric-char-and-dashes"
);

我尝试了类似的方法,但似乎不起作用。

fn slug<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
    T: InputTakeAtPosition,
    <T as InputTakeAtPosition>::Item: AsChar + Clone,
{
    input.split_at_position1(
        |item| {
            let c = item.clone().as_char();

            !(item.is_alpha() || c == '-')
        },
        ErrorKind::Char,
    )
}

PS:您知道如何告诉Nom ug中的“-”一定不能在开头或结尾吗?

查看更多

提问者
HelloEdit
被浏览
103
edwardw 2020-02-01 15:59

正是nom::multi::separated_list为了这个。而且由于您希望结果本身是字符串而不是段向量,因此将其与之结合nom::combinator::recognize将达到目的:

use std::error::Error;
use nom::{
    IResult,
    character::complete::{alphanumeric1, char},
    combinator::recognize,
    multi::separated_list,
    sequence::delimited,
};

fn slug_parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
    let slug = separated_list(char('-'), alphanumeric1);
    delimited(char(':'), recognize(slug), char(':'))(text)
}

fn main() -> Result<(), Box<dyn Error>> {
    let (_, res) = slug_parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:")?;
    assert_eq!(
      res,
      "hello-world-i-only-accept-alpha-numeric-char-and-dashes"
    );

    Ok(())
}