New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing f32 in a single rule #218
Comments
Just found #97. |
Actually, can I leave this open till it's documented? |
I'm not sure if this is what you mean by "expression groups", but the
|
I meant a la Regex, so you can add modifiers to a set. This is useful for repeating more complicated patterns. So |
That basically works the same in PEG: When converting regex to PEG, the main difference is that |
Oh, I noticed that the documentation doesn't explicitly call out that parentheses work for grouping / overriding precedence. Fixed in d06b88c. |
Great, thank you! |
For anyone coming here looking for the answer, I believe it would look like this. The bit that escaped me was the meaning of "Action blocks", and the fact I could use them inside parentheses expressions. Please ignore the terrible code actually parsing into a float. peg::parser! {
grammar float_parser() for str {
rule integer() -> u32
= i:$(['0'..='9']+) {? i.parse().or(Err("u32")) }
rule float() -> f64
= i:integer() d:("." d:integer() { d })? {?
if let Some(d) = d {
format!("{i}.{d}").parse().or(Err("f64"))
} else {
Ok(i as f64)
}
}
}
} Note how |
@BenoitRanque That technique to choose which value to return from a parenthesized sequence is useful in other cases, but it's unnecessary here, as is putting the string back together with I also wouldn't use an |
I'm trying to describe this regex
\d+(\.\d+)?
within a single rule, so I can support floats. I can't figure out how to express this without creating an entirely separate rule.The primary issue seems to be the lack of expression groups, which would enable this:
integer() ("." integer())?
Without expression groups, I thought maybe a delimiter with a restriction of a single repetition (
integer() ** .
but limited to two elements).If there's an existing solution, that would be ideal.
The text was updated successfully, but these errors were encountered: