Adding component objects.

Refactoring objects into a token library instead of a parser file.
This commit is contained in:
Jeremiah Leary 2020-08-02 08:48:18 -05:00
parent b993b2a746
commit f32d4e1a1b
6 changed files with 251 additions and 7 deletions

View File

@ -0,0 +1,19 @@
component fifo is
end component fifo;
-- Variations
component fifo is
end component;
component fifo
end component fifo;
component fifo
end component;

View File

@ -0,0 +1,114 @@
import os
import unittest
from vsg import vhdlFile
from vsg.tests import utils
from vsg.token import component
lFile = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','component','classification_test_input.vhd'))
oFile = vhdlFile.vhdlFile(lFile)
class test_component_parsing(unittest.TestCase):
def test_component_keyword(self):
lExpected = []
lExpected.append((2,0))
lExpected.append((8,0))
lExpected.append((12,0))
lExpected.append((16,0))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.keyword):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_identifier(self):
lExpected = []
lExpected.append((2,2))
lExpected.append((8,2))
lExpected.append((12,2))
lExpected.append((16,2))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.identifier):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_is_keyword(self):
lExpected = []
lExpected.append((2,4))
lExpected.append((8,4))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.is_keyword):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_end_keyword(self):
lExpected = []
lExpected.append((4,0))
lExpected.append((10,0))
lExpected.append((14,0))
lExpected.append((18,0))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.end_keyword):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_end_component_keyword(self):
lExpected = []
lExpected.append((4,2))
lExpected.append((10,2))
lExpected.append((14,2))
lExpected.append((18,2))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.end_component_keyword):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_simple_name(self):
lExpected = []
lExpected.append((4,4))
lExpected.append((14,4))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.simple_name):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)
def test_component_semicolon(self):
lExpected = []
lExpected.append((4,5))
lExpected.append((10,3))
lExpected.append((14,5))
lExpected.append((18,3))
lActual = []
for iLine, lLine in enumerate(oFile.get_lines()):
for iItem, oItem in enumerate(lLine.objects):
if isinstance(oItem, component.semicolon):
lActual.append((iLine, iItem))
self.assertEqual(lExpected, lActual)

2
vsg/token/__init__.py Normal file
View File

@ -0,0 +1,2 @@

41
vsg/token/component.py Normal file
View File

@ -0,0 +1,41 @@
from vsg import parser
###############################################################################
# Component objects
###############################################################################
class keyword(parser.keyword):
def __init__(self, sString):
parser.keyword.__init__(self, sString)
class identifier(parser.identifier):
def __init__(self, sString):
parser.identifier.__init__(self, sString)
class is_keyword(parser.keyword):
def __init__(self, sString):
parser.keyword.__init__(self, sString)
class end_keyword(parser.keyword):
def __init__(self, sString):
parser.keyword.__init__(self, sString)
class end_component_keyword(parser.keyword):
def __init__(self, sString):
parser.keyword.__init__(self, sString)
class simple_name(parser.simple_name):
def __init__(self, sString):
parser.simple_name.__init__(self, sString)
class semicolon(parser.semicolon):
def __init__(self, sString):
parser.semicolon.__init__(self)

View File

@ -1,14 +1,17 @@
import re
from vsg.token import component as token
from vsg import parser
def component(dVars, oLine):
def component(dVars, lTokens, lObjects, oLine):
'''
Classifies component declarations.
component identifier [is]
[generic (generic_interface_list) ;]
[port (port_interface_list) ;]
end component [identifier] ;
component identifier [ is ]
[ local_generic_clause ]
[ local_port_clause ]
end component [ component_simple_name ] ;
Sets the following line attributes:
@ -32,3 +35,63 @@ def component(dVars, oLine):
oLine.isComponentEnd = True
oLine.indentLevel = 1
dVars['iCurrentIndentLevel'] -= 1
for iToken, sToken in enumerate(lTokens):
if not dVars['bComponentKeywordFound']:
classify_keyword(sToken, iToken, lObjects, dVars, oLine)
else:
if not dVars['bComponentIdentifierFound']:
classify_identifier(sToken, iToken, lObjects, dVars, oLine)
else:
if not dVars['bComponentIsKeywordFound']:
classify_is_keyword(sToken, iToken, lObjects, dVars, oLine)
if not dVars['bComponentEndKeywordFound']:
classify_end_keyword(sToken, iToken, lObjects, dVars, oLine)
else:
classify_end_component_keyword(sToken, iToken, lObjects, dVars, oLine)
classify_semicolon(sToken, iToken, lObjects, dVars, oLine)
classify_simple_name(sToken, iToken, lObjects, dVars, oLine)
def classify_keyword(sToken, iToken, lObjects, dVars, oLine):
if sToken.lower() == 'component':
lObjects[iToken] = token.keyword(sToken)
dVars['bComponentKeywordFound'] = True
def classify_identifier(sToken, iToken, lObjects, dVars, oLine):
if not isinstance(lObjects[iToken], parser.whitespace) and not isinstance(lObjects[iToken], parser.comment):
lObjects[iToken] = token.identifier(sToken)
dVars['bComponentIdentifierFound'] = True
def classify_is_keyword(sToken, iToken, lObjects, dVars, oLine):
if sToken.lower() == 'is':
lObjects[iToken] = token.is_keyword(sToken)
dVars['bComponentIsKeywordFound'] = True
def classify_end_keyword(sToken, iToken, lObjects, dVars, oLine):
if sToken.lower() == 'end':
lObjects[iToken] = token.end_keyword(sToken)
dVars['bComponentEndKeywordFound'] = True
def classify_end_component_keyword(sToken, iToken, lObjects, dVars, oLine):
if sToken.lower() == 'component':
lObjects[iToken] = token.end_component_keyword(sToken)
def classify_simple_name(sToken, iToken, lObjects, dVars, oLine):
if sToken.lower() != 'component' and sToken != ';' and not isinstance(lObjects[iToken], parser.whitespace) and not isinstance(lObjects[iToken], parser.comment):
lObjects[iToken] = token.simple_name(sToken)
def classify_semicolon(sToken, iToken, lObjects, dVars, oLine):
if sToken == ';':
lObjects[iToken] = token.semicolon(sToken)
dVars['bComponentKeywordFound'] = False
dVars['bComponentIdentifierFound'] = False
dVars['bComponentIsKeywordFound'] = False
dVars['bComponentEndKeywordFound'] = False

View File

@ -104,6 +104,11 @@ class vhdlFile():
dVars['bAttributeKeywordFound'] = False
dVars['bAttributeColonFound'] = False
dVars['bComponentKeywordFound'] = False
dVars['bComponentIdentifierFound'] = False
dVars['bComponentIsKeywordFound'] = False
dVars['bComponentEndKeywordFound'] = False
oLinePrevious = line.blank_line()
for sLine in self.filecontent:
@ -116,7 +121,7 @@ class vhdlFile():
# preIndent = str(dVars['iCurrentIndentLevel'])
update.inside_attributes(dVars, self.lines[-1], oLine)
classify.blank(oLine)
classify.blank(oLine) # lTokens
classify.whitespace(lTokens, lObjects)
classify.comment(dVars, lTokens, lObjects, oLine)
classify.library(dVars, lTokens, lObjects, oLine)
@ -135,7 +140,7 @@ class vhdlFile():
classify.package_body(dVars, oLine) # lTokens
classify.block(self, dVars, oLine)
classify.package(self, dVars, lTokens, lObjects, oLine)
classify.component(dVars, oLine)
classify.component(dVars, lTokens, lObjects, oLine)
classify.signal(self, dVars, lTokens, lObjects, oLine)
classify.constant(self, dVars, lTokens, lObjects, oLine, oLinePrevious)
classify.variable(self, dVars, lTokens, lObjects, oLine)