From a473d5aea01b88d3dea37500c124a8cff89d03ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tyrychtr?= Date: Tue, 21 Nov 2023 11:54:42 +0000 Subject: [PATCH] mkenums: Allow , in a character literal This required adding a higher precedence character literal choice. Fixes #3103 --- gobject/glib-mkenums.in | 6 ++++++ gobject/tests/mkenums.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/gobject/glib-mkenums.in b/gobject/glib-mkenums.in index 353e53ae3..e10b910f0 100755 --- a/gobject/glib-mkenums.in +++ b/gobject/glib-mkenums.in @@ -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) diff --git a/gobject/tests/mkenums.py b/gobject/tests/mkenums.py index c23a80178..8e9273090 100644 --- a/gobject/tests/mkenums.py +++ b/gobject/tests/mkenums.py @@ -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"""