diff --git a/docs/HowToUsePyparsing.rst b/docs/HowToUsePyparsing.rst index e38a2df6..fa0093ef 100644 --- a/docs/HowToUsePyparsing.rst +++ b/docs/HowToUsePyparsing.rst @@ -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 "_") diff --git a/pyparsing/common.py b/pyparsing/common.py index 72875d1d..74faa460 100644 --- a/pyparsing/common.py +++ b/pyparsing/common.py @@ -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 '_')""" diff --git a/tests/test_unit.py b/tests/test_unit.py index 8b2ae5ad..349fc486 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -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( """