Skip to content

Commit

Permalink
Provide operator<TYPE> as value for function in some conditions (#63)
Browse files Browse the repository at this point in the history
* Provide `operator<TYPE>` as value for function in some conditions

* Make CI happy

* Fix lint
  • Loading branch information
kunaltyagi authored Sep 14, 2024
1 parent 4242376 commit 6255108
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
32 changes: 27 additions & 5 deletions nsiqcppstyle_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import os
import traceback
from copy import deepcopy

from nsiqcppstyle_outputer import _consoleOutputer as console
from nsiqcppstyle_rulehelper import * # @UnusedWildImport
Expand Down Expand Up @@ -80,6 +81,7 @@
# Delimeters ( ) [ ] { } , . ; :
"LPAREN",
"RPAREN",
"PARENS",
"LBRACKET",
"RBRACKET",
"LBRACE",
Expand Down Expand Up @@ -425,7 +427,6 @@ def __init__(self, filename, data=None):
tok.filename = self.filename
tok.pp = None
# self.ProcessIfdef(tok)
self.tokenlistsize = len(self.tokenlist)
self.PushTokenIndex()
while True:
t = self.GetNextToken()
Expand All @@ -434,6 +435,10 @@ def __init__(self, filename, data=None):
t.inactive = self.ProcessIfdef(t)
self.PopTokenIndex()

@property
def tokenlistsize(self):
return len(self.tokenlist)

def ProcessIfdef(self, token):
if token.type == "PREPROCESSOR":
if Match(r"^#\s*if(n)?def$", token.value):
Expand Down Expand Up @@ -1158,6 +1163,7 @@ def ConstructContextInfo(lexer):

# Function Prediction
elif t.pp is not True and t.type in ("ID", "OPERATOR") and contextPrediction is None:
operator_name = None
curNotSigContext = contextStack.Peek()
if curNotSigContext is not None and curNotSigContext.sig is False:
continue
Expand All @@ -1166,10 +1172,12 @@ def ConstructContextInfo(lexer):
if t.type == "ID":
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
t4 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(2)

else:
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
operator_name = deepcopy(t2)
if t2.type == "LPAREN":
operator_name.value = "()" # call operator
operator_name.type = "PARENS" # call operator
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(3)
t4 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(4)
else:
Expand Down Expand Up @@ -1204,7 +1212,18 @@ def ConstructContextInfo(lexer):
fullName = t.value
lexer.PushTokenIndex()
if t.type == "OPERATOR":
fullName = fullName + t3.value
fullName = fullName + operator_name.value
while True:
prevName = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
if prevName is not None:
if prevName.type == "DOUBLECOLON":
fullName = (
f"{lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value}::{fullName}"
)
else:
break
else:
break
else:
while True:
prevName = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
Expand All @@ -1221,8 +1240,8 @@ def ConstructContextInfo(lexer):
else:
break
lexer.PopTokenIndex()
if Match(r"^[A-Z_][A-Z_0-9][A-Z_0-9]+$", fullName):
continue
# if Match(r"^[A-Z_][A-Z_0-9][A-Z_0-9]+$", fullName):
# continue
impl = lexer.HasBody()
if impl:
contextStart = lexer.GetNextTokenInType("LBRACE")
Expand All @@ -1232,10 +1251,13 @@ def ConstructContextInfo(lexer):

# RunFunctionRule(lexer, functionName, decl, contextStack, contextPrediction)
t.type = "FUNCTION"
t.value = fullName
t.fullName = fullName
t.context = contextPrediction
t.decl = not impl
lexer.PopTokenIndex()
if operator_name is not None:
operator_name = None
# print "TT", lexer.GetCurTokenLine(), impl,
# contextPrediction
t.contextStack = contextStack
Expand Down
15 changes: 13 additions & 2 deletions nsiqcppstyle_rulehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def GetIndentation(token):

def IsConstructor(value, fullName, context):
"""Check if the passed value is the constructor or destructor"""
if "::" in value and "::" in fullName:
if value == fullName:
return True
value = value.replace("~", "").split("::")[-1] # remove class and dtor if needed
fullName = fullName.replace("~", "")
names = fullName.split("::")
if len(names) != 1 and names[-1] == value:
Expand All @@ -97,6 +101,13 @@ def IsConstructor(value, fullName, context):

def IsOperator(value):
"""Check if the passed value is 'operator'"""
if value is not None and value == "operator":
if value is None:
return False
if not value.startswith("operator"):
return False
operator_type = value.removeprefix("operator")
if operator_type == "":
return True
return False
if operator_type[0].isalnum():
return False
return True

0 comments on commit 6255108

Please sign in to comment.