-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifierName.hpp
174 lines (158 loc) · 5.19 KB
/
identifierName.hpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: GPL-3.0-only
/**
* @file identifierName.hpp
*
* @copyright Copyright (C) 2023 srcML, LLC. (www.srcML.org)
*
* This file is part of the nameCollector application.
*/
/**
* Created by Maletic on 6/28/23.
*/
#ifndef IDENTIFIER_NAME_HPP
#define IDENTIFIER_NAME_HPP
#include <iostream>
#include <vector>
#include <unordered_set>
#include <string>
extern bool DEBUG;
//
//The syntactic category types for user defined identifiers.
//
// These are the labels for names produced as output.
//
const std::unordered_set<std::string> IDENTIFIER_TYPES = {
"function", //Function name
"constructor", //Constructor name
"destructor", //Destructor name
"class", //Class name
"interface", //Interface name Java
"typedef", //Typedef name
"struct", //Struct name
"union", //Union name C, C++
"enum", //Enum name
"field", //Name of a class/struct/union/enum field
"label", //Name of a label (as in goto)
"local", //Local variable name (in a function)
"global", //Gobal variable name
"macro", //Macro name C, C++
"namespace", //User defined namespace C++, C#
"parameter", //Name of a parameter
"template-parameter", //Name of template paramter <typename T> C++
"function-parameter", //Name of a parameter, that is a function void *f()
"property", //Name of a property C#
"event", //Name of an event C#
"annotation" //Name of an annotation Java
};
//
//srcML tag names that have names that are user defined identifiers
//For C/C++, C#, and Java
// Need to add tag names here if you examine that syntactic category for
// a name. When adding new languages or constructs
//
const std::unordered_set<std::string> USER_DEFINED_TAGS = {
"decl", //For C, C++, C#, Java
"label",
"typedef",
"parameter",
"function",
"function_decl",
"constructor",
"constructor_decl",
"destructor",
"destructor_decl",
"class",
"class_decl",
"interface", //Java only
"struct",
"struct_decl",
"enum",
"enum_decl",
"union", //C, C++ only
"union_decl", //C, C++ only
"macro", //C, C++ only
"namespace", //C++, C# only
"event", //C# only
"property", //C# only
"abstract", //Java only
"interface", //Java only
"annotation", //Java only
"annotation_defn" //Java only
};
//
//srcML categories that have types AND are named
//For C/C++, C#, and Java
// Need to add categories here if they contain a top-level type tag
//
const std::unordered_set<std::string> TYPED_CATEGORIES = {
"local",
"global",
"field",
"typedef",
"parameter",
"function",
"function_decl",
"event", //C# only
"property" //C# only
};
//Does this tag contain a user defined name?
//
bool isUserDefinedIdentifier(const std::string& category) {
return USER_DEFINED_TAGS.find(category) != USER_DEFINED_TAGS.end();
}
// Does this tag contain a type?
bool isTypedCategory(const std::string& category) {
return TYPED_CATEGORIES.find(category) != TYPED_CATEGORIES.end();
}
struct typeInfo {
std::string type;
std::string associatedTag;
bool gatherContent;
};
void replaceSubStringInPlace(std::string& subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
// A user defined identifier and info
class identifier {
public:
identifier() {};
identifier(const std::string& nm,
const std::string& cat,
const std::string& pos,
const std::string& fname,
const std::string& flang,
const std::string& typ="") {
name = nm;
category = cat;
position = pos;
filename = fname;
language = flang;
type = typ;
};
std::string getName() const {return name;};
std::string getCategory() const {return category;};
std::string getPosition() const {return position;};
std::string getFilename() const {return filename;};
std::string getLanguage() const {return language;};
std::string getType() const {return type;};
protected:
std::string name; //The identifier name
std::string category; //Label from IDENTIFIER_TYPES
std::string position; //line:column
std::string filename; //File the identifier occurs
std::string language; //The programming language the name was in
std::string type; //Optional type for decls and functions
};
//CSV output name, category, filename, position
std::ostream& operator<<(std::ostream& out, const identifier& id) {
out << id.getName() << ", " << id.getType() << ", "
<< id.getCategory() << ", " << id.getFilename() << ", "
<< id.getPosition() << ", " << id.getLanguage();
return out;
}
#endif