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

fix parsing adjacent hyphens in a literal #616

Merged
merged 3 commits into from
Jan 26, 2020
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: 4 additions & 0 deletions tests/unit/test_vhdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ def test_that_record_type_declarations_are_found(self):
def test_remove_comments(self):
self.assertEqual(remove_comments("a\n-- foo \nb"), "a\n \nb")

def test_two_adjacent_hyphens_in_a_literal(self):
stimulus = 'signal a : std_logic_vector(3 downto 0) := "----";'
self.assertEqual(remove_comments(stimulus), stimulus)

def parse_single_entity(self, code):
"""
Helper function to parse a single entity
Expand Down
9 changes: 6 additions & 3 deletions vunit/vhdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,20 +1131,23 @@ def reference_all_names_within(self):
return self.name_within == "all"


VHDL_REMOVE_COMMENT_RE = re.compile(r"--[^\n]*")
VHDL_REMOVE_COMMENT_RE = r"(?:(?:\"[^\"]*\")|(--[^\n]*))"
VHDL_REMOVE_COMMENT_COMPILED_RE = re.compile(VHDL_REMOVE_COMMENT_RE, re.MULTILINE)


def _comment_repl(match):
"""
Replace comment with equal amount of whitespace to make
lexical position unaffected
"""
text = match.group(0)
text = match.group(1)
if text is None:
return match.group(0)
return " " * len(text)


def remove_comments(code):
"""
Return the code with comments removed
"""
return VHDL_REMOVE_COMMENT_RE.sub(_comment_repl, code)
return VHDL_REMOVE_COMMENT_COMPILED_RE.sub(_comment_repl, code)