Skip to content

Commit 6255108

Browse files
authored
Provide operator<TYPE> as value for function in some conditions (#63)
* Provide `operator<TYPE>` as value for function in some conditions * Make CI happy * Fix lint
1 parent 4242376 commit 6255108

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

nsiqcppstyle_checker.py

+27-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import os
3030
import traceback
31+
from copy import deepcopy
3132

3233
from nsiqcppstyle_outputer import _consoleOutputer as console
3334
from nsiqcppstyle_rulehelper import * # @UnusedWildImport
@@ -80,6 +81,7 @@
8081
# Delimeters ( ) [ ] { } , . ; :
8182
"LPAREN",
8283
"RPAREN",
84+
"PARENS",
8385
"LBRACKET",
8486
"RBRACKET",
8587
"LBRACE",
@@ -425,7 +427,6 @@ def __init__(self, filename, data=None):
425427
tok.filename = self.filename
426428
tok.pp = None
427429
# self.ProcessIfdef(tok)
428-
self.tokenlistsize = len(self.tokenlist)
429430
self.PushTokenIndex()
430431
while True:
431432
t = self.GetNextToken()
@@ -434,6 +435,10 @@ def __init__(self, filename, data=None):
434435
t.inactive = self.ProcessIfdef(t)
435436
self.PopTokenIndex()
436437

438+
@property
439+
def tokenlistsize(self):
440+
return len(self.tokenlist)
441+
437442
def ProcessIfdef(self, token):
438443
if token.type == "PREPROCESSOR":
439444
if Match(r"^#\s*if(n)?def$", token.value):
@@ -1158,6 +1163,7 @@ def ConstructContextInfo(lexer):
11581163

11591164
# Function Prediction
11601165
elif t.pp is not True and t.type in ("ID", "OPERATOR") and contextPrediction is None:
1166+
operator_name = None
11611167
curNotSigContext = contextStack.Peek()
11621168
if curNotSigContext is not None and curNotSigContext.sig is False:
11631169
continue
@@ -1166,10 +1172,12 @@ def ConstructContextInfo(lexer):
11661172
if t.type == "ID":
11671173
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
11681174
t4 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(2)
1169-
11701175
else:
11711176
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess()
1177+
operator_name = deepcopy(t2)
11721178
if t2.type == "LPAREN":
1179+
operator_name.value = "()" # call operator
1180+
operator_name.type = "PARENS" # call operator
11731181
t2 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(3)
11741182
t4 = lexer.PeekNextTokenSkipWhiteSpaceAndCommentAndPreprocess(4)
11751183
else:
@@ -1204,7 +1212,18 @@ def ConstructContextInfo(lexer):
12041212
fullName = t.value
12051213
lexer.PushTokenIndex()
12061214
if t.type == "OPERATOR":
1207-
fullName = fullName + t3.value
1215+
fullName = fullName + operator_name.value
1216+
while True:
1217+
prevName = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
1218+
if prevName is not None:
1219+
if prevName.type == "DOUBLECOLON":
1220+
fullName = (
1221+
f"{lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value}::{fullName}"
1222+
)
1223+
else:
1224+
break
1225+
else:
1226+
break
12081227
else:
12091228
while True:
12101229
prevName = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
@@ -1221,8 +1240,8 @@ def ConstructContextInfo(lexer):
12211240
else:
12221241
break
12231242
lexer.PopTokenIndex()
1224-
if Match(r"^[A-Z_][A-Z_0-9][A-Z_0-9]+$", fullName):
1225-
continue
1243+
# if Match(r"^[A-Z_][A-Z_0-9][A-Z_0-9]+$", fullName):
1244+
# continue
12261245
impl = lexer.HasBody()
12271246
if impl:
12281247
contextStart = lexer.GetNextTokenInType("LBRACE")
@@ -1232,10 +1251,13 @@ def ConstructContextInfo(lexer):
12321251

12331252
# RunFunctionRule(lexer, functionName, decl, contextStack, contextPrediction)
12341253
t.type = "FUNCTION"
1254+
t.value = fullName
12351255
t.fullName = fullName
12361256
t.context = contextPrediction
12371257
t.decl = not impl
12381258
lexer.PopTokenIndex()
1259+
if operator_name is not None:
1260+
operator_name = None
12391261
# print "TT", lexer.GetCurTokenLine(), impl,
12401262
# contextPrediction
12411263
t.contextStack = contextStack

nsiqcppstyle_rulehelper.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def GetIndentation(token):
8383

8484
def IsConstructor(value, fullName, context):
8585
"""Check if the passed value is the constructor or destructor"""
86+
if "::" in value and "::" in fullName:
87+
if value == fullName:
88+
return True
89+
value = value.replace("~", "").split("::")[-1] # remove class and dtor if needed
8690
fullName = fullName.replace("~", "")
8791
names = fullName.split("::")
8892
if len(names) != 1 and names[-1] == value:
@@ -97,6 +101,13 @@ def IsConstructor(value, fullName, context):
97101

98102
def IsOperator(value):
99103
"""Check if the passed value is 'operator'"""
100-
if value is not None and value == "operator":
104+
if value is None:
105+
return False
106+
if not value.startswith("operator"):
107+
return False
108+
operator_type = value.removeprefix("operator")
109+
if operator_type == "":
101110
return True
102-
return False
111+
if operator_type[0].isalnum():
112+
return False
113+
return True

0 commit comments

Comments
 (0)