cmGeneratorExpressionParser.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "cmGeneratorExpressionParser.h"
  4. #include <cassert>
  5. #include <cstddef>
  6. #include <utility>
  7. #include <cm/memory>
  8. #include <cmext/algorithm>
  9. #include <cmext/memory>
  10. #include "cmGeneratorExpressionEvaluator.h"
  11. cmGeneratorExpressionParser::cmGeneratorExpressionParser(
  12. std::vector<cmGeneratorExpressionToken> tokens)
  13. : Tokens(std::move(tokens))
  14. , NestingLevel(0)
  15. {
  16. }
  17. void cmGeneratorExpressionParser::Parse(
  18. cmGeneratorExpressionEvaluatorVector& result)
  19. {
  20. it = this->Tokens.begin();
  21. while (this->it != this->Tokens.end()) {
  22. this->ParseContent(result);
  23. }
  24. }
  25. static void extendText(
  26. cmGeneratorExpressionEvaluatorVector& result,
  27. std::vector<cmGeneratorExpressionToken>::const_iterator it)
  28. {
  29. if (!result.empty() &&
  30. (*(result.end() - 1))->GetType() ==
  31. cmGeneratorExpressionEvaluator::Text) {
  32. cm::static_reference_cast<TextContent>(*(result.end() - 1))
  33. .Extend(it->Length);
  34. } else {
  35. auto textContent = cm::make_unique<TextContent>(it->Content, it->Length);
  36. result.push_back(std::move(textContent));
  37. }
  38. }
  39. static void extendResult(
  40. cmGeneratorExpressionParser::cmGeneratorExpressionEvaluatorVector& result,
  41. cmGeneratorExpressionParser::cmGeneratorExpressionEvaluatorVector&& contents)
  42. {
  43. if (!result.empty() &&
  44. (*(result.end() - 1))->GetType() ==
  45. cmGeneratorExpressionEvaluator::Text &&
  46. contents.front()->GetType() == cmGeneratorExpressionEvaluator::Text) {
  47. cm::static_reference_cast<TextContent>(*(result.end() - 1))
  48. .Extend(
  49. cm::static_reference_cast<TextContent>(contents.front()).GetLength());
  50. contents.erase(contents.begin());
  51. }
  52. cm::append(result, std::move(contents));
  53. }
  54. void cmGeneratorExpressionParser::ParseGeneratorExpression(
  55. cmGeneratorExpressionEvaluatorVector& result)
  56. {
  57. assert(this->it != this->Tokens.end());
  58. unsigned int nestedLevel = this->NestingLevel;
  59. ++this->NestingLevel;
  60. auto startToken = this->it - 1;
  61. cmGeneratorExpressionEvaluatorVector identifier;
  62. while (this->it->TokenType != cmGeneratorExpressionToken::EndExpression &&
  63. this->it->TokenType != cmGeneratorExpressionToken::ColonSeparator) {
  64. if (this->it->TokenType == cmGeneratorExpressionToken::CommaSeparator) {
  65. extendText(identifier, this->it);
  66. ++this->it;
  67. } else {
  68. this->ParseContent(identifier);
  69. }
  70. if (this->it == this->Tokens.end()) {
  71. break;
  72. }
  73. }
  74. if (identifier.empty()) {
  75. // ERROR
  76. }
  77. if (this->it != this->Tokens.end() &&
  78. this->it->TokenType == cmGeneratorExpressionToken::EndExpression) {
  79. auto content = cm::make_unique<GeneratorExpressionContent>(
  80. startToken->Content,
  81. this->it->Content - startToken->Content + this->it->Length);
  82. assert(this->it != this->Tokens.end());
  83. ++this->it;
  84. --this->NestingLevel;
  85. content->SetIdentifier(std::move(identifier));
  86. result.push_back(std::move(content));
  87. return;
  88. }
  89. std::vector<cmGeneratorExpressionEvaluatorVector> parameters;
  90. std::vector<std::vector<cmGeneratorExpressionToken>::const_iterator>
  91. commaTokens;
  92. std::vector<cmGeneratorExpressionToken>::const_iterator colonToken;
  93. bool emptyParamTermination = false;
  94. if (this->it != this->Tokens.end() &&
  95. this->it->TokenType == cmGeneratorExpressionToken::ColonSeparator) {
  96. colonToken = this->it;
  97. parameters.resize(parameters.size() + 1);
  98. assert(this->it != this->Tokens.end());
  99. ++this->it;
  100. if (this->it == this->Tokens.end()) {
  101. emptyParamTermination = true;
  102. }
  103. while (this->it != this->Tokens.end() &&
  104. this->it->TokenType == cmGeneratorExpressionToken::CommaSeparator) {
  105. commaTokens.push_back(this->it);
  106. parameters.resize(parameters.size() + 1);
  107. assert(this->it != this->Tokens.end());
  108. ++this->it;
  109. if (this->it == this->Tokens.end()) {
  110. emptyParamTermination = true;
  111. }
  112. }
  113. while (this->it != this->Tokens.end() &&
  114. this->it->TokenType == cmGeneratorExpressionToken::ColonSeparator) {
  115. extendText(*(parameters.end() - 1), this->it);
  116. assert(this->it != this->Tokens.end());
  117. ++this->it;
  118. }
  119. while (this->it != this->Tokens.end() &&
  120. this->it->TokenType != cmGeneratorExpressionToken::EndExpression) {
  121. this->ParseContent(*(parameters.end() - 1));
  122. if (this->it == this->Tokens.end()) {
  123. break;
  124. }
  125. while (this->it != this->Tokens.end() &&
  126. this->it->TokenType ==
  127. cmGeneratorExpressionToken::CommaSeparator) {
  128. commaTokens.push_back(this->it);
  129. parameters.resize(parameters.size() + 1);
  130. assert(this->it != this->Tokens.end());
  131. ++this->it;
  132. if (this->it == this->Tokens.end()) {
  133. emptyParamTermination = true;
  134. }
  135. }
  136. while (this->it != this->Tokens.end() &&
  137. this->it->TokenType ==
  138. cmGeneratorExpressionToken::ColonSeparator) {
  139. extendText(*(parameters.end() - 1), this->it);
  140. assert(this->it != this->Tokens.end());
  141. ++this->it;
  142. }
  143. }
  144. if (this->it != this->Tokens.end() &&
  145. this->it->TokenType == cmGeneratorExpressionToken::EndExpression) {
  146. --this->NestingLevel;
  147. assert(this->it != this->Tokens.end());
  148. ++this->it;
  149. }
  150. }
  151. if (nestedLevel != this->NestingLevel) {
  152. // There was a '$<' in the text, but no corresponding '>'. Rebuild to
  153. // treat the '$<' as having been plain text, along with the
  154. // corresponding : and , tokens that might have been found.
  155. extendText(result, startToken);
  156. extendResult(result, std::move(identifier));
  157. if (!parameters.empty()) {
  158. extendText(result, colonToken);
  159. auto pit = parameters.begin();
  160. const auto pend = parameters.end();
  161. auto commaIt = commaTokens.begin();
  162. assert(parameters.size() > commaTokens.size());
  163. for (; pit != pend; ++pit, ++commaIt) {
  164. if (!pit->empty() && !emptyParamTermination) {
  165. extendResult(result, std::move(*pit));
  166. }
  167. if (commaIt != commaTokens.end()) {
  168. extendText(result, *commaIt);
  169. } else {
  170. break;
  171. }
  172. }
  173. }
  174. return;
  175. }
  176. size_t contentLength =
  177. ((this->it - 1)->Content - startToken->Content) + (this->it - 1)->Length;
  178. auto content = cm::make_unique<GeneratorExpressionContent>(
  179. startToken->Content, contentLength);
  180. content->SetIdentifier(std::move(identifier));
  181. content->SetParameters(std::move(parameters));
  182. result.push_back(std::move(content));
  183. }
  184. void cmGeneratorExpressionParser::ParseContent(
  185. cmGeneratorExpressionEvaluatorVector& result)
  186. {
  187. assert(this->it != this->Tokens.end());
  188. switch (this->it->TokenType) {
  189. case cmGeneratorExpressionToken::Text: {
  190. if (this->NestingLevel == 0) {
  191. if (!result.empty() &&
  192. (*(result.end() - 1))->GetType() ==
  193. cmGeneratorExpressionEvaluator::Text) {
  194. // A comma in 'plain text' could have split text that should
  195. // otherwise be continuous. Extend the last text content instead of
  196. // creating a new one.
  197. cm::static_reference_cast<TextContent>(*(result.end() - 1))
  198. .Extend(this->it->Length);
  199. assert(this->it != this->Tokens.end());
  200. ++this->it;
  201. return;
  202. }
  203. }
  204. auto n =
  205. cm::make_unique<TextContent>(this->it->Content, this->it->Length);
  206. result.push_back(std::move(n));
  207. assert(this->it != this->Tokens.end());
  208. ++this->it;
  209. return;
  210. }
  211. case cmGeneratorExpressionToken::BeginExpression:
  212. assert(this->it != this->Tokens.end());
  213. ++this->it;
  214. this->ParseGeneratorExpression(result);
  215. return;
  216. case cmGeneratorExpressionToken::EndExpression:
  217. case cmGeneratorExpressionToken::ColonSeparator:
  218. case cmGeneratorExpressionToken::CommaSeparator:
  219. if (this->NestingLevel == 0) {
  220. extendText(result, this->it);
  221. } else {
  222. assert(false && "Got unexpected syntax token.");
  223. }
  224. assert(this->it != this->Tokens.end());
  225. ++this->it;
  226. return;
  227. }
  228. assert(false && "Unhandled token in generator expression.");
  229. }