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.py: support conditions checking for empty string #653

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -546,12 +546,14 @@ def _TokenizeConditional(cls: "BaseParser", text: str) -> str:
mode = 0
tokens = []
for character in text:
if character == '"' and len(token) == 0:
if character == '"' and len(token) == 0 and mode != QUOTE_MODE:
mode = QUOTE_MODE
elif character == '"' and mode == QUOTE_MODE:
if len(token) > 0:
tokens.append(f'"{token}"')
token = ""
else:
tokens.append('""')
mode = TEXT_MODE
elif character == "$" and len(token) == 0:
token += character
Expand Down
35 changes: 35 additions & 0 deletions tests.unit/parsers/test_dsc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import unittest
import tempfile
import os
import textwrap
from edk2toollib.uefi.edk2.parsers.dsc_parser import DscParser
from edk2toollib.uefi.edk2.path_utilities import Edk2Path

Expand Down Expand Up @@ -155,3 +156,37 @@ def test_dsc_include_relative_path(self):
self.assertEqual(parser.LocalVars["INCLUDED"], "TRUE") # make sure we got the defines
finally:
os.chdir(cwd)

def test_dsc_define_statements(self):
"""This test some dsc define statements"""
SAMPLE_DSC_FILE1 = textwrap.dedent("""\
[Defines]
PLATFORM_NAME = SomePlatformPkg
PLATFORM_GUID = aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
PLATFORM_VERSION = 0.1
DSC_SPECIFICATION = 0x00010005
OUTPUT_DIRECTORY = Build/$(PLATFORM_NAME)

DEFINE FLAG1 =

!if FLAG1 != ""
FLAG2 = "hi"
!endif

!if $(FLAG2) == "hi"
[Components.IA32]
FakePath/FakePath2/FakeInf.inf
!endif
""")
workspace = tempfile.mkdtemp()

file1_name = "file1.dsc"
file1_path = os.path.join(workspace, file1_name)
TestDscParserIncludes.write_to_file(file1_path, SAMPLE_DSC_FILE1)
try:
parser = DscParser()
parser.SetEdk2Path(Edk2Path(workspace, []))
parser.ParseFile(file1_path)
finally:
os.remove(file1_path)
assert any("FakePath/FakePath2/FakeInf.inf" in value for value in parser.Components)