Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 55 additions & 71 deletions nsiqcppstyle_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def t_CPPCOMMENT(t):


def t_error(t):
console.Out.Verbose("Illegal character '%s'" % t.value[0], t.lexer.lineno)
console.Out.Verbose(f"Illegal character '{t.value[0]}'", t.lexer.lineno)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function t_error refactored with the following changes:

t.lexer.skip(1)


Expand All @@ -403,9 +403,9 @@ def __init__(self, filename, data=None):
try:
self.data = f.read()
except UnicodeDecodeError as ex:
console.Out.Ci("[ERROR] UnicodeDecodeError in CppLexerNavigator: " + str(ex))
console.Out.Ci(f"[ERROR] UnicodeDecodeError in CppLexerNavigator: {str(ex)}")
console.Out.Ci(
"[ERROR] Exception occurred reading file '%s', convert from UTF16LE to UTF8" % (filename),
f"[ERROR] Exception occurred reading file '{filename}', convert from UTF16LE to UTF8"
Comment on lines -406 to +408
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.__init__ refactored with the following changes:

)
raise
self.lines = self.data.splitlines()
Expand Down Expand Up @@ -445,7 +445,7 @@ def ProcessIfdef(self, token):
self.ifdefstack.append(True)
elif Match(r"^#\s*endif$", token.value) and len(self.ifdefstack) != 0:
self.ifdefstack.pop()
return any(not ifdef for ifdef in self.ifdefstack)
return not all(self.ifdefstack)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.ProcessIfdef refactored with the following changes:


def Backup(self):
"""
Expand Down Expand Up @@ -473,9 +473,7 @@ def GetCurTokenLine(self):
Get Current Token, if No current token, return None
"""
curToken = self.GetCurToken()
if curToken is not None:
return self.lines[curToken.lineno - 1]
return None
return self.lines[curToken.lineno - 1] if curToken is not None else None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetCurTokenLine refactored with the following changes:


def _MoveToToken(self, token):
self.tokenindex = token.index
Expand All @@ -488,9 +486,7 @@ def _GetColumn(self, token):
if last_cr < 0:
last_cr = -1
column = token.lexpos - last_cr
if column == 0:
return 1
return column
return 1 if column == 0 else column
Comment on lines -491 to +489
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator._GetColumn refactored with the following changes:


def GetCurToken(self):
"""
Expand Down Expand Up @@ -630,11 +626,10 @@ def GetNextToken(self, skipWhiteSpace=False, skipComment=False, skipDirective=Fa
def GetNextMatchingGT(self, keepCur=False):
if keepCur:
self.PushTokenIndex()
gtStack = []
if self.GetCurToken().type != "LT":
msg = "Matching next GT token should be examined when cur token is <"
raise RuntimeError(msg)
gtStack.append(self.GetCurToken())
gtStack = [self.GetCurToken()]
Comment on lines -633 to +632
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetNextMatchingGT refactored with the following changes:

t = self._GetNextMatchingGTToken(gtStack)
if keepCur:
self.PopTokenIndex()
Expand Down Expand Up @@ -666,11 +661,10 @@ def GetNextMatchingToken(self, keepCur=False):
"""
if keepCur:
self.PushTokenIndex()
tokenStack = []
if self.GetCurToken().type not in ["LPAREN", "LBRACE", "LBRACKET"]:
msg = "Matching token should be examined when cur token is { [ ("
raise RuntimeError(msg)
tokenStack.append(self.GetCurToken())
tokenStack = [self.GetCurToken()]
Comment on lines -669 to +667
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetNextMatchingToken refactored with the following changes:

t = self._GetNextMatchingToken(tokenStack)
if keepCur:
self.PopTokenIndex()
Expand Down Expand Up @@ -771,12 +765,10 @@ def _GetPrevMatchingLTToken(self, tokenStack):
def GetPrevMatchingToken(self, keepCur=False):
if keepCur:
self.PushTokenIndex()
tokenStack = []
if self.GetCurToken().type not in ["RPAREN", "RBRACE", "RBRACKET"]:
msg = "Matching token should be examined when cur token is } ) ]"
raise RuntimeError(msg)
tokenStack.append(self.GetCurToken())

tokenStack = [self.GetCurToken()]
Comment on lines -774 to +771
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetPrevMatchingToken refactored with the following changes:

t = self._GetPrevMatchingToken(tokenStack)
if keepCur:
self.PopTokenIndex()
Expand Down Expand Up @@ -815,40 +807,35 @@ def _GetPrevMatchingToken(self, tokenStack):
def _SkipContext(self, skipWhiteSpace=False, skipComment=False):
context = []
if skipWhiteSpace:
context.append("SPACE")
context.append("LINEFEED")
context.extend(("SPACE", "LINEFEED"))
if skipComment:
context.append("COMMENT")
context.append("CPPCOMMENT")
context.extend(("COMMENT", "CPPCOMMENT"))
Comment on lines -818 to +812
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator._SkipContext refactored with the following changes:

return context

def _GetNextToken(self):
if self.tokenindex < self.tokenlistsize - 1:
self.tokenindex = self.tokenindex + 1
return self.tokenlist[self.tokenindex]
else:
if self.tokenindex >= self.tokenlistsize - 1:
return None
self.tokenindex = self.tokenindex + 1
return self.tokenlist[self.tokenindex]
Comment on lines -826 to +819
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator._GetNextToken refactored with the following changes:


def _GetPrevToken(self):
if self.tokenindex >= 0:
self.tokenindex = self.tokenindex - 1
if self.tokenindex == -1:
return None
return self.tokenlist[self.tokenindex]
else:
if self.tokenindex < 0:
return None
self.tokenindex = self.tokenindex - 1
return None if self.tokenindex == -1 else self.tokenlist[self.tokenindex]
Comment on lines -833 to +825
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator._GetPrevToken refactored with the following changes:


def GetPrevTokenInType(self, type, keepCur=True, skipPreprocess=True):
if keepCur:
self.PushTokenIndex()
token = None
while True:
token = self.GetPrevToken()
if token is None:
break
elif token.type == type:
if skipPreprocess and token.pp:
continue
if (
token is not None
and token.type == type
and (not skipPreprocess or not token.pp)
or token is None
):
Comment on lines -847 to +838
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetPrevTokenInType refactored with the following changes:

break
if keepCur:
self.PopTokenIndex()
Expand All @@ -860,11 +847,12 @@ def GetPrevTokenInTypeList(self, typelist, keepCur=True, skipPreprocess=True):
token = None
while True:
token = self.GetPrevToken(False, False, skipPreprocess, False)
if token is None:
break
elif token.type in typelist:
if skipPreprocess and token.pp:
continue
if (
token is not None
and token.type in typelist
and (not skipPreprocess or not token.pp)
or token is None
):
Comment on lines -863 to +855
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetPrevTokenInTypeList refactored with the following changes:

break
if keepCur:
self.PopTokenIndex()
Expand All @@ -884,11 +872,12 @@ def GetNextTokenInType(self, type, keepCur=False, skipPreprocess=True):
token = None
while True:
token = self.GetNextToken()
if token is None:
break
elif token.type == type:
if skipPreprocess and token.pp:
continue
if (
token is not None
and token.type == type
and (not skipPreprocess or not token.pp)
or token is None
):
Comment on lines -887 to +880
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetNextTokenInType refactored with the following changes:

break
if keepCur:
self.PopTokenIndex()
Expand All @@ -900,11 +889,12 @@ def GetNextTokenInTypeList(self, typelist, keepCur=False, skipPreprocess=True):
token = None
while True:
token = self.GetNextToken()
if token is None:
break
elif token.type in typelist:
if skipPreprocess and token.pp:
continue
if (
token is not None
and token.type in typelist
and (not skipPreprocess or not token.pp)
or token is None
):
Comment on lines -903 to +897
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.GetNextTokenInTypeList refactored with the following changes:

break
if keepCur:
self.PopTokenIndex()
Expand All @@ -918,9 +908,7 @@ def HasBody(self):

if token_id3 is None and token_id2 is not None:
return True
if token_id2 is not None and token_id2.lexpos < token_id3.lexpos:
return True
return False
return token_id2 is not None and token_id2.lexpos < token_id3.lexpos
Comment on lines -921 to +911
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CppLexerNavigator.HasBody refactored with the following changes:



class Context:
Expand All @@ -933,7 +921,9 @@ def __init__(self, type, name, sig=False, starttoken=None, endtoken=None):
self.additional = ""

def __str__(self):
return ", ".join([self.type, "'" + self.name + "'", str(self.startToken), str(self.endToken)])
return ", ".join(
[self.type, f"'{self.name}'", str(self.startToken), str(self.endToken)]
)
Comment on lines -936 to +926
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Context.__str__ refactored with the following changes:


def IsContextStart(self, token):
return token == self.startToken
Expand All @@ -942,9 +932,10 @@ def IsContextEnd(self, token):
return token == self.endToken

def InScope(self, token):
if token.lexpos >= self.startToken.lexpos and token.lexpos <= self.endToken.lexpos:
return True
return False
return (
token.lexpos >= self.startToken.lexpos
and token.lexpos <= self.endToken.lexpos
)
Comment on lines -945 to +938
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Context.InScope refactored with the following changes:



class ContextStack:
Expand All @@ -955,14 +946,10 @@ def Push(self, context):
self.contextstack.append(context)

def Pop(self):
if self.Size() == 0:
return None
return self.contextstack.pop()
return None if self.Size() == 0 else self.contextstack.pop()
Comment on lines -958 to +949
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ContextStack.Pop refactored with the following changes:


def Peek(self):
if self.Size() == 0:
return None
return self.contextstack[-1]
return None if self.Size() == 0 else self.contextstack[-1]
Comment on lines -963 to +952
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ContextStack.Peek refactored with the following changes:


def SigPeek(self):
i = len(self.contextstack)
Expand Down Expand Up @@ -991,10 +978,9 @@ def ContainsIn(self, type):
return False

def __str__(self):
a = ""
for eachContext in self.contextstack:
a += eachContext.__str__() + " >> "
return a
return "".join(
f"{eachContext.__str__()} >> " for eachContext in self.contextstack
)
Comment on lines -994 to +983
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ContextStack.__str__ refactored with the following changes:


def Copy(self):
contextStack = ContextStack()
Expand All @@ -1015,9 +1001,7 @@ def Pop(self):
return self.contextstackstack.pop()

def Peek(self):
if len(self.contextstackstack) == 0:
return None
return self.contextstackstack[-1]
return None if len(self.contextstackstack) == 0 else self.contextstackstack[-1]
Comment on lines -1018 to +1004
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _ContextStackStack.Peek refactored with the following changes:



############################################################################
Expand Down
Loading