11 #include "GDCore/Events/Parsers/ExpressionParser2Node.h"
12 #include "GDCore/Events/Parsers/ExpressionParser2NodeWorker.h"
13 #include "GDCore/Project/ProjectScopedContainers.h"
14 #include "GDCore/Project/Variable.h"
15 #include "GDCore/Project/VariablesContainer.h"
52 gd::String parameterType = objectName.
empty() ?
"variable" :
"objectvar";
55 platform, projectScopedContainers, parameterType, objName);
56 node.Visit(typeFinder);
58 if (typeFinder.variableName.
empty() || !typeFinder.variablesContainer) {
59 return gd::Variable::Unknown;
61 auto *variable = typeFinder.WalkUntilLastChild(
62 typeFinder.variablesContainer->
Get(typeFinder.variableName),
63 typeFinder.childVariableNames);
64 return variable ? variable->
GetType() : gd::Variable::Unknown;
72 gd::String parameterType = objectName.
empty() ?
"variable" :
"objectvar";
75 platform, projectScopedContainers, parameterType, objName);
76 node.Visit(typeFinder);
78 if (typeFinder.variableName.
empty() || !typeFinder.variablesContainer) {
79 return gd::Variable::Unknown;
81 auto *variable = typeFinder.WalkUntilLastChild(
82 typeFinder.variablesContainer->
Get(typeFinder.variableName),
83 typeFinder.childVariableNames);
84 if (variable && variable->GetType() != gd::Variable::Array) {
85 return gd::Variable::Unknown;
87 return variable && variable->GetChildrenCount() > 0
88 ? variable->GetAtIndex(0).GetType()
89 : gd::Variable::Unknown;
101 : platform(platform_),
102 projectScopedContainers(projectScopedContainers_),
103 parameterType(parameterType_),
104 objectName(objectName_),
105 lastNodeToCheck(lastNodeToCheck_),
106 variablesContainer(
nullptr),
108 bailOutBecauseEmptyVariableName(
false) {};
110 void OnVisitVariableBracketAccessorNode(
114 void OnVisitOperatorNode(
OperatorNode& node)
override {}
116 void OnVisitNumberNode(
NumberNode& node)
override {}
117 void OnVisitTextNode(
TextNode& node)
override {}
119 FindVariableFor(node.name);
120 if (node.child && &node != lastNodeToCheck) node.child->Visit(*
this);
123 if (node.name.
empty() && node.child) {
128 bailOutBecauseEmptyVariableName =
true;
130 if (variableName.empty()) {
131 const auto& objectsContainersList = projectScopedContainers.GetObjectsContainersList();
132 if (objectsContainersList.HasObjectOrGroupWithVariableNamed(objectName,
134 gd::ObjectsContainersList::VariableExistence::DoesNotExist) {
135 variableName = node.name;
137 projectScopedContainers.GetObjectsContainersList()
141 childVariableNames.push_back(node.name);
143 if (node.child && &node != lastNodeToCheck) node.child->Visit(*
this);
148 void OnVisitEmptyNode(
EmptyNode& node)
override {}
153 if (!objectName.
empty()) {
154 const auto& objectsContainersList = projectScopedContainers.GetObjectsContainersList();
155 if (objectsContainersList.HasObjectOrGroupWithVariableNamed(objectName,
157 gd::ObjectsContainersList::VariableExistence::DoesNotExist) {
158 variableName = identifier;
160 projectScopedContainers.GetObjectsContainersList()
162 if (childIdentifier) {
163 childVariableNames.push_back(*childIdentifier);
167 else if (parameterType ==
"scenevar") {
170 variablesContainer = projectScopedContainers.GetVariablesContainersList()
172 variableName = identifier;
173 if (childIdentifier) {
174 childVariableNames.
push_back(*childIdentifier);
177 else if (parameterType ==
"globalvar") {
180 variablesContainer = projectScopedContainers.GetVariablesContainersList()
182 variableName = identifier;
183 if (childIdentifier) {
184 childVariableNames.
push_back(*childIdentifier);
190 projectScopedContainers.MatchIdentifierWithName<
void>(
193 objectName = identifier;
194 if (childIdentifier) {
195 if (parameterType ==
"variable") {
201 const auto& objectsContainersList = projectScopedContainers.GetObjectsContainersList();
202 if (objectsContainersList.HasObjectOrGroupWithVariableNamed(objectName,
204 gd::ObjectsContainersList::VariableExistence::DoesNotExist) {
205 variableName = *childIdentifier;
207 projectScopedContainers.GetObjectsContainersList()
215 if (projectScopedContainers.GetVariablesContainersList().
Has(identifier)) {
217 &(projectScopedContainers.GetVariablesContainersList()
219 variableName = identifier;
220 if (childIdentifier) {
221 childVariableNames.push_back(*childIdentifier);
242 const std::vector<gd::String>& childVariableNames,
243 size_t startIndex = 0) {
244 if (bailOutBecauseEmptyVariableName)
250 for (
size_t index = startIndex; index < childVariableNames.size();
252 const gd::String& childName = childVariableNames[index];
254 if (childName.
empty()) {
261 if (currentVariable->
GetType() == gd::Variable::Array) {
262 currentVariable = ¤tVariable->
GetAtIndex(0);
268 if (!currentVariable->
HasChild(childName)) {
273 currentVariable = ¤tVariable->
GetChild(childName);
279 return currentVariable;
284 const std::vector<gd::String>& childVariableNames,
285 size_t startIndex = 0) {
286 if (bailOutBecauseEmptyVariableName)
293 for (
size_t index = startIndex; index + 1 < childVariableNames.size();
295 const gd::String& childName = childVariableNames[index];
297 if (childName.
empty()) {
304 if (currentVariable->
GetType() == gd::Variable::Array) {
305 currentVariable = ¤tVariable->
GetAtIndex(0);
311 if (!currentVariable->
HasChild(childName)) {
316 currentVariable = ¤tVariable->
GetChild(childName);
322 return {.parentVariable = currentVariable};
327 const std::vector<gd::String>& childVariableNames) {
328 if (bailOutBecauseEmptyVariableName)
332 if (variableName.empty())
336 ? &variablesContainer.
Get(variableName)
338 if (childVariableNames.empty() || !variable)
340 .parentVariablesContainer = &variablesContainer};
342 return WalkUntilLastParent(*variable, childVariableNames, 0);
353 std::vector<gd::String> childVariableNames;
354 bool bailOutBecauseEmptyVariableName;
The interface for any worker class ("visitor" pattern) that want to interact with the nodes of a pars...
Definition: ExpressionParser2NodeWorker.h:36
Find a variable path from an expression node.
Definition: ExpressionVariablePathFinder.h:39
const gd::VariablesContainer * GetObjectOrGroupVariablesContainer(const gd::String &objectOrGroupName) const
Return the container of the variables for the specified object or group of objects.
Definition: ObjectsContainersList.cpp:189
Holds references to variables, objects, properties and other containers.
Definition: ProjectScopedContainers.h:34
String represents an UTF8 encoded string.
Definition: String.h:33
void push_back(value_type character)
Add a character (from its codepoint) at the end of the String.
Definition: String.cpp:223
bool empty() const
Returns true if the string is empty.
Definition: String.h:157
Defines a variable which can be used by an object, a layout or a project.
Definition: Variable.h:29
Type
Definition: Variable.h:32
size_t GetChildrenCount() const
Get the count of children that the variable has.
Definition: Variable.h:211
const std::map< gd::String, std::shared_ptr< Variable > > & GetAllChildren() const
Get the map containing all the children.
Definition: Variable.h:268
bool HasChild(const gd::String &name) const
Return true if the variable is a structure and has the specified child.
Definition: Variable.cpp:138
Variable & GetChild(const gd::String &name)
Return the child with the specified name.
Definition: Variable.cpp:148
Type GetType() const
Get the type of the variable.
Definition: Variable.h:64
Variable & GetAtIndex(const size_t index)
Return the element with the specified index.
Definition: Variable.cpp:189
Class defining a container for gd::Variable.
Definition: VariablesContainer.h:28
bool Has(const gd::String &name) const
Return true if the specified variable is in the container.
Definition: VariablesContainer.cpp:45
Variable & Get(const gd::String &name)
Return a reference to the variable called name.
Definition: VariablesContainer.cpp:51
const VariablesContainer & GetVariablesContainerFromVariableName(const gd::String &variableName) const
Definition: VariablesContainersList.cpp:77
const VariablesContainer * GetTopMostVariablesContainer() const
Avoid using apart when a scope must be forced.
Definition: VariablesContainersList.h:73
const VariablesContainer * GetBottomMostVariablesContainer() const
Avoid using apart when a scope must be forced.
Definition: VariablesContainersList.h:83
bool Has(const gd::String &name) const
Return true if the specified variable is in one of the containers.
Definition: VariablesContainersList.cpp:58
Definition: CommonTools.h:24
An empty node, used when parsing failed/a syntax error was encountered and any other node could not m...
Definition: ExpressionParser2Node.h:423
The base node, from which all nodes in the tree of an expression inherits from.
Definition: ExpressionParser2Node.h:100
A function call node (either free function, object function or object behavior function)....
Definition: ExpressionParser2Node.h:371
An identifier node, usually representing an object or a variable with an optional function name or ch...
Definition: ExpressionParser2Node.h:204
ExpressionParserLocation identifierNameDotLocation
Location of the "." after the object or variable name.
Definition: ExpressionParser2Node.h:228
gd::String childIdentifierName
The object function or variable child name.
Definition: ExpressionParser2Node.h:222
gd::String identifierName
The object or variable name.
Definition: ExpressionParser2Node.h:219
A number node. For example: "123". Its type is always "number".
Definition: ExpressionParser2Node.h:161
The name of a function to call on an object or the behavior For example: "MyObject....
Definition: ExpressionParser2Node.h:321
An operator node. For example: "lhs + rhs".
Definition: ExpressionParser2Node.h:129
Definition: ExpressionParser2Node.h:115
A text node. For example: "Hello World". Its type is always "string".
Definition: ExpressionParser2Node.h:177
A unary operator node. For example: "-2".
Definition: ExpressionParser2Node.h:145
A direct accessor to a child variable. Example: MyChild in MyVariable.MyChild.
Definition: ExpressionParser2Node.h:282
Contains a variables container or a variable. Useful to refer to the parent of a variable (which can ...
Definition: ExpressionVariablePathFinder.h:28
A bracket accessor to a child variable. Example: ["MyChild"] (in MyVariable["MyChild"]).
Definition: ExpressionParser2Node.h:300
A variable, or object variable, with bracket accessor or at least 2 "dot" accessors.
Definition: ExpressionParser2Node.h:261