Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_parser: Don't treat pound signs wrapped in quotes as comments #452

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 # test", "Test"),
("MagicLib|Include/Magic \t# this shouldn't show up", "MagicLib|Include/Magic"),
("MagicLib|Include/Magic # test", "MagicLib|Include/Magic"),
("# this is a comment", ""),
("gMyPkgTokenSpaceGuid.MyThing|'Value'|VOID*|0x10000000 # My Comment", "gMyPkgTokenSpaceGuid.MyThing|'Value'|VOID*|0x10000000"),
('gMyPkgTokenSpaceGuid.MyThing|"Value"|VOID*|0x10000000 # My Comment', 'gMyPkgTokenSpaceGuid.MyThing|"Value"|VOID*|0x10000000'),
('gMyPkgTokenSpaceGuid.MyThing|"#Value"|VOID*|0x10000000 # My Comment', 'gMyPkgTokenSpaceGuid.MyThing|"#Value"|VOID*|0x10000000'),
]

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