cmCommandArgumentsHelper.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "cmCommandArgumentsHelper.h"
  4. cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
  5. const char* key,
  6. cmCommandArgumentGroup* group)
  7. : Key(key)
  8. , Group(group)
  9. , WasActive(false)
  10. , ArgumentsBeforeEmpty(true)
  11. , CurrentIndex(0)
  12. {
  13. if (args != nullptr) {
  14. args->AddArgument(this);
  15. }
  16. if (this->Group != nullptr) {
  17. this->Group->ContainedArguments.push_back(this);
  18. }
  19. }
  20. void cmCommandArgument::Reset()
  21. {
  22. this->WasActive = false;
  23. this->CurrentIndex = 0;
  24. this->DoReset();
  25. }
  26. void cmCommandArgument::Follows(const cmCommandArgument* arg)
  27. {
  28. this->ArgumentsBeforeEmpty = false;
  29. this->ArgumentsBefore.insert(arg);
  30. }
  31. void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
  32. {
  33. if (group != nullptr) {
  34. this->ArgumentsBeforeEmpty = false;
  35. this->ArgumentsBefore.insert(group->ContainedArguments.begin(),
  36. group->ContainedArguments.end());
  37. }
  38. }
  39. bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
  40. {
  41. if (this->ArgumentsBeforeEmpty) {
  42. return true;
  43. }
  44. return this->ArgumentsBefore.find(current) != this->ArgumentsBefore.end();
  45. }
  46. bool cmCommandArgument::KeyMatches(const std::string& key) const
  47. {
  48. if ((this->Key == nullptr) || (this->Key[0] == '\0')) {
  49. return true;
  50. }
  51. return (key == this->Key);
  52. }
  53. void cmCommandArgument::ApplyOwnGroup()
  54. {
  55. if (this->Group != nullptr) {
  56. for (cmCommandArgument* cargs : this->Group->ContainedArguments) {
  57. if (cargs != this) {
  58. this->ArgumentsBefore.insert(cargs);
  59. }
  60. }
  61. }
  62. }
  63. void cmCommandArgument::Activate()
  64. {
  65. this->WasActive = true;
  66. this->CurrentIndex = 0;
  67. }
  68. bool cmCommandArgument::Consume(const std::string& arg)
  69. {
  70. bool res = this->DoConsume(arg, this->CurrentIndex);
  71. this->CurrentIndex++;
  72. return res;
  73. }
  74. cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
  75. const char* key,
  76. cmCommandArgumentGroup* group)
  77. : cmCommandArgument(args, key, group)
  78. , Ignore(nullptr)
  79. {
  80. if ((key == nullptr) || (*key == 0)) {
  81. this->DataStart = 0;
  82. } else {
  83. this->DataStart = 1;
  84. }
  85. }
  86. bool cmCAStringVector::DoConsume(const std::string& arg, unsigned int index)
  87. {
  88. if (index >= this->DataStart) {
  89. if ((this->Ignore == nullptr) || (arg != this->Ignore)) {
  90. this->Vector.push_back(arg);
  91. }
  92. }
  93. return false;
  94. }
  95. void cmCAStringVector::DoReset()
  96. {
  97. this->Vector.clear();
  98. }
  99. cmCAString::cmCAString(cmCommandArgumentsHelper* args, const char* key,
  100. cmCommandArgumentGroup* group)
  101. : cmCommandArgument(args, key, group)
  102. {
  103. if ((key == nullptr) || (*key == 0)) {
  104. this->DataStart = 0;
  105. } else {
  106. this->DataStart = 1;
  107. }
  108. }
  109. bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
  110. {
  111. if (index == this->DataStart) {
  112. this->String = arg;
  113. }
  114. return index >= this->DataStart;
  115. }
  116. void cmCAString::DoReset()
  117. {
  118. this->String.clear();
  119. }
  120. cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
  121. cmCommandArgumentGroup* group)
  122. : cmCommandArgument(args, key, group)
  123. , Enabled(false)
  124. {
  125. }
  126. bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
  127. {
  128. if (index == 0) {
  129. this->Enabled = true;
  130. }
  131. return true;
  132. }
  133. void cmCAEnabler::DoReset()
  134. {
  135. this->Enabled = false;
  136. }
  137. cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
  138. cmCommandArgumentGroup* group)
  139. : cmCommandArgument(args, key, group)
  140. , Enabled(true)
  141. {
  142. }
  143. bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
  144. {
  145. if (index == 0) {
  146. this->Enabled = false;
  147. }
  148. return true;
  149. }
  150. void cmCADisabler::DoReset()
  151. {
  152. this->Enabled = true;
  153. }
  154. void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
  155. {
  156. for (cmCommandArgument* ca : this->ContainedArguments) {
  157. ca->Follows(arg);
  158. }
  159. }
  160. void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
  161. {
  162. for (cmCommandArgument* ca : this->ContainedArguments) {
  163. ca->FollowsGroup(group);
  164. }
  165. }
  166. void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
  167. std::vector<std::string>* unconsumedArgs)
  168. {
  169. if (args == nullptr) {
  170. return;
  171. }
  172. for (cmCommandArgument* ca : this->Arguments) {
  173. ca->ApplyOwnGroup();
  174. ca->Reset();
  175. }
  176. cmCommandArgument* activeArgument = nullptr;
  177. const cmCommandArgument* previousArgument = nullptr;
  178. for (std::string const& it : *args) {
  179. for (cmCommandArgument* ca : this->Arguments) {
  180. if (ca->KeyMatches(it) && (ca->MayFollow(previousArgument))) {
  181. activeArgument = ca;
  182. activeArgument->Activate();
  183. break;
  184. }
  185. }
  186. if (activeArgument) {
  187. bool argDone = activeArgument->Consume(it);
  188. previousArgument = activeArgument;
  189. if (argDone) {
  190. activeArgument = nullptr;
  191. }
  192. } else {
  193. if (unconsumedArgs != nullptr) {
  194. unconsumedArgs->push_back(it);
  195. }
  196. }
  197. }
  198. }
  199. void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
  200. {
  201. this->Arguments.push_back(arg);
  202. }