diff --git a/vsg/rules/constant/rule_016.py b/vsg/rules/constant/rule_016.py index 219db946..115c135c 100644 --- a/vsg/rules/constant/rule_016.py +++ b/vsg/rules/constant/rule_016.py @@ -277,31 +277,55 @@ def _check_close_paren_new_line(self, oToi): if isinstance(oToken, parser.close_parenthesis): if bOthersClause: - bOthersClause = False - continue + iCloseParen += 1 + if iOpenParen == iCloseParen: + bOthersClause = False + continue + elif iCloseParen > iOpenParen: + bOthersCluase = False if isinstance(oToken, parser.open_parenthesis): + if not bOthersClause: # print(f'_check_new_line_after_comma = {_inside_others_clause(iToken, lTokens)}') - if _inside_others_clause(iToken, lTokens): - bOthersClause = True + if _inside_others_clause(iToken, lTokens): + bOthersClause = True + iOpenParen = 0 + iCloseParen = 0 + else: + iOpenParen += 1 if bOthersClause: continue -# if bAssignmentFound: -# if isinstance(oToken, parser.close_parenthesis): -# iCloseParen += 1 -# if iOpenParen == iCloseParen: -# bAssignmentFound = False -# -# if isinstance(oToken, parser.open_parenthesis): -# iOpenParen += 1 -# -# continue -# -# if not bAssignmentFound and oToken.get_value() == '=>': -# bAssignmentFound = True -# continue + + if bAssignmentFound: + if isinstance(oToken, parser.close_parenthesis): + iCloseParen += 1 + if iOpenParen == iCloseParen: + bAssignmentFound = False + continue + elif iCloseParen > iOpenParen: + bAssignmentFound = False + else: + continue + + elif isinstance(oToken, parser.open_parenthesis): + iOpenParen += 1 + continue + + elif isinstance(oToken, parser.comma): + if iOpenParen == iCloseParen: + bAssignmentFound = False + else: + continue + else: + continue + + if not bAssignmentFound and oToken.get_value() == '=>': + bAssignmentFound = True + iOpenParen = 0 + iCloseParen = 0 + continue if isinstance(oToken, parser.close_parenthesis): if utils.does_token_start_line(iToken, lTokens): @@ -359,24 +383,30 @@ def _check_new_line_after_comma(self, oToi): if bOthersClause: continue -# if bAssignmentFound: -# if isinstance(oToken, parser.close_parenthesis): -# iCloseParen += 1 -# if iOpenParen == iCloseParen: -# bAssignmentFound = False -# elif iCloseParen > iOpenParen: -# bAssignmentFound = False -# -# if isinstance(oToken, parser.open_parenthesis): -# iOpenParen += 1 -# -# continue -# -# if not bAssignmentFound and oToken.get_value() == '=>': -# bAssignmentFound = True -# iOpenParen = 0 -# iCloseParen = 0 -# continue + if bAssignmentFound: + if isinstance(oToken, parser.close_parenthesis): + iCloseParen += 1 + if iOpenParen == iCloseParen: + bAssignmentFound = False + elif iCloseParen > iOpenParen: + bAssignmentFound = False + + if isinstance(oToken, parser.open_parenthesis): + iOpenParen += 1 + + if isinstance(oToken, parser.comma): + if iOpenParen == iCloseParen: + bAssignmentFound = False + else: + continue + else: + continue + + if not bAssignmentFound and oToken.get_value() == '=>': + bAssignmentFound = True + iOpenParen = 0 + iCloseParen = 0 + continue if isinstance(oToken, parser.comma): # print('comma detected') diff --git a/vsg/tests/constant/rule_016_test_input_assignment.fixed.vhd b/vsg/tests/constant/rule_016_test_input_assignment.fixed.vhd new file mode 100644 index 00000000..54bcd750 --- /dev/null +++ b/vsg/tests/constant/rule_016_test_input_assignment.fixed.vhd @@ -0,0 +1,12 @@ + +architecture rtl of fifo is + + constant cons1 : t_type := + ( + 1 => func1(std_logic_vector(G_GEN), G_GEN2), + 2 => func1(std_logic_vector(G_GEN3), G_GEN4) + ); + +begin + +end architecture rtl; diff --git a/vsg/tests/constant/rule_016_test_input_assignment.vhd b/vsg/tests/constant/rule_016_test_input_assignment.vhd new file mode 100644 index 00000000..c86860e1 --- /dev/null +++ b/vsg/tests/constant/rule_016_test_input_assignment.vhd @@ -0,0 +1,9 @@ + +architecture rtl of fifo is + + constant cons1 : t_type := ( + 1 => func1(std_logic_vector(G_GEN), G_GEN2), 2 => func1(std_logic_vector(G_GEN3), G_GEN4)); + +begin + +end architecture rtl; diff --git a/vsg/tests/constant/rule_016_test_input_others.fixed.vhd b/vsg/tests/constant/rule_016_test_input_others.fixed.vhd index b731c4c1..78062f77 100644 --- a/vsg/tests/constant/rule_016_test_input_others.fixed.vhd +++ b/vsg/tests/constant/rule_016_test_input_others.fixed.vhd @@ -1,12 +1,12 @@ architecture rtl of fifo is --- constant AVMM_SLAVE_NULL : t_avmm_slave := --- ( --- (others => '0'), --- '0', --- '0' --- ); + constant AVMM_SLAVE_NULL : t_avmm_slave := + ( + (others => '0'), + '0', + '0' + ); constant cons1 : t_type := ( @@ -15,6 +15,11 @@ architecture rtl of fifo is (others => '0') ); + constant cons2 : t_type := + (others => (valid => '0', data => (others => '0')), + others => (1 => '0', (others => '0') + ); + begin end architecture rtl; diff --git a/vsg/tests/constant/rule_016_test_input_others.vhd b/vsg/tests/constant/rule_016_test_input_others.vhd index 1ccd002d..f2d9efd9 100644 --- a/vsg/tests/constant/rule_016_test_input_others.vhd +++ b/vsg/tests/constant/rule_016_test_input_others.vhd @@ -1,16 +1,19 @@ architecture rtl of fifo is --- constant AVMM_SLAVE_NULL : t_avmm_slave := --- ( --- (others => '0'), --- '0', --- '0' --- ); + constant AVMM_SLAVE_NULL : t_avmm_slave := + ( + (others => '0'), + '0', + '0' + ); constant cons1 : t_type := ((others => '0'),(1 => '0', others => '1'),(others => '0')); + constant cons2 : t_type := + (others => (valid => '0', data => (others => '0')), others => (1 => '0', (others => '0')); + begin end architecture rtl; diff --git a/vsg/tests/constant/test_rule_016.py b/vsg/tests/constant/test_rule_016.py index efd95f5a..fb3c7bb0 100644 --- a/vsg/tests/constant/test_rule_016.py +++ b/vsg/tests/constant/test_rule_016.py @@ -10,6 +10,7 @@ sTestDir = os.path.dirname(__file__) lFile, eError =vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir,'rule_016_test_input.vhd')) lFileOthers, eError = vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir, 'rule_016_test_input_others.vhd')) +lFileAssignment, eError = vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir, 'rule_016_test_input_assignment.vhd')) lExpected_first_paren_new_line_true = [] lExpected_first_paren_new_line_true.append('') @@ -63,6 +64,10 @@ lExpected_others = [] lExpected_others.append('') utils.read_file(os.path.join(sTestDir, 'rule_016_test_input_others.fixed.vhd'), lExpected_others) +lExpected_assignment = [] +lExpected_assignment.append('') +utils.read_file(os.path.join(sTestDir, 'rule_016_test_input_assignment.fixed.vhd'), lExpected_assignment) + class test_constant_rule(unittest.TestCase): @@ -496,3 +501,22 @@ class test_constant_rule(unittest.TestCase): oRule.analyze(oFileOthers) self.assertEqual(oRule.violations, []) + def test_fix_rule_016_assignment(self): + self.maxDiff = None + oRule = constant.rule_016() + oRule.first_paren_new_line = True + oRule.last_paren_new_line = True + oRule.open_paren_new_line = True + oRule.close_paren_new_line = True + oRule.new_line_after_comma = True + + oFileAssignment = vhdlFile.vhdlFile(lFileAssignment) + oRule.fix(oFileAssignment) + + lActual = oFileAssignment.get_lines() + + self.assertEqual(lExpected_assignment, lActual) + + oRule.analyze(oFileAssignment) + self.assertEqual(oRule.violations, []) +