我正在尝试使用 nom 创建一个解析器,它将解析一些可能是许多选项之一的文本。当值在编译时已知时, Nom 具有alt!
,但我的值不会。
这是我尝试创建自己的解析器Vec<String>
来匹配的,我遇到了几个问题。
#[macro_use]
extern crate nom;
use nom::IResult;
fn alternative_wrapper<'a>(input: &'a [u8], alternatives: Vec<String>) -> IResult<&'a [u8], &'a [u8]> {
for alternative in alternatives {
// tag!("alternative");
println!("{}", alternative);
}
return IResult::Done(input, "test".as_bytes());
}
#[test]
fn test_date() {
let input = "May";
named!(alternative, call!(alternative_wrapper));
let months = vec!(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
).iter().map(|s| s.to_string()).collect();
println!("{:?}", alternative("May".as_bytes(), months));
}
我知道我的alternative_wrapper
函数实际上并没有做任何有用的事情,但这不是问题。这就是 Rust 抱怨这个片段的原因:
error[E0061]: this function takes 1 parameter but 2 parameters were supplied
--> src/parser.rs:32:34
|
17 | named!(alternative, call!(alternative_wrapper));
| ------------------------------------------------ defined here
...
32 | println!("{:?}", alternative("May".as_bytes(), months));
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter
|
= note: this error originates in a macro outside of the current crate
error[E0061]: this function takes 2 parameters but 1 parameter was supplied
--> src/parser.rs:17:5
|
6 | / fn alternative_wrapper<'a>(input: &'a [u8], alternatives: Vec<String>) -> IResult<&'a [u8], &'a
[u8]> {
7 | | for alternative in alternatives {
8 | | // tag!("alternative");
9 | | println!("{}", alternative);
10 | | }
11 | | return IResult::Done(input, "test".as_bytes());
12 | | }
| |_- defined here
...
17 | named!(alternative, call!(alternative_wrapper));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 2 parameters
|
= note: this error originates in a macro outside of the current crate
如何从我的函数中创建解析器?以及如何使用现有的解析器,例如tag!
from inside alternative_wrapper
?