OSDN Git Service

Merge remote branch 'origin/2.1'
[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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33 #ifndef QMLDOCUMENT_H
34 #define QMLDOCUMENT_H
35
36 #include <QtCore/QList>
37 #include <QtCore/QMap>
38 #include <QtCore/QPair>
39 #include <QtCore/QSharedPointer>
40 #include <QtCore/QString>
41
42 #include <languageutils/fakemetaobject.h>
43
44 #include "parser/qmldirparser_p.h"
45 #include "parser/qmljsengine_p.h"
46 #include "qmljs_global.h"
47
48 QT_QML_BEGIN_NAMESPACE
49
50 namespace QmlJS {
51
52 class Bind;
53 class Snapshot;
54
55 class QMLJS_EXPORT Document
56 {
57 public:
58     typedef QSharedPointer<Document> Ptr;
59
60 protected:
61     Document(const QString &fileName);
62
63 public:
64     ~Document();
65
66     static Document::Ptr create(const QString &fileName);
67
68     bool isQmlDocument() const;
69     bool isJSDocument() const;
70
71     AST::UiProgram *qmlProgram() const;
72     AST::Program *jsProgram() const;
73     AST::ExpressionNode *expression() const;
74     AST::Node *ast() const;
75
76     const QmlJS::Engine *engine() const;
77
78     QList<DiagnosticMessage> diagnosticMessages() const;
79
80     QString source() const;
81     void setSource(const QString &source);
82
83     bool parse();
84     bool parseQml();
85     bool parseJavaScript();
86     bool parseExpression();
87
88     bool isParsedCorrectly() const
89     { return _parsedCorrectly; }
90
91     Bind *bind() const;
92
93     int editorRevision() const;
94     void setEditorRevision(int revision);
95
96     QString fileName() const;
97     QString path() const;
98     QString componentName() const;
99
100 private:
101     bool parse_helper(int kind);
102     static void extractPragmas(QString *source);
103
104 private:
105     QmlJS::Engine *_engine;
106     NodePool *_pool;
107     AST::Node *_ast;
108     Bind *_bind;
109     bool _isQmlDocument;
110     int _editorRevision;
111     bool _parsedCorrectly;
112     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
113     QString _fileName;
114     QString _path;
115     QString _componentName;
116     QString _source;
117
118     // for documentFromSource
119     friend class Snapshot;
120 };
121
122 class QMLJS_EXPORT LibraryInfo
123 {
124 public:
125     enum DumpStatus {
126         DumpNotStartedOrRunning,
127         DumpDone,
128         DumpError
129     };
130
131 private:
132     bool _valid;
133     QList<QmlDirParser::Component> _components;
134     QList<QmlDirParser::Plugin> _plugins;
135     typedef QList<LanguageUtils::FakeMetaObject::ConstPtr> FakeMetaObjectList;
136     FakeMetaObjectList _metaObjects;
137
138     DumpStatus _dumpStatus;
139     QString _dumpError;
140
141 public:
142     LibraryInfo();
143     LibraryInfo(const QmlDirParser &parser);
144     ~LibraryInfo();
145
146     QList<QmlDirParser::Component> components() const
147     { return _components; }
148
149     QList<QmlDirParser::Plugin> plugins() const
150     { return _plugins; }
151
152     FakeMetaObjectList metaObjects() const
153     { return _metaObjects; }
154
155     void setMetaObjects(const FakeMetaObjectList &objects)
156     { _metaObjects = objects; }
157
158     bool isValid() const
159     { return _valid; }
160
161     DumpStatus dumpStatus() const
162     { return _dumpStatus; }
163
164     QString dumpError() const
165     { return _dumpError; }
166
167     void setDumpStatus(DumpStatus dumped, const QString &error = QString())
168     { _dumpStatus = dumped; _dumpError = error; }
169 };
170
171 class QMLJS_EXPORT Snapshot
172 {
173     typedef QHash<QString, Document::Ptr> _Base;
174     QHash<QString, Document::Ptr> _documents;
175     QHash<QString, QList<Document::Ptr> > _documentsByPath;
176     QHash<QString, LibraryInfo> _libraries;
177
178 public:
179     Snapshot();
180     ~Snapshot();
181
182     typedef _Base::iterator iterator;
183     typedef _Base::const_iterator const_iterator;
184
185     const_iterator begin() const { return _documents.begin(); }
186     const_iterator end() const { return _documents.end(); }
187
188     void insert(const Document::Ptr &document);
189     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
190     void remove(const QString &fileName);
191
192     Document::Ptr document(const QString &fileName) const;
193     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
194     LibraryInfo libraryInfo(const QString &path) const;
195
196     Document::Ptr documentFromSource(const QString &code,
197                                      const QString &fileName) const;
198 };
199
200 } // namespace QmlJS
201
202 QT_QML_END_NAMESPACE
203
204 #endif // QMLDOCUMENT_H