C++, C, support digit separators in literals

This commit is contained in:
Jakob Lykke Andersen
2021-06-24 21:12:26 +02:00
parent 799c53aa11
commit 7bc2e052c5
4 changed files with 31 additions and 17 deletions

View File

@@ -59,7 +59,10 @@ Features added
- ``consteval`` functions,
- ``constinit`` variables,
- ``char8_t``,
- ``explicit(<constant expression>)`` specifier.
- ``explicit(<constant expression>)`` specifier,
- digit separators in literals.
* C, add support for digit separators in literals.
Bugs fixed

View File

@@ -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')

View File

@@ -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

View File

@@ -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