1313export module mcppide.scan;
1414
1515import std;
16+ import mcpplibs.mtest.manifest;
1617
1718export namespace mcppide ::scan {
1819
@@ -25,8 +26,10 @@ struct Call {
2526 std::vector<std::string> arguments;
2627};
2728
28- // A case of the mtest library (`"name"_test`): its identifier -- the names of the suites it is
29- // declared in and its own, joined by `/` -- and where its name is (0-based).
29+ // A case of the mtest library -- an object (`UTest parse{"parse", ...}`, any suite type) or a
30+ // `"name"_test` declared in one -- as mtest's own static manifest reads it
31+ // (mcpplibs.mtest.manifest): its identifier, the names of what declares it and its own joined by
32+ // `/`, and where its name is (0-based).
3033struct Literal {
3134 std::string id;
3235 int line = 0 ;
@@ -36,11 +39,13 @@ struct Literal {
3639struct Result {
3740 std::optional<int > mainLine; // 0-based
3841 std::vector<Call> calls;
39- std::vector<Literal> cases; // `"name"_test`, in `suite x{"name", ...}` scopes
42+ std::vector<Literal> cases; // mtest's cases, by mcpplibs.mtest.manifest
4043};
4144
42- // Scans `text` for `main` and for calls of the macros named in `macros`.
43- Result scan (std::string_view text, const std::vector<std::string>& macros);
45+ // Scans `text` for `main` and for calls of the macros named in `macros`. `types` are the suite types
46+ // the project's tests declare elsewhere (`class Fixture : public CTS` in a module beside them).
47+ Result scan (std::string_view text, const std::vector<std::string>& macros,
48+ const mcpplibs::mtest::manifest::suite_types& types = {});
4449
4550// A string literal's contents, as the program sees them for the plain escapes (\" \\ \n \t); the
4651// argument itself when it is not one literal. Adjacent literals are joined.
@@ -157,24 +162,14 @@ std::vector<std::string> arguments(Cursor& c) {
157162
158163} // namespace
159164
160- Result scan (std::string_view text, const std::vector<std::string>& macros) {
165+ Result scan (std::string_view text, const std::vector<std::string>& macros,
166+ const mcpplibs::mtest::manifest::suite_types& types) {
161167 Result r;
162168 Cursor c{text};
163169 int depth = 0 ; // braces
164170 bool atDeclaration = true ; // the next identifier starts a declaration
165171 bool lineHasCode = false ; // a `#` is a directive only first on its line
166172 std::string previous; // the previous identifier, for `int main(`
167- // A suite of the mtest library: `suite <name>{"<id>", ...}`, whose `{` opens the scope its
168- // cases' identifiers are prefixed in. `suiteState` follows the declaration: 1 after `suite`,
169- // 2 after its variable's name, 3 after the `{` (the next literal is the suite's identifier).
170- struct Scope { std::string name; int depth; };
171- std::vector<Scope> suites;
172- int suiteState = 0 , suiteDepth = 0 ;
173- auto prefix = [&] {
174- std::string out;
175- for (const auto & s : suites) if (!s.name .empty ()) out += s.name + " /" ;
176- return out;
177- };
178173 while (!c.done ()) {
179174 const char ch = c.peek ();
180175 if (ch == ' \n ' ) { lineHasCode = false ; c.step (); continue ; }
@@ -185,16 +180,7 @@ Result scan(std::string_view text, const std::vector<std::string>& macros) {
185180 lineHasCode = true ;
186181 if (c.on_raw ()) { c.skip_raw (); atDeclaration = false ; previous.clear (); continue ; }
187182 if (ch == ' "' ) {
188- const auto from = c.at ;
189- const int line = c.line , column = c.column ();
190183 c.skip_quoted (ch);
191- const auto value = literal_value (text.substr (from, c.at - from));
192- if (suiteState == 3 ) {
193- suites.push_back ({value, suiteDepth});
194- suiteState = 0 ;
195- } else if (text.substr (c.at , 5 ) == " _test" && !(c.at + 5 < text.size () && ident_char (text[c.at + 5 ]))) {
196- r.cases .push_back (Literal{prefix () + value, line, column});
197- }
198184 atDeclaration = false ;
199185 previous.clear ();
200186 continue ;
@@ -214,10 +200,6 @@ Result scan(std::string_view text, const std::vector<std::string>& macros) {
214200 break ;
215201 }
216202 const bool call = look.peek () == ' (' ;
217- // `suite`, or `mtest::suite` (the identifier before `::` is kept in `previous`).
218- if (word == " suite" && (atDeclaration || previous == " mtest" )) suiteState = 1 ;
219- else if (suiteState == 1 ) suiteState = 2 ;
220- else suiteState = 0 ;
221203 if (call && atDeclaration && std::ranges::find (macros, word) != macros.end ()) {
222204 c = look;
223205 r.calls .push_back (Call{word, line, column, arguments (c)});
@@ -232,21 +214,21 @@ Result scan(std::string_view text, const std::vector<std::string>& macros) {
232214 continue ;
233215 }
234216 if (ch == ' {' ) {
235- if (suiteState == 2 ) { suiteState = 3 ; suiteDepth = depth; }
236- else suiteState = 0 ;
237217 ++depth;
238218 atDeclaration = true ;
239219 } else if (ch == ' }' ) {
240220 if (depth > 0 ) --depth;
241- while (!suites.empty () && suites.back ().depth >= depth) suites.pop_back ();
242221 atDeclaration = true ;
243222 }
244- else if (ch == ' ;' ) { atDeclaration = true ; suiteState = 0 ; }
245- else { atDeclaration = false ; suiteState = 0 ; }
223+ else if (ch == ' ;' ) atDeclaration = true ;
224+ else atDeclaration = false ;
246225 // `std::main`, `x.main`: not the program's.
247226 if (ch != ' :' ) previous.clear ();
248227 c.step ();
249228 }
229+ // mtest's cases as mtest reads them: its manifest is the one reader of its declarations.
230+ for (const auto & found : mcpplibs::mtest::manifest::scan (text, types))
231+ r.cases .push_back (Literal{found.id , found.line - 1 , found.column - 1 });
250232 return r;
251233}
252234
0 commit comments