I'm trying to write a markdown parser using nom
and the EBNF shown here. I'm a little stuck as how to express begins with non-zero digit when trying to parse a number.
Number = NonZeroDigit { Digit };
NonZeroDigit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
Digit = "0" | NonZeroDigit;
TLDR: How do you implement Number = NonZeroDigit { Digit };
My current implementation looks like
fn number(s:&[u8]) -> IResult<&[u8], &[u8]> {
let (_, head) = peek(1)(s)?
if (is_non_zero_digit(head[0])) {
return digit1(s)
} else {
return make_error(s, kind: ErrorKind.IsA)
}
}
This one also seems a bit more simple and in line with nom
.
fn number(s:&[u8]) -> IResult<&[u8], &[u8]> {
let (_, head) = peek(1)(s)?
cond(is_non_zero_digit(head[0]),digit1)(s)
}
I'm not sure if I want to be throwing an error of some sort? Because it could just be that this is not a number, but some kind of string like a date or time. Perhaps it doesn't even make sense to include the notion of a Number
in the grammar for markdown?