Skip to content

Commit

Permalink
base_parser: Update StripComment to consider quote groupings (#456)
Browse files Browse the repository at this point in the history
Previous update to StripComment would ignore any pound signs that were
surrounded by quotes, but did not consider if the sign was inside two
quotes or between two sets of two quotes
(i.e. `" # "` vs `" 1 " # " 2 "`). This commit updates StripComment to
check this scenario and not strip if the pound sign is inside two
quotes.
  • Loading branch information
Javagedes authored Nov 21, 2023
1 parent e795831 commit e8586dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
29 changes: 26 additions & 3 deletions edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""Code to support parsing EDK2 files."""
import logging
import os
import re
from warnings import warn

from edk2toollib.uefi.edk2 import path_utilities
Expand Down Expand Up @@ -795,7 +794,6 @@ def ResetParserState(self):

class HashFileParser(BaseParser):
"""Base class for Edk2 build files that use # for comments."""
COMMENT_PATTERN = re.compile(r'(?<!["\'])#(?!["\'])')

def __init__(self, log):
"""Inits an empty Parser for files that use # for comments.."""
Expand All @@ -807,7 +805,32 @@ def StripComment(self, line):
Args:
line (str): line with a comment (#)
"""
return re.split(self.COMMENT_PATTERN, line)[0].strip()
if "#" not in line:
return line.strip()

result = []
inside_quotes = False
quote_char = None
escaped = False

for char in line:
if char in ('"', "'") and not escaped:
if not inside_quotes:
inside_quotes = True
quote_char = char
elif char == quote_char:
inside_quotes = False
quote_char = None
elif char == '#' and not inside_quotes:
break
elif char == '\\' and not escaped:
escaped = True
else:
escaped = False

result.append(char)

return ''.join(result).rstrip()

def ParseNewSection(self, line):
"""Parses a new section line.
Expand Down
7 changes: 7 additions & 0 deletions tests.unit/parsers/test_hash_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def test_strip_comment(self):
("gMyPkgTokenSpaceGuid.MyThing|'Value'|VOID*|0x10000000", " # My Comment"),
('gMyPkgTokenSpaceGuid.MyThing|"Value"|VOID*|0x10000000', "# My Comment"),
('gMyPkgTokenSpaceGuid.MyThing|"#Value"|VOID*|0x10000000', "# My Comment"),
('file_data = "DEFINE TEST DEFINE = \"DEFINE_VALUE\""', ' # "Test String" # Test String'),
("file_data = 'DEFINE TEST DEFINE = \"DEFINE_VALUE\"'", ' # "Test String" # Test String'),
('file_data = "DEFINE TEST DEFINE = \"DEFINE_VALUE\" \' more to check \'"', ' # "Test String" # Test String'),
('file_data = \'DEFINE\" # TEMP \" UPDATE \'', "# Found a quote"),
(r'test = \"', r' # Temp \"'),
('file_data = "DEFINE TEST DEFINE = \"DEFINE\\"_VALUE\""', ' # "Test String" # Test String'),
('file_data = "DEFINE TEST DEFINE = \"DEFINE\\\'_VALUE\""', ' # "Test String" # Test String'),
]

for line in lines_to_test:
Expand Down

0 comments on commit e8586dc

Please sign in to comment.