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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <cassert>
#include <cstddef>
#include <functional>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <cm/optional>
#include <cm/string_view>
#include <cm/type_traits>
#include <cmext/string_view>
#include <cmext/type_traits>
#include "cmArgumentParserTypes.h" // IWYU pragma: keep
template <typename Result>
class cmArgumentParser; // IWYU pragma: keep
class cmMakefile;
namespace ArgumentParser {
class ParseResult
{
std::map<cm::string_view, std::string> KeywordErrors;
public:
explicit operator bool() const { return this->KeywordErrors.empty(); }
void AddKeywordError(cm::string_view key, cm::string_view text)
{
this->KeywordErrors[key] += text;
}
std::map<cm::string_view, std::string> const& GetKeywordErrors() const
{
return this->KeywordErrors;
}
bool MaybeReportError(cmMakefile& mf) const;
};
template <typename Result>
typename std::enable_if<std::is_base_of<ParseResult, Result>::value,
ParseResult*>::type
AsParseResultPtr(Result& result)
{
return &result;
}
template <typename Result>
typename std::enable_if<!std::is_base_of<ParseResult, Result>::value,
ParseResult*>::type
AsParseResultPtr(Result&)
{
return nullptr;
}
enum class Continue
{
No,
Yes,
};
struct ExpectAtLeast
{
std::size_t Count = 0;
ExpectAtLeast(std::size_t count)
: Count(count)
{
}
};
class Instance;
using KeywordAction = std::function<void(Instance&)>;
using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
using PositionAction =
std::function<void(Instance&, std::size_t, cm::string_view)>;
// using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
class KeywordActionMap
: public std::vector<std::pair<cm::string_view, KeywordAction>>
{
public:
std::pair<iterator, bool> Emplace(cm::string_view name,
KeywordAction action);
const_iterator Find(cm::string_view name) const;
};
// using PositionActionMap = cm::flat_map<cm::string_view, PositionAction>;
class PositionActionMap
: public std::vector<std::pair<std::size_t, PositionAction>>
{
public:
std::pair<iterator, bool> Emplace(std::size_t pos, PositionAction action);
const_iterator Find(std::size_t pos) const;
};
class ActionMap
{
public:
KeywordActionMap Keywords;
KeywordNameAction KeywordMissingValue;
KeywordNameAction ParsedKeyword;
PositionActionMap Positions;
KeywordAction TrailingArgs;
};
class Base
{
public:
using ExpectAtLeast = ArgumentParser::ExpectAtLeast;
using Continue = ArgumentParser::Continue;
using Instance = ArgumentParser::Instance;
using ParseResult = ArgumentParser::ParseResult;
ArgumentParser::ActionMap Bindings;
bool MaybeBind(cm::string_view name, KeywordAction action)
{
return this->Bindings.Keywords.Emplace(name, std::move(action)).second;
}
void Bind(cm::string_view name, KeywordAction action)
{
bool const inserted = this->MaybeBind(name, std::move(action));
assert(inserted);
static_cast<void>(inserted);
}
void BindParsedKeyword(KeywordNameAction action)
{
assert(!this->Bindings.ParsedKeyword);
this->Bindings.ParsedKeyword = std::move(action);
}
void BindKeywordMissingValue(KeywordNameAction action)
{
assert(!this->Bindings.KeywordMissingValue);
this->Bindings.KeywordMissingValue = std::move(action);
}
void BindTrailingArgs(KeywordAction action)
{
assert(!this->Bindings.TrailingArgs);
this->Bindings.TrailingArgs = std::move(action);
}
void Bind(std::size_t pos, PositionAction action)
{
bool const inserted =
this->Bindings.Positions.Emplace(pos, std::move(action)).second;
assert(inserted);
static_cast<void>(inserted);
}
};
class ParserState
{
public:
cm::string_view Keyword;
std::size_t Pos = 0;
std::size_t KeywordValuesSeen = 0;
std::size_t KeywordValuesExpected = 0;
std::function<Continue(cm::string_view)> KeywordValueFunc = nullptr;
ActionMap const& Bindings;
void* Result = nullptr;
bool DoneWithPositional = false;
ParserState(ActionMap const& bindings, void* result)
: Bindings(bindings)
, Result(result)
{
}
};
class Instance
{
public:
Instance(ActionMap const& bindings, ParseResult* parseResult,
std::vector<std::string>* unparsedArguments, void* result = nullptr)
: ParseResults(parseResult)
, UnparsedArguments(unparsedArguments)
{
PushState(bindings, result);
}
ParserState& GetState() { return this->Stack.back(); }
void PushState(ActionMap const& bindings, void* result)
{
this->Stack.emplace_back(bindings, result);
}
void PopState()
{
if (!this->Stack.empty()) {
this->Stack.pop_back();
}
}
void Bind(std::function<Continue(cm::string_view)> f, ExpectAtLeast expect);
void Bind(bool& val);
void Bind(std::string& val);
void Bind(NonEmpty<std::string>& val);
void Bind(Maybe<std::string>& val);
void Bind(MaybeEmpty<std::vector<std::string>>& val);
void Bind(NonEmpty<std::vector<std::string>>& val);
void Bind(std::vector<std::vector<std::string>>& val);
template <typename U>
void Bind(NonEmpty<std::vector<std::pair<std::string, U>>>& val,
U const& context)
{
this->Bind(
[&val, &context](cm::string_view arg) -> Continue {
val.emplace_back(std::string(arg), context);
return Continue::Yes;
},
ExpectAtLeast{ 1 });
}
// cm::optional<> records the presence the keyword to which it binds.
template <typename T>
void Bind(cm::optional<T>& optVal)
{
if (!optVal) {
optVal.emplace();
}
this->Bind(*optVal);
}
template <typename T, typename U>
void Bind(cm::optional<T>& optVal, U const& context)
{
if (!optVal) {
optVal.emplace();
}
this->Bind(*optVal, context);
}
template <typename Range>
void Parse(Range& args, std::size_t const pos = 0)
{
GetState().Pos = pos;
for (cm::string_view arg : args) {
for (size_t j = 0; j < FindKeywordOwnerLevel(arg); ++j) {
this->PopState();
}
this->Consume(arg);
GetState().Pos++;
}
this->FinishKeyword();
while (this->Stack.size() > 1) {
this->FinishKeyword();
this->PopState();
}
}
std::size_t FindKeywordOwnerLevel(cm::string_view arg) const
{
for (std::size_t i = Stack.size(); i--;) {
if (this->Stack[i].Bindings.Keywords.Find(arg) !=
this->Stack[i].Bindings.Keywords.end()) {
return (this->Stack.size() - 1) - i;
}
}
return 0;
}
private:
std::vector<ParserState> Stack;
ParseResult* ParseResults = nullptr;
std::vector<std::string>* UnparsedArguments = nullptr;
void Consume(cm::string_view arg);
void FinishKeyword();
template <typename Result>
friend class ::cmArgumentParser;
};
} // namespace ArgumentParser
template <typename Result>
class cmArgumentParser : private ArgumentParser::Base
{
public:
using Base::Bindings;
// I *think* this function could be made `constexpr` when the code is
// compiled as C++20. This would allow building a parser at compile time.
template <typename T, typename cT = cm::member_pointer_class_t<T>,
typename mT = cm::remove_member_pointer_t<T>,
typename = cm::enable_if_t<std::is_base_of<cT, Result>::value>,
typename = cm::enable_if_t<!std::is_function<mT>::value>>
cmArgumentParser& Bind(cm::static_string_view name, T member)
{
this->Base::Bind(name, [member](Instance& instance) {
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member);
});
return *this;
}
template <typename T, typename U>
cmArgumentParser& BindWithContext(cm::static_string_view name,
T Result::*member, U Result::*context)
{
this->Base::Bind(name, [member, context](Instance& instance) {
auto* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(result->*member, result->*context);
});
return *this;
}
cmArgumentParser& Bind(cm::static_string_view name,
Continue (Result::*member)(cm::string_view),
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [member, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(
[result, member](cm::string_view arg) -> Continue {
return (result->*member)(arg);
},
expect);
});
return *this;
}
cmArgumentParser& Bind(cm::static_string_view name,
Continue (Result::*member)(cm::string_view,
cm::string_view),
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [member, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.GetState().Result);
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[result, member, keyword](cm::string_view arg) -> Continue {
return (result->*member)(keyword, arg);
},
expect);
});
return *this;
}
cmArgumentParser& Bind(cm::static_string_view name,
std::function<Continue(Result&, cm::string_view)> f,
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(
[result, &f](cm::string_view arg) -> Continue {
return f(*result, arg);
},
expect);
});
return *this;
}
cmArgumentParser& Bind(
cm::static_string_view name,
std::function<Continue(Result&, cm::string_view, cm::string_view)> f,
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.GetState().Result);
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[result, keyword, &f](cm::string_view arg) -> Continue {
return f(*result, keyword, arg);
},
expect);
});
return *this;
}
cmArgumentParser& Bind(std::size_t position,
cm::optional<std::string> Result::*member)
{
this->Base::Bind(
position,
[member](Instance& instance, std::size_t, cm::string_view arg) {
Result* result = static_cast<Result*>(instance.GetState().Result);
result->*member = arg;
});
return *this;
}
cmArgumentParser& BindParsedKeywords(
std::vector<cm::string_view> Result::*member)
{
this->Base::BindParsedKeyword(
[member](Instance& instance, cm::string_view arg) {
(static_cast<Result*>(instance.GetState().Result)->*member)
.emplace_back(arg);
});
return *this;
}
template <typename T, typename cT = cm::member_pointer_class_t<T>,
typename mT = cm::remove_member_pointer_t<T>,
typename = cm::enable_if_t<std::is_base_of<cT, Result>::value>,
typename = cm::enable_if_t<!std::is_function<mT>::value>>
cmArgumentParser& BindTrailingArgs(T member)
{
this->Base::BindTrailingArgs([member](Instance& instance) {
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member);
});
return *this;
}
template <typename T, typename U>
cmArgumentParser& BindSubParser(cm::static_string_view name,
cmArgumentParser<T>& parser,
cm::optional<T> U::*member)
{
this->Base::Bind(name, [name, parser, member](Instance& instance) {
auto* parentResult = static_cast<Result*>(instance.GetState().Result);
auto& childResult = parentResult->*member;
childResult.emplace(T());
instance.Bind(nullptr, ExpectAtLeast{ 0 });
instance.PushState(parser.Bindings, &(*childResult));
instance.Consume(name);
});
return *this;
}
template <typename T, typename U>
cmArgumentParser& BindSubParser(cm::static_string_view name,
cmArgumentParser<T>& parser, T U::*member)
{
this->Base::Bind(name, [name, parser, member](Instance& instance) {
auto* parentResult = static_cast<Result*>(instance.GetState().Result);
auto* childResult = &(parentResult->*member);
instance.Bind(nullptr, ExpectAtLeast{ 1 });
instance.PushState(parser.Bindings, childResult);
instance.Consume(name);
});
return *this;
}
template <typename Range>
bool Parse(Result& result, Range const& args,
std::vector<std::string>* unparsedArguments,
std::size_t pos = 0) const
{
using ArgumentParser::AsParseResultPtr;
ParseResult* parseResultPtr = AsParseResultPtr(result);
Instance instance(this->Bindings, parseResultPtr, unparsedArguments,
&result);
instance.Parse(args, pos);
return parseResultPtr ? static_cast<bool>(*parseResultPtr) : true;
}
template <typename Range>
Result Parse(Range const& args, std::vector<std::string>* unparsedArguments,
std::size_t pos = 0) const
{
Result result;
this->Parse(result, args, unparsedArguments, pos);
return result;
}
bool HasKeyword(cm::string_view key) const
{
return this->Bindings.Keywords.Find(key) != this->Bindings.Keywords.end();
}
};
template <>
class cmArgumentParser<void> : private ArgumentParser::Base
{
public:
using Base::Bindings;
template <typename T>
cmArgumentParser& Bind(cm::static_string_view name, T& ref)
{
this->Base::Bind(name, [&ref](Instance& instance) { instance.Bind(ref); });
return *this;
}
cmArgumentParser& Bind(cm::static_string_view name,
std::function<Continue(cm::string_view)> f,
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
instance.Bind([&f](cm::string_view arg) -> Continue { return f(arg); },
expect);
});
return *this;
}
cmArgumentParser& Bind(
cm::static_string_view name,
std::function<Continue(cm::string_view, cm::string_view)> f,
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[keyword, &f](cm::string_view arg) -> Continue {
return f(keyword, arg);
},
expect);
});
return *this;
}
cmArgumentParser& Bind(std::size_t position, cm::optional<std::string>& ref)
{
this->Base::Bind(position,
[&ref](Instance&, std::size_t, cm::string_view arg) {
ref = std::string(arg);
});
return *this;
}
cmArgumentParser& BindParsedKeywords(std::vector<cm::string_view>& ref)
{
this->Base::BindParsedKeyword(
[&ref](Instance&, cm::string_view arg) { ref.emplace_back(arg); });
return *this;
}
template <typename T>
cmArgumentParser& BindTrailingArgs(T& ref)
{
this->Base::BindTrailingArgs(
[&ref](Instance& instance) { instance.Bind(ref); });
return *this;
}
template <typename T, typename U>
cmArgumentParser& BindSubParser(cm::static_string_view name,
cmArgumentParser<T>& parser,
cm::optional<U>& subResult)
{
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) {
subResult.emplace(U());
instance.Bind(nullptr, ExpectAtLeast{ 0 });
instance.PushState(parser.Bindings, &(*subResult));
instance.Consume(name);
});
return *this;
}
template <typename T, typename U>
cmArgumentParser& BindSubParser(cm::static_string_view name,
cmArgumentParser<T>& parser, U& subResult)
{
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) {
instance.Bind(nullptr, ExpectAtLeast{ 1 });
instance.PushState(parser.Bindings, &subResult);
instance.Consume(name);
});
return *this;
}
template <typename Range>
ParseResult Parse(Range const& args,
std::vector<std::string>* unparsedArguments,
std::size_t pos = 0) const
{
ParseResult parseResult;
Instance instance(this->Bindings, &parseResult, unparsedArguments);
instance.Parse(args, pos);
return parseResult;
}
bool HasKeyword(cm::string_view key) const
{
return this->Bindings.Keywords.Find(key) != this->Bindings.Keywords.end();
}
protected:
using Base::Instance;
using Base::BindKeywordMissingValue;
template <typename T>
bool Bind(cm::string_view name, T& ref)
{
return this->MaybeBind(name,
[&ref](Instance& instance) { instance.Bind(ref); });
}
};
|