cmArgumentParser.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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(NonEmpty<std::string>& val)
  69. {
  70. this->Bind(
  71. [this, &val](cm::string_view arg) -> Continue {
  72. if (arg.empty() && this->ParseResults) {
  73. this->ParseResults->AddKeywordError(this->Keyword,
  74. " empty string not allowed\n");
  75. }
  76. val.assign(std::string(arg));
  77. return Continue::No;
  78. },
  79. ExpectAtLeast{ 1 });
  80. }
  81. void Instance::Bind(Maybe<std::string>& val)
  82. {
  83. this->Bind(
  84. [&val](cm::string_view arg) -> Continue {
  85. static_cast<std::string&>(val) = std::string(arg);
  86. return Continue::No;
  87. },
  88. ExpectAtLeast{ 0 });
  89. }
  90. void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
  91. {
  92. this->Bind(
  93. [&val](cm::string_view arg) -> Continue {
  94. val.emplace_back(arg);
  95. return Continue::Yes;
  96. },
  97. ExpectAtLeast{ 0 });
  98. }
  99. void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
  100. {
  101. this->Bind(
  102. [&val](cm::string_view arg) -> Continue {
  103. val.emplace_back(arg);
  104. return Continue::Yes;
  105. },
  106. ExpectAtLeast{ 1 });
  107. }
  108. void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
  109. {
  110. multiVal.emplace_back();
  111. std::vector<std::string>& val = multiVal.back();
  112. this->Bind(
  113. [&val](cm::string_view arg) -> Continue {
  114. val.emplace_back(arg);
  115. return Continue::Yes;
  116. },
  117. ExpectAtLeast{ 0 });
  118. }
  119. void Instance::Consume(std::size_t pos, cm::string_view arg)
  120. {
  121. auto const it = this->Bindings.Keywords.Find(arg);
  122. if (it != this->Bindings.Keywords.end()) {
  123. this->FinishKeyword();
  124. this->Keyword = it->first;
  125. this->KeywordValuesSeen = 0;
  126. this->DoneWithPositional = true;
  127. if (this->Bindings.ParsedKeyword) {
  128. this->Bindings.ParsedKeyword(*this, it->first);
  129. }
  130. it->second(*this);
  131. return;
  132. }
  133. if (this->KeywordValueFunc) {
  134. switch (this->KeywordValueFunc(arg)) {
  135. case Continue::Yes:
  136. break;
  137. case Continue::No:
  138. this->KeywordValueFunc = nullptr;
  139. break;
  140. }
  141. ++this->KeywordValuesSeen;
  142. return;
  143. }
  144. if (!this->DoneWithPositional) {
  145. auto const pit = this->Bindings.Positions.Find(pos);
  146. if (pit != this->Bindings.Positions.end()) {
  147. pit->second(*this, pos, arg);
  148. return;
  149. }
  150. }
  151. if (this->UnparsedArguments != nullptr) {
  152. this->UnparsedArguments->emplace_back(arg);
  153. }
  154. }
  155. void Instance::FinishKeyword()
  156. {
  157. if (this->Keyword.empty()) {
  158. return;
  159. }
  160. if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
  161. if (this->ParseResults != nullptr) {
  162. this->ParseResults->AddKeywordError(this->Keyword,
  163. " missing required value\n");
  164. }
  165. if (this->Bindings.KeywordMissingValue) {
  166. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  167. }
  168. }
  169. }
  170. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  171. {
  172. if (*this) {
  173. return false;
  174. }
  175. std::string e;
  176. for (auto const& ke : this->KeywordErrors) {
  177. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  178. }
  179. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  180. return true;
  181. }
  182. } // namespace ArgumentParser