Skip to content

scala.util.parsing.combinator.JavaTokenParsers.floatingPointNumber Incorrect #1547

Closed
@scabug

Description

@scabug

The floating point parser in scala.util.parsing.combinator.JavaTokenParsers appears to be incorrect. The function in the code is:

def floatingPointNumber: Parser[String] = 
    """-?(\d+(\.\d*)?|\d*\.\d+)[eEfFdD]?([+-]?\d+)?""".r

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:

def floatingPointNumber: Parser[String] =
    """[+-]?(\d+(\.\d*)?|\d*\.\d+)([eE][+-]?\d+)?[fFdD]?""".r

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions