qmljsdocument.cpp 5.9 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

30
#include "qmljsdocument.h"
31
#include "qmljsbind.h"
32
33
34
35
36
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/parser/qmljslexer_p.h>
#include <qmljs/parser/qmljsparser_p.h>
#include <qmljs/parser/qmljsnodepool_p.h>
#include <qmljs/parser/qmljsastfwd_p.h>
37
#include <QtCore/QDir>
38
39
40
41

using namespace QmlJS;
using namespace QmlJS::AST;

42
Document::Document(const QString &fileName)
43
44
    : _engine(0)
    , _pool(0)
45
    , _ast(0)
Roberto Raggi's avatar
Roberto Raggi committed
46
    , _bind(0)
47
    , _documentRevision(0)
48
    , _parsedCorrectly(false)
49
    , _fileName(fileName)
50
{
51
52
    QFileInfo fileInfo(fileName);
    _path = fileInfo.absolutePath();
53
54
55
56
57
58
59
60
61
62

    if (fileInfo.suffix() == QLatin1String("qml")) {
        _componentName = fileInfo.baseName();

        if (! _componentName.isEmpty()) {
            // ### TODO: check the component name.

            if (! _componentName.at(0).isUpper())
                _componentName.clear();
        }
63
    }
64
65
}

66
Document::~Document()
67
{
Roberto Raggi's avatar
Roberto Raggi committed
68
69
70
    if (_bind)
        delete _bind;

71
72
73
74
75
76
77
    if (_engine)
        delete _engine;

    if (_pool)
        delete _pool;
}

78
Document::Ptr Document::create(const QString &fileName)
79
{
80
    Document::Ptr doc(new Document(fileName));
81
82
83
    return doc;
}

84
AST::UiProgram *Document::qmlProgram() const
85
{
86
    return cast<UiProgram *>(_ast);
87
88
}

89
AST::Program *Document::jsProgram() const
90
{
91
    return cast<Program *>(_ast);
92
93
}

94
95
96
97
98
99
100
101
AST::ExpressionNode *Document::expression() const
{
    if (_ast)
        return _ast->expressionCast();

    return 0;
}

102
103
AST::Node *Document::ast() const
{
104
    return _ast;
105
106
}

107
QList<DiagnosticMessage> Document::diagnosticMessages() const
108
109
110
111
{
    return _diagnosticMessages;
}

112
QString Document::source() const
113
114
115
116
{
    return _source;
}

117
void Document::setSource(const QString &source)
118
119
120
121
{
    _source = source;
}

122
123
124
125
126
127
128
129
130
131
int Document::documentRevision() const
{
    return _documentRevision;
}

void Document::setDocumentRevision(int revision)
{
    _documentRevision = revision;
}

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
QString Document::fileName() const
{
    return _fileName;

}

QString Document::path() const
{
    return _path;
}

QString Document::componentName() const
{
    return _componentName;
}

148
bool Document::parse_helper(int startToken)
149
150
151
{
    Q_ASSERT(! _engine);
    Q_ASSERT(! _pool);
152
    Q_ASSERT(! _ast);
Roberto Raggi's avatar
Roberto Raggi committed
153
    Q_ASSERT(! _bind);
154
155
156
157
158
159
160
161
162

    _engine = new Engine();
    _pool = new NodePool(_fileName, _engine);

    Lexer lexer(_engine);
    Parser parser(_engine);

    lexer.setCode(_source, /*line = */ 1);

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    switch (startToken) {
    case QmlJSGrammar::T_FEED_UI_PROGRAM:
        _parsedCorrectly = parser.parse();
        break;
    case QmlJSGrammar::T_FEED_JS_PROGRAM:
        _parsedCorrectly = parser.parseProgram();
        break;
    case QmlJSGrammar::T_FEED_JS_EXPRESSION:
        _parsedCorrectly = parser.parseExpression();
        break;
    default:
        Q_ASSERT(0);
    }

    _ast = parser.rootNode();
178
179
    _diagnosticMessages = parser.diagnosticMessages();

Roberto Raggi's avatar
Roberto Raggi committed
180
    _bind = new Bind(this);
181

182
183
184
    return _parsedCorrectly;
}

185
bool Document::parseQml()
186
{
187
188
    return parse_helper(QmlJSGrammar::T_FEED_UI_PROGRAM);
}
189

190
191
192
bool Document::parseJavaScript()
{
    return parse_helper(QmlJSGrammar::T_FEED_JS_PROGRAM);
193
194
195
196
}

bool Document::parseExpression()
{
197
    return parse_helper(QmlJSGrammar::T_FEED_JS_EXPRESSION);
198
199
}

Roberto Raggi's avatar
Roberto Raggi committed
200
Bind *Document::bind() const
201
202
203
204
{
    return _bind;
}

205
206
207
208
209
210
211
212
Snapshot::Snapshot()
{
}

Snapshot::~Snapshot()
{
}

213
void Snapshot::insert(const Document::Ptr &document)
214
215
{
    if (document && (document->qmlProgram() || document->jsProgram()))
216
        _documents.insert(document->fileName(), document);
217
218
}

Roberto Raggi's avatar
Roberto Raggi committed
219
QList<Document::Ptr> Snapshot::importedDocuments(const Document::Ptr &doc, const QString &importPath) const
220
{
221
222
    // ### TODO: maybe we should add all imported documents in the parse Document::parse() method, regardless of whether they're in the path or not.

Roberto Raggi's avatar
Roberto Raggi committed
223
    QList<Document::Ptr> result;
224

225
226
227
228
    QString docPath = doc->path();
    docPath += QLatin1Char('/');
    docPath += importPath;
    docPath = QDir::cleanPath(docPath);
229

230
    foreach (Document::Ptr candidate, _documents) {
231
        if (candidate == doc)
232
233
234
            continue; // ignore this document
        else if (! candidate->qmlProgram())
            continue; // skip JS documents
235
236
237
238
239
240
241
242

        if (candidate->path() == doc->path() || candidate->path() == docPath)
            result.append(candidate);
    }

    return result;
}

243
QMap<QString, Document::Ptr> Snapshot::componentsDefinedByImportedDocuments(const Document::Ptr &doc, const QString &importPath) const
244
{
245
    QMap<QString, Document::Ptr> result;
246
247
248

    const QString docPath = doc->path() + '/' + importPath;

249
    foreach (Document::Ptr candidate, *this) {
250
251
252
253
254
255
256
257
258
        if (candidate == doc)
            continue;

        if (candidate->path() == doc->path() || candidate->path() == docPath)
            result.insert(candidate->componentName(), candidate);
    }

    return result;
}