Skip to content
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

Accept floating-point NaN and Inf as fnumber #538

Merged
merged 1 commit into from Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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