tools/linguist/lupdate/qdeclarative.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    85     void accept(AST::Node *node)
    85     void accept(AST::Node *node)
    86     { AST::Node::acceptChild(node, this); }
    86     { AST::Node::acceptChild(node, this); }
    87 
    87 
    88     virtual void endVisit(AST::CallExpression *node)
    88     virtual void endVisit(AST::CallExpression *node)
    89     {
    89     {
       
    90         m_bSource.clear();
    90         if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(node->base)) {
    91         if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(node->base)) {
    91             if (idExpr->name->asString() == QLatin1String("qsTr") ||
    92             if (idExpr->name->asString() == QLatin1String("qsTr") ||
    92                 idExpr->name->asString() == QLatin1String("QT_TR_NOOP")) {
    93                 idExpr->name->asString() == QLatin1String("QT_TR_NOOP")) {
    93                 if (node->arguments && AST::cast<AST::StringLiteral *>(node->arguments->expression)) {
    94                 if (!node->arguments)
    94                     AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
    95                     return;
    95                     const QString source = literal->value->asString();
    96                 AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(node->arguments->expression);
       
    97                 if (binary) {
       
    98                     if (!createString(binary))
       
    99                         m_bSource.clear();
       
   100                 }
       
   101                 AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
       
   102                 if (literal || !m_bSource.isEmpty()) {
       
   103                     const QString source = literal ? literal->value->asString() : m_bSource;
    96 
   104 
    97                     QString comment;
   105                     QString comment;
    98                     bool plural = false;
   106                     bool plural = false;
    99                     AST::ArgumentList *commentNode = node->arguments->next;
   107                     AST::ArgumentList *commentNode = node->arguments->next;
   100                     if (commentNode) {
   108                     if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
   101                         literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
   109                         literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
   102                         comment = literal->value->asString();
   110                         comment = literal->value->asString();
   103 
   111 
   104                         AST::ArgumentList *nNode = commentNode->next;
   112                         AST::ArgumentList *nNode = commentNode->next;
   105                         if (nNode) {
   113                         if (nNode)
   106                             AST::NumericLiteral *numLiteral = AST::cast<AST::NumericLiteral *>(nNode->expression);
   114                             plural = true;
   107                             if (numLiteral) {
       
   108                                 plural = true;
       
   109                             }
       
   110                         }
       
   111                     }
   115                     }
   112 
   116 
   113                     TranslatorMessage msg(m_component, source,
   117                     TranslatorMessage msg(m_component, source,
   114                         comment, QString(), m_fileName,
   118                         comment, QString(), m_fileName,
   115                         node->firstSourceLocation().startLine, QStringList(),
   119                         node->firstSourceLocation().startLine, QStringList(),
   124 
   128 
   125                     QString source;
   129                     QString source;
   126                     QString comment;
   130                     QString comment;
   127                     bool plural = false;
   131                     bool plural = false;
   128                     AST::ArgumentList *sourceNode = node->arguments->next;
   132                     AST::ArgumentList *sourceNode = node->arguments->next;
   129                     if (sourceNode) {
   133                     if (!sourceNode)
   130                         literal = AST::cast<AST::StringLiteral *>(sourceNode->expression);
   134                         return;
   131                         source = literal->value->asString();
   135                     literal = AST::cast<AST::StringLiteral *>(sourceNode->expression);
   132                         AST::ArgumentList *commentNode = sourceNode->next;
   136                     AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(sourceNode->expression);
   133                         if (commentNode) {
   137                     if (binary) {
   134                             literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
   138                         if (!createString(binary))
   135                             comment = literal->value->asString();
   139                             m_bSource.clear();
   136 
   140                     }
   137                             AST::ArgumentList *nNode = commentNode->next;
   141                     if (!literal && m_bSource.isEmpty())
   138                             if (nNode) {
   142                         return;
   139                                 AST::NumericLiteral *numLiteral = AST::cast<AST::NumericLiteral *>(nNode->expression);
   143                     source = literal ? literal->value->asString() : m_bSource;
   140                                 if (numLiteral) {
   144                     AST::ArgumentList *commentNode = sourceNode->next;
   141                                     plural = true;
   145                     if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
   142                                 }
   146                         literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
   143                             }
   147                         comment = literal->value->asString();
   144                         }
   148 
       
   149                         AST::ArgumentList *nNode = commentNode->next;
       
   150                         if (nNode)
       
   151                             plural = true;
   145                     }
   152                     }
   146 
   153 
   147                     TranslatorMessage msg(context, source,
   154                     TranslatorMessage msg(context, source,
   148                         comment, QString(), m_fileName,
   155                         comment, QString(), m_fileName,
   149                         node->firstSourceLocation().startLine, QStringList(),
   156                         node->firstSourceLocation().startLine, QStringList(),
   154             }
   161             }
   155         }
   162         }
   156     }
   163     }
   157 
   164 
   158 private:
   165 private:
       
   166     bool createString(AST::BinaryExpression *b) {
       
   167         if (!b || b->op != 0)
       
   168             return false;
       
   169         AST::BinaryExpression *l = AST::cast<AST::BinaryExpression *>(b->left);
       
   170         AST::BinaryExpression *r = AST::cast<AST::BinaryExpression *>(b->right);
       
   171         AST::StringLiteral *ls = AST::cast<AST::StringLiteral *>(b->left);
       
   172         AST::StringLiteral *rs = AST::cast<AST::StringLiteral *>(b->right);
       
   173         if ((!l && !ls) || (!r && !rs))
       
   174             return false;
       
   175         if (l) {
       
   176             if (!createString(l))
       
   177                 return false;
       
   178         } else
       
   179             m_bSource.prepend(ls->value->asString());
       
   180 
       
   181         if (r) {
       
   182             if (!createString(r))
       
   183                 return false;
       
   184         } else
       
   185             m_bSource.append(rs->value->asString());
       
   186 
       
   187         return true;
       
   188     }
       
   189 
   159     Translator *m_translator;
   190     Translator *m_translator;
   160     QString m_fileName;
   191     QString m_fileName;
   161     QString m_component;
   192     QString m_component;
       
   193     QString m_bSource;
   162 };
   194 };
   163 
   195 
   164 QString createErrorString(const QString &filename, const QString &code, Parser &parser)
   196 QString createErrorString(const QString &filename, const QString &code, Parser &parser)
   165 {
   197 {
   166     // print out error
   198     // print out error