Skip to content

Commit

Permalink
fix parsing adjacent hyphens in a literal (#616)
Browse files Browse the repository at this point in the history
* Fixes #529: comment parsing error
* Add unit test for Issue#529
* fmt

Co-authored-by: eine <[email protected]>
  • Loading branch information
ktbarrett and eine committed Jan 26, 2020
1 parent d8c07d1 commit 53f681c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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)

0 comments on commit 53f681c

Please sign in to comment.