cmGeneratorExpressionParser.cxx 8.2 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 "cmAlgorithms.h"
  5. #include "cmGeneratorExpressionEvaluator.h"
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <utility>
  9. cmGeneratorExpressionParser::cmGeneratorExpressionParser(
  10. std::vector<cmGeneratorExpressionToken> tokens)
  11. : Tokens(std::move(tokens))
  12. , NestingLevel(0)
  13. {
  14. }
  15. void cmGeneratorExpressionParser::Parse(
  16. std::vector<cmGeneratorExpressionEvaluator*>& result)
  17. {
  18. it = this->Tokens.begin();
  19. while (this->it != this->Tokens.end()) {
  20. this->ParseContent(result);
  21. }
  22. }
  23. static void extendText(
  24. std::vector<cmGeneratorExpressionEvaluator*>& result,
  25. std::vector<cmGeneratorExpressionToken>::const_iterator it)
  26. {
  27. if (!result.empty() &&
  28. (*(result.end() - 1))->GetType() ==
  29. cmGeneratorExpressionEvaluator::Text) {
  30. TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
  31. textContent->Extend(it->Length);
  32. } else {
  33. TextContent* textContent = new TextContent(it->Content, it->Length);
  34. result.push_back(textContent);
  35. }
  36. }
  37. static void extendResult(
  38. std::vector<cmGeneratorExpressionEvaluator*>& result,
  39. const std::vector<cmGeneratorExpressionEvaluator*>& contents)
  40. {
  41. if (!result.empty() &&
  42. (*(result.end() - 1))->GetType() ==
  43. cmGeneratorExpressionEvaluator::Text &&
  44. contents.front()->GetType() == cmGeneratorExpressionEvaluator::Text) {
  45. TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
  46. textContent->Extend(
  47. static_cast<TextContent*>(contents.front())->GetLength());
  48. delete contents.front();
  49. cmAppend(result, contents.begin() + 1, contents.end());
  50. } else {
  51. cmAppend(result, contents);
  52. }
  53. }
  54. void cmGeneratorExpressionParser::ParseGeneratorExpression(
  55. std::vector<cmGeneratorExpressionEvaluator*>& 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. std::vector<cmGeneratorExpressionEvaluator*> 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. GeneratorExpressionContent* content = new 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(content);
  87. return;
  88. }
  89. std::vector<std::vector<cmGeneratorExpressionEvaluator*>> 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, 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, *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. GeneratorExpressionContent* content =
  179. new GeneratorExpressionContent(startToken->Content, contentLength);
  180. content->SetIdentifier(std::move(identifier));
  181. content->SetParameters(std::move(parameters));
  182. result.push_back(content);
  183. }
  184. void cmGeneratorExpressionParser::ParseContent(
  185. std::vector<cmGeneratorExpressionEvaluator*>& 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. TextContent* textContent =
  198. static_cast<TextContent*>(*(result.end() - 1));
  199. textContent->Extend(this->it->Length);
  200. assert(this->it != this->Tokens.end());
  201. ++this->it;
  202. return;
  203. }
  204. }
  205. cmGeneratorExpressionEvaluator* n =
  206. new TextContent(this->it->Content, this->it->Length);
  207. result.push_back(n);
  208. assert(this->it != this->Tokens.end());
  209. ++this->it;
  210. return;
  211. }
  212. case cmGeneratorExpressionToken::BeginExpression:
  213. assert(this->it != this->Tokens.end());
  214. ++this->it;
  215. this->ParseGeneratorExpression(result);
  216. return;
  217. case cmGeneratorExpressionToken::EndExpression:
  218. case cmGeneratorExpressionToken::ColonSeparator:
  219. case cmGeneratorExpressionToken::CommaSeparator:
  220. if (this->NestingLevel == 0) {
  221. extendText(result, this->it);
  222. } else {
  223. assert(false && "Got unexpected syntax token.");
  224. }
  225. assert(this->it != this->Tokens.end());
  226. ++this->it;
  227. return;
  228. }
  229. assert(false && "Unhandled token in generator expression.");
  230. }