Skip to content

Commit 0f0adcc

Browse files
committed
refactor: move index_tokens to scanner files
1 parent 1df0d6a commit 0f0adcc

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

include/opzioni/parsing.hpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ find_cmd(std::tuple<std::reference_wrapper<Cmds const> const...> const haystack,
6060
// clang-format on
6161
}
6262

63-
struct TokensIndexes {
64-
std::vector<std::size_t> positionals;
65-
std::map<std::string_view, std::vector<std::size_t>> opts_n_flgs;
66-
67-
[[nodiscard]] std::optional<std::size_t> first_pos_idx_after(std::size_t const offset) const noexcept {
68-
if (this->positionals.empty()) return std::nullopt;
69-
std::size_t i = 0;
70-
auto idx = this->positionals[i];
71-
while (idx <= offset) {
72-
if (i + 1 >= this->positionals.size()) return std::nullopt;
73-
idx = this->positionals[++i];
74-
}
75-
return idx;
76-
}
77-
};
78-
7963
// +-----------------------+
8064
// | CmdParser |
8165
// +-----------------------+
@@ -94,9 +78,9 @@ class CmdParser {
9478
ArgsMap<Cmd const> operator()(std::span<char const *> args) {
9579
auto scanner = Scanner(args);
9680
auto const tokens = scanner();
97-
auto const tokens_indexes = this->index_tokens(tokens);
81+
auto const indexes = index_tokens(tokens);
9882
// TODO: check we did not receive unknown arguments (throw UnknownArgument)
99-
auto map = this->get_args_map(tokens, tokens_indexes, 0, -1);
83+
auto map = this->get_args_map(tokens, indexes, 0, -1);
10084
return map;
10185
}
10286

@@ -122,31 +106,6 @@ class CmdParser {
122106

123107
auto get_cmd_fmt() const noexcept { return CmdFmt(this->cmd_ref.get(), this->extra_info); }
124108

125-
auto index_tokens(std::span<Token const> const tokens) {
126-
auto indexes = TokensIndexes();
127-
if (!tokens.empty()) { // should never happen
128-
for (std::size_t index = 1; index < tokens.size(); ++index) {
129-
auto const &tok = tokens[index];
130-
switch (tok.type) {
131-
case TokenType::PROG_NAME: break;
132-
case TokenType::DASH_DASH:
133-
// +1 to ignore the dash-dash
134-
for (std::size_t offset = index + 1; offset < tokens.size(); ++offset) {
135-
indexes.positionals.push_back(offset);
136-
}
137-
index = tokens.size(); // break below refers to the switch, not the for loop
138-
break;
139-
case TokenType::FLG: [[fallthrough]];
140-
case TokenType::OPT_OR_FLG_LONG: [[fallthrough]];
141-
case TokenType::OPT_LONG_AND_VALUE: [[fallthrough]];
142-
case TokenType::OPT_SHORT_AND_VALUE: indexes.opts_n_flgs[*tok.name].push_back(index); break;
143-
case TokenType::IDENTIFIER: indexes.positionals.push_back(index); break;
144-
}
145-
}
146-
}
147-
return indexes;
148-
}
149-
150109
auto get_args_map(
151110
std::span<Token const> const tokens,
152111
TokensIndexes const &indexes,

include/opzioni/scanner.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define OPZIONI_SCANNER_HPP
33

44
#include <cstdint>
5+
#include <map>
56
#include <optional>
67
#include <span>
78
#include <string_view>
@@ -28,12 +29,29 @@ struct Token {
2829
std::optional<std::string_view> value;
2930
};
3031

32+
struct TokensIndexes {
33+
std::vector<std::size_t> positionals;
34+
std::map<std::string_view, std::vector<std::size_t>> opts_n_flgs;
35+
36+
[[nodiscard]] std::optional<std::size_t> first_pos_idx_after(std::size_t const offset) const noexcept {
37+
if (this->positionals.empty()) return std::nullopt;
38+
std::size_t i = 0;
39+
auto idx = this->positionals[i];
40+
while (idx <= offset) {
41+
if (i + 1 >= this->positionals.size()) return std::nullopt;
42+
idx = this->positionals[++i];
43+
}
44+
return idx;
45+
}
46+
};
47+
3148
std::string_view to_string(TokenType type) noexcept;
49+
TokensIndexes index_tokens(std::span<Token const> const tokens);
3250

3351
class Scanner {
3452
public:
3553

36-
Scanner(std::span<char const *> args) {
54+
explicit Scanner(std::span<char const *> args) {
3755
this->tokens.reserve(args.size());
3856
this->args.reserve(args.size());
3957
for (char const *a : args) {

src/scanner.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ std::string_view to_string(TokenType type) noexcept {
1515
}
1616
}
1717

18+
TokensIndexes index_tokens(std::span<Token const> const tokens) {
19+
auto indexes = TokensIndexes();
20+
if (!tokens.empty()) { // should never happen
21+
for (std::size_t index = 1; index < tokens.size(); ++index) {
22+
auto const &tok = tokens[index];
23+
switch (tok.type) {
24+
case TokenType::PROG_NAME: break;
25+
case TokenType::DASH_DASH:
26+
// +1 to ignore the dash-dash
27+
for (std::size_t offset = index + 1; offset < tokens.size(); ++offset) {
28+
indexes.positionals.push_back(offset);
29+
}
30+
index = tokens.size(); // break below refers to the switch, not the for loop
31+
break;
32+
case TokenType::FLG: [[fallthrough]];
33+
case TokenType::OPT_OR_FLG_LONG: [[fallthrough]];
34+
case TokenType::OPT_LONG_AND_VALUE: [[fallthrough]];
35+
case TokenType::OPT_SHORT_AND_VALUE: indexes.opts_n_flgs[*tok.name].push_back(index); break;
36+
case TokenType::IDENTIFIER: indexes.positionals.push_back(index); break;
37+
}
38+
}
39+
}
40+
return indexes;
41+
}
42+
1843
// +-----------------------------------------+
1944
// | Scanner |
2045
// +-----------------------------------------+

0 commit comments

Comments
 (0)