cmArgumentParser.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. auto PositionActionMap::Emplace(std::size_t pos, PositionAction action)
  32. -> std::pair<iterator, bool>
  33. {
  34. auto const it = std::lower_bound(
  35. this->begin(), this->end(), pos,
  36. [](value_type const& elem, std::size_t k) { return elem.first < k; });
  37. return (it != this->end() && it->first == pos)
  38. ? std::make_pair(it, false)
  39. : std::make_pair(this->emplace(it, pos, std::move(action)), true);
  40. }
  41. auto PositionActionMap::Find(std::size_t pos) const -> const_iterator
  42. {
  43. auto const it = std::lower_bound(
  44. this->begin(), this->end(), pos,
  45. [](value_type const& elem, std::size_t k) { return elem.first < k; });
  46. return (it != this->end() && it->first == pos) ? it : this->end();
  47. }
  48. void Instance::Bind(std::function<Continue(cm::string_view)> f,
  49. ExpectAtLeast expect)
  50. {
  51. this->KeywordValueFunc = std::move(f);
  52. this->KeywordValuesExpected = expect.Count;
  53. }
  54. void Instance::Bind(bool& val)
  55. {
  56. val = true;
  57. this->Bind(nullptr, ExpectAtLeast{ 0 });
  58. }
  59. void Instance::Bind(std::string& val)
  60. {
  61. this->Bind(
  62. [&val](cm::string_view arg) -> Continue {
  63. val = std::string(arg);
  64. return Continue::No;
  65. },
  66. ExpectAtLeast{ 1 });
  67. }
  68. void Instance::Bind(Maybe<std::string>& val)
  69. {
  70. this->Bind(
  71. [&val](cm::string_view arg) -> Continue {
  72. static_cast<std::string&>(val) = std::string(arg);
  73. return Continue::No;
  74. },
  75. ExpectAtLeast{ 0 });
  76. }
  77. void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
  78. {
  79. this->Bind(
  80. [&val](cm::string_view arg) -> Continue {
  81. val.emplace_back(arg);
  82. return Continue::Yes;
  83. },
  84. ExpectAtLeast{ 0 });
  85. }
  86. void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
  87. {
  88. this->Bind(
  89. [&val](cm::string_view arg) -> Continue {
  90. val.emplace_back(arg);
  91. return Continue::Yes;
  92. },
  93. ExpectAtLeast{ 1 });
  94. }
  95. void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
  96. {
  97. multiVal.emplace_back();
  98. std::vector<std::string>& val = multiVal.back();
  99. this->Bind(
  100. [&val](cm::string_view arg) -> Continue {
  101. val.emplace_back(arg);
  102. return Continue::Yes;
  103. },
  104. ExpectAtLeast{ 0 });
  105. }
  106. void Instance::Consume(std::size_t pos, cm::string_view arg)
  107. {
  108. auto const it = this->Bindings.Keywords.Find(arg);
  109. if (it != this->Bindings.Keywords.end()) {
  110. this->FinishKeyword();
  111. this->Keyword = it->first;
  112. this->KeywordValuesSeen = 0;
  113. if (this->Bindings.ParsedKeyword) {
  114. this->Bindings.ParsedKeyword(*this, it->first);
  115. }
  116. it->second(*this);
  117. return;
  118. }
  119. if (this->KeywordValueFunc) {
  120. switch (this->KeywordValueFunc(arg)) {
  121. case Continue::Yes:
  122. break;
  123. case Continue::No:
  124. this->KeywordValueFunc = nullptr;
  125. break;
  126. }
  127. ++this->KeywordValuesSeen;
  128. return;
  129. }
  130. auto const pit = this->Bindings.Positions.Find(pos);
  131. if (pit != this->Bindings.Positions.end()) {
  132. pit->second(*this, pos, arg);
  133. return;
  134. }
  135. if (this->UnparsedArguments != nullptr) {
  136. this->UnparsedArguments->emplace_back(arg);
  137. }
  138. }
  139. void Instance::FinishKeyword()
  140. {
  141. if (this->Keyword.empty()) {
  142. return;
  143. }
  144. if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
  145. if (this->ParseResults != nullptr) {
  146. this->ParseResults->AddKeywordError(this->Keyword,
  147. " missing required value\n");
  148. }
  149. if (this->Bindings.KeywordMissingValue) {
  150. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  151. }
  152. }
  153. }
  154. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  155. {
  156. if (*this) {
  157. return false;
  158. }
  159. std::string e;
  160. for (auto const& ke : this->KeywordErrors) {
  161. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  162. }
  163. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  164. return true;
  165. }
  166. } // namespace ArgumentParser