Using Repeat in seq! or a better way to do what im trying to do #615
-
Hi, I'm fairly new to using Winnow and parser combinators in general, so I'm not sure how to deal with the errors that I'm facing. What I am trying to parse at the moment is the following:
Using the following functions: pub fn Fields<'a>(input: &mut TokenSlice<'a>) -> Result<Vec<String>, ErrMode<ContextError>> {
let field = seq!(identifier, Fieldsep, Type, opt(Comma))
.take()
.map(|t| {
let mut res_vec = Vec::new();
for f in t.iter() {
res_vec.push(f.text.to_string());
}
res_vec
})
.parse_next(input);
return field;
}
pub fn FunCall<'a>(input: &mut TokenSlice<'a>) -> Result<Vec<String>, ErrMode<ContextError>> {
let res: Result<Vec<String>, ErrMode<ContextError>> = seq!(
identifier,
OpenParen,
repeat(0.., Fields).take(),
CloseParen,
SemiColon
)
.take()
.map(|t| {
let mut res_vec = Vec::new();
for f in t.iter() {
res_vec.push(f.text.to_string());
}
res_vec
})
.parse_next(input);
return res;
} When the second field in the cat function call is not there and the repeat in the FunCall function is replaced with just Fields it works. However, when they are there, I get this unhelpful error, which repeats multiple times:
So I'm just wondering what I'm missing, as the error mentions missing type annotations. However, I'm unsure how to declare them, as declaring it at the start of the variable didn't help. Or if I'm just missing something, or if there is a better way to do it |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What you can do is |
Beta Was this translation helpful? Give feedback.
repeat
s output is generic buttake()
ignores the output and provides a different one, so the compiler can't determine what type to use forrepeat
. Adding a type annotation tores
doesn't work because amap
operation happens.map
doesn't work because that is appying to anothertake()
.What you can do is
repeat(...).map(|()| ()).take()