示例#1
0
bool processComment(const QChar *chars, int length, Comment &comment)
{
    // Try to match the logic of the QtScript parser.
    if (!length)
        return comment.isValid();
    if (*chars == QLatin1Char(':') && chars[1].isSpace()) {
        comment.extracomment += QString(chars+1, length-1);
    } else if (*chars == QLatin1Char('=') && chars[1].isSpace()) {
        comment.msgid = QString(chars+2, length-2).simplified();
    } else if (*chars == QLatin1Char('~') && chars[1].isSpace()) {
        QString text = QString(chars+2, length-2).trimmed();
        int k = text.indexOf(QLatin1Char(' '));
        if (k > -1)
            comment.extra.insert(text.left(k), text.mid(k + 1).trimmed());
    } else if (*chars == QLatin1Char('%') && chars[1].isSpace()) {
        comment.sourcetext.reserve(comment.sourcetext.length() + length-2);
        ushort *ptr = (ushort *)comment.sourcetext.data() + comment.sourcetext.length();
        int p = 2, c;
        forever {
            if (p >= length)
                break;
            c = chars[p++].unicode();
            if (isspace(c))
                continue;
            if (c != '"')
                break;
            forever {
                if (p >= length)
                    break;
                c = chars[p++].unicode();
                if (c == '"')
                    break;
                if (c == '\\') {
                    if (p >= length)
                        break;
                    c = chars[p++].unicode();
                    if (c == '\n')
                        break;
                    *ptr++ = '\\';
                }
                *ptr++ = c;
            }
        }
        comment.sourcetext.resize(ptr - (ushort *)comment.sourcetext.data());
    }
示例#2
0
    virtual void endVisit(AST::CallExpression *node)
    {
        m_bSource.clear();
        if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(node->base)) {
            if (idExpr->name->asString() == QLatin1String("qsTr") ||
                idExpr->name->asString() == QLatin1String("QT_TR_NOOP")) {
                if (!node->arguments)
                    return;
                AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(node->arguments->expression);
                if (binary) {
                    if (!createString(binary))
                        m_bSource.clear();
                }
                AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
                if (literal || !m_bSource.isEmpty()) {
                    const QString source = literal ? literal->value->asString() : m_bSource;

                    QString comment;
                    bool plural = false;
                    AST::ArgumentList *commentNode = node->arguments->next;
                    if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
                        literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
                        comment = literal->value->asString();

                        AST::ArgumentList *nNode = commentNode->next;
                        if (nNode)
                            plural = true;
                    }

                    QString id;
                    QString extracomment;
                    TranslatorMessage::ExtraData extra;
                    Comment scomment = findComment(node->firstSourceLocation().startLine);
                    if (scomment.isValid()) {
                        extracomment = scomment.extracomment;
                        extra = scomment.extra;
                        id = scomment.msgid;
                    }

                    TranslatorMessage msg(m_component, source,
                        comment, QString(), m_fileName,
                        node->firstSourceLocation().startLine, QStringList(),
                        TranslatorMessage::Unfinished, plural);
                    msg.setExtraComment(extracomment.simplified());
                    msg.setId(id);
                    msg.setExtras(extra);
                    m_translator->extend(msg);
                }
            } else if (idExpr->name->asString() == QLatin1String("qsTranslate") ||
                       idExpr->name->asString() == QLatin1String("QT_TRANSLATE_NOOP")) {
                if (node->arguments && AST::cast<AST::StringLiteral *>(node->arguments->expression)) {
                    AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
                    const QString context = literal->value->asString();

                    QString source;
                    QString comment;
                    bool plural = false;
                    AST::ArgumentList *sourceNode = node->arguments->next;
                    if (!sourceNode)
                        return;
                    literal = AST::cast<AST::StringLiteral *>(sourceNode->expression);
                    AST::BinaryExpression *binary = AST::cast<AST::BinaryExpression *>(sourceNode->expression);
                    if (binary) {
                        if (!createString(binary))
                            m_bSource.clear();
                    }
                    if (!literal && m_bSource.isEmpty())
                        return;

                    QString id;
                    QString extracomment;
                    TranslatorMessage::ExtraData extra;
                    Comment scomment = findComment(node->firstSourceLocation().startLine);
                    if (scomment.isValid()) {
                        extracomment = scomment.extracomment;
                        extra = scomment.extra;
                        id = scomment.msgid;
                    }

                    source = literal ? literal->value->asString() : m_bSource;
                    AST::ArgumentList *commentNode = sourceNode->next;
                    if (commentNode && AST::cast<AST::StringLiteral *>(commentNode->expression)) {
                        literal = AST::cast<AST::StringLiteral *>(commentNode->expression);
                        comment = literal->value->asString();

                        AST::ArgumentList *nNode = commentNode->next;
                        if (nNode)
                            plural = true;
                    }

                    TranslatorMessage msg(context, source,
                        comment, QString(), m_fileName,
                        node->firstSourceLocation().startLine, QStringList(),
                        TranslatorMessage::Unfinished, plural);
                    msg.setExtraComment(extracomment.simplified());
                    msg.setId(id);
                    msg.setExtras(extra);
                    m_translator->extend(msg);
                }
            } else if (idExpr->name->asString() == QLatin1String("qsTrId") ||
                       idExpr->name->asString() == QLatin1String("QT_TRID_NOOP")) {
                if (!node->arguments)
                    return;

                AST::StringLiteral *literal = AST::cast<AST::StringLiteral *>(node->arguments->expression);
                if (literal) {

                    QString extracomment;
                    QString sourcetext;
                    TranslatorMessage::ExtraData extra;
                    Comment comment = findComment(node->firstSourceLocation().startLine);
                    if (comment.isValid()) {
                        extracomment = comment.extracomment;
                        sourcetext = comment.sourcetext;
                        extra = comment.extra;
                    }

                    const QString id = literal->value->asString();
                    bool plural = node->arguments->next;

                    TranslatorMessage msg(QString(), sourcetext,
                        QString(), QString(), m_fileName,
                        node->firstSourceLocation().startLine, QStringList(),
                        TranslatorMessage::Unfinished, plural);
                    msg.setExtraComment(extracomment.simplified());
                    msg.setId(id);
                    msg.setExtras(extra);
                    m_translator->extend(msg);
                }
            }
        }
    }