cmArgumentParser.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. };
  67. class Base
  68. {
  69. public:
  70. using Instance = ArgumentParser::Instance;
  71. using ParseResult = ArgumentParser::ParseResult;
  72. ArgumentParser::ActionMap Bindings;
  73. bool MaybeBind(cm::string_view name, KeywordAction action)
  74. {
  75. return this->Bindings.Keywords.Emplace(name, std::move(action)).second;
  76. }
  77. void Bind(cm::string_view name, KeywordAction action)
  78. {
  79. bool const inserted = this->MaybeBind(name, std::move(action));
  80. assert(inserted);
  81. static_cast<void>(inserted);
  82. }
  83. void BindKeywordMissingValue(KeywordNameAction action)
  84. {
  85. assert(!this->Bindings.KeywordMissingValue);
  86. this->Bindings.KeywordMissingValue = std::move(action);
  87. }
  88. };
  89. class Instance
  90. {
  91. public:
  92. Instance(ActionMap const& bindings, ParseResult* parseResult,
  93. std::vector<std::string>* unparsedArguments,
  94. std::vector<cm::string_view>* keywordsMissingValue,
  95. std::vector<cm::string_view>* parsedKeywords,
  96. void* result = nullptr)
  97. : Bindings(bindings)
  98. , ParseResults(parseResult)
  99. , UnparsedArguments(unparsedArguments)
  100. , KeywordsMissingValue(keywordsMissingValue)
  101. , ParsedKeywords(parsedKeywords)
  102. , Result(result)
  103. {
  104. }
  105. void Bind(bool& val);
  106. void Bind(std::string& val);
  107. void Bind(Maybe<std::string>& val);
  108. void Bind(MaybeEmpty<std::vector<std::string>>& val);
  109. void Bind(NonEmpty<std::vector<std::string>>& val);
  110. void Bind(std::vector<std::vector<std::string>>& val);
  111. // cm::optional<> records the presence the keyword to which it binds.
  112. template <typename T>
  113. void Bind(cm::optional<T>& optVal)
  114. {
  115. if (!optVal) {
  116. optVal.emplace();
  117. }
  118. this->Bind(*optVal);
  119. }
  120. template <typename Range>
  121. void Parse(Range const& args)
  122. {
  123. for (cm::string_view arg : args) {
  124. this->Consume(arg);
  125. }
  126. this->FinishKeyword();
  127. }
  128. private:
  129. ActionMap const& Bindings;
  130. ParseResult* ParseResults = nullptr;
  131. std::vector<std::string>* UnparsedArguments = nullptr;
  132. std::vector<cm::string_view>* KeywordsMissingValue = nullptr;
  133. std::vector<cm::string_view>* ParsedKeywords = nullptr;
  134. void* Result = nullptr;
  135. cm::string_view Keyword;
  136. std::string* CurrentString = nullptr;
  137. std::vector<std::string>* CurrentList = nullptr;
  138. bool ExpectValue = false;
  139. void Consume(cm::string_view arg);
  140. void FinishKeyword();
  141. template <typename Result>
  142. friend class ::cmArgumentParser;
  143. };
  144. } // namespace ArgumentParser
  145. template <typename Result>
  146. class cmArgumentParser : private ArgumentParser::Base
  147. {
  148. public:
  149. // I *think* this function could be made `constexpr` when the code is
  150. // compiled as C++20. This would allow building a parser at compile time.
  151. template <typename T>
  152. cmArgumentParser& Bind(cm::static_string_view name, T Result::*member)
  153. {
  154. this->Base::Bind(name, [member](Instance& instance) {
  155. instance.Bind(static_cast<Result*>(instance.Result)->*member);
  156. });
  157. return *this;
  158. }
  159. template <typename Range>
  160. bool Parse(Result& result, Range const& args,
  161. std::vector<std::string>* unparsedArguments,
  162. std::vector<cm::string_view>* keywordsMissingValue = nullptr,
  163. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  164. {
  165. using ArgumentParser::AsParseResultPtr;
  166. ParseResult* parseResultPtr = AsParseResultPtr(result);
  167. Instance instance(this->Bindings, parseResultPtr, unparsedArguments,
  168. keywordsMissingValue, parsedKeywords, &result);
  169. instance.Parse(args);
  170. return parseResultPtr ? static_cast<bool>(*parseResultPtr) : true;
  171. }
  172. template <typename Range>
  173. Result Parse(Range const& args, std::vector<std::string>* unparsedArguments,
  174. std::vector<cm::string_view>* keywordsMissingValue = nullptr,
  175. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  176. {
  177. Result result;
  178. this->Parse(result, args, unparsedArguments, keywordsMissingValue,
  179. parsedKeywords);
  180. return result;
  181. }
  182. };
  183. template <>
  184. class cmArgumentParser<void> : private ArgumentParser::Base
  185. {
  186. public:
  187. template <typename T>
  188. cmArgumentParser& Bind(cm::static_string_view name, T& ref)
  189. {
  190. this->Base::Bind(name, [&ref](Instance& instance) { instance.Bind(ref); });
  191. return *this;
  192. }
  193. template <typename Range>
  194. ParseResult Parse(
  195. Range const& args, std::vector<std::string>* unparsedArguments,
  196. std::vector<cm::string_view>* keywordsMissingValue = nullptr,
  197. std::vector<cm::string_view>* parsedKeywords = nullptr) const
  198. {
  199. ParseResult parseResult;
  200. Instance instance(this->Bindings, &parseResult, unparsedArguments,
  201. keywordsMissingValue, parsedKeywords);
  202. instance.Parse(args);
  203. return parseResult;
  204. }
  205. protected:
  206. using Base::Instance;
  207. using Base::BindKeywordMissingValue;
  208. template <typename T>
  209. bool Bind(cm::string_view name, T& ref)
  210. {
  211. return this->MaybeBind(name,
  212. [&ref](Instance& instance) { instance.Bind(ref); });
  213. }
  214. };