Adding documentation to check.py.

This commit is contained in:
Jeremiah C Leary 2017-12-28 20:54:26 -06:00
parent 969f374e75
commit 0b68e9f7f1
4 changed files with 220 additions and 30 deletions

11
docs/api.rst Normal file
View File

@ -0,0 +1,11 @@
API
---
VSG was written to be included in to other tools.
The command line script provides one means of using VSG.
It operates through an API.
.. toctree::
vsg.fix

View File

@ -15,5 +15,6 @@ Welcome to vhdl-style-guide's documentation!
usage
configuring
localizing
rules
phases
rules
api

5
docs/vsg.fix.rst Normal file
View File

@ -0,0 +1,5 @@
vsg.check
=========
.. automodule:: vsg.check
:members:

View File

@ -1,17 +1,40 @@
'''
This module contains functions for rules to perform their checks.
'''
import re
def indent(self, oLine, iLineNumber):
'''Adds a violation if the indent of the line does not match the desired level.'''
'''Adds a violation if the indent of the line does not match the desired level.
Parameters
oLine: (line object)
iLineNumber: (integer)
'''
if not oLine.isBlank:
if not re.match('^\s{' + str(self.indentSize * oLine.indentLevel) + '}\S', oLine.line):
self.add_violation(iLineNumber)
def is_no_blank_line_after(self, oFile, iLineNumber, sUnless=None):
'''Adds a violation if the line after iLineNumber is blank.
This is typically used to compress lines together.'''
'''
Adds a violation if the line after iLineNumber is blank.
This is typically used to compress lines together.
If sUnless is given, then a violation will not occur if there is a blank line following the line with the attribute given for sUnless.
Parameters
oLine: (line object)
iLineNumber: (integer)
sUnless: (string) (line attribute)
'''
if sUnless:
if oFile.lines[iLineNumber + 1].isBlank:
if not oFile.lines[iLineNumber + 2].__dict__[sUnless]:
@ -22,8 +45,20 @@ def is_no_blank_line_after(self, oFile, iLineNumber, sUnless=None):
def is_no_blank_line_before(self, oFile, iLineNumber, sUnless=None):
'''Adds a violation if the line before iLineNumber is blank.
This is typically used to compress lines together.'''
'''
Adds a violation if the line before iLineNumber is blank.
This is typically used to compress lines together.
If sUnless is given, then a violation will not occur if there is a blank line before the line with the attribute given for sUnless.
Parameters
oLine: (line object)
iLineNumber: (integer)
sUnless: (string) (line attribute)
'''
if sUnless:
if oFile.lines[iLineNumber - 1].isBlank:
if not oFile.lines[iLineNumber - 2].__dict__[sUnless]:
@ -33,30 +68,57 @@ def is_no_blank_line_before(self, oFile, iLineNumber, sUnless=None):
def is_blank_line_after(self, oFile, iLineNumber):
'''Adds a violation if the line after iLineNumber is not blank.
This is typically used to compress lines together.'''
'''
Adds a violation if the line after iLineNumber is not blank.
This is typically used to compress lines together.
Parameters
oFile: (vhdlFile object)
iLineNumber: (integer)
'''
if not oFile.lines[iLineNumber + 1].isBlank:
self.add_violation(iLineNumber)
def is_blank_line_before(self, oFile, iLineNumber):
'''Adds a violation if the line before iLineNumber is not blank.
This is typically used to compress lines together.'''
'''
Adds a violation if the line before iLineNumber is not blank.
This is typically used to compress lines together.
Parameters
oFile: (vhdlFile object)
iLineNumber: (integer)
'''
if not oFile.lines[iLineNumber - 1].isBlank:
self.add_violation(iLineNumber)
def keyword_alignment(self, iStartGroupIndex, sKeyword, lGroup):
def keyword_alignment(self, iLineNumber, sKeyword, lGroup):
'''
Checks keywords in a group of line objects are aligned in the same column.
Parameters:
iLineNumber: (integer)
sKeyword: (string)
lGroup: (list of line objects)
'''
iKeywordAlignment = None
iMaximumKeywordColumn = 0
sViolationRange = str(iStartGroupIndex) + '-' + str(iStartGroupIndex + len(lGroup) - 1)
sViolationRange = str(iLineNumber) + '-' + str(iLineNumber + len(lGroup) - 1)
self.dFix['violations'][sViolationRange] = {}
self.dFix['violations'][sViolationRange]['line'] = {}
for iIndex, oGroupLine in enumerate(lGroup):
if sKeyword in oGroupLine.line:
self.dFix['violations'][sViolationRange]['line'][iStartGroupIndex + iIndex] = {}
self.dFix['violations'][sViolationRange]['line'][iStartGroupIndex + iIndex]['keywordColumn'] = oGroupLine.line.find(sKeyword)
self.dFix['violations'][sViolationRange]['line'][iLineNumber + iIndex] = {}
self.dFix['violations'][sViolationRange]['line'][iLineNumber + iIndex]['keywordColumn'] = oGroupLine.line.find(sKeyword)
iMaximumKeywordColumn = get_maximum_keyword_column(oGroupLine, sKeyword, iMaximumKeywordColumn)
@ -69,23 +131,67 @@ def keyword_alignment(self, iStartGroupIndex, sKeyword, lGroup):
def get_maximum_keyword_column(oLine, sKeyword, iMaximumKeywordColumn):
'''
Return the highest column count a keyword is found.
Parameters:
oLine: (line object)
sKeyword: (string)
iMaximumKeywordColumn: (integer)
Returns: (integer)
'''
if oLine.line.find(sKeyword) > iMaximumKeywordColumn:
return oLine.line.find(sKeyword)
return iMaximumKeywordColumn
def update_keyword_alignment(oLine, sKeyword, iKeywordAlignment):
'''
Returns the column where the keyword was detected.
Parameters:
oLine: (line object)
sKeyword: (string)
iKeywordAlignment: (integer)
Returns: (integer)
'''
if not iKeywordAlignment:
return oLine.line.find(sKeyword)
return iKeywordAlignment
def add_range_violation(self, sViolationRange):
if sViolationRange not in self.violations:
self.add_violation(sViolationRange)
'''
Appends a range violation to the violation list if it does not already exist.
Parameters:
sViolationRange: (string)
'''
if sViolationRange not in self.violations:
self.add_violation(sViolationRange)
def multiline_alignment(self, iColumn, oLine, iLineNumber):
'''
Checks the alignment of multiline statements.
Parameters:
iColumn: (integer)
oLine: (line object)
iLineNumber: (integer)
'''
if not re.match('\s{' + str(iColumn) + '}\S', oLine.line):
self.add_violation(iLineNumber)
self.dFix['violations'][iLineNumber] = {}
@ -93,16 +199,47 @@ def multiline_alignment(self, iColumn, oLine, iLineNumber):
def is_uppercase(self, sString, iLineNumber):
'''
Checks if a string is uppercase.
Parameters:
sString: (string)
iLineNumber: (integer)
'''
if not sString == sString.upper():
self.add_violation(iLineNumber)
def is_lowercase(self, sString, iLineNumber):
'''
Checks if a string is lowercase.
Parameters:
sString: (string)
iLineNumber: (integer)
'''
if not sString == sString.lower():
self.add_violation(iLineNumber)
def is_single_space_after(self, sString, oLine, iLineNumber):
'''
Checks if a single space is after the string given.
The string is considered a whole word.
Allowances are made for end of line and semicolons.
Parameters:
sString: (string)
oLine: (line object)
iLineNumber: (integer)
'''
if not sString.lower() in oLine.lineLower:
return
if re.match('^.*' + sString + ';', oLine.lineLower):
@ -116,6 +253,17 @@ def is_single_space_after(self, sString, oLine, iLineNumber):
def is_single_space_before(self, sString, oLine, iLineNumber):
'''
Checks if a single space exists before the string given.
The string is considered a whole word.
Parameters:
sString: (string)
oLine: (line object)
iLineNumber: (integer)
'''
if not sString.lower() in oLine.lineLower:
return
if not re.match('^.*\S\s' + sString + '\s', oLine.lineNoComment.lower()) and \
@ -125,6 +273,17 @@ def is_single_space_before(self, sString, oLine, iLineNumber):
def is_single_space_after_character(self, sCharacter, oLine, iLineNumber):
'''
Checks if a single space exists after a series of characters.
NOTE: The characters will match partial words.
Parameters:
sCharacter: (string)
oLine: (line object)
iLineNumber: (integer)
'''
if not re.match('^.*' + sCharacter.lower() + '\s*--', oLine.lineNoComment):
if re.match('^.*' + sCharacter.lower() + '$', oLine.lineNoComment):
return
@ -133,12 +292,32 @@ def is_single_space_after_character(self, sCharacter, oLine, iLineNumber):
def is_single_space_before_character(self, sCharacter, oLine, iLineNumber):
'''
Checks if a single space exists before a series of characters.
NOTE: The characters will match partial words.
Parameters:
sCharacter: (string)
oLine: (line object)
iLineNumber: (integer)
'''
iIndex = oLine.line.find(sCharacter) + len(sCharacter)
if not re.match('^.*\s' + sCharacter.lower(), oLine.lineNoComment[:iIndex]):
self.add_violation(iLineNumber)
def indent_of_comments_above(self, oFile, iLineNumber):
'''
Checks the indent level of consecutive comment lines above the line number given.
Parameters:
oFile: (vhdlFile object)
iLineNumber: (integer)
'''
iIndex = 0
while iLineNumber - iIndex > 1:
iIndex += 1
@ -155,18 +334,15 @@ def has_package_name(oLine):
'''
Returns boolean if package name is found in line.
Parameters
----------
Parameters:
oLine : line object
oLine: (line object)
Returns
-------
Returns: (boolean)
True if package name exists in line.
False if package name does not exist in line.
True if package name exists in line.
False if package name does not exist in line.
'''
lLine = oLine.lineNoComment.lower().split()
for iIndex, sWord in enumerate(lLine):
if sWord == 'package':
@ -180,16 +356,13 @@ def get_package_name(oLine):
Returns the package name in the line.
Parameters
----------
oLine : line object
oLine: (line object)
Returns
-------
Returns: (string)
string = package name or empty string if package name not found.
Package name or empty string if package name not found.
'''
lLine = oLine.lineNoComment.split()
for iIndex, sWord in enumerate(lLine):
if sWord.lower() == 'package' and not lLine[iIndex + 1] == '--':