From f8cd2d571d532eb9aa81a5c3136c78149a72bbcf Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Mon, 7 Sep 2020 10:05:33 -0500 Subject: [PATCH] Adding parsing of record_type_definition. --- .../classification_results.txt | 120 ++++++++++++++++++ .../classification_test_input.vhd | 18 +++ .../test_record_type_definition.py | 30 +++++ vsg/token/element_declaration.py | 27 ++-- vsg/token/record_type_definition.py | 22 +++- .../classify_new/composite_type_definition.py | 8 +- .../classify_new/element_declaration.py | 24 ++++ .../element_subtype_definition.py | 11 ++ .../classify_new/record_type_definition.py | 32 +++++ vsg/vhdlFile/utils.py | 9 ++ 10 files changed, 278 insertions(+), 23 deletions(-) create mode 100644 vsg/tests/record_type_definition/classification_results.txt create mode 100644 vsg/tests/record_type_definition/classification_test_input.vhd create mode 100644 vsg/tests/vhdlFile_new/test_record_type_definition.py create mode 100644 vsg/vhdlFile/classify_new/element_declaration.py create mode 100644 vsg/vhdlFile/classify_new/element_subtype_definition.py create mode 100644 vsg/vhdlFile/classify_new/record_type_definition.py diff --git a/vsg/tests/record_type_definition/classification_results.txt b/vsg/tests/record_type_definition/classification_results.txt new file mode 100644 index 00000000..5fb111fd --- /dev/null +++ b/vsg/tests/record_type_definition/classification_results.txt @@ -0,0 +1,120 @@ +-------------------------------------------------------------------------------- +0 | None | +-------------------------------------------------------------------------------- +1 | None | + +-------------------------------------------------------------------------------- +2 | None | architecture RTL of FIFO is + + + + + +-------------------------------------------------------------------------------- +3 | None | + +-------------------------------------------------------------------------------- +4 | None | type Operation is record + + + + +-------------------------------------------------------------------------------- +5 | None | Mnemonic : String (1 to 10); + + + + + + + + + +-------------------------------------------------------------------------------- +6 | None | OpCode : Bit_Vector(3 downto 0); + + + + + + + + + +-------------------------------------------------------------------------------- +7 | None | Op1, Op2, Res : RegName; + + + + + + + + +-------------------------------------------------------------------------------- +8 | None | end record; + + + +-------------------------------------------------------------------------------- +9 | None | + +-------------------------------------------------------------------------------- +10 | None | type Operation is record + + + + +-------------------------------------------------------------------------------- +11 | None | Mnemonic : String (1 to 10); + + + + + + + + + +-------------------------------------------------------------------------------- +12 | None | OpCode : Bit_Vector(3 downto 0); + + + + + + + + + +-------------------------------------------------------------------------------- +13 | None | Op1, Op2, Res : RegName; + + + + + + + + +-------------------------------------------------------------------------------- +14 | None | end record Operation; + + + + +-------------------------------------------------------------------------------- +15 | None | + +-------------------------------------------------------------------------------- +16 | None | begin + +-------------------------------------------------------------------------------- +17 | None | + +-------------------------------------------------------------------------------- +18 | None | end architecture RTL; + + + + diff --git a/vsg/tests/record_type_definition/classification_test_input.vhd b/vsg/tests/record_type_definition/classification_test_input.vhd new file mode 100644 index 00000000..67d7a68b --- /dev/null +++ b/vsg/tests/record_type_definition/classification_test_input.vhd @@ -0,0 +1,18 @@ + +architecture RTL of FIFO is + + type Operation is record + Mnemonic : String (1 to 10); + OpCode : Bit_Vector(3 downto 0); + Op1, Op2, Res : RegName; + end record; + + type Operation is record + Mnemonic : String (1 to 10); + OpCode : Bit_Vector(3 downto 0); + Op1, Op2, Res : RegName; + end record Operation; + +begin + +end architecture RTL; diff --git a/vsg/tests/vhdlFile_new/test_record_type_definition.py b/vsg/tests/vhdlFile_new/test_record_type_definition.py new file mode 100644 index 00000000..009f48a0 --- /dev/null +++ b/vsg/tests/vhdlFile_new/test_record_type_definition.py @@ -0,0 +1,30 @@ +import os + +import unittest + +from vsg.tests import utils +from vsg.vhdlFile import vhdlFile_new + +sLrmUnit = utils.extract_lrm_unit_name(__name__) + +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/element_declaration.py b/vsg/token/element_declaration.py index 4170f145..0867dbf4 100644 --- a/vsg/token/element_declaration.py +++ b/vsg/token/element_declaration.py @@ -1,9 +1,19 @@ from vsg import parser -############################################################################### -# Entity objects -############################################################################### + +class colon(parser.colon): + + def __init__(self, sString=':'): + parser.colon.__init__(self) + + +class semicolon(parser.semicolon): + + def __init__(self, sString=';'): + parser.semicolon.__init__(self) + +# jcl - remove the folling objects when the new parser is done class identifier(parser.identifier): @@ -15,18 +25,7 @@ class comma(parser.comma): def __init__(self): parser.comma.__init__(self) -class colon(parser.colon): - - def __init__(self): - parser.colon.__init__(self) - class element_subtype_definition(parser.subtype_definition): def __init__(self, sString): parser.subtype_definition.__init__(self, sString) - -class semicolon(parser.semicolon): - - def __init__(self): - parser.semicolon.__init__(self) - diff --git a/vsg/token/record_type_definition.py b/vsg/token/record_type_definition.py index 4fc7bf0d..cb55bcc0 100644 --- a/vsg/token/record_type_definition.py +++ b/vsg/token/record_type_definition.py @@ -1,21 +1,33 @@ from vsg import parser -############################################################################### -# Entity objects -############################################################################### -class keyword(parser.keyword): +class record_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 record_keyword(parser.keyword): + +class end_record_keyword(parser.keyword): + + def __init__(self, sString): + parser.keyword.__init__(self, sString) + + +class record_type_simple_name(parser.simple_name): + + def __init__(self, sString): + parser.simple_name.__init__(self, sString) + +# jcl - remove the following objects when the new parser is done + +class keyword(parser.keyword): def __init__(self, sString): parser.keyword.__init__(self, sString) diff --git a/vsg/vhdlFile/classify_new/composite_type_definition.py b/vsg/vhdlFile/classify_new/composite_type_definition.py index 09d05e99..d5a21ac6 100644 --- a/vsg/vhdlFile/classify_new/composite_type_definition.py +++ b/vsg/vhdlFile/classify_new/composite_type_definition.py @@ -1,7 +1,7 @@ from vsg.vhdlFile.classify_new import array_type_definition -#from vsg.vhdlFile.classify_new import record_type_definition +from vsg.vhdlFile.classify_new import record_type_definition ''' composite_type_definition ::= @@ -16,8 +16,8 @@ def detect(iToken, lObjects): if iReturn != iToken: return iReturn -# iReturn = record_type_definition.detect(iToken, lObjects) -# if iReturn != iToken: -# return iReturn + iReturn = record_type_definition.detect(iToken, lObjects) + if iReturn != iToken: + return iReturn return iToken diff --git a/vsg/vhdlFile/classify_new/element_declaration.py b/vsg/vhdlFile/classify_new/element_declaration.py new file mode 100644 index 00000000..1b6f52ac --- /dev/null +++ b/vsg/vhdlFile/classify_new/element_declaration.py @@ -0,0 +1,24 @@ + +from vsg.token import element_declaration as token + +from vsg.vhdlFile import utils + +from vsg.vhdlFile.classify_new import identifier_list +from vsg.vhdlFile.classify_new import element_subtype_definition + +''' + element_declaration ::= + identifier_list : element_subtype_definition ; +''' + + +def classify(iToken, lObjects): + iCurrent = identifier_list.classify(iToken, lObjects) + + iCurrent = utils.assign_next_token_required(':', token.colon, iCurrent, lObjects) + + iCurrent = utils.classify_subelement_until(';', element_subtype_definition, iCurrent, lObjects) + + iCurrent = utils.assign_next_token_required(';', token.semicolon, iCurrent, lObjects) + + return iCurrent diff --git a/vsg/vhdlFile/classify_new/element_subtype_definition.py b/vsg/vhdlFile/classify_new/element_subtype_definition.py new file mode 100644 index 00000000..c6d4a8aa --- /dev/null +++ b/vsg/vhdlFile/classify_new/element_subtype_definition.py @@ -0,0 +1,11 @@ + +from vsg.vhdlFile.classify_new import subtype_indication + +''' + element_subtype_definition ::= + subtype_indication +''' + + +def classify(iToken, lObjects): + return subtype_indication.classify(iToken, lObjects) diff --git a/vsg/vhdlFile/classify_new/record_type_definition.py b/vsg/vhdlFile/classify_new/record_type_definition.py new file mode 100644 index 00000000..d721e0a4 --- /dev/null +++ b/vsg/vhdlFile/classify_new/record_type_definition.py @@ -0,0 +1,32 @@ + +from vsg.token import record_type_definition as token + +from vsg.vhdlFile import utils + +from vsg.vhdlFile.classify_new import element_declaration + +''' + record_type_definition ::= + record + element_declaration + { element_declaration } + end record [ *record_type*_simple_name ] +''' + + +def detect(iToken, lObjects): + if utils.is_next_token('record', iToken, lObjects): + return classify(iToken, lObjects) + + return iToken + + +def classify(iToken, lObjects): + iCurrent = utils.assign_next_token_required('record', token.record_keyword, iToken, lObjects) + + iCurrent = utils.classify_subelement_until('end', element_declaration, iCurrent, lObjects) + + iCurrent = utils.assign_next_token_required('end', token.end_keyword, iCurrent, lObjects) + iCurrent = utils.assign_next_token_required('record', token.end_record_keyword, iCurrent, lObjects) + iCurrent = utils.assign_next_token_if_not(';', token.record_type_simple_name, iCurrent, lObjects) + return iCurrent diff --git a/vsg/vhdlFile/utils.py b/vsg/vhdlFile/utils.py index 2b3bdf0b..b5741328 100644 --- a/vsg/vhdlFile/utils.py +++ b/vsg/vhdlFile/utils.py @@ -99,6 +99,15 @@ def assign_next_token_if(sToken, token, iToken, lObjects): return iToken +def assign_next_token_if_not(sToken, token, iToken, lObjects): + iCurrent = find_next_token(iToken, lObjects) + if not object_value_is(lObjects, iCurrent, sToken): + lObjects[iCurrent] = token(lObjects[iCurrent].get_value()) + iCurrent += 1 + return iCurrent + return iToken + + def assign_next_token_required(sToken, token, iToken, lObjects): iCurrent = find_next_token(iToken, lObjects) if object_value_is(lObjects, iCurrent, sToken):