Repository navigation
Expand file tree
/
Copy pathscan.cppm
More file actions
260 lines (240 loc) · 10.4 KB
/
Copy pathscan.cppm
File metadata and controls
260 lines (240 loc) · 10.4 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// What a C++ source says about where it starts and what it tests, read as text (mcpp design
// 2026-10-30 part 2 §6.3, §7.3): the line of `main`, and the calls of a test framework's macros.
//
// NOTHING IS BUILT AND NOTHING IS RUN. A file is scanned when it is opened, before any build and
// without mcppls, so the editors can place ▶ at once. Comments, string and character literals (raw
// strings included) and preprocessor lines are skipped, so a `main(` in a comment or a `TEST(` in a
// string is not one. A position found this way is approximate where a language service has a
// better one: the editors take the language service's position of `main` when it gives one.
//
// A MACRO CALL COUNTS AT THE START OF A DECLARATION: after `;`, `{`, `}`, a preprocessor line or
// the start of the file. That is where a framework's test macros are written (at namespace scope,
// or in a test body for a section), and an expression that merely contains the name is not one.
export module mcppide.scan;
import std;
import mcpplibs.mtest.manifest;
export namespace mcppide::scan {
// One call of a macro: its name, where it is (0-based line and column of the name), and its
// arguments as written, split at the top-level commas, each trimmed.
struct Call {
std::string name;
int line = 0;
int column = 0;
std::vector<std::string> arguments;
};
// A case of the mtest library -- an object (`UTest parse{"parse", ...}`, any suite type) or a
// `"name"_test` declared in one -- as mtest's own static manifest reads it
// (mcpplibs.mtest.manifest): its identifier, the names of what declares it and its own joined by
// `/`, and where its name is (0-based).
struct Literal {
std::string id;
int line = 0;
int column = 0;
};
struct Result {
std::optional<int> mainLine; // 0-based
std::vector<Call> calls;
std::vector<Literal> cases; // mtest's cases, by mcpplibs.mtest.manifest
};
// Scans `text` for `main` and for calls of the macros named in `macros`. `types` are the suite types
// the project's tests declare elsewhere (`class Fixture : public CTS` in a module beside them).
Result scan(std::string_view text, const std::vector<std::string>& macros,
const mcpplibs::mtest::manifest::suite_types& types = {});
// A string literal's contents, as the program sees them for the plain escapes (\" \\ \n \t); the
// argument itself when it is not one literal. Adjacent literals are joined.
std::string literal_value(std::string_view argument);
} // namespace mcppide::scan
namespace mcppide::scan {
namespace {
bool ident_start(char c) { return std::isalpha(static_cast<unsigned char>(c)) || c == '_'; }
bool ident_char(char c) { return std::isalnum(static_cast<unsigned char>(c)) || c == '_'; }
std::string trim(std::string_view s) {
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.front()))) s.remove_prefix(1);
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.back()))) s.remove_suffix(1);
return std::string(s);
}
// A cursor over the text that knows its line and steps over what is not code.
struct Cursor {
std::string_view text;
std::size_t at = 0;
int line = 0;
std::size_t lineStart = 0;
bool done() const { return at >= text.size(); }
char peek(std::size_t ahead = 0) const { return at + ahead < text.size() ? text[at + ahead] : '\0'; }
void step() {
if (text[at] == '\n') { ++line; lineStart = at + 1; }
++at;
}
int column() const { return static_cast<int>(at - lineStart); }
void skip_line_comment() { while (!done() && peek() != '\n') step(); }
void skip_block_comment() {
step(); step();
while (!done() && !(peek() == '*' && peek(1) == '/')) step();
if (!done()) { step(); step(); }
}
void skip_quoted(char quote) {
step();
while (!done() && peek() != quote && peek() != '\n') {
if (peek() == '\\' && at + 1 < text.size()) step();
step();
}
if (!done() && peek() == quote) step();
}
// R"delim( ... )delim" -- the cursor is on the R.
void skip_raw() {
step(); step(); // R"
std::string delimiter;
while (!done() && peek() != '(' && delimiter.size() < 16) { delimiter.push_back(peek()); step(); }
const std::string end = ")" + delimiter + "\"";
while (!done() && text.substr(at, end.size()) != end) step();
for (std::size_t i = 0; i < end.size() && !done(); ++i) step();
}
// A preprocessor line, with its continuations.
void skip_directive() {
while (!done() && peek() != '\n') {
if (peek() == '\\' && peek(1) == '\n') step();
else if (peek() == '/' && peek(1) == '*') { skip_block_comment(); continue; }
step();
}
}
// Whether the cursor is on the start of a raw string literal (R, u8R, uR, UR, LR).
bool on_raw() const {
std::size_t i = at;
if (text.substr(i, 2) == "u8") i += 2;
else if (peek() == 'u' || peek() == 'U' || peek() == 'L') i += 1;
return i + 1 < text.size() && text[i] == 'R' && text[i + 1] == '"';
}
};
// The arguments of a call whose `(` the cursor is on, split at top-level commas; the cursor ends
// after the `)`.
std::vector<std::string> arguments(Cursor& c) {
std::vector<std::string> out;
std::string current;
int depth = 0;
c.step(); // (
while (!c.done()) {
const char ch = c.peek();
if (ch == '/' && c.peek(1) == '/') { c.skip_line_comment(); continue; }
if (ch == '/' && c.peek(1) == '*') { c.skip_block_comment(); continue; }
if (ch == '"' || ch == '\'') {
const auto from = c.at;
c.skip_quoted(ch);
current.append(c.text.substr(from, c.at - from));
continue;
}
if (ch == '(' || ch == '[' || ch == '{' || ch == '<') { if (ch != '<') ++depth; }
if (ch == ')' || ch == ']' || ch == '}') {
if (depth == 0) {
c.step();
out.push_back(trim(current));
return out;
}
--depth;
}
if (ch == ',' && depth == 0) {
out.push_back(trim(current));
current.clear();
c.step();
continue;
}
current.push_back(ch);
c.step();
}
out.push_back(trim(current));
return out;
}
} // namespace
Result scan(std::string_view text, const std::vector<std::string>& macros,
const mcpplibs::mtest::manifest::suite_types& types) {
Result r;
Cursor c{text};
int depth = 0; // braces
bool atDeclaration = true; // the next identifier starts a declaration
bool lineHasCode = false; // a `#` is a directive only first on its line
std::string previous; // the previous identifier, for `int main(`
while (!c.done()) {
const char ch = c.peek();
if (ch == '\n') { lineHasCode = false; c.step(); continue; }
if (std::isspace(static_cast<unsigned char>(ch))) { c.step(); continue; }
if (ch == '/' && c.peek(1) == '/') { c.skip_line_comment(); continue; }
if (ch == '/' && c.peek(1) == '*') { c.skip_block_comment(); continue; }
if (ch == '#' && !lineHasCode) { c.skip_directive(); atDeclaration = true; previous.clear(); continue; }
lineHasCode = true;
if (c.on_raw()) { c.skip_raw(); atDeclaration = false; previous.clear(); continue; }
if (ch == '"') {
c.skip_quoted(ch);
atDeclaration = false;
previous.clear();
continue;
}
if (ch == '\'') { c.skip_quoted(ch); atDeclaration = false; previous.clear(); continue; }
if (ident_start(ch)) {
const auto from = c.at;
const int line = c.line, column = c.column();
while (!c.done() && ident_char(c.peek())) c.step();
const std::string word(text.substr(from, c.at - from));
// What follows the name, past blanks and comments on the way.
Cursor look = c;
while (!look.done()) {
if (std::isspace(static_cast<unsigned char>(look.peek()))) { look.step(); continue; }
if (look.peek() == '/' && look.peek(1) == '*') { look.skip_block_comment(); continue; }
if (look.peek() == '/' && look.peek(1) == '/') { look.skip_line_comment(); continue; }
break;
}
const bool call = look.peek() == '(';
if (call && atDeclaration && std::ranges::find(macros, word) != macros.end()) {
c = look;
r.calls.push_back(Call{word, line, column, arguments(c)});
atDeclaration = false;
previous.clear();
continue;
}
if (call && word == "main" && depth == 0 && !r.mainLine && (previous == "int" || previous == "auto"))
r.mainLine = line;
previous = word;
atDeclaration = false;
continue;
}
if (ch == '{') {
++depth;
atDeclaration = true;
} else if (ch == '}') {
if (depth > 0) --depth;
atDeclaration = true;
}
else if (ch == ';') atDeclaration = true;
else atDeclaration = false;
// `std::main`, `x.main`: not the program's.
if (ch != ':' ) previous.clear();
c.step();
}
// mtest's cases as mtest reads them: its manifest is the one reader of its declarations.
for (const auto& found : mcpplibs::mtest::manifest::scan(text, types))
r.cases.push_back(Literal{found.id, found.line - 1, found.column - 1});
return r;
}
std::string literal_value(std::string_view argument) {
std::string out;
std::size_t i = 0;
bool any = false;
while (i < argument.size()) {
while (i < argument.size() && std::isspace(static_cast<unsigned char>(argument[i]))) ++i;
if (i >= argument.size()) break;
if (argument[i] != '"') return std::string(argument);
++i;
while (i < argument.size() && argument[i] != '"') {
if (argument[i] == '\\' && i + 1 < argument.size()) {
const char e = argument[i + 1];
out.push_back(e == 'n' ? '\n' : e == 't' ? '\t' : e);
i += 2;
continue;
}
out.push_back(argument[i++]);
}
if (i >= argument.size()) return std::string(argument);
++i;
any = true;
}
return any ? out : std::string(argument);
}
} // namespace mcppide::scan