-
Hello! How can I create a standalone parser for floating-point values? e.g., val double: Parsley[Double] = ??? I'm porting my Advent of Code solutions over to Scala and building a foundation for this year's Advent of Code. So, I would like to have a simple parser for floating-point values on-hand, just in case. I see that I could create a parser for floating-point values by creating a lexer (as described here https://github.com/j-mie6/Parsley/wiki/Building-a-Parser-for-Haskell#lexing), but this feels a bit over-kill for my use-case. Is this the best way to approach this with Parsley or is there a simpler way? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The val lexer = new Lexer(LexicalDesc.plain)
val double: Parsley[Double] = lexer.nonlexeme.numeric.floating.doubleRounded // or just `double` if you want it to ensure it's a valid Java Double Even with the default configuration this is probably good for most needs ( |
Beta Was this translation helpful? Give feedback.
The
Lexer
is absolutely the simplest way to do this in terms of LOC:Even with the default configuration this is probably good for most needs (
nonlexeme
here to not consume any whitespace).If you don't want that, you'd just have to implement your own floating point parser, which isn't too hard but is more work than this solution for sure 😛