cmArgumentParser.h 6.4 KB

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