mkenums: Allow , in a character literal

This required adding a higher precedence character literal choice.

Fixes #3103
This commit is contained in:
Lukáš Tyrychtr 2023-11-21 11:54:42 +00:00 committed by Philip Withnall
parent 09b728b2da
commit a473d5aea0
2 changed files with 32 additions and 0 deletions

View File

@ -223,6 +223,8 @@ def parse_entries(file, file_name):
(\w+)\s* # name
(\s+[A-Z]+_(?:AVAILABLE|DEPRECATED)_ENUMERATOR_IN_[0-9_]+(?:_FOR\s*\(\s*\w+\s*\))?\s*)? # availability
(?:=( # value
\s*'[^']*'\s* # char
| # OR
\s*\w+\s*\(.*\)\s* # macro with multiple args
| # OR
(?:[^,/]|/(?!\*))* # anything but a comma or comment
@ -728,6 +730,10 @@ def process_file(curfilename):
# approximation to C constant folding
inum = eval(num, {}, c_namespace)
# Support character literals
if isinstance(inum, str) and len(inum) == 1:
inum = ord(inum)
# make sure it parsed to an integer
if not isinstance(inum, int):
sys.exit("Unable to parse enum value '%s'" % num)

View File

@ -785,6 +785,32 @@ comment: {standard_bottom_comment}
"7",
)
def test_comma_in_enum_value(self):
"""Test use of comma in enum value."""
h_contents = """
typedef enum {
ENUM_VALUE_WITH_COMMA = ',',
} TestCommaEnum;
"""
result = self.runMkenumsWithHeader(h_contents)
self.assertEqual("", result.err)
self.assertSingleEnum(
result,
"TestCommaEnum",
"test_comma_enum",
"TEST_COMMA_ENUM",
"COMMA_ENUM",
"TEST",
"",
"enum",
"Enum",
"ENUM",
"ENUM_VALUE_WITH_COMMA",
"comma",
44,
)
class TestRspMkenums(TestMkenums):
"""Run all tests again in @rspfile mode"""