From 7b61836e8be4e1b3f39f9d8f97aba52c0de96e9d Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Fri, 31 Jul 2020 10:58:59 -0500 Subject: [PATCH] Adding parsing of architecture elements into objects using the parser. --- vsg/parser.py | 63 ++++++++ ...architecture_classification_test_input.vhd | 34 ++++ .../vhdlFile/test_vhdlFile_architecture.py | 152 ++++++++++++++++++ vsg/vhdlFile/classify/architecture.py | 106 +++++++++++- vsg/vhdlFile/vhdlFile.py | 9 +- 5 files changed, 360 insertions(+), 4 deletions(-) create mode 100644 vsg/tests/architecture/architecture_classification_test_input.vhd diff --git a/vsg/parser.py b/vsg/parser.py index 19edefc5..5a5cd527 100644 --- a/vsg/parser.py +++ b/vsg/parser.py @@ -76,6 +76,69 @@ class selected_name(item): def __init__(self, sString): item.__init__(self, sString) +class name(item): + + def __init__(self, sString): + item.__init__(self, sString) + +class simple_name(item): + + def __init__(self, sString): + item.__init__(self, sString) + +############################################################################### +# Architecture objects +############################################################################### + +class architecture_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_identifier(identifier): + + def __init__(self, sString): + identifier.__init__(self, sString) + +class architecture_of_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_entity_name(name): + + def __init__(self, sString): + name.__init__(self, sString) + +class architecture_is_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_begin_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_end_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_end_architecture_keyword(keyword): + + def __init__(self, sString): + keyword.__init__(self, sString) + +class architecture_simple_name(simple_name): + + def __init__(self, sString): + simple_name.__init__(self, sString) + +class architecture_semicolon(semicolon): + + def __init__(self, sString): + semicolon.__init__(self) ############################################################################### # Context objects diff --git a/vsg/tests/architecture/architecture_classification_test_input.vhd b/vsg/tests/architecture/architecture_classification_test_input.vhd new file mode 100644 index 00000000..0f17b1e3 --- /dev/null +++ b/vsg/tests/architecture/architecture_classification_test_input.vhd @@ -0,0 +1,34 @@ + + +architecture RTL of ENTITY1 is + +begin + +end architecture RTL; + + + +architecture RTL of ENTITY1 is + +begin + +end; + + +architecture RTL of ENTITY1 is + +begin + +end architecture; + + +architecture +RTL +of +ENTITY1 +is +begin +end +architecture +RTL +; diff --git a/vsg/tests/vhdlFile/test_vhdlFile_architecture.py b/vsg/tests/vhdlFile/test_vhdlFile_architecture.py index bff70e5c..440c4ea0 100644 --- a/vsg/tests/vhdlFile/test_vhdlFile_architecture.py +++ b/vsg/tests/vhdlFile/test_vhdlFile_architecture.py @@ -3,6 +3,8 @@ import os import unittest from vsg import vhdlFile from vsg.tests import utils +from vsg import parser + lFileArchitecture = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','architecture','architecture_test_input.vhd')) oFileArchitecture = vhdlFile.vhdlFile(lFileArchitecture) @@ -10,6 +12,9 @@ oFileArchitecture = vhdlFile.vhdlFile(lFileArchitecture) lFileArch2 = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','architecture','rule_029_test_input.vhd')) oFileArch2 = vhdlFile.vhdlFile(lFileArch2) +lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','architecture','architecture_classification_test_input.vhd')) +oFile = vhdlFile.vhdlFile(lFile) + class testVhdlFileMethods(unittest.TestCase): def test_insideArchitecture_assignment(self): @@ -83,3 +88,150 @@ class testVhdlFileMethods(unittest.TestCase): lActual.append(iIndex) # Compare self.assertEqual(lActual, lExpected) + + def test_architecture_keyword(self): + lExpected = [] + lExpected.append((3,0)) + lExpected.append((11,0)) + lExpected.append((18,0)) + lExpected.append((25,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_semicolon(self): + lExpected = [] + lExpected.append((7,5)) + lExpected.append((15,1)) + lExpected.append((22,3)) + lExpected.append((34,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_semicolon): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_end_keyword(self): + lExpected = [] + lExpected.append((7,0)) + lExpected.append((15,0)) + lExpected.append((22,0)) + lExpected.append((31,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_end_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_end_architecture_keyword(self): + lExpected = [] + lExpected.append((7,2)) + lExpected.append((22,2)) + lExpected.append((32,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_end_architecture_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_simple_name(self): + lExpected = [] + lExpected.append((7,4)) + lExpected.append((33,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_simple_name): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_identifier(self): + lExpected = [] + lExpected.append((3,2)) + lExpected.append((11,2)) + lExpected.append((18,2)) + lExpected.append((26,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_identifier): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_of_keyword(self): + lExpected = [] + lExpected.append((3,4)) + lExpected.append((11,4)) + lExpected.append((18,4)) + lExpected.append((27,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_of_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_entity_name(self): + lExpected = [] + lExpected.append((3,6)) + lExpected.append((11,6)) + lExpected.append((18,6)) + lExpected.append((28,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_entity_name): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_is_keyword(self): + lExpected = [] + lExpected.append((3,8)) + lExpected.append((11,8)) + lExpected.append((18,8)) + lExpected.append((29,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_is_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) + + def test_architecture_begin_keyword(self): + lExpected = [] + lExpected.append((5,0)) + lExpected.append((13,0)) + lExpected.append((20,0)) + lExpected.append((30,0)) + + lActual = [] + for iLine, lLine in enumerate(oFile.get_lines()): + for iItem, oItem in enumerate(lLine.objects): + if isinstance(oItem, parser.architecture_begin_keyword): + lActual.append((iLine, iItem)) + + self.assertEqual(lExpected, lActual) diff --git a/vsg/vhdlFile/classify/architecture.py b/vsg/vhdlFile/classify/architecture.py index 3558542a..4ec0603b 100644 --- a/vsg/vhdlFile/classify/architecture.py +++ b/vsg/vhdlFile/classify/architecture.py @@ -1,15 +1,115 @@ import re +from vsg import parser -def architecture(self, dVars, oLine): - classify_architecture_keyword(self, dVars, oLine) +''' +architecture identifier of entity_name is + + architecture_declarative_part + +begin + + architecture_statement_part + +end [ architecture ] [ architecture_simple_name ] +''' + + +def architecture(self, dVars, lTokens, lObjects, oLine): + classify_architecture_keyword_old(self, dVars, oLine) if oLine.insideArchitecture: classify_begin_keyword(dVars, oLine) classify_end_keyword(dVars, oLine) + for iToken, sToken in enumerate(lTokens): + if not dVars['bArchitectureKeywordFound']: + classify_architecture_keyword(sToken, iToken, lObjects, dVars, oLine) + else: + if not dVars['bArchitectureIdentifierFound']: + classify_architecture_identifier(sToken, iToken, lObjects, dVars, oLine) + else: + classify_architecture_of_keyword(sToken, iToken, lObjects, dVars, oLine) + if not dVars['bArchitectureEntityNameFound']: + classify_architecture_entity_name(sToken, iToken, lObjects, dVars, oLine) + else: + if not dVars['bArchitectureIsKeywordFound']: + classify_architecture_is_keyword(sToken, iToken, lObjects, dVars, oLine) + else: + if not dVars['bArchitectureBeginKeywordFound']: + classify_architecture_begin_keyword(sToken, iToken, lObjects, dVars, oLine) + else: + if not dVars['bArchitectureEndKeywordFound']: + classify_architecture_end(sToken, iToken, lObjects, dVars, oLine) + else: + classify_end_architecture_keyword(sToken, iToken, lObjects, dVars, oLine) + classify_architecture_semicolon(sToken, iToken, lObjects, dVars, oLine) + classify_architecture_simple_name(sToken, iToken, lObjects, dVars, oLine) -def classify_architecture_keyword(self, dVars, oLine): + +def classify_architecture_identifier(sToken, iToken, lObjects, dVars, oLine): + if not isinstance(lObjects[iToken], parser.whitespace) and not isinstance(lObjects[iToken], parser.comment): + lObjects[iToken] = parser.architecture_identifier(sToken) + dVars['bArchitectureIdentifierFound'] = True + + +def classify_architecture_entity_name(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() != 'of' and sToken.lower() != 'is' and not isinstance(lObjects[iToken], parser.whitespace) and not isinstance(lObjects[iToken], parser.comment): + lObjects[iToken] = parser.architecture_entity_name(sToken) + dVars['bArchitectureEntityNameFound'] = True + + +def classify_architecture_simple_name(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() != 'architecture' and sToken != ';' and not isinstance(lObjects[iToken], parser.whitespace) and not isinstance(lObjects[iToken], parser.comment): + lObjects[iToken] = parser.architecture_simple_name(sToken) + + +def classify_architecture_is_keyword(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'is': + lObjects[iToken] = parser.architecture_is_keyword(sToken) + dVars['bArchitectureIsKeywordFound'] = True + + +def classify_architecture_begin_keyword(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'begin': + lObjects[iToken] = parser.architecture_begin_keyword(sToken) + dVars['bArchitectureBeginKeywordFound'] = True + + +def classify_architecture_of_keyword(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'of': + lObjects[iToken] = parser.architecture_of_keyword(sToken) + + +def classify_architecture_keyword(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'architecture': + lObjects[iToken] = parser.architecture_keyword(sToken) + dVars['bArchitectureKeywordFound'] = True + + +def classify_end_architecture_keyword(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'architecture': + lObjects[iToken] = parser.architecture_end_architecture_keyword(sToken) + + +def classify_architecture_end(sToken, iToken, lObjects, dVars, oLine): + if sToken.lower() == 'end': + lObjects[iToken] = parser.architecture_end_keyword(sToken) + dVars['bArchitectureEndKeywordFound'] = True + + +def classify_architecture_semicolon(sToken, iToken, lObjects, dVars, oLine): + if sToken == ';': + lObjects[iToken] = parser.architecture_semicolon(sToken) + dVars['bArchitectureKeywordFound'] = False + dVars['bArchitectureIdentifierFound'] = False + dVars['bArchitectureEntityNameFound'] = False + dVars['bArchitectureIsKeywordFound'] = False + dVars['bArchitectureBeginKeywordFound'] = False + dVars['bArchitectureEndKeywordFound'] = False + + +def classify_architecture_keyword_old(self, dVars, oLine): if re.match('^\s*architecture', oLine.lineLower) and not oLine.insideArchitecture: self.hasArchitecture = True oLine.isArchitectureKeyword = True diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 9b082fcb..0d6bcd74 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -62,6 +62,13 @@ class vhdlFile(): dVars['bInsideUse'] = False + dVars['bArchitectureKeywordFound'] = False + dVars['bArchitectureIdentifierFound'] = False + dVars['bArchitectureEntityNameFound'] = False + dVars['bArchitectureIsKeywordFound'] = False + dVars['bArchitectureBeginKeywordFound'] = False + dVars['bArchitectureEndKeywordFound'] = False + oLinePrevious = line.blank_line() for sLine in self.filecontent: @@ -90,7 +97,7 @@ class vhdlFile(): classify.generic(dVars, oLine) classify.concurrent(dVars, oLine) - classify.architecture(self, dVars, oLine) + classify.architecture(self, dVars, lTokens, lObjects, oLine) classify.package_body(dVars, oLine) classify.block(self, dVars, oLine) classify.package(dVars, oLine)