Refactoring block_statement to improve readability.

This commit is contained in:
Jeremiah Leary 2020-09-01 13:08:39 -05:00
parent 1bd9e31c96
commit b694ff2e42
2 changed files with 36 additions and 108 deletions

View File

@ -21,12 +21,10 @@ from vsg.vhdlFile.classify_new import block_statement_part
def detect(iCurrent, lObjects):
# print('---> detecting block_statement')
iItemCount = 0
iIndex = iCurrent
try:
while iItemCount < 3:
# print(lObjects[iIndex].get_value())
if utils.is_item(lObjects, iIndex):
iItemCount += 1
iIndex += 1
@ -39,12 +37,10 @@ def detect(iCurrent, lObjects):
def classify(iCurrent, lObjects):
# print('---> Classifying opening block_statement')
### Classify opening keywords
iStart, iEnd = utils.get_range(lObjects, iCurrent, 'block')
for iToken in range(iStart, iEnd + 1):
# print(lObjects[iToken].get_value())
if not utils.is_item(lObjects, iToken):
continue
if utils.classify_token('block', token.keyword, iToken, lObjects):
@ -57,57 +53,27 @@ def classify(iCurrent, lObjects):
if utils.have_guard_condition(iToken, lObjects):
iStart, iEnd = utils.get_range(lObjects, iToken, ')')
for iToken in range(iStart, iEnd + 1):
# print(lObjects[iToken].get_value())
if not utils.is_item(lObjects, iToken):
continue
utils.assign_token(lObjects, iToken, token.guard_condition)
### Find next item
while not utils.is_item(lObjects, iToken):
# print(lObjects[iToken].get_value())
iToken += 1
iToken = utils.find_next_token(iToken, lObjects)
### Classify the is keyword if it exists
if utils.classify_token('is', token.is_keyword, iToken, lObjects):
# print(lObjects[iToken].get_value())
iToken += 1
### Check for block_header
iLast = 0
while iLast != iToken:
while not utils.is_item(lObjects, iToken):
# print(lObjects[iToken].get_value())
iToken += 1
iLast = iToken
iToken = block_header.detect(iToken, lObjects)
iToken = utils.classify_is_keyword(iToken, token.is_keyword, lObjects)
### Check for block_declarative_part
iLast = 0
while iLast != iToken:
while not utils.is_item(lObjects, iToken):
# print(lObjects[iToken].get_value())
iToken += 1
iLast = iToken
iToken = block_declarative_part.detect(iToken, lObjects)
iToken = utils.detect_submodule(iToken, lObjects, block_header)
iToken = utils.detect_submodule(iToken, lObjects, block_declarative_part)
### Classify Begin keyword
utils.classify_token('begin', token.begin_keyword, iToken, lObjects)
# print(lObjects[iToken].get_value())
iToken += 1
iToken = utils.classify_begin_keyword(iToken, token.begin_keyword, lObjects)
### Check for block_statement_part
iLast = 0
while iLast != iToken:
while not utils.is_item(lObjects, iToken):
iToken += 1
iLast = iToken
iToken = block_statement_part.detect(iToken, lObjects)
iToken = utils.detect_submodule(iToken, lObjects, block_statement_part)
# print('---> Classifying closing block_statement')
### Classify the closing keywords
iStart, iEnd = utils.get_range(lObjects, iToken, ';')
for iToken in range(iStart, iEnd + 1):
# print(lObjects[iToken].get_value())
if not utils.is_item(lObjects, iToken):
continue
if utils.classify_token('block', token.end_block_keyword, iToken, lObjects):
@ -119,71 +85,3 @@ def classify(iCurrent, lObjects):
utils.assign_token(lObjects, iToken, token.end_block_label)
return iToken
#*******************************************************************************
#*******************************************************************************
#*******************************************************************************
#def tokenize(iObject, oObject, lAllObjects, lNewObjects, dVars):
#
# iToken = iObject
#
# while not utils.object_value_is(lAllObjects iToken, 'block'):
# if utils.is_item(lAllObjects, iToken):
# if utils.object_value_is(lAllObjects, iToken, ':'):
# utils.assign_token(lAllObjects, iToken, token.label_colon)
# else:
# utils.assign_token(lAllObjects, iToken, token.label_name)
# iToken += 1
# else:
# utils.assign_token(lAllObjects, iToken, token.keyword)
# iToken += 1
#
# while not
#
# for iToken in range(iObject, len(lAllObjects):
# kk
# if utils.is_item(lAllObjects, iToken):
# if utils.object_value_is(lAllObjects, iToken, ':'):
# utils.assign_token(lAllObjects, iToken, token.label_colon)
# elif utils.object_value_is(lAllObjects, iToken, 'block'):
# utils.assign_token(lAllObjects, iToken, token.label_colon)
#
#def tokenize_begin_declaration(iObject, oObject, lAllObjects, lNewObjects, dVars):
#
# if oObject.get_value().lower() == ':':
# lNewObjects.append(token.label_colon())
# return True
# elif oObject.get_value().lower() == 'block':
# lNewObjects.append(token.keyword(oObject.get_value()))
# return True
# elif utils.have_guard_condition(iObject, lAllObjects):
# print("need to process guard condition first")
# return True
# elif utils.have_is_keyword(iObject, lAllObjects):
# lNewObjects.append(token.is_keyword(oObject.get_value()))
# return True
# elif oObject.get_value().lower() == 'begin':
# lNewObjects.append(token.begin_keyword(oObject.get_value()))
# utils.pop_level(dVars)
# utils.push_level(dVars, 'block_statement_part')
# return True
#
# return False
#
#def tokenize_end_declaration(iObject, oObject, lAllObjects, lNewObjects, dVars):
# if oObject.get_value().lower() == 'block':
# lNewObjects.append(token.end_block_keyword(oObject.get_value()))
# return True
# elif oObject.get_value().lower() == ';':
# lNewObjects.append(token.semicolon())
# utils.pop_level(dVars)
# return True
# else:
# lNewObjects.append(token.end_block_label(oObject.get_value()))
# return True
#
# return False
#

View File

@ -119,3 +119,33 @@ def find_in_range(sValue, iToken, sEnd, lObjects):
if object_value_is(lObjects, iIndex, sValue):
return True
return False
def find_next_token(iToken, lObjects):
iReturn = iToken
while not is_item(lObjects, iReturn):
iReturn += 1
return iReturn
def detect_submodule(iToken, lObjects, module):
iLast = 0
iReturn = iToken
while iLast != iReturn:
iReturn = find_next_token(iReturn, lObjects)
iLast = iReturn
iReturn = module.detect(iReturn, lObjects)
return iReturn
def classify_begin_keyword(iToken, token, lObjects):
iReturn = iToken
if classify_token('begin', token, iToken, lObjects):
iReturn = iToken + 1
return iReturn
return iToken
def classify_is_keyword(iToken, token, lObjects):
iReturn = iToken
if classify_token('is', token, iToken, lObjects):
iReturn = iToken + 1
return iReturn
return iToken