vhdl-style-guide/modules/rule_if.py

197 lines
6.6 KiB
Python

import rule
import re
class if_rule(rule.rule):
def __init__(self):
rule.rule.__init__(self)
self.name = 'if'
class rule_001(if_rule):
'''If rule 001 checks for the proper indentation at the beginning of the line.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '001'
self.solution = 'Ensure proper indentation.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isIfKeyword or oLine.isElseIfKeyword or oLine.isEndIfKeyword or oLine.isElseKeyword:
self._check_indent(oLine, iLineNumber)
class rule_002(if_rule):
'''If rule 002 checks the if boolean expression is enclosed in ()'s.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '002'
self.solution = 'Enclose boolean expression in ()\'s.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isIfKeyword:
if not re.match('^\s*if\s*\(', oLine.lineLower):
self.add_violation(iLineNumber)
if oLine.isElseIfKeyword:
if not re.match('^\s*elsif\s*\(', oLine.lineLower):
self.add_violation(iLineNumber)
class rule_003(if_rule):
'''If rule 003 checks there is a single space between the if keyword and the (.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '003'
self.solution = 'Ensure only a single space exists between the "if" keyword and the (.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isIfKeyword:
if re.match('^\s*if\s*\(', oLine.lineLower):
if not re.match('^\s*if\s\(', oLine.lineLower):
self.add_violation(iLineNumber)
class rule_004(if_rule):
'''If rule 004 checks there is a single space between the ) and "then" keyword.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '004'
self.solution = 'Ensure only a single space exists between the ) and "then" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isThenKeyword:
if re.match('^\s*.*\)\s*then', oLine.lineLower):
if not re.match('^\s*.*\)\sthen', oLine.lineLower):
self.add_violation(iLineNumber)
class rule_005(if_rule):
'''If rule 005 checks there is a single space between the "elsif" keyword and the (.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '005'
self.solution = 'Ensure only a single space exists between the "elsif" keyword and the (.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isElseIfKeyword:
if re.match('^\s*elsif\s*\(', oLine.lineLower):
if not re.match('^\s*elsif\s\(', oLine.lineLower):
self.add_violation(iLineNumber)
class rule_006(if_rule):
'''If rule 006 checks for an empty line after the if statement.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '006'
self.solution = 'Remove blank line(s) after the "then" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isThenKeyword:
self._is_no_blank_line_after(oFile, iLineNumber)
class rule_007(if_rule):
'''If rule 007 checks for an empty line before the "elsif" keyword.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '007'
self.solution = 'Remove blank line(s) before the "elsif" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isElseIfKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
class rule_008(if_rule):
'''If rule 008 checks for an empty line before the "end if" keyword.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '008'
self.solution = 'Remove blank line(s) before the "end if" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isEndIfKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
class rule_009(if_rule):
'''If rule 009 checks the alignment of multiline boolean expressions.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '009'
self.solution = 'Align with space after ( in first line of boolean expression.'
def analyze(self, oFile):
fCheckAlignment = False
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isIfKeyword and oLine.isThenKeyword:
continue
if oLine.isElseIfKeyword and oLine.isThenKeyword:
continue
if oLine.isIfKeyword:
if re.match('^\s+if\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue
if oLine.isElseIfKeyword:
if re.match('^\s+elsif\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue
if oLine.insideIf and fCheckAlignment:
if not re.match('^\s{' + str(iAlignmentColumn + 1) + '}\S', oLine.line):
self.add_violation(iLineNumber)
if oLine.isThenKeyword:
fCheckAlignment = False
class rule_010(if_rule):
'''If rule 010 checks for an empty line before the "else" keyword.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '010'
self.solution = 'Remove blank line(s) before the "else" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isElseKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
class rule_011(if_rule):
'''If rule 011 checks for an empty line after the "else" keyword.'''
def __init__(self):
if_rule.__init__(self)
self.identifier = '011'
self.solution = 'Remove blank line(s) after the "else" keyword.'
def analyze(self, oFile):
for iLineNumber, oLine in enumerate(oFile.lines):
if oLine.isElseKeyword:
self._is_no_blank_line_after(oFile, iLineNumber)
#TODO:
# check if, then, elsif, end if keywords are lower case