2

我想解析一个字符串,其中包含单引号之间的 ASCII 字符,并且可以连续包含两个 ' 的转义单引号。

'单引号之间包含的字符串值 -> '' 等等...'

这应该导致:

包含在单引号之间的字符串值 -> ' 等等...

use nom::{
    bytes::complete::{tag, take_while},
    error::{ErrorKind, ParseError},
    sequence::delimited,
    IResult,
};

fn main() {
    let res = string_value::<(&str, ErrorKind)>("'abc''def'");

    assert_eq!(res, Ok(("", "abc\'def")));
}

pub fn is_ascii_char(chr: char) -> bool {
    chr.is_ascii()
}

fn string_value<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
    delimited(tag("'"), take_while(is_ascii_char), tag("'"))(i)
}

如何检测转义引号而不是字符串的结尾?

4

2 回答 2

5

这非常棘手,但以下工作:

//# nom = "5.0.1"
use nom::{
    bytes::complete::{escaped_transform, tag},
    character::complete::none_of,
    combinator::{recognize, map_parser},
    multi::{many0, separated_list},
    sequence::delimited,
    IResult,
};

fn main() {
    let (_, res) = parse_quoted("'abc''def'").unwrap();
    assert_eq!(res, "abc'def");
    let (_, res) = parse_quoted("'xy@$%!z'").unwrap();
    assert_eq!(res, "xy@$%!z");
    let (_, res) = parse_quoted("'single quotes -> '' and so on...'").unwrap();
    assert_eq!(res, "single quotes -> ' and so on...");
}

fn parse_quoted(input: &str) -> IResult<&str, String> {
    let seq = recognize(separated_list(tag("''"), many0(none_of("'"))));
    let unquote = escaped_transform(none_of("'"), '\'', tag("'"));
    let res = delimited(tag("'"), map_parser(seq, unquote), tag("'"))(input)?;

    Ok(res)
}

一些解释:

  1. 解析器seq识别在双引号和其他任何东西之间交替的任何序列;
  2. unquote将任何双引号转换为单引号;
  3. map_parser然后将两者结合在一起以产生所需的结果。

请注意,由于使用了escaped_transform组合器,解析结果String不是&str. 即,有额外的分配。

于 2019-10-23T10:32:39.600 回答
0

我正在学习 nom,下面是我的尝试。

let a = r###"'string value contained between single quotes -> '' and so on...'"###;

fn parser(input: &str) -> IResult<&str, &str> {
    let len = input.chars().count() - 2;
    delimited(tag("'"), take(len), tag("'"))(input)
}

let (remaining, mut matched) = parser(a).unwrap_or_default();

let sss = matched.replace("''", "'");
matched = &sss;
println!("remaining: {:#?}", remaining);
println!("matched: {:#?}", matched);

它打印这个结果:

remaining: ""
matched: "string value contained between single quotes -> ' and so on..."

我的测试基于 nom 6.2.1。

于 2021-08-16T00:13:26.750 回答