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_),
37  foundNode(nullptr),
38  parentNode(nullptr) {};
39  virtual ~ExpressionNodeLocationFinder() {};
40 
46  size_t searchedPosition) {
47  gd::ExpressionNodeLocationFinder finder(searchedPosition);
48  node.Visit(finder);
49  return finder.GetNode();
50  }
51 
59  size_t searchedPosition) {
60  gd::ExpressionNodeLocationFinder finder(searchedPosition);
61  node.Visit(finder);
62  return finder.GetParentNode();
63  }
64 
68  ExpressionNode* GetNode() { return foundNode; };
69 
74  ExpressionNode* GetParentNode() { return parentNode; };
75 
76  protected:
77  void OnVisitSubExpressionNode(SubExpressionNode& node) override {
78  CheckSearchPositionInNode(node);
79  node.expression->Visit(*this);
80  }
81  void OnVisitOperatorNode(OperatorNode& node) override {
82  if (CheckSearchPositionInNode(node)) {
83  node.leftHandSide->Visit(*this);
84  node.rightHandSide->Visit(*this);
85  }
86  }
87  void OnVisitUnaryOperatorNode(UnaryOperatorNode& node) override {
88  CheckSearchPositionInNode(node);
89  node.factor->Visit(*this);
90  }
91  void OnVisitNumberNode(NumberNode& node) override {
92  CheckSearchPositionInNode(node);
93  }
94  void OnVisitTextNode(TextNode& node) override {
95  CheckSearchPositionInNode(node);
96  }
97  void OnVisitVariableNode(VariableNode& node) override {
98  if (CheckSearchPositionInNode(node)) {
99  if (node.child) node.child->Visit(*this);
100  }
101  }
102  void OnVisitVariableAccessorNode(VariableAccessorNode& node) override {
103  if (CheckSearchPositionInNode(node)) {
104  if (node.child) node.child->Visit(*this);
105  }
106  }
107  void OnVisitVariableBracketAccessorNode(
108  VariableBracketAccessorNode& node) override {
109  if (CheckSearchPositionInNode(node)) {
110  node.expression->Visit(*this);
111  if (node.child) node.child->Visit(*this);
112  }
113  }
114  void OnVisitIdentifierNode(IdentifierNode& node) override {
115  CheckSearchPositionInNode(node);
116  }
117  void OnVisitObjectFunctionNameNode(ObjectFunctionNameNode& node) override {
118  CheckSearchPositionInNode(node);
119  }
120  void OnVisitFunctionCallNode(FunctionCallNode& node) override {
121  CheckSearchPositionInNode(node);
122  for (auto& parameter : node.parameters) {
123  parameter->Visit(*this);
124  }
125  }
126  void OnVisitEmptyNode(EmptyNode& node) override {
127  CheckSearchPositionInNode(node, /*inclusive=*/true);
128  }
129 
130  private:
131  bool CheckSearchPositionInNode(ExpressionNode& node, bool inclusive = false) {
132  if (node.location.GetStartPosition() <= searchedPosition &&
133  ((!inclusive && searchedPosition < node.location.GetEndPosition()) ||
134  (inclusive && searchedPosition <= node.location.GetEndPosition()))) {
135  parentNode = foundNode;
136  foundNode = &node;
137  return true;
138  }
139 
140  return false;
141  }
142 
143  size_t searchedPosition;
144  ExpressionNode* foundNode;
145  ExpressionNode* parentNode;
146 };
147 
148 } // namespace gd
149 
150 #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:58
ExpressionNode * GetParentNode()
Return the parent of deepest node found at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:74
ExpressionNode * GetNode()
Return the deepest node found at the search position, if any.
Definition: ExpressionNodeLocationFinder.h:68
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:45
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:100
Definition: ExpressionParser2Node.h:115