3

我正在尝试熟悉 Nom,目前是第 5 版,其中没有CompleteStr其他内容,因此相关问题没有太大帮助。

我怎样才能解析类似的东西

"@pook Some free text @another_pook And another text"

进入

vec![("pook", "Some free text"), ("another_pook", "And another text")]

?

@前置字符串称为“字段标识符”;下一个子字符串是描述;两者都称为“场”

这是我成功解析一个字段的方法:

use nom::bytes::complete::take_while1;
use nom::*;
use nom::character::is_alphabetic;

fn ident(c: char) -> bool {
    is_alphabetic(c as u8) || c == '_'
}

fn freetext(c: char) -> bool {
    c != '@'
}

fn parse_ident(s: &str) -> IResult<&str, &str> {
    take_while1(ident)(s)
}

fn parse_freetext(s: &str) -> IResult<&str, &str> {
    take_while1(freetext)(s)
}


named! {field_ident<&str, &str>,
    do_parse!(
        tag!("@") >>
        name: parse_ident >>
        (name)
    )
}

named! { field <&str, (&str, &str)>,
    do_parse!(
        name: ws!(field_ident) >>
        description: parse_freetext >>
        (name, description)
    )
}

当我按照开头所述将其包装many1并提供输入时,我收到了Err(Incomplete(Size(1))),但是如果我将其放在@输入的末尾,它就可以工作。如何在输入结束时将其标记为已完成?

4

1 回答 1

0

You want many_till combinator, not many1, like so:

use nom::bytes::complete::take_while1;
use nom::character::is_alphabetic;
use nom::*;

fn ident(c: char) -> bool {
    is_alphabetic(c as u8) || c == '_'
}

fn freetext(c: char) -> bool {
    c != '@'
}

fn parse_ident(s: &str) -> IResult<&str, &str> {
    take_while1(ident)(s)
}

fn parse_freetext(s: &str) -> IResult<&str, &str> {
    take_while1(freetext)(s)
}

named! {field_ident<&str, &str>,
    do_parse!(
        tag!("@") >>
        name: parse_ident >>
        (name)
    )
}

named! { field <&str, (&str, &str)>,
    do_parse!(
        name: ws!(field_ident) >>
        description: parse_freetext >>
        (name, description)
    )
}

named!(fields<&str, (Vec<(&str, &str)>, &str)>, many_till!(field, eof!()));

fn main() {
    println!("{:?}", field("@pook Some free text"));
    println!(
        "{:?}",
        fields("@pook Some free text @another_pook And another text")
    );
}

Rather counter-intuitive. It has to do with the streaming nature of nom, I guess.

于 2019-08-22T14:53:32.970 回答