Commit 0a4310d3 authored by Fawzi Mohamed's avatar Fawzi Mohamed

qmljs: added qmljsconstants.h

Moved Document::Language, Import::Type and StaticAnalysis::Severity
enums to qmljsconstants.h and renamed values removing the redundant
part.
Thus the effective length changed little or improved
(Document::QmlLanguage => Language::Qml).

The separate file allows better reuse of enum values without introducing
circular dependencies.

Change-Id: I5186d7c04f5d3f6c289068b919be5ff1ff118326
Reviewed-by: default avatarFawzi Mohamed <fawzi.mohamed@digia.com>
parent ab717553
......@@ -44,6 +44,7 @@
#include "qmljsglobal_p.h"
#include "qmljsastfwd_p.h"
#include "qmljsmemorypool_p.h"
#include <qmljs/qmljsconstants.h>
#include <QString>
#include <QSet>
......@@ -59,21 +60,19 @@ class MemoryPool;
class QML_PARSER_EXPORT DiagnosticMessage
{
public:
enum Kind { Warning, Error };
DiagnosticMessage()
: kind(Error) {}
: kind(Severity::Error) {}
DiagnosticMessage(Kind kind, const AST::SourceLocation &loc, const QString &message)
DiagnosticMessage(Severity::Enum kind, const AST::SourceLocation &loc, const QString &message)
: kind(kind), loc(loc), message(message) {}
bool isWarning() const
{ return kind == Warning; }
{ return kind == Severity::Warning; }
bool isError() const
{ return kind == Error; }
{ return kind == Severity::Error; }
Kind kind;
Severity::Enum kind;
AST::SourceLocation loc;
QString message;
};
......
......@@ -289,7 +289,7 @@ case 20: {
if (node) {
node->importToken = loc(1);
} else {
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, loc(1),
diagnostic_messages.append(DiagnosticMessage(Severity::Error, loc(1),
QLatin1String("Expected a qualified name id or a string literal")));
return false; // ### remove me
......@@ -583,7 +583,7 @@ case 78: {
case 79: {
bool rx = lexer->scanRegExp(Lexer::NoPrefix);
if (!rx) {
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, location(lexer), lexer->errorMessage()));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, location(lexer), lexer->errorMessage()));
return false; // ### remove me
}
......@@ -599,7 +599,7 @@ case 79: {
case 80: {
bool rx = lexer->scanRegExp(Lexer::EqualPrefix);
if (!rx) {
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, location(lexer), lexer->errorMessage()));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, location(lexer), lexer->errorMessage()));
return false;
}
......@@ -680,7 +680,7 @@ case 88: {
case 89: {
if (AST::ArrayMemberExpression *mem = AST::cast<AST::ArrayMemberExpression *>(sym(1).Expression)) {
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, mem->lbracketToken,
diagnostic_messages.append(DiagnosticMessage(Severity::Warning, mem->lbracketToken,
QLatin1String("Ignored annotation")));
sym(1).Expression = mem->base;
......@@ -691,7 +691,7 @@ case 89: {
} else {
sym(1).UiQualifiedId = 0;
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, loc(1),
diagnostic_messages.append(DiagnosticMessage(Severity::Error, loc(1),
QLatin1String("Expected a qualified name id")));
return false; // ### recover
......@@ -1747,7 +1747,7 @@ case 347: {
msg = qApp->translate("QmlParser", "Syntax error");
else
msg = qApp->translate("QmlParser", "Unexpected token `%1'").arg(QLatin1String(spell[token]));
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, token_buffer[0].loc, msg));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, token_buffer[0].loc, msg));
action = errorState;
goto _Lcheck_token;
......@@ -1775,7 +1775,7 @@ case 347: {
int a = t_action(errorState, *tk);
if (a > 0 && t_action(a, yytoken)) {
const QString msg = qApp->translate("QmlParser", "Expected token `%1'").arg(QLatin1String(spell[*tk]));
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, token_buffer[0].loc, msg));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, token_buffer[0].loc, msg));
yytoken = *tk;
yylval = 0;
......@@ -1799,7 +1799,7 @@ case 347: {
int a = t_action(errorState, tk);
if (a > 0 && t_action(a, yytoken)) {
const QString msg = qApp->translate("QmlParser", "Expected token `%1'").arg(QLatin1String(spell[tk]));
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, token_buffer[0].loc, msg));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, token_buffer[0].loc, msg));
yytoken = tk;
yylval = 0;
......@@ -1812,7 +1812,7 @@ case 347: {
}
const QString msg = qApp->translate("QmlParser", "Syntax error");
diagnostic_messages.append(DiagnosticMessage(DiagnosticMessage::Error, token_buffer[0].loc, msg));
diagnostic_messages.append(DiagnosticMessage(Severity::Error, token_buffer[0].loc, msg));
}
return false;
......
......@@ -162,7 +162,7 @@ public:
inline DiagnosticMessage diagnosticMessage() const
{
foreach (const DiagnosticMessage &d, diagnostic_messages) {
if (d.kind != DiagnosticMessage::Warning)
if (d.kind != Severity::Warning)
return d;
}
......
......@@ -38,7 +38,8 @@ HEADERS += \
$$PWD/iscriptevaluator.h \
$$PWD/qmljssimplereader.h \
$$PWD/persistenttrie.h \
$$PWD/qmljsqrcparser.h
$$PWD/qmljsqrcparser.h \
$$PWD/qmljsconstants.h
SOURCES += \
$$PWD/qmljsbind.cpp \
......
......@@ -28,6 +28,7 @@ QtcLibrary {
"qmljscheck.cpp", "qmljscheck.h",
"qmljscodeformatter.cpp", "qmljscodeformatter.h",
"qmljscompletioncontextfinder.cpp", "qmljscompletioncontextfinder.h",
"qmljsconstants.h",
"qmljscontext.cpp", "qmljscontext.h",
"qmljsdelta.cpp", "qmljsdelta.h",
"qmljsdocument.cpp", "qmljsdocument.h",
......
......@@ -204,17 +204,17 @@ bool Bind::visit(UiImport *ast)
const QString importId = ast->importId.toString();
ImportInfo import = ImportInfo::moduleImport(toString(ast->importUri), version,
importId, ast);
if (_doc->language() == Document::QmlLanguage) {
if (_doc->language() == Language::Qml) {
const QString importStr = import.name() + importId;
QmlLanguageBundles langBundles = ModelManagerInterface::instance()->extendedBundles();
QmlBundle qq1 = langBundles.bundleForLanguage(Document::QmlQtQuick1Language);
QmlBundle qq2 = langBundles.bundleForLanguage(Document::QmlQtQuick2Language);
QmlBundle qq1 = langBundles.bundleForLanguage(Language::QmlQtQuick1);
QmlBundle qq2 = langBundles.bundleForLanguage(Language::QmlQtQuick2);
bool isQQ1 = qq1.supportedImports().contains(importStr);
bool isQQ2 = qq2.supportedImports().contains(importStr);
if (isQQ1 && ! isQQ2)
_doc->setLanguage(Document::QmlQtQuick1Language);
_doc->setLanguage(Language::QmlQtQuick1);
if (isQQ2 && ! isQQ1)
_doc->setLanguage(Document::QmlQtQuick2Language);
_doc->setLanguage(Language::QmlQtQuick2);
}
_imports += import;
} else if (!ast->fileName.isEmpty()) {
......
......@@ -288,14 +288,14 @@ bool QmlBundle::readFrom(QString path, QStringList *errors)
return errs.isEmpty();
}
QmlBundle QmlLanguageBundles::bundleForLanguage(Document::Language l) const
QmlBundle QmlLanguageBundles::bundleForLanguage(Language::Enum l) const
{
if (m_bundles.contains(l))
return m_bundles.value(l);
return QmlBundle();
}
void QmlLanguageBundles::mergeBundleForLanguage(Document::Language l, const QmlBundle &bundle)
void QmlLanguageBundles::mergeBundleForLanguage(Language::Enum l, const QmlBundle &bundle)
{
if (bundle.isEmpty())
return;
......@@ -305,14 +305,14 @@ void QmlLanguageBundles::mergeBundleForLanguage(Document::Language l, const QmlB
m_bundles.insert(l,bundle);
}
QList<Document::Language> QmlLanguageBundles::languages() const
QList<Language::Enum> QmlLanguageBundles::languages() const
{
return m_bundles.keys();
}
void QmlLanguageBundles::mergeLanguageBundles(const QmlLanguageBundles &o)
{
foreach (Document::Language l, o.languages())
foreach (Language::Enum l, o.languages())
mergeBundleForLanguage(l, o.bundleForLanguage(l));
}
......
......@@ -102,12 +102,12 @@ private:
class QMLJS_EXPORT QmlLanguageBundles
{
public:
QmlBundle bundleForLanguage(Document::Language l) const;
void mergeBundleForLanguage(Document::Language l, const QmlBundle &bundle);
QList<Document::Language> languages() const;
QmlBundle bundleForLanguage(Language::Enum l) const;
void mergeBundleForLanguage(Language::Enum l, const QmlBundle &bundle);
QList<Language::Enum> languages() const;
void mergeLanguageBundles(const QmlLanguageBundles &);
private:
QHash<Document::Language,QmlBundle> m_bundles;
QHash<Language::Enum,QmlBundle> m_bundles;
};
} // namespace QmlJS
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QMLJSCONSTANTS_H
#define QMLJSCONSTANTS_H
namespace QmlJS {
namespace ImportType {
enum Enum {
Invalid,
ImplicitDirectory,
Library,
File,
Directory,
QrcFile,
QrcDirectory,
ImplicitQrcDirectory,
UnknownFile // refers a file/directory that wasn't found
};
}
namespace Severity {
enum Enum
{
Hint, // cosmetic or convention
MaybeWarning, // possibly a warning, insufficient information
Warning, // could cause unintended behavior
MaybeError, // possibly an error, insufficient information
Error // definitely an error
};
}
namespace Language {
enum Enum
{
Unknown = 0,
JavaScript = 1,
Json = 2,
Qml = 3,
QmlQtQuick1 = 4,
QmlQtQuick2 = 5,
QmlQbs = 6,
QmlProject = 7,
QmlTypeInfo = 8
};
}
} // namespace QmlJS
#endif // QMLJSCONSTANTS_H
......@@ -29,6 +29,7 @@
#include "qmljsdocument.h"
#include "qmljsbind.h"
#include "qmljsconstants.h"
#include <qmljs/parser/qmljslexer_p.h>
#include <qmljs/parser/qmljsparser_p.h>
......@@ -81,56 +82,56 @@ using namespace QmlJS::AST;
*/
bool Document::isQmlLikeLanguage(Document::Language language)
bool Document::isQmlLikeLanguage(Language::Enum language)
{
switch (language) {
case QmlLanguage:
case QmlQtQuick1Language:
case QmlQtQuick2Language:
case QmlQbsLanguage:
case QmlProjectLanguage:
case QmlTypeInfoLanguage:
case Language::Qml:
case Language::QmlQtQuick1:
case Language::QmlQtQuick2:
case Language::QmlQbs:
case Language::QmlProject:
case Language::QmlTypeInfo:
return true;
default:
return false;
}
}
bool Document::isFullySupportedLanguage(Document::Language language)
bool Document::isFullySupportedLanguage(Language::Enum language)
{
switch (language) {
case JavaScriptLanguage:
case JsonLanguage:
case QmlLanguage:
case QmlQtQuick1Language:
case QmlQtQuick2Language:
case Language::JavaScript:
case Language::Json:
case Language::Qml:
case Language::QmlQtQuick1:
case Language::QmlQtQuick2:
return true;
case UnknownLanguage:
case QmlQbsLanguage:
case QmlProjectLanguage:
case QmlTypeInfoLanguage:
case Language::Unknown:
case Language::QmlQbs:
case Language::QmlProject:
case Language::QmlTypeInfo:
break;
}
return false;
}
bool Document::isQmlLikeOrJsLanguage(Document::Language language)
bool Document::isQmlLikeOrJsLanguage(Language::Enum language)
{
switch (language) {
case QmlLanguage:
case QmlQtQuick1Language:
case QmlQtQuick2Language:
case QmlQbsLanguage:
case QmlProjectLanguage:
case QmlTypeInfoLanguage:
case JavaScriptLanguage:
case Language::Qml:
case Language::QmlQtQuick1:
case Language::QmlQtQuick2:
case Language::QmlQbs:
case Language::QmlProject:
case Language::QmlTypeInfo:
case Language::JavaScript:
return true;
default:
return false;
}
}
Document::Document(const QString &fileName, Language language)
Document::Document(const QString &fileName, Language::Enum language)
: _engine(0)
, _ast(0)
, _bind(0)
......@@ -163,24 +164,24 @@ Document::~Document()
delete _engine;
}
Document::MutablePtr Document::create(const QString &fileName, Language language)
Document::MutablePtr Document::create(const QString &fileName, Language::Enum language)
{
Document::MutablePtr doc(new Document(fileName, language));
doc->_ptr = doc;
return doc;
}
Document::Language Document::guessLanguageFromSuffix(const QString &fileName)
Language::Enum Document::guessLanguageFromSuffix(const QString &fileName)
{
if (fileName.endsWith(QLatin1String(".qml"), Qt::CaseInsensitive))
return QmlLanguage;
return Language::Qml;
if (fileName.endsWith(QLatin1String(".qbs"), Qt::CaseInsensitive))
return QmlQbsLanguage;
return Language::QmlQbs;
if (fileName.endsWith(QLatin1String(".js"), Qt::CaseInsensitive))
return JavaScriptLanguage;
return Language::JavaScript;
if (fileName.endsWith(QLatin1String(".json"), Qt::CaseInsensitive))
return JsonLanguage;
return UnknownLanguage;
return Language::Json;
return Language::Unknown;
}
Document::Ptr Document::ptr() const
......@@ -193,12 +194,12 @@ bool Document::isQmlDocument() const
return isQmlLikeLanguage(_language);
}
Document::Language Document::language() const
Language::Enum Document::language() const
{
return _language;
}
void Document::setLanguage(Document::Language l)
void Document::setLanguage(Language::Enum l)
{
_language = l;
}
......@@ -428,7 +429,7 @@ void Snapshot::remove(const QString &fileName)
Document::MutablePtr Snapshot::documentFromSource(
const QString &code, const QString &fileName,
Document::Language language) const
Language::Enum language) const
{
Document::MutablePtr newDoc = Document::create(fileName, language);
......
......@@ -50,36 +50,23 @@ public:
typedef QSharedPointer<const Document> Ptr;
typedef QSharedPointer<Document> MutablePtr;
enum Language
{
UnknownLanguage = 0,
JavaScriptLanguage = 1,
JsonLanguage = 2,
QmlLanguage = 3,
QmlQtQuick1Language = 4,
QmlQtQuick2Language = 5,
QmlQbsLanguage = 6,
QmlProjectLanguage = 7,
QmlTypeInfoLanguage = 8
};
static bool isQmlLikeLanguage(Language languge);
static bool isFullySupportedLanguage(Language language);
static bool isQmlLikeOrJsLanguage(Language language);
static bool isQmlLikeLanguage(Language::Enum languge);
static bool isFullySupportedLanguage(Language::Enum language);
static bool isQmlLikeOrJsLanguage(Language::Enum language);
protected:
Document(const QString &fileName, Language language);
Document(const QString &fileName, Language::Enum language);
public:
~Document();
static MutablePtr create(const QString &fileName, Language language);
static Language guessLanguageFromSuffix(const QString &fileName);
static MutablePtr create(const QString &fileName, Language::Enum language);
static Language::Enum guessLanguageFromSuffix(const QString &fileName);
Document::Ptr ptr() const;
bool isQmlDocument() const;
Language language() const;
void setLanguage(Language l);
Language::Enum language() const;
void setLanguage(Language::Enum l);
AST::UiProgram *qmlProgram() const;
AST::Program *jsProgram() const;
......@@ -124,7 +111,7 @@ private:
QString _source;
QWeakPointer<Document> _ptr;
int _editorRevision;
Language _language;
Language::Enum _language;
bool _parsedCorrectly;
// for documentFromSource
......@@ -237,7 +224,7 @@ public:
Document::MutablePtr documentFromSource(const QString &code,
const QString &fileName,
Document::Language language) const;
Language::Enum language) const;
};
} // namespace QmlJS
......
......@@ -2074,7 +2074,7 @@ bool ASTSignal::getSourceLocation(QString *fileName, int *line, int *column) con
ImportInfo::ImportInfo()
: _type(InvalidImport)
: _type(ImportType::Invalid)
, _ast(0)
{
}
......@@ -2089,7 +2089,7 @@ ImportInfo ImportInfo::moduleImport(QString uri, ComponentVersion version,
}
ImportInfo info;
info._type = LibraryImport;
info._type = ImportType::Library;
info._name = uri;
info._path = uri;
info._path.replace(QLatin1Char('.'), QDir::separator());
......@@ -2111,17 +2111,17 @@ ImportInfo ImportInfo::pathImport(const QString &docPath, const QString &path,
info._path = importFileInfo.absoluteFilePath();
if (importFileInfo.isFile()) {
info._type = FileImport;
info._type = ImportType::File;
} else if (importFileInfo.isDir()) {
info._type = DirectoryImport;
info._type = ImportType::Directory;
} else if (path.startsWith(QLatin1String("qrc:"))) {
info._path = path;
if (ModelManagerInterface::instance()->filesAtQrcPath(info.path()).isEmpty())
info._type = QrcDirectoryImport;
info._type = ImportType::QrcDirectory;
else
info._type = QrcFileImport;
info._type = ImportType::QrcFile;
} else {
info._type = UnknownFileImport;
info._type = ImportType::UnknownFile;
}
info._version = version;
info._as = as;
......@@ -2132,7 +2132,7 @@ ImportInfo ImportInfo::pathImport(const QString &docPath, const QString &path,
ImportInfo ImportInfo::invalidImport(UiImport *ast)
{
ImportInfo info;
info._type = InvalidImport;
info._type = ImportType::Invalid;
info._ast = ast;
return info;
}
......@@ -2140,17 +2140,17 @@ ImportInfo ImportInfo::invalidImport(UiImport *ast)
ImportInfo ImportInfo::implicitDirectoryImport(const QString &directory)
{
ImportInfo info;
info._type = ImplicitDirectoryImport;
info._type = ImportType::ImplicitDirectory;
info._path = directory;
return info;
}
bool ImportInfo::isValid() const
{
return _type != InvalidImport;
return _type != ImportType::Invalid;
}
ImportInfo::Type ImportInfo::type() const
ImportType::Enum ImportInfo::type() const
{
return _type;
}
......@@ -2201,7 +2201,7 @@ const Value *TypeScope::lookupMember(const QString &name, const Context *context
const ImportInfo &info = i.info;
// JS import has no types
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile)
continue;
if (!info.as().isEmpty()) {
......@@ -2231,7 +2231,7 @@ void TypeScope::processMembers(MemberProcessor *processor) const
const ImportInfo &info = i.info;
// JS import has no types
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile)
continue;
if (!info.as().isEmpty())
......@@ -2258,7 +2258,7 @@ const Value *JSImportScope::lookupMember(const QString &name, const Context *,
const ImportInfo &info = i.info;
// JS imports are always: import "somefile.js" as Foo
if (info.type() != ImportInfo::FileImport && info.type() != ImportInfo::QrcFileImport)
if (info.type() != ImportType::File && info.type() != ImportType::QrcFile)
continue;
if (info.as() == name) {
......@@ -2281,7 +2281,7 @@ void JSImportScope::processMembers(MemberProcessor *processor) const
const ObjectValue *import = i.object;
const ImportInfo &info = i.info;
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport)
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile)
processor->processProperty(info.as(), import);
}
}
......@@ -2338,7 +2338,7 @@ ImportInfo Imports::info(const QString &name, const Context *context) const
continue;
}
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport) {
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile) {
if (import->className() == firstId)
return info;
} else {
......@@ -2358,7 +2358,7 @@ QString Imports::nameForImportedObject(const ObjectValue *value, const Context *
const ObjectValue *import = i.object;
const ImportInfo &info = i.info;
if (info.type() == ImportInfo::FileImport || info.type() == ImportInfo::QrcFileImport) {
if (info.type() == ImportType::File || info.type() == ImportType::QrcFile) {
if (import == value)
return import->className();
} else {
......
......@@ -32,6 +32,7 @@
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljs_global.h>
#include <qmljs/qmljsconstants.h>
#include <QFileInfoList>
#include <QList>
......@@ -866,17 +867,6 @@ public:
class QMLJS_EXPORT ImportInfo
{
public:
enum Type {
InvalidImport,
ImplicitDirectoryImport,
LibraryImport,
FileImport,
DirectoryImport,
QrcFileImport,
QrcDirectoryImport,
UnknownFileImport // refers a file/directory that wasn't found
};
ImportInfo();
static ImportInfo moduleImport(QString uri, LanguageUtils::ComponentVersion version,
......@@ -888,7 +878,7 @@ public:
static ImportInfo implicitDirectoryImport(const QString &directory);
bool isValid() const;
Type type() const;
ImportType::Enum type() const;
// LibraryImport: uri with ',' separator
// Other: non-absolute path
......@@ -905,7 +895,7 @@ public:
AST::UiImport *ast() const;
private:
Type _type;
ImportType::Enum _type;
LanguageUtils::ComponentVersion _version;
QString _name;
QString _path;
......
......@@ -244,16 +244,16 @@ void LinkPrivate::populateImportedTypes(Imports *imports, Document::Ptr doc)
if (!import.object) {
switch (info.type()) {
case ImportInfo::FileImport:
case ImportInfo::DirectoryImport:
case ImportInfo::QrcFileImport:
case ImportInfo::QrcDirectoryImport:
case ImportType::File:
case ImportType::Directory:
case ImportType::QrcFile:
case ImportType::QrcDirectory:
import = importFileOrDirectory(doc, info);
break;
case ImportInfo::LibraryImport:
case ImportType::Library: