aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/parser
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-01-15 14:47:35 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2020-01-23 15:58:10 +0100
commit684f9df7849bc79f1f02a60844fb43c7a3927d2f (patch)
tree531be4d102388395e4ddd2d14f38a679e15c283d /src/qml/parser
parent020a6e67766595351bcf911e965b26952a7c81b8 (diff)
Long live QML inline components
[ChangeLog][QtQml] It is now possible to declare new QML components in a QML file via the component keyword. They can be used just as if they were declared in another file, with the only difference that the type name needs to be prefixed with the name of the containing type outside of the file were the inline component has been declared. Notably, inline components are not closures: In the following example, the output would be 42 // MyItem.qml Item { property int i: 33 component IC: Item { Component.onCompleted: console.log(i) } } // user.qml Item { property int i: 42 MyItem.IC {} } Fixes: QTBUG-79382 Change-Id: I6a5ffc43f093a76323f435cfee9bab217781b8f5 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/parser')
-rw-r--r--src/qml/parser/qqmljs.g64
-rw-r--r--src/qml/parser/qqmljsast.cpp9
-rw-r--r--src/qml/parser/qqmljsast_p.h23
-rw-r--r--src/qml/parser/qqmljsastfwd_p.h1
-rw-r--r--src/qml/parser/qqmljsastvisitor_p.h2
-rw-r--r--src/qml/parser/qqmljskeywords_p.h19
6 files changed, 94 insertions, 24 deletions
diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g
index 6fbb9df164..397c0b51a5 100644
--- a/src/qml/parser/qqmljs.g
+++ b/src/qml/parser/qqmljs.g
@@ -91,6 +91,7 @@
%token T_EXPORT "export"
%token T_FROM "from"
%token T_REQUIRED "required"
+%token T_COMPONENT "component"
--- template strings
%token T_NO_SUBSTITUTION_TEMPLATE"(no subst template)"
@@ -123,7 +124,7 @@
%token T_FOR_LOOKAHEAD_OK "(for lookahead ok)"
--%left T_PLUS T_MINUS
-%nonassoc T_IDENTIFIER T_COLON T_SIGNAL T_PROPERTY T_READONLY T_ON T_SET T_GET T_OF T_STATIC T_FROM T_AS T_REQUIRED
+%nonassoc T_IDENTIFIER T_COLON T_SIGNAL T_PROPERTY T_READONLY T_ON T_SET T_GET T_OF T_STATIC T_FROM T_AS T_REQUIRED T_COMPONENT
%nonassoc REDUCE_HERE
%right T_THEN T_ELSE
%right T_WITHOUTAS T_AS
@@ -1426,6 +1427,19 @@ UiObjectMember: T_ENUM T_IDENTIFIER T_LBRACE EnumMemberList T_RBRACE;
}
./
+UiObjectMember: T_COMPONENT T_IDENTIFIER T_COLON UiObjectDefinition;
+/.
+ case $rule_number: {
+ if (!stringRef(2).front().isUpper()) {
+ diagnostic_messages.append(compileError(loc(2),
+ QLatin1String("Type name must be upper case"), QtWarningMsg));
+ }
+ auto inlineComponent = new (pool) AST::UiInlineComponent(stringRef(2), sym(4).UiObjectDefinition);
+ inlineComponent->componentToken = loc(1);
+ sym(1).Node = inlineComponent;
+ } break;
+./
+
EnumMemberList: T_IDENTIFIER;
/.
case $rule_number: {
@@ -1468,29 +1482,31 @@ EnumMemberList: EnumMemberList T_COMMA T_IDENTIFIER T_EQ T_NUMERIC_LITERAL;
}
./
-QmlIdentifier: T_IDENTIFIER;
-QmlIdentifier: T_PROPERTY;
-QmlIdentifier: T_SIGNAL;
-QmlIdentifier: T_READONLY;
-QmlIdentifier: T_ON;
-QmlIdentifier: T_GET;
-QmlIdentifier: T_SET;
-QmlIdentifier: T_FROM;
-QmlIdentifier: T_OF;
-QmlIdentifier: T_REQUIRED;
-
-JsIdentifier: T_IDENTIFIER;
-JsIdentifier: T_PROPERTY;
-JsIdentifier: T_SIGNAL;
-JsIdentifier: T_READONLY;
-JsIdentifier: T_ON;
-JsIdentifier: T_GET;
-JsIdentifier: T_SET;
-JsIdentifier: T_FROM;
-JsIdentifier: T_STATIC;
-JsIdentifier: T_OF;
-JsIdentifier: T_AS;
-JsIdentifier: T_REQUIRED;
+QmlIdentifier: T_IDENTIFIER
+ | T_PROPERTY
+ | T_SIGNAL
+ | T_READONLY
+ | T_ON
+ | T_GET
+ | T_SET
+ | T_FROM
+ | T_OF
+ | T_REQUIRED
+ | T_COMPONENT;
+
+JsIdentifier: T_IDENTIFIER
+ | T_PROPERTY
+ | T_SIGNAL
+ | T_READONLY
+ | T_ON
+ | T_GET
+ | T_SET
+ | T_FROM
+ | T_STATIC
+ | T_OF
+ | T_AS
+ | T_REQUIRED
+ | T_COMPONENT;
IdentifierReference: JsIdentifier;
BindingIdentifier: IdentifierReference;
diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp
index 03355b3e38..aa3e8ab5e3 100644
--- a/src/qml/parser/qqmljsast.cpp
+++ b/src/qml/parser/qqmljsast.cpp
@@ -1545,6 +1545,15 @@ void Type::toString(QString *out) const
};
}
+void UiInlineComponent::accept0(Visitor *visitor)
+{
+ if (visitor->visit(this)) {
+ accept(component, visitor);
+ }
+
+ visitor->endVisit(this);
+}
+
} } // namespace QQmlJS::AST
QT_END_NAMESPACE
diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h
index aa355fed85..48a994cd33 100644
--- a/src/qml/parser/qqmljsast_p.h
+++ b/src/qml/parser/qqmljsast_p.h
@@ -244,6 +244,7 @@ public:
Kind_UiImport,
Kind_UiObjectBinding,
Kind_UiObjectDefinition,
+ Kind_UiInlineComponent,
Kind_UiObjectInitializer,
Kind_UiObjectMemberList,
Kind_UiArrayMemberList,
@@ -3377,6 +3378,28 @@ public:
UiObjectInitializer *initializer;
};
+class QML_PARSER_EXPORT UiInlineComponent: public UiObjectMember
+{
+public:
+ QQMLJS_DECLARE_AST_NODE(UiInlineComponent)
+
+ UiInlineComponent(const QStringRef& inlineComponentName, UiObjectDefinition* inlineComponent)
+ : name(inlineComponentName), component(inlineComponent)
+ { kind = K; }
+
+ QStringRef name;
+ UiObjectDefinition* component;
+ SourceLocation componentToken;
+
+ SourceLocation lastSourceLocation() const override
+ {return component->lastSourceLocation();}
+
+ SourceLocation firstSourceLocation() const override
+ {return componentToken;}
+
+ void accept0(Visitor *visitor) override;
+};
+
class QML_PARSER_EXPORT UiSourceElement: public UiObjectMember
{
public:
diff --git a/src/qml/parser/qqmljsastfwd_p.h b/src/qml/parser/qqmljsastfwd_p.h
index 05226fd043..8a8ee2dfae 100644
--- a/src/qml/parser/qqmljsastfwd_p.h
+++ b/src/qml/parser/qqmljsastfwd_p.h
@@ -169,6 +169,7 @@ class UiImport;
class UiPublicMember;
class UiParameterList;
class UiObjectDefinition;
+class UiInlineComponent;
class UiObjectInitializer;
class UiObjectBinding;
class UiScriptBinding;
diff --git a/src/qml/parser/qqmljsastvisitor_p.h b/src/qml/parser/qqmljsastvisitor_p.h
index 7146cd00ac..d6b92990ad 100644
--- a/src/qml/parser/qqmljsastvisitor_p.h
+++ b/src/qml/parser/qqmljsastvisitor_p.h
@@ -112,6 +112,7 @@ public:
virtual bool visit(UiEnumDeclaration *) { return true; }
virtual bool visit(UiEnumMemberList *) { return true; }
virtual bool visit(UiVersionSpecifier *) { return true; }
+ virtual bool visit(UiInlineComponent *) { return true; }
virtual void endVisit(UiProgram *) {}
virtual void endVisit(UiImport *) {}
@@ -131,6 +132,7 @@ public:
virtual void endVisit(UiEnumDeclaration *) {}
virtual void endVisit(UiEnumMemberList *) { }
virtual void endVisit(UiVersionSpecifier *) {}
+ virtual void endVisit(UiInlineComponent *) {}
// QQmlJS
virtual bool visit(ThisExpression *) { return true; }
diff --git a/src/qml/parser/qqmljskeywords_p.h b/src/qml/parser/qqmljskeywords_p.h
index 3eb054341f..5f08cc4353 100644
--- a/src/qml/parser/qqmljskeywords_p.h
+++ b/src/qml/parser/qqmljskeywords_p.h
@@ -836,6 +836,25 @@ static inline int classify9(const QChar *s, int parseModeFlags) {
}
}
}
+ else if (s[0].unicode() == 'c') {
+ if (s[1].unicode() == 'o') {
+ if (s[2].unicode() == 'm') {
+ if (s[3].unicode() == 'p') {
+ if (s[4].unicode() == 'o') {
+ if (s[5].unicode() == 'n') {
+ if (s[6].unicode() == 'e') {
+ if (s[7].unicode() == 'n') {
+ if (s[8].unicode() == 't') {
+ return (parseModeFlags & Lexer::QmlMode) ? int(Lexer::T_COMPONENT) : int(Lexer::T_IDENTIFIER);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
return Lexer::T_IDENTIFIER;
}