@@ -8,36 +8,106 @@ def __init__(self):
8
8
9
9
@staticmethod
10
10
def file_extension () -> str :
11
+ """
12
+ Return the file extension used for C++ files.
13
+
14
+ :return: The file extension for C++ files.
15
+ :rtype: str
16
+ """
11
17
return extension .cpp
12
18
13
19
@staticmethod
14
20
def keywords () -> list :
21
+ """
22
+ Return a list of C++ keywords and built-in functions.
23
+
24
+ :return: A list of C++ keywords and built-in function names.
25
+ :rtype: list
26
+ """
15
27
keyword = 'alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|class|compl|const|constexpr|const_cast|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|mutable|namespace|new|noexcept|nullptr|operator|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while|true|false' .split ('|' )
16
28
return keyword
17
29
18
30
@staticmethod
19
31
def comment_regex ():
32
+ """
33
+ Compile and return a regular expression pattern to identify different types of comments and non-comment code in C source files.
34
+
35
+ :return: A compiled regex pattern with named groups to match single-line comments, multiline comments, and non-comment code elements.
36
+ :rtype: re.Pattern
37
+ """
20
38
pattern = re .compile (r'(?P<comment>//.*?$|/\*[^*]*\*+(?:[^/*][^*]*\*+)*?/)|(?P<noncomment>[^/]+)' , re .DOTALL | re .MULTILINE )
21
39
return pattern
22
40
23
41
@staticmethod
24
42
def number_regex ():
43
+ """
44
+ Compile and return a regular expression pattern to identify numeric literals in C++ code.
45
+
46
+ :return: A compiled regex pattern to match C++ numeric literals, including integers, floats, and complex numbers.
47
+ :rtype: re.Pattern
48
+ """
25
49
pattern = re .compile (r'(?:\b0x[\da-f]+|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*' )
26
50
return pattern
27
51
28
52
@staticmethod
29
53
def operator_regex ():
54
+ """
55
+ Compile and return a regular expression pattern to identify C++ operators.
56
+
57
+ :return: A compiled regex pattern to match various C++ operators and logical keywords.
58
+ :rtype: re.Pattern
59
+ """
30
60
pattern = re .compile (r'--?|\+\+?|!=?|<{1,2}=?|>{1,2}=?|->|:{1,2}|={1,2}|\^|~|%|&{1,2}|\|\|?|\?|\*|\/|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b' )
31
61
return pattern
32
62
33
63
@staticmethod
34
64
def keywords_regex ():
65
+ """
66
+ Return a list of C++ keywords and built-in functions.
67
+
68
+ :return: A list of C++ keywords and built-in function names.
69
+ :rtype: list
70
+ """
35
71
return re .compile (r'\b(' + '|' .join (CPP .keywords ()) + r')\b' )
36
72
73
+ @staticmethod
74
+ def boolean_regex () -> re .Pattern :
75
+ """
76
+ Compile and return a regular expression pattern to identify C++ boolean literals.
77
+
78
+ :return: A compiled regex pattern to match C++ boolean literals.
79
+ :rtype: re.Pattern
80
+ """
81
+ return re .compile (r'\b(?:true|false)\b' )
82
+
83
+ @staticmethod
84
+ def delimiters_regex () -> re .Pattern :
85
+ """
86
+ Compile and return a regular expression pattern to identify C and C++ delimiters.
87
+
88
+ :return: A compiled regex pattern to match C and C++ delimiters.
89
+ :rtype: re.Pattern
90
+ """
91
+ return re .compile (r'[()\[\]{}.,:;@<>*&]' )
92
+
37
93
@staticmethod
38
94
def remove_comments (source_code : str ) -> str :
95
+ """
96
+ Remove comments from the provided C++ source code string.
97
+
98
+ :param str source_code: The C++ source code from which to remove comments.
99
+ :return: The source code with all comments removed.
100
+ :rtype: str
101
+ """
39
102
return CPP .comment_regex ().sub (lambda match : match .group ('noncomment' ) if match .group ('noncomment' ) else '' , source_code ).strip ()
40
103
41
104
@staticmethod
42
105
def remove_keywords (source : str ):
106
+ """
107
+ Remove all C++ keywords from the provided source code string.
108
+
109
+ :param str source: The source code string from which to remove C++ keywords.
110
+ :return: The source code string with all C++ keywords removed.
111
+ :rtype: str
112
+ """
43
113
return re .sub (re .compile (CPP .keywords_regex ()), '' , source )
0 commit comments