cmArgumentParser.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if (this->Bindings.ParsedKeyword) {
  127. this->Bindings.ParsedKeyword(*this, it->first);
  128. }
  129. it->second(*this);
  130. return;
  131. }
  132. if (this->KeywordValueFunc) {
  133. switch (this->KeywordValueFunc(arg)) {
  134. case Continue::Yes:
  135. break;
  136. case Continue::No:
  137. this->KeywordValueFunc = nullptr;
  138. break;
  139. }
  140. ++this->KeywordValuesSeen;
  141. return;
  142. }
  143. auto const pit = this->Bindings.Positions.Find(pos);
  144. if (pit != this->Bindings.Positions.end()) {
  145. pit->second(*this, pos, arg);
  146. return;
  147. }
  148. if (this->UnparsedArguments != nullptr) {
  149. this->UnparsedArguments->emplace_back(arg);
  150. }
  151. }
  152. void Instance::FinishKeyword()
  153. {
  154. if (this->Keyword.empty()) {
  155. return;
  156. }
  157. if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
  158. if (this->ParseResults != nullptr) {
  159. this->ParseResults->AddKeywordError(this->Keyword,
  160. " missing required value\n");
  161. }
  162. if (this->Bindings.KeywordMissingValue) {
  163. this->Bindings.KeywordMissingValue(*this, this->Keyword);
  164. }
  165. }
  166. }
  167. bool ParseResult::MaybeReportError(cmMakefile& mf) const
  168. {
  169. if (*this) {
  170. return false;
  171. }
  172. std::string e;
  173. for (auto const& ke : this->KeywordErrors) {
  174. e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
  175. }
  176. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  177. return true;
  178. }
  179. } // namespace ArgumentParser