私は多くのオプションの1つである可能性のあるテキストを解析するnomを持つパーサーを作成しようとしています。コンパイル時に値がわかっているのにNomにはalt!
がありますが、私の値はそうではありません。どの文字列のベクトルをnomと一致させるには?
これは、一致するように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
私は自分の関数からパーサーを作成できますか?そして、alternative_wrapper
のtag!
のような既存のパーサーをどうすれば使用できますか?