Skip to content

Commit

Permalink
Accept floating-point NaN and Inf literals (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwpeterson committed Feb 25, 2024
1 parent 63ace0b commit 640d75b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/HowToUsePyparsing.rst
Expand Up @@ -1367,6 +1367,8 @@ Common string and token constants

- ``common.fnumber`` - any numeric expression; parsed tokens are converted to float

- ``common.ieee_float`` - any floating-point literal (int, real number, infinity, or NaN), returned as float

- ``common.identifier`` - a programming identifier (follows Python's syntax convention of leading alpha or "_",
followed by 0 or more alpha, num, or "_")

Expand Down
7 changes: 7 additions & 0 deletions pyparsing/common.py
Expand Up @@ -216,6 +216,13 @@ class pyparsing_common:
)
"""any int or real number, returned as float"""

ieee_float = (
Regex(r"(?i)[+-]?((\d+\.?\d*(e[+-]?\d+)?)|nan|inf(inity)?)")
.set_name("ieee_float")
.set_parse_action(convert_to_float)
)
"""any floating-point literal (int, real number, infinity, or NaN), returned as float"""

identifier = Word(identchars, identbodychars).set_name("identifier")
"""typical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_')"""

Expand Down
17 changes: 17 additions & 0 deletions tests/test_unit.py
Expand Up @@ -6599,6 +6599,23 @@ def testCommonExpressions(self):
)[0]
self.assertTrue(success, "error in parsing valid numerics")

with self.subTest("ppc.ieee_float success run_tests"):
success = ppc.ieee_float.runTests(
"""
100
3.14159
6.02e23
1E-12
0
-0
NaN
-nan
inf
-Infinity
"""
)[0]
self.assertTrue(success, "error in parsing valid floating-point literals")

with self.subTest("ppc.iso8601_date success run_tests"):
success, results = ppc.iso8601_date.runTests(
"""
Expand Down

0 comments on commit 640d75b

Please sign in to comment.