cmArgumentParser.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmArgumentParser.h"
  4. #include <algorithm>
  5. #include "cmArgumentParserTypes.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmStringAlgorithms.h"
  9. namespace ArgumentParser {
  10. auto KeywordActionMap::Emplace(cm::string_view name, KeywordAction action)
  11. -> std::pair<iterator, bool>
  12. {
  13. auto const it =
  14. std::lower_bound(this->begin(), this->end(), name,
  15. [](value_type const& elem, cm::string_view const& k) {
  16. return elem.first < k;
  17. });
  18. return (it != this->end() && it->first == name)
  19. ? std::make_pair(it, false)
  20. : std::make_pair(this->emplace(it, name, std::move(action)), true);
  21. }
  22. auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
  23. {
  24. auto const it =
  25. std::lower_bound(this->begin(), this->end(), name,
  26. [](value_type const& elem, cm::string_view const& k) {
  27. return elem.first < k;
  28. });
  29. return (it != this->end() && it->first == name) ? it : this->end();
  30. }
  31. void Instance::Bind(bool& val)
  32. {
  33. val = true;
  34. this->CurrentString = nullptr;
  35. this->CurrentList = nullptr;
  36. this->ExpectValue = false;
  37. }
  38. void Instance::Bind(std::string& val)
  39. {
  40. this->CurrentString = &val;
  41. this->CurrentList = nullptr;
  42. this->ExpectValue = true;
  43. }
  44. void Instance::Bind(Maybe<std::string>& val)
  45. {
  46. this->CurrentString = &val;
  47. this->CurrentList = nullptr;
  48. this->ExpectValue = false;
  49. }
  50. void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
  51. {
  52. this->CurrentString = nullptr;
  53. this->CurrentList = &val;
  54. this->ExpectValue = false;
  55. }
  56. void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
  57. {
  58. this->CurrentString = nullptr;
  59. this->CurrentList = &val;
  60. this->ExpectValue = true;
  61. }
  62. void Instance::Bind(std::vector<std::vector<std::string>>& val)
  63. {
  64. this->CurrentString = nullptr;
  65. this->CurrentList = (static_cast<void>(val.emplace_back()), &val.back());
  66. this->ExpectValue = false;
  67. }
  68. void Instance::Consume(cm::string_view arg)
  69. {
  70. auto const it = this->Bindings.Keywords.Find(arg);
  71. if (it != this->Bindings.Keywords.end()) {
  72. this->FinishKeyword();
  73. this->Keyword = it->first;
  74. if (this->ParsedKeywords != nullptr) {
  75. this->ParsedKeywords->emplace_back(it->first);
  76. }
  77. it->second(*this);
  78. return;
  79. }
  80. if (this->CurrentString != nullptr) {
  81. this->CurrentString->assign(std::string(arg));
  82. this->CurrentString = nullptr;
  83. this->CurrentList = nullptr;
  84. } else if (this->CurrentList != nullptr) {
  85. this->CurrentList->emplace_back(arg);
  86. } else if (this->UnparsedArguments != nullptr) {
  87. this->UnparsedArguments->emplace_back(arg);
  88. }
  89. this->ExpectValue = false;
  90. }
  91. void Instance::FinishKeyword()
  92. {
  93. if (this->Keyword.empty()) {
  94. return;
  95. }
  96. if (this->ExpectValue) {
  97. if (this->ParseResults != nullptr) {
  98. this->ParseResults->AddKeywordError(this->Keyword,
  99. " missing required value\n");
  100. }
  101. if (this->Bindings.KeywordMissingValue) {
  102. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  103. }
  104. }
  105. }
  106. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  107. {
  108. if (*this) {
  109. return false;
  110. }
  111. std::string e;
  112. for (auto const& ke : this->KeywordErrors) {
  113. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  114. }
  115. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  116. return true;
  117. }
  118. } // namespace ArgumentParser