From 02ef9dcb901008aa882841ff2256fb2bacea8ff1 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Thu, 3 Dec 2020 08:53:53 -0600 Subject: [PATCH] Refactoring extract method. 1) Moved method from vsg.vhdlFile to vsg.vhdlFile.extract 2) Refactored method to use token map --- vsg/vhdlFile/extract/__init__.py | 1 + ..._with_token_until_ending_token_is_found.py | 21 ++++++++++++++ vsg/vhdlFile/vhdlFile.py | 29 +------------------ 3 files changed, 23 insertions(+), 28 deletions(-) create mode 100644 vsg/vhdlFile/extract/get_tokens_where_line_starts_with_token_until_ending_token_is_found.py diff --git a/vsg/vhdlFile/extract/__init__.py b/vsg/vhdlFile/extract/__init__.py index e327ee49..a364b43b 100644 --- a/vsg/vhdlFile/extract/__init__.py +++ b/vsg/vhdlFile/extract/__init__.py @@ -29,3 +29,4 @@ from .get_sequence_of_tokens_not_matching import get_sequence_of_tokens_not_matc from .get_tokens_between_tokens_inclusive_while_storing_value_from_token import get_tokens_between_tokens_inclusive_while_storing_value_from_token from .get_tokens_from_line import get_tokens_from_line from .get_consecutive_lines_starting_with_token_and_stopping_when_token_starting_line_is_found import get_consecutive_lines_starting_with_token_and_stopping_when_token_starting_line_is_found +from .get_tokens_where_line_starts_with_token_until_ending_token_is_found import get_tokens_where_line_starts_with_token_until_ending_token_is_found diff --git a/vsg/vhdlFile/extract/get_tokens_where_line_starts_with_token_until_ending_token_is_found.py b/vsg/vhdlFile/extract/get_tokens_where_line_starts_with_token_until_ending_token_is_found.py new file mode 100644 index 00000000..398580ef --- /dev/null +++ b/vsg/vhdlFile/extract/get_tokens_where_line_starts_with_token_until_ending_token_is_found.py @@ -0,0 +1,21 @@ + +from vsg.vhdlFile.extract import tokens +from vsg.vhdlFile.extract import utils + + +def get_tokens_where_line_starts_with_token_until_ending_token_is_found(start_token, stop_token, lAllTokens, oTokenMap): + + lReturn = [] + + lStartIndexes = utils.filter_indexes_which_start_a_line(start_token, oTokenMap) + + lStopIndexes = [] + for iStartIndex in lStartIndexes: + lStopIndexes.append(oTokenMap.get_index_of_token_after_index(stop_token, iStartIndex)) + + for iStart, iStop in zip(lStartIndexes, lStopIndexes): + iLine = oTokenMap.get_line_number_of_index(iStart) + lTemp = lAllTokens[iStart:iStop + 1] + lReturn.append(tokens.New(iStart, iLine, lTemp)) + + return lReturn diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 6b930029..75de01f5 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -198,34 +198,7 @@ class vhdlFile(): return extract.get_consecutive_lines_starting_with_token_and_stopping_when_token_starting_line_is_found(search_token, stop_token, self.lAllObjects, self.oTokenMap) def get_tokens_where_line_starts_with_token_until_ending_token_is_found(self, start_token, stop_token): - iLine = 1 - lTemp = [] - lReturn = [] - iStart = 0 - bStore = False - iLineNumber = None - for iIndex in range(0, len(self.lAllObjects)): - - if bStore: - lTemp.append(self.lAllObjects[iIndex]) - - if isinstance(self.lAllObjects[iIndex], stop_token): - oTokens = Tokens(iStart, iLineNumber, lTemp) - lReturn.append(oTokens) - bStore = False - lTemp = [] - - if isinstance(self.lAllObjects[iIndex], parser.carriage_return): - iLine +=1 - if not bStore: - if utils.are_next_consecutive_token_types([parser.whitespace, start_token], iIndex + 1, self.lAllObjects) or \ - utils.are_next_consecutive_token_types([start_token], iIndex + 1, self.lAllObjects): - iStart = iIndex + 1 - bStore = True - iLineNumber = iLine - - return lReturn - + return extract.get_tokens_where_line_starts_with_token_until_ending_token_is_found(start_token, stop_token, self.lAllObjects, self.oTokenMap) def get_token_and_n_tokens_before_it(self, oToken, iTokens): return extract.get_token_and_n_tokens_before_it(oToken, iTokens, self.lAllObjects, self.oTokenMap)