OSDN Git Service

e5e26e608dd33df6aa6329f0cf2ab4a4c2ba5989
[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
75     Document::Ptr ptr() const;
76
77     bool isQmlDocument() const;
78     Language language() const;
79
80     AST::UiProgram *qmlProgram() const;
81     AST::Program *jsProgram() const;
82     AST::ExpressionNode *expression() const;
83     AST::Node *ast() const;
84
85     const QmlJS::Engine *engine() const;
86
87     QList<DiagnosticMessage> diagnosticMessages() const;
88
89     QString source() const;
90     void setSource(const QString &source);
91
92     bool parse();
93     bool parseQml();
94     bool parseJavaScript();
95     bool parseExpression();
96
97     bool isParsedCorrectly() const
98     { return _parsedCorrectly; }
99
100     Bind *bind() const;
101
102     int editorRevision() const;
103     void setEditorRevision(int revision);
104
105     QString fileName() const;
106     QString path() const;
107     QString componentName() const;
108
109 private:
110     bool parse_helper(int kind);
111
112 private:
113     QmlJS::Engine *_engine;
114     AST::Node *_ast;
115     Bind *_bind;
116     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
117     QString _fileName;
118     QString _path;
119     QString _componentName;
120     QString _source;
121     QWeakPointer<Document> _ptr;
122     int _editorRevision;
123     Language _language : 3;
124     bool _parsedCorrectly : 1;
125
126     // for documentFromSource
127     friend class Snapshot;
128 };
129
130 class QMLJS_EXPORT LibraryInfo
131 {
132 public:
133     enum PluginTypeInfoStatus {
134         NoTypeInfo,
135         DumpDone,
136         DumpError,
137         TypeInfoFileDone,
138         TypeInfoFileError
139     };
140
141     enum Status {
142         NotScanned,
143         NotFound,
144         Found
145     };
146
147 private:
148     Status _status;
149     QList<QmlDirParser::Component> _components;
150     QList<QmlDirParser::Plugin> _plugins;
151     QList<QmlDirParser::TypeInfo> _typeinfos;
152     typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
153     FakeMetaObjectList _metaObjects;
154
155     PluginTypeInfoStatus _dumpStatus;
156     QString _dumpError;
157
158 public:
159     explicit LibraryInfo(Status status = NotScanned);
160     explicit LibraryInfo(const QmlDirParser &parser);
161     ~LibraryInfo();
162
163     QList<QmlDirParser::Component> components() const
164     { return _components; }
165
166     QList<QmlDirParser::Plugin> plugins() const
167     { return _plugins; }
168
169     QList<QmlDirParser::TypeInfo> typeInfos() const
170     { return _typeinfos; }
171
172     FakeMetaObjectList metaObjects() const
173     { return _metaObjects; }
174
175     void setMetaObjects(const FakeMetaObjectList &objects)
176     { _metaObjects = objects; }
177
178     bool isValid() const
179     { return _status == Found; }
180
181     bool wasScanned() const
182     { return _status != NotScanned; }
183
184     PluginTypeInfoStatus pluginTypeInfoStatus() const
185     { return _dumpStatus; }
186
187     QString pluginTypeInfoError() const
188     { return _dumpError; }
189
190     void setPluginTypeInfoStatus(PluginTypeInfoStatus dumped, const QString &error = QString())
191     { _dumpStatus = dumped; _dumpError = error; }
192 };
193
194 class QMLJS_EXPORT Snapshot
195 {
196     typedef QHash<QString, Document::Ptr> _Base;
197     QHash<QString, Document::Ptr> _documents;
198     QHash<QString, QList<Document::Ptr> > _documentsByPath;
199     QHash<QString, LibraryInfo> _libraries;
200
201 public:
202     Snapshot();
203     ~Snapshot();
204
205     typedef _Base::iterator iterator;
206     typedef _Base::const_iterator const_iterator;
207
208     const_iterator begin() const { return _documents.begin(); }
209     const_iterator end() const { return _documents.end(); }
210
211     void insert(const Document::Ptr &document, bool allowInvalid = false);
212     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
213     void remove(const QString &fileName);
214
215     Document::Ptr document(const QString &fileName) const;
216     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
217     LibraryInfo libraryInfo(const QString &path) const;
218
219     Document::Ptr documentFromSource(const QString &code,
220                                      const QString &fileName,
221                                      Document::Language language) const;
222 };
223
224 } // namespace QmlJS
225
226 QT_QML_END_NAMESPACE
227
228 #endif // QMLDOCUMENT_H