Skip to content

Fix regex for VHDL component instantiations #990

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion tests/unit/test_vhdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,27 @@ def test_getting_component_instantiations_from_design_file(self):

label3Foo : foo3 port map (clk, rst, X"A");

label4Foo : foo4
generic map (
g_POWER => 2 ** 10,
g_DIVIDE => 10 / 5
)
port map(
clk => '1',
rst => '0',
output => "00"
) ;

end architecture;

"""
)
component_instantiations = design_file.component_instantiations
self.assertEqual(len(component_instantiations), 3)
self.assertEqual(len(component_instantiations), 4)
self.assertEqual(component_instantiations[0], "foo")
self.assertEqual(component_instantiations[1], "foo2")
self.assertEqual(component_instantiations[2], "foo3")
self.assertEqual(component_instantiations[3], "foo4")

def test_adding_generics_to_entity(self):
entity = VHDLEntity("name")
Expand Down
13 changes: 10 additions & 3 deletions vunit/vhdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ def parse(cls, code):
)

_component_re = re.compile(
r"[a-zA-Z]\w*\s*\:\s*(?:component)?\s*(?:(?:[a-zA-Z]\w*)\.)?([a-zA-Z]\w*)\s*"
r"(?:generic|port) map\s*\([\s\w\=\>\,\.\)\(\+\-\'\"]*\);",
re.IGNORECASE,
r"""
[a-zA-Z]\w* # Label
\s*\:\s* # Semicolon
(?:component)?\s* # Optional component keyword
(?:(?:[a-zA-Z]\w*)\.)? # Optional library name
([a-zA-Z]\w*)\s* # Capture component name
(?:generic|port)\s+map\s* # Generic/port map
\(.*?\)\s*; # Open and closing brackets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than allowing anything between the opening and closing brackets I would suggest extending the previously more restrictive expression with what is needed to pass the new test case:

([\s\w=>,.)(+-'"*/]*)

I cannot recall if the more restrictive expression is there for a reason that is not made explicit in the test cases. For that reason I prefer a more defensive modification.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed reply.
I will modify the regex as you suggested, but I guess there will be other cases in which it won't work

In the mean time I've monkey patched vhdl_parser.py and am matching everything inside the brackets.

""",
re.MULTILINE | re.IGNORECASE | re.VERBOSE | re.DOTALL,
)


Expand Down