2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2012-09-11 21:53:38 +04:00
|
|
|
#include "cmGeneratorExpressionParser.h"
|
|
|
|
|
|
|
|
#include "cmGeneratorExpressionEvaluator.h"
|
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stddef.h>
|
2012-10-12 18:51:16 +04:00
|
|
|
|
2012-09-11 21:53:38 +04:00
|
|
|
cmGeneratorExpressionParser::cmGeneratorExpressionParser(
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::vector<cmGeneratorExpressionToken>& tokens)
|
|
|
|
: Tokens(tokens)
|
|
|
|
, NestingLevel(0)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGeneratorExpressionParser::Parse(
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>& result)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
|
|
|
it = this->Tokens.begin();
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
while (this->it != this->Tokens.end()) {
|
2012-09-11 21:53:38 +04:00
|
|
|
this->ParseContent(result);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
static void extendText(
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>& result,
|
|
|
|
std::vector<cmGeneratorExpressionToken>::const_iterator it)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!result.empty() &&
|
|
|
|
(*(result.end() - 1))->GetType() ==
|
|
|
|
cmGeneratorExpressionEvaluator::Text) {
|
|
|
|
TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
|
2012-09-11 21:53:38 +04:00
|
|
|
textContent->Extend(it->Length);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
TextContent* textContent = new TextContent(it->Content, it->Length);
|
2012-09-11 21:53:38 +04:00
|
|
|
result.push_back(textContent);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
static void extendResult(
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>& result,
|
|
|
|
const std::vector<cmGeneratorExpressionEvaluator*>& contents)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!result.empty() &&
|
|
|
|
(*(result.end() - 1))->GetType() ==
|
|
|
|
cmGeneratorExpressionEvaluator::Text &&
|
|
|
|
(*contents.begin())->GetType() == cmGeneratorExpressionEvaluator::Text) {
|
|
|
|
TextContent* textContent = static_cast<TextContent*>(*(result.end() - 1));
|
2012-09-11 21:53:38 +04:00
|
|
|
textContent->Extend(
|
2016-05-16 17:34:04 +03:00
|
|
|
static_cast<TextContent*>(*contents.begin())->GetLength());
|
2012-09-11 21:53:38 +04:00
|
|
|
delete *contents.begin();
|
|
|
|
result.insert(result.end(), contents.begin() + 1, contents.end());
|
|
|
|
} else {
|
|
|
|
result.insert(result.end(), contents.begin(), contents.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGeneratorExpressionParser::ParseGeneratorExpression(
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>& result)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
2012-10-12 19:34:16 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
unsigned int nestedLevel = this->NestingLevel;
|
|
|
|
++this->NestingLevel;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<cmGeneratorExpressionToken>::const_iterator startToken =
|
|
|
|
this->it - 1;
|
2012-09-11 21:53:38 +04:00
|
|
|
|
|
|
|
std::vector<cmGeneratorExpressionEvaluator*> identifier;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (this->it->TokenType != cmGeneratorExpressionToken::EndExpression &&
|
|
|
|
this->it->TokenType != cmGeneratorExpressionToken::ColonSeparator) {
|
|
|
|
if (this->it->TokenType == cmGeneratorExpressionToken::CommaSeparator) {
|
2012-11-27 01:44:13 +04:00
|
|
|
extendText(identifier, this->it);
|
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-11-27 01:44:13 +04:00
|
|
|
this->ParseContent(identifier);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->it == this->Tokens.end()) {
|
2012-09-11 21:53:38 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (identifier.empty()) {
|
2012-09-11 21:53:38 +04:00
|
|
|
// ERROR
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2012-10-12 19:34:16 +04:00
|
|
|
if (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType == cmGeneratorExpressionToken::EndExpression) {
|
|
|
|
GeneratorExpressionContent* content =
|
|
|
|
new GeneratorExpressionContent(startToken->Content, this->it->Content -
|
|
|
|
startToken->Content + this->it->Length);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
|
|
|
--this->NestingLevel;
|
|
|
|
content->SetIdentifier(identifier);
|
|
|
|
result.push_back(content);
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
|
|
|
std::vector<std::vector<cmGeneratorExpressionEvaluator*> > parameters;
|
|
|
|
std::vector<std::vector<cmGeneratorExpressionToken>::const_iterator>
|
2016-05-16 17:34:04 +03:00
|
|
|
commaTokens;
|
2012-09-11 21:53:38 +04:00
|
|
|
std::vector<cmGeneratorExpressionToken>::const_iterator colonToken;
|
2013-08-13 15:32:27 +04:00
|
|
|
|
|
|
|
bool emptyParamTermination = false;
|
|
|
|
|
2012-10-12 19:34:16 +04:00
|
|
|
if (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType == cmGeneratorExpressionToken::ColonSeparator) {
|
2012-09-11 21:53:38 +04:00
|
|
|
colonToken = this->it;
|
|
|
|
parameters.resize(parameters.size() + 1);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->it == this->Tokens.end()) {
|
2013-08-13 15:32:27 +04:00
|
|
|
emptyParamTermination = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-12 19:34:16 +04:00
|
|
|
|
|
|
|
while (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType == cmGeneratorExpressionToken::CommaSeparator) {
|
2012-10-04 02:34:03 +04:00
|
|
|
commaTokens.push_back(this->it);
|
|
|
|
parameters.resize(parameters.size() + 1);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-10-04 02:34:03 +04:00
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->it == this->Tokens.end()) {
|
2013-08-13 15:32:27 +04:00
|
|
|
emptyParamTermination = true;
|
2012-10-04 02:34:03 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-12 19:34:16 +04:00
|
|
|
while (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType == cmGeneratorExpressionToken::ColonSeparator) {
|
2012-10-12 18:51:16 +04:00
|
|
|
extendText(*(parameters.end() - 1), this->it);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-10-12 18:51:16 +04:00
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-12 19:34:16 +04:00
|
|
|
while (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType != cmGeneratorExpressionToken::EndExpression) {
|
2012-09-11 21:53:38 +04:00
|
|
|
this->ParseContent(*(parameters.end() - 1));
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->it == this->Tokens.end()) {
|
2012-10-12 19:34:16 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-12 19:34:16 +04:00
|
|
|
while (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType ==
|
|
|
|
cmGeneratorExpressionToken::CommaSeparator) {
|
2012-09-11 21:53:38 +04:00
|
|
|
commaTokens.push_back(this->it);
|
|
|
|
parameters.resize(parameters.size() + 1);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->it == this->Tokens.end()) {
|
2013-08-13 15:32:27 +04:00
|
|
|
emptyParamTermination = true;
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-12 19:34:16 +04:00
|
|
|
while (this->it != this->Tokens.end() &&
|
2016-05-16 17:34:04 +03:00
|
|
|
this->it->TokenType ==
|
|
|
|
cmGeneratorExpressionToken::ColonSeparator) {
|
2012-09-21 13:31:03 +04:00
|
|
|
extendText(*(parameters.end() - 1), this->it);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-21 13:31:03 +04:00
|
|
|
++this->it;
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->it != this->Tokens.end() &&
|
|
|
|
this->it->TokenType == cmGeneratorExpressionToken::EndExpression) {
|
|
|
|
--this->NestingLevel;
|
|
|
|
assert(this->it != this->Tokens.end());
|
|
|
|
++this->it;
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (nestedLevel != this->NestingLevel) {
|
2012-09-11 21:53:38 +04:00
|
|
|
// There was a '$<' in the text, but no corresponding '>'. Rebuild to
|
|
|
|
// treat the '$<' as having been plain text, along with the
|
|
|
|
// corresponding : and , tokens that might have been found.
|
|
|
|
extendText(result, startToken);
|
|
|
|
extendResult(result, identifier);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parameters.empty()) {
|
2012-09-11 21:53:38 +04:00
|
|
|
extendText(result, colonToken);
|
|
|
|
|
|
|
|
typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
|
|
|
|
typedef std::vector<cmGeneratorExpressionToken> TokenVector;
|
|
|
|
std::vector<EvaluatorVector>::const_iterator pit = parameters.begin();
|
|
|
|
const std::vector<EvaluatorVector>::const_iterator pend =
|
2016-05-16 17:34:04 +03:00
|
|
|
parameters.end();
|
2012-09-11 21:53:38 +04:00
|
|
|
std::vector<TokenVector::const_iterator>::const_iterator commaIt =
|
2016-05-16 17:34:04 +03:00
|
|
|
commaTokens.begin();
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(parameters.size() > commaTokens.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
for (; pit != pend; ++pit, ++commaIt) {
|
|
|
|
if (!pit->empty() && !emptyParamTermination) {
|
2013-08-13 15:32:27 +04:00
|
|
|
extendResult(result, *pit);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (commaIt != commaTokens.end()) {
|
2012-09-11 21:53:38 +04:00
|
|
|
extendText(result, *commaIt);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-10-15 17:33:06 +04:00
|
|
|
break;
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-09-11 21:53:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
size_t contentLength =
|
|
|
|
((this->it - 1)->Content - startToken->Content) + (this->it - 1)->Length;
|
|
|
|
GeneratorExpressionContent* content =
|
|
|
|
new GeneratorExpressionContent(startToken->Content, contentLength);
|
2012-09-11 21:53:38 +04:00
|
|
|
content->SetIdentifier(identifier);
|
|
|
|
content->SetParameters(parameters);
|
|
|
|
result.push_back(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGeneratorExpressionParser::ParseContent(
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<cmGeneratorExpressionEvaluator*>& result)
|
2012-09-11 21:53:38 +04:00
|
|
|
{
|
2012-10-12 19:34:16 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (this->it->TokenType) {
|
|
|
|
case cmGeneratorExpressionToken::Text: {
|
|
|
|
if (this->NestingLevel == 0) {
|
|
|
|
if (!result.empty() &&
|
|
|
|
(*(result.end() - 1))->GetType() ==
|
|
|
|
cmGeneratorExpressionEvaluator::Text) {
|
2012-09-11 21:53:38 +04:00
|
|
|
// A comma in 'plain text' could have split text that should
|
|
|
|
// otherwise be continuous. Extend the last text content instead of
|
|
|
|
// creating a new one.
|
2016-05-16 17:34:04 +03:00
|
|
|
TextContent* textContent =
|
|
|
|
static_cast<TextContent*>(*(result.end() - 1));
|
2012-09-11 21:53:38 +04:00
|
|
|
textContent->Extend(this->it->Length);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
|
|
|
return;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
cmGeneratorExpressionEvaluator* n =
|
|
|
|
new TextContent(this->it->Content, this->it->Length);
|
2012-09-11 21:53:38 +04:00
|
|
|
result.push_back(n);
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
2016-05-16 17:34:04 +03:00
|
|
|
return;
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|
|
|
|
case cmGeneratorExpressionToken::BeginExpression:
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
|
|
|
this->ParseGeneratorExpression(result);
|
|
|
|
return;
|
|
|
|
case cmGeneratorExpressionToken::EndExpression:
|
|
|
|
case cmGeneratorExpressionToken::ColonSeparator:
|
|
|
|
case cmGeneratorExpressionToken::CommaSeparator:
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->NestingLevel == 0) {
|
2012-09-11 21:53:38 +04:00
|
|
|
extendText(result, this->it);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
assert(0 && "Got unexpected syntax token.");
|
|
|
|
}
|
2012-10-15 17:34:43 +04:00
|
|
|
assert(this->it != this->Tokens.end());
|
2012-09-11 21:53:38 +04:00
|
|
|
++this->it;
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
assert(0 && "Unhandled token in generator expression.");
|
2012-09-11 21:53:38 +04:00
|
|
|
}
|