gpt4 book ai didi

rust - 使用 Nom 5 解析带有转义引号的单引号字符串

转载 作者:行者123 更新时间:2023-11-29 08:28:10 25 4
gpt4 key购买 nike

我是 Rust 和 Nom 的新手,我正在尝试解析可能包含转义引号的(单)引号字符串,例如'foo\' 🤖 bar''λx → x' , ''' ' .

我找到了 escaped!宏,其documentation说:

The first argument matches the normal characters (it must not accept the control character), the second argument is the control character (like \ in most languages), the third argument matches the escaped characters

由于我想在匹配器中为“普通字符”匹配反斜杠以外的任何内容,我尝试使用 take_till! :

    named!(till_backslash<&str, &str>, take_till!(|ch| ch == '\\'));
named!(esc<&str, &str>, escaped!(call!(till_backslash), '\\', one_of!("'n\\")));

let (input, _) = nom::character::complete::char('\'')(input)?;
let (input, value) = esc(input)?;
let (input, _) = nom::character::complete::char('\'')(input)?;

// … use `value`

但是,在尝试解析 'x' 时,这将返回 Err(Incomplete(Size(1))) .搜索此内容时,人们通常建议使用 CompleteStr ,但这不在 Nom 5 中。解决这个问题的正确方法是什么?

最佳答案

当以所谓的流模式运行时,nom 可能会返回Incomplete 以表明它无法决定并需要更多数据。 nom 4 引入了CompleteStr。与 CompleteByteSlice 一起,它们是 &str&[u8] 的完整输入副本。解析器将它们作为完整模式的输入工作。

它们在 nom 5 中消失了。在 nom 5 中,正如您所观察到的,基于宏的解析器始终以流模式工作。对于在流模式和完整模式下工作方式不同的解析器组合器,它们在单独的子模块中有不同的版本,例如 nom::bytes::streamingnom::bytes: :完成

对于所有这些血淋淋的细节,您可能需要查看 this blog post ,尤其是流式处理与完整解析器部分。

此外,在 nom 5 中,函数组合子优于宏组合子。这是一种实现方法:

//# nom = "5.0.1"
use nom::{
branch::alt,
bytes::complete::{escaped, tag},
character::complete::none_of,
sequence::delimited,
IResult,
};

fn main() {
let (_, res) = parse_quoted(r#"'foo\' 🤖 bar'"#).unwrap();
assert_eq!(res, r#"foo\' 🤖 bar"#);
let (_, res) = parse_quoted("'λx → x'").unwrap();
assert_eq!(res, "λx → x");
let (_, res) = parse_quoted("' '").unwrap();
assert_eq!(res, " ");
let (_, res) = parse_quoted("''").unwrap();
assert_eq!(res, "");
}

fn parse_quoted(input: &str) -> IResult<&str, &str> {
let esc = escaped(none_of("\\\'"), '\\', tag("'"));
let esc_or_empty = alt((esc, tag("")));
let res = delimited(tag("'"), esc_or_empty, tag("'"))(input)?;

Ok(res)
}

关于rust - 使用 Nom 5 解析带有转义引号的单引号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com