Skip to content

Commit

Permalink
base_parser: Don't treat pound signs wrapped in quotes as comments (#452
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Javagedes authored Nov 18, 2023
1 parent bc0813c commit bd971c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""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 @@ -794,6 +795,7 @@ 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 @@ -805,7 +807,7 @@ def StripComment(self, line):
Args:
line (str): line with a comment (#)
"""
return line.split('#')[0].strip()
return re.split(self.COMMENT_PATTERN, line)[0].strip()

def ParseNewSection(self, line):
"""Parses a new section line.
Expand Down
22 changes: 15 additions & 7 deletions tests.unit/parsers/test_hash_file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
##

import unittest

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser


Expand Down Expand Up @@ -36,10 +37,17 @@ def test_parse_new_section(self):

def test_strip_comment(self):
parser = HashFileParser("")
lines = ["Test", "MagicLib|Include/Magic", ""]
comments = ["\t#this shouldn't show up", " # test", ""]
for line in lines:
for comment in comments:
test_line = line + comment
result = parser.StripComment(test_line)
self.assertEqual(result, line)

lines_to_test = [
("Test", "\t# this shouldn't show up"),
("Test", " # test"),
("MagicLib|Include/Magic", "\t# this shouldn't show up"),
("MagicLib|Include/Magic", "# test"),
("", "# this is a comment"),
("gMyPkgTokenSpaceGuid.MyThing|'Value'|VOID*|0x10000000", " # My Comment"),
('gMyPkgTokenSpaceGuid.MyThing|"Value"|VOID*|0x10000000', "# My Comment"),
('gMyPkgTokenSpaceGuid.MyThing|"#Value"|VOID*|0x10000000', "# My Comment"),
]

for line in lines_to_test:
self.assertEqual(parser.StripComment(line[0]+line[1]), line[0])

0 comments on commit bd971c9

Please sign in to comment.