OSDN Git Service

Merge branch '2.0'
[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) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29 #ifndef QMLDOCUMENT_H
30 #define QMLDOCUMENT_H
31
32 #include <QtCore/QList>
33 #include <QtCore/QMap>
34 #include <QtCore/QPair>
35 #include <QtCore/QSharedPointer>
36 #include <QtCore/QString>
37
38 #include "parser/qmldirparser_p.h"
39 #include "parser/qmljsengine_p.h"
40 #include "qmljs_global.h"
41
42 namespace QmlJS {
43
44 class Bind;
45 class Snapshot;
46
47 namespace Interpreter {
48     class FakeMetaObject;
49 }
50
51 class QMLJS_EXPORT Document
52 {
53 public:
54     typedef QSharedPointer<Document> Ptr;
55
56 protected:
57     Document(const QString &fileName);
58
59 public:
60     ~Document();
61
62     static Document::Ptr create(const QString &fileName);
63
64     bool isQmlDocument() const;
65     bool isJSDocument() const;
66
67     AST::UiProgram *qmlProgram() const;
68     AST::Program *jsProgram() const;
69     AST::ExpressionNode *expression() const;
70     AST::Node *ast() const;
71
72     QList<DiagnosticMessage> diagnosticMessages() const;
73
74     QString source() const;
75     void setSource(const QString &source);
76
77     bool parse();
78     bool parseQml();
79     bool parseJavaScript();
80     bool parseExpression();
81
82     bool isParsedCorrectly() const
83     { return _parsedCorrectly; }
84
85     Bind *bind() const;
86
87     int editorRevision() const;
88     void setEditorRevision(int revision);
89
90     QString fileName() const;
91     QString path() const;
92     QString componentName() const;
93
94 private:
95     bool parse_helper(int kind);
96     static void extractPragmas(QString *source);
97
98 private:
99     QmlJS::Engine *_engine;
100     NodePool *_pool;
101     AST::Node *_ast;
102     Bind *_bind;
103     bool _isQmlDocument;
104     int _editorRevision;
105     bool _parsedCorrectly;
106     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
107     QString _fileName;
108     QString _path;
109     QString _componentName;
110     QString _source;
111
112     // for documentFromSource
113     friend class Snapshot;
114 };
115
116 class QMLJS_EXPORT LibraryInfo
117 {
118     bool _valid;
119     QList<QmlDirParser::Component> _components;
120     QList<QmlDirParser::Plugin> _plugins;
121     typedef QList<const Interpreter::FakeMetaObject *> FakeMetaObjectList;
122     FakeMetaObjectList _metaObjects;
123
124 public:
125     LibraryInfo();
126     LibraryInfo(const QmlDirParser &parser);
127     ~LibraryInfo();
128
129     QList<QmlDirParser::Component> components() const
130     { return _components; }
131
132     QList<QmlDirParser::Plugin> plugins() const
133     { return _plugins; }
134
135     FakeMetaObjectList metaObjects() const
136     { return _metaObjects; }
137
138     void setMetaObjects(const FakeMetaObjectList &objects)
139     { _metaObjects = objects; }
140
141     bool isValid() const
142     { return _valid; }
143 };
144
145 class QMLJS_EXPORT Snapshot
146 {
147     typedef QHash<QString, Document::Ptr> _Base;
148     QHash<QString, Document::Ptr> _documents;
149     QMultiHash<QString, Document::Ptr> _documentsByPath;
150     QHash<QString, LibraryInfo> _libraries;
151
152 public:
153     Snapshot();
154     ~Snapshot();
155
156     typedef _Base::iterator iterator;
157     typedef _Base::const_iterator const_iterator;
158
159     const_iterator begin() const { return _documents.begin(); }
160     const_iterator end() const { return _documents.end(); }
161
162     void insert(const Document::Ptr &document);
163     void insertLibraryInfo(const QString &path, const LibraryInfo &info);
164     void remove(const QString &fileName);
165
166     Document::Ptr document(const QString &fileName) const;
167     QList<Document::Ptr> documentsInDirectory(const QString &path) const;
168     LibraryInfo libraryInfo(const QString &path) const;
169
170     Document::Ptr documentFromSource(const QString &code,
171                                      const QString &fileName) const;
172 };
173
174 } // end of namespace Qml
175
176 #endif // QMLDOCUMENT_H