cmGeneratorExpressionParser.cxx 8.5 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. #include "cmGeneratorExpressionParser.h"
  4. #include "cmGeneratorExpressionEvaluator.h"
  5. #include <assert.h>
  6. #include <stddef.h>
  7. #include <utility>
  8. cmGeneratorExpressionParser::cmGeneratorExpressionParser(
  9. std::vector<cmGeneratorExpressionToken> tokens)
  10. : Tokens(std::move(tokens))
  11. , NestingLevel(0)
  12. {
  13. }
  14. void cmGeneratorExpressionParser::Parse(
  15. std::vector<cmGeneratorExpressionEvaluator*>& result)
  16. {
  17. it = this->Tokens.begin();
  18. while (this->it != this->Tokens.end()) {
  19. this->ParseContent(result);
  20. }
  21. }
  22. static void extendText(
  23. std::vector<cmGeneratorExpressionEvaluator*>& result,
  24. std::vector<cmGeneratorExpressionToken>::const_iterator it)
  25. {
  26. if (!result.empty() &&
  27. (*(result.end() - 1))->GetType() ==
  28. cmGeneratorExpressionEvaluator::Text) {
  29. TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
  30. textContent->Extend(it->Length);
  31. } else {
  32. TextContent* textContent = new TextContent(it->Content, it->Length);
  33. result.push_back(textContent);
  34. }
  35. }
  36. static void extendResult(
  37. std::vector<cmGeneratorExpressionEvaluator*>& result,
  38. const std::vector<cmGeneratorExpressionEvaluator*>& contents)
  39. {
  40. if (!result.empty() &&
  41. (*(result.end() - 1))->GetType() ==
  42. cmGeneratorExpressionEvaluator::Text &&
  43. (*contents.begin())->GetType() == cmGeneratorExpressionEvaluator::Text) {
  44. TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
  45. textContent->Extend(
  46. static_cast<TextContent*>(*contents.begin())->GetLength());
  47. delete *contents.begin();
  48. result.insert(result.end(), contents.begin() + 1, contents.end());
  49. } else {
  50. result.insert(result.end(), contents.begin(), contents.end());
  51. }
  52. }
  53. void cmGeneratorExpressionParser::ParseGeneratorExpression(
  54. std::vector<cmGeneratorExpressionEvaluator*>& result)
  55. {
  56. assert(this->it != this->Tokens.end());
  57. unsigned int nestedLevel = this->NestingLevel;
  58. ++this->NestingLevel;
  59. std::vector<cmGeneratorExpressionToken>::const_iterator startToken =
  60. 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. typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  160. typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  161. std::vector<EvaluatorVector>::const_iterator pit = parameters.begin();
  162. const std::vector<EvaluatorVector>::const_iterator pend =
  163. parameters.end();
  164. std::vector<TokenVector::const_iterator>::const_iterator commaIt =
  165. commaTokens.begin();
  166. assert(parameters.size() > commaTokens.size());
  167. for (; pit != pend; ++pit, ++commaIt) {
  168. if (!pit->empty() && !emptyParamTermination) {
  169. extendResult(result, *pit);
  170. }
  171. if (commaIt != commaTokens.end()) {
  172. extendText(result, *commaIt);
  173. } else {
  174. break;
  175. }
  176. }
  177. }
  178. return;
  179. }
  180. size_t contentLength =
  181. ((this->it - 1)->Content - startToken->Content) + (this->it - 1)->Length;
  182. GeneratorExpressionContent* content =
  183. new GeneratorExpressionContent(startToken->Content, contentLength);
  184. content->SetIdentifier(std::move(identifier));
  185. content->SetParameters(std::move(parameters));
  186. result.push_back(content);
  187. }
  188. void cmGeneratorExpressionParser::ParseContent(
  189. std::vector<cmGeneratorExpressionEvaluator*>& result)
  190. {
  191. assert(this->it != this->Tokens.end());
  192. switch (this->it->TokenType) {
  193. case cmGeneratorExpressionToken::Text: {
  194. if (this->NestingLevel == 0) {
  195. if (!result.empty() &&
  196. (*(result.end() - 1))->GetType() ==
  197. cmGeneratorExpressionEvaluator::Text) {
  198. // A comma in 'plain text' could have split text that should
  199. // otherwise be continuous. Extend the last text content instead of
  200. // creating a new one.
  201. TextContent* textContent =
  202. static_cast<TextContent*>(*(result.end() - 1));
  203. textContent->Extend(this->it->Length);
  204. assert(this->it != this->Tokens.end());
  205. ++this->it;
  206. return;
  207. }
  208. }
  209. cmGeneratorExpressionEvaluator* n =
  210. new TextContent(this->it->Content, this->it->Length);
  211. result.push_back(n);
  212. assert(this->it != this->Tokens.end());
  213. ++this->it;
  214. return;
  215. }
  216. case cmGeneratorExpressionToken::BeginExpression:
  217. assert(this->it != this->Tokens.end());
  218. ++this->it;
  219. this->ParseGeneratorExpression(result);
  220. return;
  221. case cmGeneratorExpressionToken::EndExpression:
  222. case cmGeneratorExpressionToken::ColonSeparator:
  223. case cmGeneratorExpressionToken::CommaSeparator:
  224. if (this->NestingLevel == 0) {
  225. extendText(result, this->it);
  226. } else {
  227. assert(false && "Got unexpected syntax token.");
  228. }
  229. assert(this->it != this->Tokens.end());
  230. ++this->it;
  231. return;
  232. }
  233. assert(false && "Unhandled token in generator expression.");
  234. }