OSDN Git Service

QmlJS: Improve tests/tools/qml-ast2dot.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljsdocument.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32 #ifndef QMLDOCUMENT_H
33 #define QMLDOCUMENT_H
34
35 #include <QtCore/QList>
36 #include <QtCore/QPair>
37 #include <QtCore/QSharedPointer>
38 #include <QtCore/QString>
39
40 #include <languageutils/fakemetaobject.h>
41
42 #include "parser/qmldirparser_p.h"
43 #include "parser/qmljsengine_p.h"
44 #include "qmljs_global.h"
45
46 QT_QML_BEGIN_NAMESPACE
47
48 namespace QmlJS {
49
50 class Bind;
51 class Snapshot;
52
53 class QMLJS_EXPORT Document
54 {
55 public:
56     typedef QSharedPointer<Document> Ptr;
57
58     // used in a 3-bit bitfield
59     enum Language
60     {
61         QmlLanguage = 0,
62         JavaScriptLanguage = 1,
63         JsonLanguage = 2,
64         UnknownLanguage = 3
65     };
66
67 protected:
68     Document(const QString &fileName, Language language);
69
70 public:
71     ~Document();
72
73     static Document::Ptr create(const QString &fileName, Language language);
74     static Language guessLanguageFromSuffix(const QString &fileName);
75
76     Document::Ptr ptr() const;
77
78     bool isQmlDocument() const;
79     Language language() const;
80
81     AST::UiProgram *qmlProgram() const;
82     AST::Program *jsProgram() const;
83     AST::ExpressionNode *expression() const;
84     AST::Node *ast() const;
85
86     const QmlJS::Engine *engine() const;
87
88     QList<DiagnosticMessage> diagnosticMessages() const;
89
90     QString source() const;
91     void setSource(const QString &source);
92
93     bool parse();
94     bool parseQml();
95     bool parseJavaScript();
96     bool parseExpression();
97
98     bool isParsedCorrectly() const
99     { return _parsedCorrectly; }
100
101     Bind *bind() const;
102
103     int editorRevision() const;
104     void setEditorRevision(int revision);
105
106     QString fileName() const;
107     QString path() const;
108     QString componentName() const;
109
110 private:
111     bool parse_helper(int kind);
112
113 private:
114     QmlJS::Engine *_engine;
115     AST::Node *_ast;
116     Bind *_bind;
117     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
118     QString _fileName;
119     QString _path;
120     QString _componentName;
121     QString _source;
122     QWeakPointer<Document> _ptr;
123     int _editorRevision;
124     Language _language : 3;
125     bool _parsedCorrectly : 1;
126
127     // for documentFromSource
128     friend class Snapshot;
129 };
130
131 class QMLJS_EXPORT LibraryInfo
132 {
133 public:
134     enum PluginTypeInfoStatus {
135         NoTypeInfo,
136         DumpDone,
137         DumpError,
138         TypeInfoFileDone,
139         TypeInfoFileError
140     };
141
142     enum Status {
143         NotScanned,
144         NotFound,
145         Found
146     };
147
148 private:
149     Status _status;
150     QList<QmlDirParser::Component> _components;
151     QList<QmlDirParser::Plugin> _plugins;
152     QList<QmlDirParser::TypeInfo> _typeinfos;
153     typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
154     FakeMetaObjectList _metaObjects;
155
156     PluginTypeInfoStatus _dumpStatus;
157     QString _dumpError;
158
159 public:
160     explicit LibraryInfo(Status status = NotScanned);
161     explicit LibraryInfo(const QmlDirParser &parser);
162     ~LibraryInfo();
163
164     QList<QmlDirParser::Component> components() const
165     { return _components; }
166
167     QList<QmlDirParser::Plugin> plugins() const
168     { return _plugins; }
169
170     QList<QmlDirParser::TypeInfo> typeInfos() const
171     { return _typeinfos; }
172
173     FakeMetaObjectList metaObjects() const
174     { return _metaObjects; }
175
176     void setMetaObjects(const FakeMetaObjectList &objects)
177     { _metaObjects = objects; }
178
179     bool isValid() const
180     { return _status == Found; }
181
182     bool wasScanned() const
183     { return _status != NotScanned; }
184
185     PluginTypeInfoStatus pluginTypeInfoStatus() const
186     { return _dumpStatus; }
187
188     QString pluginTypeInfoError() const
189     { return _dumpError; }
190
191     void setPluginTypeInfoStatus(PluginTypeInfoStatus dumped, const QString &error = QString())
192     { _dumpStatus = dumped; _dumpError = error; }
193 };
194
195 class QMLJS_EXPORT Snapshot
196 {
197     typedef QHash<QString, Document::Ptr> _Base;
198     QHash<QString, Document::Ptr> _documents;
199     QHash<QString, QList<Document::Ptr> > _documentsByPath;
200     QHash<QString, LibraryInfo> _libraries;
201
202 public:
203     Snapshot();
204     ~Snapshot();
205
206     typedef _Base::iterator iterator;
207     typedef _Base::const_iterator const_iterator;
208
209     const_iterator begin() const { return _documents.begin(); }
210     const_iterator end() const { return _documents.end(); }
211
212     void insert(const Document::Ptr &document, bool allowInvalid = false);
213     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
214     void remove(const QString &fileName);
215
216     Document::Ptr document(const QString &fileName) const;
217     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
218     LibraryInfo libraryInfo(const QString &path) const;
219
220     Document::Ptr documentFromSource(const QString &code,
221                                      const QString &fileName,
222                                      Document::Language language) const;
223 };
224
225 } // namespace QmlJS
226
227 QT_QML_END_NAMESPACE
228
229 #endif // QMLDOCUMENT_H