lexy
lexy copied to clipboard
dsl for real numbers
I am new to the library so I apologize if I am missing something.
Exploring the examples I can't find anything similar to dsl::integer
for real numbers.
O course I understand that parsing real numbers is complicated having a lot of different representations, but I would need something simple such as 5.67 to start with.
My application is an AST similar to the calculator example.
I plan on adding that in the future.
What I'm doing right now as a workaround is to manually parse digits + period + digits
, capture that and call std::stod
/std::from_chars
on the lexeme.
Would you mind adding a code snippet ? (I am trying to do the same)
https://godbolt.org/z/1WExWsod9
If you want to use a different decimal separator, you need to capture each dsl::digits
part individually and then manually build a string in the format expected by std::stod
.
Thanks. You saved me a ton of time :smile: .
I am still learning, and I would have needed a lot of time to figure this out.
I'm reopening this as I do want to add it properly.
I am using type erased numbers (for the sake of simplicity, let's assume it is std::variant<int,double>
).
The matching doesn't seem to work as expected if I do this: https://godbolt.org/z/jWG4zoKTP
test string would be:
-
-7
-> expects int: -7 -
3.14
-> expects double: 3.14 -
1e2
-> expects double: 100 -
0x7
-> expects int: 7 -
0x7e8
-> expects error
My point is that if you are implementing dsl::real
, it would be interesting to use it alongside dsl::integer
.
I am realizing that what I should probably do is similar to ast::json_number
in the json.cpp example
The matching doesn't seem to work as expected if I do this: https://godbolt.org/z/jWG4zoKTP
Choice is ordered and takes the first branch that matches. When it sees 5.3
dsl::integer
matches so that is parsed; it then leaves .3
unconsumed. I recommend attempting to match the real number first and ensuring that it does not match integers only: https://godbolt.org/z/dn83sxqdG
Please ask more questions in the discussions in the future.