Is matching EOF considered a "consume" for the repeat combinator? #523
-
Hi, My experience with winnow is limited to a couple of days so perhaps I've missed something, or there's a better way to do what I want to do. I wish to match on a "blank line", and to be clear I mean:
The problem I'm having is that even though this seems fairly simple: enum Expression {
BlankLine,
// ... other types of lines
}
fn blank_line(i: &mut &str) -> PResult<Expression> {
(
space0,
alt((line_ending, eof)),
)
.map(|_| Expression::BlankLine)
.parse_next(i)
} this does not seem to work with the fn expression(i: &mut &str) -> PResult<Expression> {
alt((
// ... other parsers for other types of lines
blank_line,
))
.parse_next(i)
}
fn file(i: &mut &str) -> PResult<Vec<Expression>> {
repeat(0.., expression).parse_next(i)
}
I think this is because the I'm also not sure if this error is due a check performed when this condition arises (if ever), or if the check is done based on what might happen by examining the parser tree. Is there a workaround? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Yes, One way to workaround this would be enum Expression {
BlankLine,
// ... other types of lines
}
fn blank_line(i: &mut &str) -> PResult<Expression> {
alt((
(space0, line_ending),
(space1, eof),
))
.map(|_| Expression::BlankLine)
.parse_next(i)
} This could be optimized as enum Expression {
BlankLine,
// ... other types of lines
}
fn blank_line(i: &mut &str) -> PResult<Expression> {
alt((
line_ending,
(space1, alt((line_ending, eof))),
))
.map(|_| Expression::BlankLine)
.parse_next(i)
} There might be additional optimizations that could be made with |
Beta Was this translation helpful? Give feedback.
-
As for your problem, you have "repeat(line_w_newline or line_wo_newline)", which implies that you could have "<line_wo_newline><line_w_newline>", which makes no sense. So what you need is "repeat(line_w_newline) then maybe line_wo_newline" (as you said yourself in your BNF grammar) But probably a better way of thinking is to not see the newline character as part of the line, but as a separator (see |
Beta Was this translation helpful? Give feedback.
eof
doesn't not consume because it can't, by definition. It means "end-of-file", i.e "there-is-nothing-left-to-consume".As for your problem, you have "repeat(line_w_newline or line_wo_newline)", which implies that you could have "<line_wo_newline><line_w_newline>", which makes no sense. So what you need is "repeat(line_w_newline) then maybe line_wo_newline" (as you said yourself in your BNF grammar)
But probably a better way of thinking is to not see the newline character as part of the line, but as a separator (see
separated()
)