GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ExpressionNodeLocationFinder.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #ifndef GDCORE_EXPRESSIONNODELOCATIONFINDER_H
7 #define GDCORE_EXPRESSIONNODELOCATIONFINDER_H
8 
9 #include <memory>
10 #include <vector>
11 
12 #include "GDCore/Events/Parsers/ExpressionParser2Node.h"
13 #include "GDCore/Events/Parsers/ExpressionParser2NodeWorker.h"
14 namespace gd {
15 class Expression;
16 class ObjectsContainer;
17 class Platform;
18 class ParameterMetadata;
19 class ExpressionMetadata;
20 } // namespace gd
21 
22 namespace gd {
23 
31  public:
35  ExpressionNodeLocationFinder(size_t searchedPosition_)
36  : searchedPosition(searchedPosition_), foundNode(nullptr){};
37  virtual ~ExpressionNodeLocationFinder(){};
38 
44  size_t searchedPosition) {
45  gd::ExpressionNodeLocationFinder finder(searchedPosition);
46  node.Visit(finder);
47  return finder.GetNode();
48  }
49 
57  size_t searchedPosition) {
58  gd::ExpressionNodeLocationFinder finder(searchedPosition);
59  node.Visit(finder);
60  return finder.GetParentNode();
61  }
62 
66  ExpressionNode* GetNode() { return foundNode; };
67 
72  ExpressionNode* GetParentNode() { return parentNode; };
73 
74  protected:
75  void OnVisitSubExpressionNode(SubExpressionNode& node) override {
76  CheckSearchPositionInNode(node);
77  node.expression->Visit(*this);
78  }
79  void OnVisitOperatorNode(OperatorNode& node) override {
80  if (CheckSearchPositionInNode(node)) {
81  node.leftHandSide->Visit(*this);
82  node.rightHandSide->Visit(*this);
83  }
84  }
85  void OnVisitUnaryOperatorNode(UnaryOperatorNode& node) override {
86  CheckSearchPositionInNode(node);
87  node.factor->Visit(*this);
88  }
89  void OnVisitNumberNode(NumberNode& node) override {
90  CheckSearchPositionInNode(node);
91  }
92  void OnVisitTextNode(TextNode& node) override {
93  CheckSearchPositionInNode(node);
94  }
95  void OnVisitVariableNode(VariableNode& node) override {
96  if (CheckSearchPositionInNode(node)) {
97  if (node.child) node.child->Visit(*this);
98  }
99  }
100  void OnVisitVariableAccessorNode(VariableAccessorNode& node) override {
101  if (CheckSearchPositionInNode(node)) {
102  if (node.child) node.child->Visit(*this);
103  }
104  }
105  void OnVisitVariableBracketAccessorNode(
106  VariableBracketAccessorNode& node) override {
107  if (CheckSearchPositionInNode(node)) {
108  node.expression->Visit(*this);
109  if (node.child) node.child->Visit(*this);
110  }
111  }
112  void OnVisitIdentifierNode(IdentifierNode& node) override {
113  CheckSearchPositionInNode(node);
114  }
115  void OnVisitObjectFunctionNameNode(ObjectFunctionNameNode& node) override {
116  CheckSearchPositionInNode(node);
117  }
118  void OnVisitFunctionCallNode(FunctionCallNode& node) override {
119  CheckSearchPositionInNode(node);
120  for (auto& parameter : node.parameters) {
121  parameter->Visit(*this);
122  }
123  }
124  void OnVisitEmptyNode(EmptyNode& node) override {
125  CheckSearchPositionInNode(node, /*inclusive=*/true);
126  }
127 
128  private:
129  bool CheckSearchPositionInNode(ExpressionNode& node, bool inclusive = false) {
130  if (node.location.GetStartPosition() <= searchedPosition &&
131  ((!inclusive && searchedPosition < node.location.GetEndPosition()) ||
132  (inclusive && searchedPosition <= node.location.GetEndPosition()))) {
133  parentNode = foundNode;
134  foundNode = &node;
135  return true;
136  }
137 
138  return false;
139  }
140 
141  size_t searchedPosition;
142  ExpressionNode* foundNode;
143  ExpressionNode* parentNode;
144 };
145 
146 } // namespace gd
147 
148 #endif // GDCORE_EXPRESSIONNODELOCATIONFINDER_H
Find the deepest node at the specified location in an expression.
Definition: ExpressionNodeLocationFinder.h:30
static ExpressionNode * GetParentNodeAtPosition(gd::ExpressionNode &node, size_t searchedPosition)
Helper function to find the parent of the deepest node at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:56
ExpressionNode * GetParentNode()
Return the parent of deepest node found at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:72
ExpressionNode * GetNode()
Return the deepest node found at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:66
ExpressionNodeLocationFinder(size_t searchedPosition_)
Initialize the finder to search at the specified position.
Definition: ExpressionNodeLocationFinder.h:35
static ExpressionNode * GetNodeAtPosition(gd::ExpressionNode &node, size_t searchedPosition)
Helper function to find the deepest node at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:43
The interface for any worker class ("visitor" pattern) that want to interact with the nodes of a pars...
Definition: ExpressionParser2NodeWorker.h:36
Definition: CommonTools.h:24
The base node, from which all nodes in the tree of an expression inherits from.
Definition: ExpressionParser2Node.h:93
Definition: ExpressionParser2Node.h:108