如何在 Nom 中解析大写字符串?
How do I parse uppercase strings in Nom?
我在 Nom 5 中使用函数而不是宏编写解析器。我的目标是编写一个解析器来识别完全由大写字符组成的字符串。理想情况下,它将具有与 alpha1.
相同的 return 签名
use nom::{
character::complete::{alpha1, char, line_ending, not_line_ending},
combinator::{cut, map, not, recognize},
error::{context, ParseError, VerboseError},
multi::{many0, many1},
IResult,
};
fn uppercase_char<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
take_while(move |c| chars.contains(c))(i)
}
// Matches 1 or more consecutive uppercase characters
fn upper1<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
recognize(many1(uppercase_char))(i)
}
虽然编译通过了,但我写的简单单元测试失败了:
#[test]
fn test_upper_string_ok() {
let input_text = "ADAM";
let output = upper1::<VerboseError<&str>>(input_text);
dbg!(&output);
let expected = Ok(("ADAM", ""));
assert_eq!(output, expected);
}
失败输出为
---- parse::tests::test_upper_string_ok stdout ----
[src/parse.rs:110] &output = Err(
Error(
VerboseError {
errors: [
(
"",
Nom(
Many1,
),
),
],
},
),
)
thread 'parse::tests::test_upper_string_ok' panicked at 'assertion failed: `(left == right)`
left: `Err(Error(VerboseError { errors: [("", Nom(Many1))] }))`,
right: `Ok(("ADAM", ""))`', src/parse.rs:112:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
take_while
将识别 0 个或多个字符,因此当像您一样在 many1
内部使用时,它将首先解析整个 "ADAM"
字符串。然后当 many1
再次调用它时,由于 take_while
可以识别一个空字符串,它会成功,但是 many0
和 many1
有一个防止错误的保护:如果底层解析器没有消耗任何输入,他们会return一个错误。
你需要的,uppercase_char
功能应该就够了,不需要recognize
和many1
。尽管您可能希望将 take_while
替换为 take_while1
我在 Nom 5 中使用函数而不是宏编写解析器。我的目标是编写一个解析器来识别完全由大写字符组成的字符串。理想情况下,它将具有与 alpha1.
相同的 return 签名use nom::{
character::complete::{alpha1, char, line_ending, not_line_ending},
combinator::{cut, map, not, recognize},
error::{context, ParseError, VerboseError},
multi::{many0, many1},
IResult,
};
fn uppercase_char<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
take_while(move |c| chars.contains(c))(i)
}
// Matches 1 or more consecutive uppercase characters
fn upper1<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
recognize(many1(uppercase_char))(i)
}
虽然编译通过了,但我写的简单单元测试失败了:
#[test]
fn test_upper_string_ok() {
let input_text = "ADAM";
let output = upper1::<VerboseError<&str>>(input_text);
dbg!(&output);
let expected = Ok(("ADAM", ""));
assert_eq!(output, expected);
}
失败输出为
---- parse::tests::test_upper_string_ok stdout ----
[src/parse.rs:110] &output = Err(
Error(
VerboseError {
errors: [
(
"",
Nom(
Many1,
),
),
],
},
),
)
thread 'parse::tests::test_upper_string_ok' panicked at 'assertion failed: `(left == right)`
left: `Err(Error(VerboseError { errors: [("", Nom(Many1))] }))`,
right: `Ok(("ADAM", ""))`', src/parse.rs:112:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
take_while
将识别 0 个或多个字符,因此当像您一样在 many1
内部使用时,它将首先解析整个 "ADAM"
字符串。然后当 many1
再次调用它时,由于 take_while
可以识别一个空字符串,它会成功,但是 many0
和 many1
有一个防止错误的保护:如果底层解析器没有消耗任何输入,他们会return一个错误。
你需要的,uppercase_char
功能应该就够了,不需要recognize
和many1
。尽管您可能希望将 take_while
替换为 take_while1