-
Notifications
You must be signed in to change notification settings - Fork 24
/
nsiqcppstyle_rulehelper.py
110 lines (90 loc) · 4.16 KB
/
nsiqcppstyle_rulehelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (c) 2009 NHN Inc. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of NHN Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import re
import nsiqcppstyle_state
_regexp_compile_cache = {}
def Match(pattern, s):
"""Matches the string with the pattern, caching the compiled regexp."""
# The regexp compilation caching is inlined in both Match and Search for
# performance reasons; factoring it out into a separate function turns out
# to be noticeably expensive.
if pattern not in _regexp_compile_cache:
_regexp_compile_cache[pattern] = re.compile(pattern)
return _regexp_compile_cache[pattern].match(s)
def Search(pattern, s):
"""Searches the string for the pattern, caching the compiled regexp."""
if pattern not in _regexp_compile_cache:
_regexp_compile_cache[pattern] = re.compile(pattern)
return _regexp_compile_cache[pattern].search(s)
def FindAll(pattern, s):
"""Searches the string for the pattern, caching the compiled regexp."""
if pattern not in _regexp_compile_cache:
_regexp_compile_cache[pattern] = re.compile(pattern)
return _regexp_compile_cache[pattern].findall(s)
def GetRealColumn(token):
"""Get the token's real column"""
tabsize = int(nsiqcppstyle_state._nsiqcppstyle_state.GetVar("tabsize", 4))
line = token.line[: token.column]
tabCount = line.count("\t")
return len(line) + tabCount * (tabsize - 1)
def GetIndentation(token):
"""Get indentation of the line in which the tokens exists"""
tabsize = int(nsiqcppstyle_state._nsiqcppstyle_state.GetVar("tabsize", 4))
line = token.line
indent = 0
for char in line:
if char == " ":
indent += 1
elif char == "\t":
indent += tabsize
else:
break
return indent
def IsConstructor(value, fullName, context):
"""Check if the passed value is the constructor or destructor"""
if "::" in value and "::" in fullName and 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:
return True
if context is not None and context.type in ["CLASS_BLOCK", "STRUCT_BLOCK"]:
names = context.name.split("::")
if names[-1] == value:
return True
return False
def IsOperator(value):
"""Check if the passed value is '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 not operator_type[0].isalnum()