-
Notifications
You must be signed in to change notification settings - Fork 21
scala.util.parsing.combinator.JavaTokenParsers.floatingPointNumber Incorrect #1547
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
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1547?orig=1 |
Mohsen Lesani [X] (lesani) said: Please also supply a complete example. |
@rkuhn said: def decimalIntegerLiteral: Parser[String] =
"""-?(0|[1-9]\d*)[lL]?""".r
def octalIntegerLiteral: Parser[String] =
"""-?0[0-7]+[lL]?""".r
def hexIntegerLiteral: Parser[String] =
"""-?0[xX][0-9a-fA-F]+[lL]?""".r
def floatingPointLiteral: Parser[String] =
("-?(" +
"""(\d+\.\d*|\.\d+)([eE][+-]?\d+)?[fFdD]?|""" +
"""\d+[eE][+-]?\d+[fFdD]?|""" +
"""\d+([eE][+-]?\d+)?[fFdD]""" +
")").r The integers can be parsed using java.lang.Integer.decode(), parseInt() does not handle the radix prefix. I've broken up the float expression for readability, but I hope that this does not incur a runtime cost. |
@dcsobral said: |
@SethTisue said: Interested community members: if you consider this issue significant, feel free to open a new issue for it on GitHub, with links in both directions. |
the initial bug reported looks like it was fixed in scala/scala@b0de8aa |
The floating point parser in scala.util.parsing.combinator.JavaTokenParsers appears to be incorrect. The function in the code is:
However, this regular expression fails to correctly parse all valid floats. For example, 9e4f, which is valid in java, fails with the above regex. Even worse, it allows through invalid expressions. For example, 9+3, which is not a parsable float, is successfully captured by the above regex.
I believe the regular expression should be modified to allow trailing exponents only if "e" or "E" trails the first number sequence; to allow "D"/"d"/"F"/"f" if there is an exponent; and to allow an optional "+" at the front of the expression. I am not a regex expert, but I believe the following definition will satisfy the above requirements:
The text was updated successfully, but these errors were encountered: