aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qmljs/qmljssimplereader.cpp
blob: d4b8a21bf7c52c3b171fa656541ef0fbd1484bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljssimplereader.h"

#ifdef QT_CREATOR
#include "parser/qmljsengine_p.h"
#include "parser/qmljslexer_p.h"
#include "parser/qmljsparser_p.h"
#else
#include "parser/qqmljsengine_p.h"
#include "parser/qqmljslexer_p.h"
#include "qqmljsparser_p.h"
#endif

#include "qmljstr.h"
#include "qmljsutils.h"

#include <QFile>
#include <QLoggingCategory>
#include <QVariant>

static Q_LOGGING_CATEGORY(simpleReaderLog, "qtc.qmljs.simpleReader", QtWarningMsg)

    namespace QmlJS
{
#ifdef QT_CREATOR
    using UiQualifiedId = QmlJS::AST::UiQualifiedId;
#else
    using UiQualifiedId = QQmlJS::AST::UiQualifiedId;
#endif

    static SourceLocation toSourceLocation(SourceLocation first, SourceLocation last)
    {
        first.length = last.end() - first.begin();
        return first;
    }

    static SourceLocation toSourceLocation(UiQualifiedId * qualifiedId)
    {
        SourceLocation first = qualifiedId->firstSourceLocation();
        SourceLocation last;
        for (UiQualifiedId *iter = qualifiedId; iter; iter = iter->next) {
            if (iter->lastSourceLocation().isValid())
                last = iter->lastSourceLocation();
        }
        return toSourceLocation(first, last);
    }

    SimpleReaderNode::Property SimpleReaderNode::property(const QString &name) const
    {
        return m_properties.value(name);
    }

    QStringList SimpleReaderNode::propertyNames() const { return m_properties.keys(); }

    SimpleReaderNode::PropertyHash SimpleReaderNode::properties() const { return m_properties; }

    bool SimpleReaderNode::isRoot() const { return m_parentNode.isNull(); }

    bool SimpleReaderNode::isValid() const { return !m_name.isEmpty(); }

    SimpleReaderNode::Ptr SimpleReaderNode::invalidNode() { return Ptr(new SimpleReaderNode); }

    SimpleReaderNode::WeakPtr SimpleReaderNode::parent() const { return m_parentNode; }

    QString SimpleReaderNode::name() const { return m_name; }

    SourceLocation SimpleReaderNode::nameLocation() const { return m_nameLocation; }

    SimpleReaderNode::SimpleReaderNode() {}

    SimpleReaderNode::SimpleReaderNode(const QString &name,
                                       const SourceLocation &nameLocation,
                                       WeakPtr parent)
        : m_name(name)
        , m_nameLocation(nameLocation)
        , m_parentNode(parent)
    {}

    SimpleReaderNode::Ptr SimpleReaderNode::create(const QString &name,
                                                   const SourceLocation &nameLocation,
                                                   WeakPtr parent)
    {
        Ptr newNode(new SimpleReaderNode(name, nameLocation, parent));
        newNode->m_weakThis = newNode;
        if (parent)
            parent.toStrongRef().data()->m_children.append(newNode);
        return newNode;
    }

    const SimpleReaderNode::List &SimpleReaderNode::children() const
    {
        return m_children;
    }

    void SimpleReaderNode::setProperty(const QString &name,
                                       const SourceLocation &nameLocation,
                                       const QVariant &value,
                                       const SourceLocation &valueLocation)
    {
        m_properties.insert(name, {value, nameLocation, valueLocation});
    }

    SimpleAbstractStreamReader::SimpleAbstractStreamReader() {}

    SimpleAbstractStreamReader::~SimpleAbstractStreamReader() {}

    bool SimpleAbstractStreamReader::readFile(const QString &fileName)
    {
        QFile file(fileName);
        if (file.open(QIODevice::ReadOnly)) {
            QByteArray source = file.readAll();
            file.close();
            return readFromSource(QString::fromLocal8Bit(source));
        }
        addError(Tr::tr("Cannot find file %1.").arg(fileName));
        return false;
    }

    bool SimpleAbstractStreamReader::readFromSource(const QString &source)
    {
        m_errors.clear();
        m_currentSourceLocation = SourceLocation();

        m_source = source;

        Engine engine;
        Lexer lexer(&engine);
        Parser parser(&engine);

        lexer.setCode(source, /*line = */ 1, /*qmlMode = */ true);

        if (!parser.parse()) {
            QString errorMessage = QString::fromLatin1("%1:%2: %3")
                                       .arg(QString::number(parser.errorLineNumber()),
                                            QString::number(parser.errorColumnNumber()),
                                            parser.errorMessage());
            addError(errorMessage);
            return false;
        }
        return readDocument(parser.ast());
    }

    QStringList SimpleAbstractStreamReader::errors() const { return m_errors; }

    void SimpleAbstractStreamReader::addError(const QString &error,
                                              const SourceLocation &sourceLocation)
    {
        m_errors << QString::fromLatin1("%1:%2: %3\n")
                        .arg(QString::number(sourceLocation.startLine),
                             QString::number(sourceLocation.startColumn),
                             error);
    }

    SourceLocation SimpleAbstractStreamReader::currentSourceLocation() const
    {
        return m_currentSourceLocation;
    }

    bool SimpleAbstractStreamReader::readDocument(AST::UiProgram * ast)
    {
        if (!ast) {
            addError(Tr::tr("Could not parse document."));
            return false;
        }

        AST::UiObjectDefinition *uiObjectDefinition = AST::cast<AST::UiObjectDefinition *>(
            ast->members->member);
        if (!uiObjectDefinition) {
            addError(Tr::tr("Expected document to contain a single object definition."));
            return false;
        }
        readChild(uiObjectDefinition);

        m_source.clear();

        return errors().isEmpty();
    }

    void SimpleAbstractStreamReader::readChildren(AST::UiObjectDefinition * uiObjectDefinition)
    {
        Q_ASSERT(uiObjectDefinition);

        for (AST::UiObjectMemberList *it = uiObjectDefinition->initializer->members; it;
             it = it->next) {
            AST::UiObjectMember *member = it->member;
            AST::UiObjectDefinition *uiObjectDefinition = AST::cast<AST::UiObjectDefinition *>(
                member);
            if (uiObjectDefinition)
                readChild(uiObjectDefinition);
        }
    }

    void SimpleAbstractStreamReader::readChild(AST::UiObjectDefinition * uiObjectDefinition)
    {
        Q_ASSERT(uiObjectDefinition);

        setSourceLocation(uiObjectDefinition->firstSourceLocation());

        elementStart(toString(uiObjectDefinition->qualifiedTypeNameId),
                     toSourceLocation(uiObjectDefinition->qualifiedTypeNameId));

        readProperties(uiObjectDefinition);
        readChildren(uiObjectDefinition);

        elementEnd();
    }

    void SimpleAbstractStreamReader::readProperties(AST::UiObjectDefinition * uiObjectDefinition)
    {
        Q_ASSERT(uiObjectDefinition);

        for (AST::UiObjectMemberList *it = uiObjectDefinition->initializer->members; it;
             it = it->next) {
            AST::UiObjectMember *member = it->member;
            AST::UiScriptBinding *scriptBinding = AST::cast<AST::UiScriptBinding *>(member);
            if (scriptBinding)
                readProperty(scriptBinding);
        }
    }

    void SimpleAbstractStreamReader::readProperty(AST::UiScriptBinding * uiScriptBinding)
    {
        Q_ASSERT(uiScriptBinding);

        setSourceLocation(uiScriptBinding->firstSourceLocation());

        const QString name = toString(uiScriptBinding->qualifiedId);
        auto nameLoc = toSourceLocation(uiScriptBinding->qualifiedId);
        auto value = parsePropertyScriptBinding(uiScriptBinding);

        propertyDefinition(name, nameLoc, value.first, value.second);
    }

    std::pair<QVariant, SourceLocation> SimpleAbstractStreamReader::parsePropertyScriptBinding(
        AST::UiScriptBinding * uiScriptBinding)
    {
        Q_ASSERT(uiScriptBinding);

        AST::ExpressionStatement *expStmt = AST::cast<AST::ExpressionStatement *>(
            uiScriptBinding->statement);
        if (!expStmt) {
            addError(Tr::tr("Expected expression statement after colon."),
                     uiScriptBinding->statement->firstSourceLocation());
            return std::make_pair(QVariant(), SourceLocation());
        }

        return std::make_pair(parsePropertyExpression(expStmt->expression),
                              toSourceLocation(expStmt->firstSourceLocation(),
                                               expStmt->lastSourceLocation()));
    }

    QVariant SimpleAbstractStreamReader::parsePropertyExpression(AST::ExpressionNode
                                                                 * expressionNode)
    {
        Q_ASSERT(expressionNode);

        AST::ArrayPattern *arrayLiteral = AST::cast<AST::ArrayPattern *>(expressionNode);

        if (arrayLiteral) {
            QList<QVariant> variantList;
            for (AST::PatternElementList *it = arrayLiteral->elements; it; it = it->next)
                variantList << parsePropertyExpression(it->element->initializer);
            return variantList;
        }

        AST::StringLiteral *stringLiteral = AST::cast<AST::StringLiteral *>(expressionNode);
        if (stringLiteral)
            return stringLiteral->value.toString();

        AST::TrueLiteral *trueLiteral = AST::cast<AST::TrueLiteral *>(expressionNode);
        if (trueLiteral)
            return true;

        AST::FalseLiteral *falseLiteral = AST::cast<AST::FalseLiteral *>(expressionNode);
        if (falseLiteral)
            return false;

        AST::NumericLiteral *numericLiteral = AST::cast<AST::NumericLiteral *>(expressionNode);
        if (numericLiteral)
            return numericLiteral->value;

        return textAt(expressionNode->firstSourceLocation(), expressionNode->lastSourceLocation());
    }

    void SimpleAbstractStreamReader::setSourceLocation(const SourceLocation &sourceLocation)
    {
        m_currentSourceLocation = sourceLocation;
    }

    QString SimpleAbstractStreamReader::textAt(const SourceLocation &from, const SourceLocation &to)
    {
        return m_source.mid(from.offset, to.end() - from.begin());
    }

    SimpleReader::SimpleReader() {}

    SimpleReaderNode::Ptr SimpleReader::readFile(const QString &fileName)
    {
        SimpleAbstractStreamReader::readFile(fileName);
        return m_rootNode;
    }

    SimpleReaderNode::Ptr SimpleReader::readFromSource(const QString &source)
    {
        SimpleAbstractStreamReader::readFromSource(source);
        return m_rootNode;
    }

    void SimpleReader::elementStart(const QString &name, const SourceLocation &nameLocation)
    {
        qCDebug(simpleReaderLog) << "elementStart()" << name;

        SimpleReaderNode::Ptr newNode = SimpleReaderNode::create(name, nameLocation, m_currentNode);

        if (newNode->isRoot())
            m_rootNode = newNode;

        Q_ASSERT(newNode->isValid());

        m_currentNode = newNode;
    }

    void SimpleReader::elementEnd()
    {
        Q_ASSERT(m_currentNode);

        qCDebug(simpleReaderLog) << "elementEnd()" << m_currentNode.toStrongRef().data()->name();

        m_currentNode = m_currentNode.toStrongRef().data()->parent();
    }

    void SimpleReader::propertyDefinition(const QString &name,
                                          const SourceLocation &nameLocation,
                                          const QVariant &value,
                                          const SourceLocation &valueLocation)
    {
        Q_ASSERT(m_currentNode);

        qCDebug(simpleReaderLog) << "propertyDefinition()"
                                 << m_currentNode.toStrongRef().data()->name() << name << value;

        if (m_currentNode.toStrongRef().data()->propertyNames().contains(name)) {
            auto previousSourceLoc = m_currentNode.toStrongRef().data()->property(name).nameLocation;
            addError(Tr::tr("Property is defined twice, previous definition at %1:%2")
                         .arg(QString::number(previousSourceLoc.startLine),
                              QString::number(previousSourceLoc.startColumn)),
                     currentSourceLocation());
        }

        m_currentNode.toStrongRef().data()->setProperty(name, nameLocation, value, valueLocation);
    }

} // namespace QmlJS