From 60c64b06de29e3538f849aebd41a998a65988bc9 Mon Sep 17 00:00:00 2001 From: Nishanth Sanjeevi Date: Thu, 25 Jul 2024 08:43:27 +0530 Subject: [PATCH] Fix PCD parsing for PCDs that have a single quote in them (#600) The PCD parsing regex pattern failed for PCDs that had a single quote For example, gEfiMdePkgTokenSpaceGuid.PcdSingleQuote|"eng'fraengfra"|VOID*|0x0000002f CRITICAL - EXCEPTION: Too few parts: ['PcdSingleQuote|"eng\'fraengfra"', 'VOID*', '0x0000002f'] The new pattern should allow the usage of single quotes Co-authored-by: Nishanth1311 <41198847+Nishanth1311@users.noreply.github.com> --- edk2toollib/uefi/edk2/parsers/dec_parser.py | 2 +- tests.unit/parsers/test_dec_parser.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/edk2toollib/uefi/edk2/parsers/dec_parser.py b/edk2toollib/uefi/edk2/parsers/dec_parser.py index 036a8a17..2f0b86a7 100644 --- a/edk2toollib/uefi/edk2/parsers/dec_parser.py +++ b/edk2toollib/uefi/edk2/parsers/dec_parser.py @@ -127,7 +127,7 @@ def _parse(self, rawtext: str) -> None: self.token_space_name = sp[0].strip() # Regular expression pattern to match the symbol '|' that is not inside quotes - pattern = r'\|(?=(?:[^\'"]*[\'"][^\'"]*[\'"])*[^\'"]*$)' + pattern = r'\|(?=(?:(?:[^\'"]*(?:\'[^\']*\'|"[^"]*"))*[^\'"]*)$)' op = re.split(pattern, sp[2]) # if it's 2 long, we need to check that it's a structured PCD diff --git a/tests.unit/parsers/test_dec_parser.py b/tests.unit/parsers/test_dec_parser.py index a34bd1b1..cfae8431 100644 --- a/tests.unit/parsers/test_dec_parser.py +++ b/tests.unit/parsers/test_dec_parser.py @@ -127,6 +127,15 @@ def test_string_containing_a_pipe(self): self.assertEqual(a.type, "VOID*") self.assertEqual(a.id, "0x00010001") + def test_string_containing_single_quote(self): + SAMPLE_DATA_DECL = """gTestTokenSpaceGuid.PcdTestString|"eng'fraengfra"|VOID*|0x00010001""" + a = PcdDeclarationEntry("testpkg", SAMPLE_DATA_DECL) + self.assertEqual(a.token_space_name, "gTestTokenSpaceGuid") + self.assertEqual(a.name, "PcdTestString") + self.assertEqual(a.default_value, "\"eng'fraengfra\"") + self.assertEqual(a.type, "VOID*") + self.assertEqual(a.id, "0x00010001") + class TestDecParser(unittest.TestCase): SAMPLE_DEC_FILE = \