From ce3fdc956cd4632ea0f950a1fbf5cc4bed26fc62 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Mon, 31 Aug 2020 19:51:52 -0500 Subject: [PATCH] Another refactor in the parsing. Now only supports use_clause parsing. --- .../use_clause/classification_results.txt | 26 ++++++++++++++++ .../use_clause/classification_test_input.vhd | 6 ++++ vsg/tests/vhdlFile_new/test_use_clause.py | 31 +++++++++++++++++++ vsg/token/use_clause.py | 7 ++--- vsg/vhdlFile/classify_new/block_statement.py | 27 +++++++++++++++- .../classify_new/concurrent_statement.py | 2 +- vsg/vhdlFile/classify_new/context_clause.py | 11 +++++++ vsg/vhdlFile/classify_new/context_item.py | 14 +++++++++ vsg/vhdlFile/classify_new/design_file.py | 16 ++++++++++ vsg/vhdlFile/classify_new/design_unit.py | 16 ++++++++++ vsg/vhdlFile/classify_new/use_clause.py | 31 +++++++++++++++++++ vsg/vhdlFile/utils.py | 31 +++++++++++++++---- vsg/vhdlFile/vhdlFile_new.py | 24 ++++++++++---- 13 files changed, 224 insertions(+), 18 deletions(-) create mode 100644 vsg/tests/use_clause/classification_results.txt create mode 100644 vsg/tests/use_clause/classification_test_input.vhd create mode 100644 vsg/tests/vhdlFile_new/test_use_clause.py create mode 100644 vsg/vhdlFile/classify_new/context_clause.py create mode 100644 vsg/vhdlFile/classify_new/context_item.py create mode 100644 vsg/vhdlFile/classify_new/design_file.py create mode 100644 vsg/vhdlFile/classify_new/design_unit.py create mode 100644 vsg/vhdlFile/classify_new/use_clause.py diff --git a/vsg/tests/use_clause/classification_results.txt b/vsg/tests/use_clause/classification_results.txt new file mode 100644 index 00000000..428b41b4 --- /dev/null +++ b/vsg/tests/use_clause/classification_results.txt @@ -0,0 +1,26 @@ +-------------------------------------------------------------------------------- +0 | None | +-------------------------------------------------------------------------------- +1 | None | + +-------------------------------------------------------------------------------- +2 | None | + +-------------------------------------------------------------------------------- +3 | None | use ieee.std_logic.1164; + + + +-------------------------------------------------------------------------------- +4 | None | + +-------------------------------------------------------------------------------- +5 | None | use ieee.std_logic.1164, ieee.std_logic_arith.all; + + + + + +-------------------------------------------------------------------------------- +6 | None | + diff --git a/vsg/tests/use_clause/classification_test_input.vhd b/vsg/tests/use_clause/classification_test_input.vhd new file mode 100644 index 00000000..c6fba37a --- /dev/null +++ b/vsg/tests/use_clause/classification_test_input.vhd @@ -0,0 +1,6 @@ + + +use ieee.std_logic.1164; + +use ieee.std_logic.1164, ieee.std_logic_arith.all; + diff --git a/vsg/tests/vhdlFile_new/test_use_clause.py b/vsg/tests/vhdlFile_new/test_use_clause.py new file mode 100644 index 00000000..215b2ddb --- /dev/null +++ b/vsg/tests/vhdlFile_new/test_use_clause.py @@ -0,0 +1,31 @@ +import os + +import unittest + +from vsg.vhdlFile import vhdlFile_new +from vsg.tests import utils + +sLrmUnit = 'use_clause' + +lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..',sLrmUnit,'classification_test_input.vhd')) +oFile = vhdlFile_new.vhdlFile(lFile) + + +class test_token(unittest.TestCase): + + + def test_classification(self): + sTestDir = os.path.join(os.path.dirname(__file__),'..',sLrmUnit) + + lExpected = [] + utils.read_file(os.path.join(sTestDir, 'classification_results.txt'), lExpected, False) + + lActual = [] + + for oObject in utils.extract_objects(oFile, True): + lActual.append(str(oObject)) + + self.assertEqual(lExpected, lActual) + +# def test_debug(self): +# utils.print_objects(oFile,True) diff --git a/vsg/token/use_clause.py b/vsg/token/use_clause.py index 47a96600..0908d0ae 100644 --- a/vsg/token/use_clause.py +++ b/vsg/token/use_clause.py @@ -1,26 +1,25 @@ from vsg import parser -############################################################################### -# Use objects -############################################################################### - class keyword(parser.keyword): def __init__(self, sString): parser.keyword.__init__(self, sString) + class selected_name(parser.selected_name): def __init__(self, sString): parser.selected_name.__init__(self, sString) + class comma(parser.comma): def __init__(self): parser.comma.__init__(self) + class semicolon(parser.semicolon): def __init__(self): diff --git a/vsg/vhdlFile/classify_new/block_statement.py b/vsg/vhdlFile/classify_new/block_statement.py index 52336ac4..4f794ec3 100644 --- a/vsg/vhdlFile/classify_new/block_statement.py +++ b/vsg/vhdlFile/classify_new/block_statement.py @@ -8,7 +8,7 @@ from vsg.vhdlFile.classify import block_header from vsg.vhdlFile.classify import block_declarative_part from vsg.vhdlFile.classify import block_statement_part -def is_it(iObject, oObject, lAllObjects, lNewObjects, dVars): +def detect(iObject, oObject, lAllObjects, lNewObjects, dVars): iItemCount = 0 iIndex = iObject try: @@ -27,6 +27,31 @@ def is_it(iObject, oObject, lAllObjects, lNewObjects, dVars): return False +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() == ':': diff --git a/vsg/vhdlFile/classify_new/concurrent_statement.py b/vsg/vhdlFile/classify_new/concurrent_statement.py index 4401408b..e79e1776 100644 --- a/vsg/vhdlFile/classify_new/concurrent_statement.py +++ b/vsg/vhdlFile/classify_new/concurrent_statement.py @@ -19,7 +19,7 @@ def is_it(iObject, oObject, lAllObjects, lNewObjects, dVars): | generate_statement | *PSL*_PSL_Directive ''' - if block_statement.is_it(iObject, oObject, lAllObjects, lNewObjects, dVars): + if block_statement.detect(iObject, oObject, lAllObjects, lNewObjects, dVars): return True if generate_statement.is_it(iObject, oObject, lAllObjects, lNewObjects, dVars): diff --git a/vsg/vhdlFile/classify_new/context_clause.py b/vsg/vhdlFile/classify_new/context_clause.py new file mode 100644 index 00000000..433fb945 --- /dev/null +++ b/vsg/vhdlFile/classify_new/context_clause.py @@ -0,0 +1,11 @@ + +from vsg.vhdlFile.classify_new import context_item + + +def detect(iCurrent, lObjects): + ''' + context_clause ::= + { context_item } + ''' + + return context_item.detect(iCurrent, lObjects) diff --git a/vsg/vhdlFile/classify_new/context_item.py b/vsg/vhdlFile/classify_new/context_item.py new file mode 100644 index 00000000..376696eb --- /dev/null +++ b/vsg/vhdlFile/classify_new/context_item.py @@ -0,0 +1,14 @@ + +from vsg.vhdlFile.classify_new import use_clause + + +def detect(iCurrent, lObjects): + ''' + context_item ::= + library_clause + | use_clause + | context_reference + ''' + iReturn = use_clause.detect(iCurrent, lObjects) + + return iReturn diff --git a/vsg/vhdlFile/classify_new/design_file.py b/vsg/vhdlFile/classify_new/design_file.py new file mode 100644 index 00000000..9e6a3655 --- /dev/null +++ b/vsg/vhdlFile/classify_new/design_file.py @@ -0,0 +1,16 @@ + +from vsg.vhdlFile.classify_new import design_unit + +from vsg.vhdlFile import utils + + +def tokenize(lObjects): + ''' + design_file ::= + design_unit { design_unit } + ''' + for iCurrent in range(0, len(lObjects)): + if utils.is_item(lObjects, iCurrent): + iCurrent = design_unit.detect(iCurrent, lObjects) + else: + iCurrent += 1 diff --git a/vsg/vhdlFile/classify_new/design_unit.py b/vsg/vhdlFile/classify_new/design_unit.py new file mode 100644 index 00000000..f8b2f915 --- /dev/null +++ b/vsg/vhdlFile/classify_new/design_unit.py @@ -0,0 +1,16 @@ + +from vsg.vhdlFile.classify_new import context_clause +#from vsg.vhdlFile.classify_new import library_unit + + +def detect(iCurrent, lObjects): + ''' + design_unit ::= + context_clause library_unit + ''' + iReturned = context_clause.detect(iCurrent, lObjects) + +# if library_unit.tokenize(oObject, iObject, lObjects, dVars): +# return True +# +# return False diff --git a/vsg/vhdlFile/classify_new/use_clause.py b/vsg/vhdlFile/classify_new/use_clause.py new file mode 100644 index 00000000..f9be6a18 --- /dev/null +++ b/vsg/vhdlFile/classify_new/use_clause.py @@ -0,0 +1,31 @@ + +from vsg import parser +from vsg.token import use_clause as token + +from vsg.vhdlFile import utils + + +def detect(iCurrent, lObjects): + ''' + use_clause ::= + use selected_name { , selected_name } ; + ''' + if utils.object_value_is(lObjects, iCurrent, 'use'): + return classify(iCurrent, lObjects) + return iCurrent + + +def classify(iCurrent, lObjects): + + iStart, iEnd = utils.get_range(lObjects, iCurrent, ';') + + for iToken in range(iStart, iEnd): + if utils.is_item(lObjects, iToken): + if utils.classify_token('use', token.keyword, iToken, lObjects): + continue + if utils.classify_token(',', token.comma, iToken, lObjects): + continue + utils.assign_token(lObjects, iToken, token.selected_name) + + utils.classify_token(';', token.semicolon, iEnd, lObjects) + return iEnd diff --git a/vsg/vhdlFile/utils.py b/vsg/vhdlFile/utils.py index e5468880..f4f126fe 100644 --- a/vsg/vhdlFile/utils.py +++ b/vsg/vhdlFile/utils.py @@ -70,11 +70,14 @@ def have_is_keyword(iObject, lAllObjects): iIndex += 1 return False -def assign_token(lAllObjects, iToken, token): - lAllObjects[iToken] = token(lAllObjects[iToken].get_value()) +def assign_token(lObjects, iToken, token): + try: + lObjects[iToken] = token(lObjects[iToken].get_value()) + except TypeError: + lObjects[iToken] = token() -def object_value_is(lAllObjects, iToken, iString): - if lAllObjects[iToken].get_value().lower() == iString.lower(): +def object_value_is(lAllObjects, iToken, sString): + if lAllObjects[iToken].get_value().lower() == sString.lower(): return True return False @@ -83,12 +86,28 @@ def is_item(lAllObjects, iToken): return True return False -def get_bounds(lAllObjects, iStart, sEnd): +def get_bounds(lObjects, iStart, sEnd): iIndex = iStart - while lAllObjects[iIndex].get_value() != ';': + while lObjects[iIndex].get_value() != sEnd: iIndex += 1 else: iEnd = iIndex iStart = iStart iCurrent = iStart return iStart, iCurrent, iEnd + +def get_range(lObjects, iStart, sEnd): + iIndex = iStart + while lObjects[iIndex].get_value() != sEnd: + iIndex += 1 + else: + iEnd = iIndex + iStart = iStart + return iStart, iEnd + +def classify_token(sToken, token, iToken, lObjects): + if object_value_is(lObjects, iToken, sToken): + assign_token(lObjects, iToken, token) + return True + return False + diff --git a/vsg/vhdlFile/vhdlFile_new.py b/vsg/vhdlFile/vhdlFile_new.py index 87673040..70fc555d 100644 --- a/vsg/vhdlFile/vhdlFile_new.py +++ b/vsg/vhdlFile/vhdlFile_new.py @@ -4,12 +4,13 @@ from vsg import line from vsg.vhdlFile.classify_new import blank from vsg.vhdlFile.classify_new import whitespace from vsg.vhdlFile.classify_new import comment -from vsg.vhdlFile.classify_new import architecture_body -from vsg.vhdlFile.classify_new import entity_declaration -from vsg.vhdlFile.classify_new import generate_statement -from vsg.vhdlFile.classify_new import block_statement -from vsg.vhdlFile.classify_new import concurrent_statement -from vsg.vhdlFile.classify_new import case_generate_alternative +from vsg.vhdlFile.classify_new import design_file +#from vsg.vhdlFile.classify_new import architecture_body +#from vsg.vhdlFile.classify_new import entity_declaration +#from vsg.vhdlFile.classify_new import generate_statement +#from vsg.vhdlFile.classify_new import block_statement +#from vsg.vhdlFile.classify_new import concurrent_statement +#from vsg.vhdlFile.classify_new import case_generate_alternative from vsg import parser @@ -65,6 +66,17 @@ class vhdlFile(): lAllObjects.extend(lObjects) lAllObjects.append(parser.carriage_return()) + iToken = 0 + + design_file.tokenize(lAllObjects) + + for iLine, lLine in enumerate(split_on_carriage_return(lAllObjects)): + self.lines[iLine + 1].objects = lLine + + + +def old_stuff(): + lNewObjects = [] for iObject, oObject in enumerate(lAllObjects): if not type(oObject) == parser.item: