Skip to content

Commit

Permalink
Accept floating-point nan and inf as fnumber
Browse files Browse the repository at this point in the history
  • Loading branch information
rwpeterson committed Jan 21, 2024
1 parent 2a1a8e8 commit e029f0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/HowToUsePyparsing.rst
Expand Up @@ -1365,7 +1365,7 @@ Common string and token constants

- ``common.number`` - any numeric expression; parsed tokens are returned as converted by the matched expression

- ``common.fnumber`` - any numeric expression; parsed tokens are converted to float
- ``common.fnumber`` - any numeric expression; parsed tokens are converted to float (including floating-point nan and inf)

- ``common.identifier`` - a programming identifier (follows Python's syntax convention of leading alpha or "_",
followed by 0 or more alpha, num, or "_")
Expand Down
15 changes: 11 additions & 4 deletions pyparsing/common.py
Expand Up @@ -43,13 +43,20 @@ class pyparsing_common:
''')
pyparsing_common.fnumber.run_tests('''
# any int or real number, returned as float
# any numeric expression, returned as float (including floating-point nan and inf)
100
-100
+100
3.14159
6.02e23
1e-12
1E-12
0
-0
NaN
nan
-NAN
inf
-Infinity
''')
pyparsing_common.hex_integer.run_tests('''
Expand Down Expand Up @@ -210,11 +217,11 @@ class pyparsing_common:
"""any numeric expression, returns the corresponding Python type"""

fnumber = (
Regex(r"[+-]?\d+\.?\d*([eE][+-]?\d+)?")
Regex(r"(?i)[+-]?((\d+\.?\d*(e[+-]?\d+)?)|nan|inf(inity)?)")
.set_name("fnumber")
.set_parse_action(convert_to_float)
)
"""any int or real number, returned as float"""
"""any numeric expression, returned as float (including floating-point nan and inf)"""

identifier = Word(identchars, identbodychars).set_name("identifier")
"""typical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_')"""
Expand Down
9 changes: 8 additions & 1 deletion tests/test_unit.py
Expand Up @@ -6594,7 +6594,14 @@ def testCommonExpressions(self):
+100
3.14159
6.02e23
1e-12
1E-12
0
-0
NaN
nan
-NAN
inf
-Infinity
"""
)[0]
self.assertTrue(success, "error in parsing valid numerics")
Expand Down

0 comments on commit e029f0c

Please sign in to comment.