cmArgumentParser.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cassert>
  6. #include <functional>
  7. #include <map>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm/optional>
  12. #include <cm/string_view>
  13. #include <cm/type_traits>
  14. #include <cmext/string_view>
  15. #include "cmArgumentParserTypes.h" // IWYU pragma: keep
  16. template <typename Result>
  17. class cmArgumentParser; // IWYU pragma: keep
  18. class cmMakefile;
  19. namespace ArgumentParser {
  20. class ParseResult
  21. {
  22. std::map<cm::string_view, std::string> KeywordErrors;
  23. public:
  24. explicit operator bool() const { return this->KeywordErrors.empty(); }
  25. void AddKeywordError(cm::string_view key, cm::string_view text)
  26. {
  27. this->KeywordErrors[key] += text;
  28. }
  29. std::map<cm::string_view, std::string> const& GetKeywordErrors() const
  30. {
  31. return this->KeywordErrors;
  32. }
  33. bool MaybeReportError(cmMakefile& mf) const;
  34. };
  35. template <typename Result>
  36. typename std::enable_if<std::is_base_of<ParseResult, Result>::value,
  37. ParseResult*>::type
  38. AsParseResultPtr(Result& result)
  39. {
  40. return &result;
  41. }
  42. template <typename Result>
  43. typename std::enable_if<!std::is_base_of<ParseResult, Result>::value,
  44. ParseResult*>::type
  45. AsParseResultPtr(Result&)
  46. {
  47. return nullptr;
  48. }
  49. class Instance;
  50. using KeywordAction = std::function<void(Instance&)>;
  51. using KeywordNameAction = std::function<void(Instance&, cm::string_view)>;
  52. // using KeywordActionMap = cm::flat_map<cm::string_view, KeywordAction>;
  53. class KeywordActionMap
  54. : public std::vector<std::pair<cm::string_view, KeywordAction>>
  55. {
  56. public:
  57. std::pair<iterator, bool> Emplace(cm::string_view name,
  58. KeywordAction action);
  59. const_iterator Find(cm::string_view name) const;
  60. };
  61. class ActionMap
  62. {
  63. public:
  64. KeywordActionMap Keywords;
  65. KeywordNameAction KeywordMissingValue;
  66. KeywordNameAction ParsedKeyword;
  67. };
  68. class Base
  69. {
  70. public:
  71. using Instance = ArgumentParser::Instance;
  72. using ParseResult = ArgumentParser::ParseResult;
  73. ArgumentParser::ActionMap Bindings;
  74. bool MaybeBind(cm::string_view name, KeywordAction action)
  75. {
  76. return this->Bindings.Keywords.Emplace(name, std::move(action)).second;
  77. }
  78. void Bind(cm::string_view name, KeywordAction action)
  79. {
  80. bool const inserted = this->MaybeBind(name, std::move(action));
  81. assert(inserted);
  82. static_cast<void>(inserted);
  83. }
  84. void BindParsedKeyword(KeywordNameAction action)
  85. {
  86. assert(!this->Bindings.ParsedKeyword);
  87. this->Bindings.ParsedKeyword = std::move(action);
  88. }
  89. void BindKeywordMissingValue(KeywordNameAction action)
  90. {
  91. assert(!this->Bindings.KeywordMissingValue);
  92. this->Bindings.KeywordMissingValue = std::move(action);
  93. }
  94. };
  95. class Instance
  96. {
  97. public:
  98. Instance(ActionMap const& bindings, ParseResult* parseResult,
  99. std::vector<std::string>* unparsedArguments,
  100. std::vector<cm::string_view>* parsedKeywords,
  101. void* result = nullptr)
  102. : Bindings(bindings)
  103. , ParseResults(parseResult)
  104. , UnparsedArguments(unparsedArguments)
  105. , ParsedKeywords(parsedKeywords)
  106. , Result(result)
  107. {
  108. }
  109. void Bind(bool& val);
  110. void Bind(std::string& val);
  111. void Bind(Maybe<std::string>& val);
  112. void Bind(MaybeEmpty<std::vector<std::string>>& val);
  113. void Bind(NonEmpty<std::vector<std::string>>& val);
  114. void Bind(std::vector<std::vector<std::string>>& val);
  115. // cm::optional<> records the presence the keyword to which it binds.
  116. template <typename T>
  117. void Bind(cm::optional<T>& optVal)
  118. {
  119. if (!optVal) {
  120. optVal.emplace();
  121. }
  122. this->Bind(*optVal);
  123. }
  124. template <typename Range>
  125. void Parse(Range const& args)
  126. {
  127. for (cm::string_view arg : args) {
  128. this->Consume(arg);
  129. }
  130. this->FinishKeyword();
  131. }
  132. private:
  133. ActionMap const& Bindings;
  134. ParseResult* ParseResults = nullptr;
  135. std::vector<std::string>* UnparsedArguments = nullptr;
  136. std::vector<cm::string_view>* ParsedKeywords = nullptr;
  137. void* Result = nullptr;
  138. cm::string_view Keyword;
  139. std::string* CurrentString = nullptr;
  140. std::vector<std::string>* CurrentList = nullptr;
  141. bool ExpectValue = false;
  142. void Consume(cm::string_view arg);
  143. void FinishKeyword();
  144. template <typename Result>
  145. friend class ::cmArgumentParser;
  146. };
  147. } // namespace ArgumentParser
  148. template <typename Result>
  149. class cmArgumentParser : private ArgumentParser::Base
  150. {
  151. public:
  152. // I *think* this function could be made `constexpr` when the code is
  153. // compiled as C++20. This would allow building a parser at compile time.
  154. template <typename T>
  155. cmArgumentParser& Bind(cm::static_string_view name, T Result::*member)
  156. {
  157. this->Base::Bind(name, [member](Instance& instance) {
  158. instance.Bind(static_cast<Result*>(instance.Result)->*member);
  159. });
  160. return *this;
  161. }
  162. cmArgumentParser& BindParsedKeywords(
  163. std::vector<cm::string_view> Result::*member)
  164. {
  165. this->Base::BindParsedKeyword(
  166. [member](Instance& instance, cm::string_view arg) {
  167. (static_cast<Result*>(instance.Result)->*member).emplace_back(arg);
  168. });
  169. return *this;
  170. }
  171. template <typename Range>
  172. bool Parse(Result& result, Range const& args,
  173. std::vector<std::string>* unparsedArguments,
  174. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  175. {
  176. using ArgumentParser::AsParseResultPtr;
  177. ParseResult* parseResultPtr = AsParseResultPtr(result);
  178. Instance instance(this->Bindings, parseResultPtr, unparsedArguments,
  179. parsedKeywords, &result);
  180. instance.Parse(args);
  181. return parseResultPtr ? static_cast<bool>(*parseResultPtr) : true;
  182. }
  183. template <typename Range>
  184. Result Parse(Range const& args, std::vector<std::string>* unparsedArguments,
  185. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  186. {
  187. Result result;
  188. this->Parse(result, args, unparsedArguments, parsedKeywords);
  189. return result;
  190. }
  191. };
  192. template <>
  193. class cmArgumentParser<void> : private ArgumentParser::Base
  194. {
  195. public:
  196. template <typename T>
  197. cmArgumentParser& Bind(cm::static_string_view name, T& ref)
  198. {
  199. this->Base::Bind(name, [&ref](Instance& instance) { instance.Bind(ref); });
  200. return *this;
  201. }
  202. cmArgumentParser& BindParsedKeywords(std::vector<cm::string_view>& ref)
  203. {
  204. this->Base::BindParsedKeyword(
  205. [&ref](Instance&, cm::string_view arg) { ref.emplace_back(arg); });
  206. return *this;
  207. }
  208. template <typename Range>
  209. ParseResult Parse(
  210. Range const& args, std::vector<std::string>* unparsedArguments,
  211. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  212. {
  213. ParseResult parseResult;
  214. Instance instance(this->Bindings, &parseResult, unparsedArguments,
  215. parsedKeywords);
  216. instance.Parse(args);
  217. return parseResult;
  218. }
  219. protected:
  220. using Base::Instance;
  221. using Base::BindKeywordMissingValue;
  222. template <typename T>
  223. bool Bind(cm::string_view name, T& ref)
  224. {
  225. return this->MaybeBind(name,
  226. [&ref](Instance& instance) { instance.Bind(ref); });
  227. }
  228. };