Skip to content

Commit 7aac68c

Browse files
authored
Merge pull request #97 from guineawheek/fix_cpp_int_literals
Allow int literals to be separated with single quotes
2 parents 29b71ab + 2fe8a88 commit 7aac68c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

cxxheaderparser/lexer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,18 @@ class PlyLexer:
225225
#
226226

227227
hex_prefix = "0[xX]"
228-
hex_digits = "[0-9a-fA-F]+"
228+
hex_digits = "[0-9a-fA-F']+"
229229
bin_prefix = "0[bB]"
230-
bin_digits = "[01]+"
230+
bin_digits = "[01']+"
231231

232232
# integer constants (K&R2: A.2.5.1)
233233
integer_suffix_opt = (
234234
r"(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?"
235235
)
236236
decimal_constant = (
237-
"(0" + integer_suffix_opt + ")|([1-9][0-9]*" + integer_suffix_opt + ")"
237+
"(0" + integer_suffix_opt + ")|([1-9][0-9']*" + integer_suffix_opt + ")"
238238
)
239-
octal_constant = "0[0-7]*" + integer_suffix_opt
239+
octal_constant = "0[0-7']*" + integer_suffix_opt
240240
hex_constant = hex_prefix + hex_digits + integer_suffix_opt
241241
bin_constant = bin_prefix + bin_digits + integer_suffix_opt
242242

tests/test_numeric_literals.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Note: testcases generated via `python -m cxxheaderparser.gentest`
2+
3+
from cxxheaderparser.types import (
4+
FundamentalSpecifier,
5+
NameSpecifier,
6+
PQName,
7+
Token,
8+
Type,
9+
Value,
10+
Variable,
11+
)
12+
from cxxheaderparser.simple import (
13+
Include,
14+
NamespaceScope,
15+
Pragma,
16+
parse_string,
17+
ParsedData,
18+
)
19+
20+
21+
def test_numeric_literals() -> None:
22+
content = """
23+
#pragma once
24+
#include <cstdint>
25+
26+
int test_binary = 0b01'10'01;
27+
int test_decimal = 123'456'789u;
28+
int test_octal = 012'42'11l;
29+
"""
30+
data = parse_string(content, cleandoc=True)
31+
32+
assert data == ParsedData(
33+
namespace=NamespaceScope(
34+
variables=[
35+
Variable(
36+
name=PQName(segments=[NameSpecifier(name="test_binary")]),
37+
type=Type(
38+
typename=PQName(segments=[FundamentalSpecifier(name="int")])
39+
),
40+
value=Value(tokens=[Token(value="0b01'10'01")]),
41+
),
42+
Variable(
43+
name=PQName(segments=[NameSpecifier(name="test_decimal")]),
44+
type=Type(
45+
typename=PQName(segments=[FundamentalSpecifier(name="int")])
46+
),
47+
value=Value(tokens=[Token(value="123'456'789u")]),
48+
),
49+
Variable(
50+
name=PQName(segments=[NameSpecifier(name="test_octal")]),
51+
type=Type(
52+
typename=PQName(segments=[FundamentalSpecifier(name="int")])
53+
),
54+
value=Value(tokens=[Token(value="012'42'11l")]),
55+
),
56+
]
57+
),
58+
pragmas=[Pragma(content=Value(tokens=[Token(value="once")]))],
59+
includes=[Include(filename="<cstdint>")],
60+
)

0 commit comments

Comments
 (0)