From 7bc2e052c54694fea8c257516d90d80db9672322 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 21:12:26 +0200 Subject: [PATCH] C++, C, support digit separators in literals --- CHANGES | 5 ++++- sphinx/util/cfamily.py | 21 +++++++++++---------- tests/test_domain_c.py | 11 ++++++++--- tests/test_domain_cpp.py | 11 ++++++++--- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGES b/CHANGES index aafc2d3da..61d9b4945 100644 --- a/CHANGES +++ b/CHANGES @@ -59,7 +59,10 @@ Features added - ``consteval`` functions, - ``constinit`` variables, - ``char8_t``, - - ``explicit()`` specifier. + - ``explicit()`` specifier, + - digit separators in literals. + +* C, add support for digit separators in literals. Bugs fixed diff --git a/sphinx/util/cfamily.py b/sphinx/util/cfamily.py index 2be2e390f..8d0896624 100644 --- a/sphinx/util/cfamily.py +++ b/sphinx/util/cfamily.py @@ -33,10 +33,10 @@ identifier_re = re.compile(r'''(?x) ) [a-zA-Z0-9_]*\b ''') -integer_literal_re = re.compile(r'[1-9][0-9]*') -octal_literal_re = re.compile(r'0[0-7]*') -hex_literal_re = re.compile(r'0[xX][0-9a-fA-F][0-9a-fA-F]*') -binary_literal_re = re.compile(r'0[bB][01][01]*') +integer_literal_re = re.compile(r'[1-9][0-9]*(\'[0-9]+)*') +octal_literal_re = re.compile(r'0[0-7]*(\'[0-7]+)*') +hex_literal_re = re.compile(r'0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*') +binary_literal_re = re.compile(r'0[bB][01]+(\'[01]+)*') integers_literal_suffix_re = re.compile(r'''(?x) # unsigned and/or (long) long, in any order, but at least one of them ( @@ -50,13 +50,14 @@ integers_literal_suffix_re = re.compile(r'''(?x) float_literal_re = re.compile(r'''(?x) [+-]?( # decimal - ([0-9]+[eE][+-]?[0-9]+) - | ([0-9]*\.[0-9]+([eE][+-]?[0-9]+)?) - | ([0-9]+\.([eE][+-]?[0-9]+)?) + ([0-9]+(\'[0-9]+)*[eE][+-]?[0-9]+(\'[0-9]+)*) + | (([0-9]+(\'[0-9]+)*)?\.[0-9]+(\'[0-9]+)*([eE][+-]?[0-9]+(\'[0-9]+)*)?) + | ([0-9]+(\'[0-9]+)*\.([eE][+-]?[0-9]+(\'[0-9]+)*)?) # hex - | (0[xX][0-9a-fA-F]+[pP][+-]?[0-9a-fA-F]+) - | (0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+([pP][+-]?[0-9a-fA-F]+)?) - | (0[xX][0-9a-fA-F]+\.([pP][+-]?[0-9a-fA-F]+)?) + | (0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*[pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*) + | (0[xX]([0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?\. + [0-9a-fA-F]+(\'[0-9a-fA-F]+)*([pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?) + | (0[xX][0-9a-fA-F]+(\'[0-9a-fA-F]+)*\.([pP][+-]?[0-9a-fA-F]+(\'[0-9a-fA-F]+)*)?) ) ''') float_literal_suffix_re = re.compile(r'[fFlL]\b') diff --git a/tests/test_domain_c.py b/tests/test_domain_c.py index d57738ec4..575b65362 100644 --- a/tests/test_domain_c.py +++ b/tests/test_domain_c.py @@ -155,7 +155,8 @@ def test_domain_c_ast_expressions(): # primary exprCheck('true') exprCheck('false') - ints = ['5', '0', '075', '0x0123456789ABCDEF', '0XF', '0b1', '0B1'] + ints = ['5', '0', '075', '0x0123456789ABCDEF', '0XF', '0b1', '0B1', + "0b0'1'0", "00'1'2", "0x0'1'2", "1'2'3"] unsignedSuffix = ['', 'u', 'U'] longSuffix = ['', 'l', 'L', 'll', 'LL'] for i in ints: @@ -170,14 +171,18 @@ def test_domain_c_ast_expressions(): '5e42', '5e+42', '5e-42', '5.', '5.e42', '5.e+42', '5.e-42', '.5', '.5e42', '.5e+42', '.5e-42', - '5.0', '5.0e42', '5.0e+42', '5.0e-42']: + '5.0', '5.0e42', '5.0e+42', '5.0e-42', + "1'2'3e7'8'9", "1'2'3.e7'8'9", + ".4'5'6e7'8'9", "1'2'3.4'5'6e7'8'9"]: expr = e + suffix exprCheck(expr) for e in [ 'ApF', 'Ap+F', 'Ap-F', 'A.', 'A.pF', 'A.p+F', 'A.p-F', '.A', '.ApF', '.Ap+F', '.Ap-F', - 'A.B', 'A.BpF', 'A.Bp+F', 'A.Bp-F']: + 'A.B', 'A.BpF', 'A.Bp+F', 'A.Bp-F', + "A'B'Cp1'2'3", "A'B'C.p1'2'3", + ".D'E'Fp1'2'3", "A'B'C.D'E'Fp1'2'3"]: expr = "0x" + e + suffix exprCheck(expr) exprCheck('"abc\\"cba"') # string diff --git a/tests/test_domain_cpp.py b/tests/test_domain_cpp.py index a4990e71f..cf2774af4 100644 --- a/tests/test_domain_cpp.py +++ b/tests/test_domain_cpp.py @@ -174,7 +174,8 @@ def test_domain_cpp_ast_expressions(): exprCheck('nullptr', 'LDnE') exprCheck('true', 'L1E') exprCheck('false', 'L0E') - ints = ['5', '0', '075', '0x0123456789ABCDEF', '0XF', '0b1', '0B1'] + ints = ['5', '0', '075', '0x0123456789ABCDEF', '0XF', '0b1', '0B1', + "0b0'1'0", "00'1'2", "0x0'1'2", "1'2'3"] unsignedSuffix = ['', 'u', 'U'] longSuffix = ['', 'l', 'L', 'll', 'LL'] for i in ints: @@ -187,11 +188,15 @@ def test_domain_cpp_ast_expressions(): decimalFloats = ['5e42', '5e+42', '5e-42', '5.', '5.e42', '5.e+42', '5.e-42', '.5', '.5e42', '.5e+42', '.5e-42', - '5.0', '5.0e42', '5.0e+42', '5.0e-42'] + '5.0', '5.0e42', '5.0e+42', '5.0e-42', + "1'2'3e7'8'9", "1'2'3.e7'8'9", + ".4'5'6e7'8'9", "1'2'3.4'5'6e7'8'9"] hexFloats = ['ApF', 'Ap+F', 'Ap-F', 'A.', 'A.pF', 'A.p+F', 'A.p-F', '.A', '.ApF', '.Ap+F', '.Ap-F', - 'A.B', 'A.BpF', 'A.Bp+F', 'A.Bp-F'] + 'A.B', 'A.BpF', 'A.Bp+F', 'A.Bp-F', + "A'B'Cp1'2'3", "A'B'C.p1'2'3", + ".D'E'Fp1'2'3", "A'B'C.D'E'Fp1'2'3"] for suffix in ['', 'f', 'F', 'l', 'L']: for e in decimalFloats: expr = e + suffix