GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ExpressionParser2Node.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 #pragma once
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "ExpressionParser2NodeWorker.h"
12 #include "GDCore/String.h"
13 
14 namespace gd {
15 class Expression;
16 class ObjectsContainer;
17 class Platform;
18 class ParameterMetadata;
19 class ExpressionMetadata;
20 struct FunctionCallNode;
21 } // namespace gd
22 
23 namespace gd {
24 
25 struct GD_CORE_API ExpressionParserLocation {
26  ExpressionParserLocation() : isValid(false){};
27  ExpressionParserLocation(size_t position)
28  : isValid(true), startPosition(position), endPosition(position){};
29  ExpressionParserLocation(size_t startPosition_, size_t endPosition_)
30  : isValid(true),
31  startPosition(startPosition_),
32  endPosition(endPosition_){};
33  size_t GetStartPosition() const { return startPosition; }
34  size_t GetEndPosition() const { return endPosition; }
35  bool IsValid() const { return isValid; }
36 
37  private:
38  bool isValid;
39  size_t startPosition = 0;
40  size_t endPosition = 0;
41 };
42 
46 struct GD_CORE_API ExpressionParserError {
47  enum ErrorType {
48  SyntaxError,
49  InvalidOperator,
50  MismatchedType,
51  UndeclaredVariable,
52  UnknownIdentifier,
53  BracketsNotAllowedForObjects,
54  TooFewParameters,
55  TooManyParameters,
56  InvalidFunctionName,
57  MalformedVariableParameter,
58  MalformedObjectParameter,
59  UnknownParameterType,
60  MissingBehavior,
61  VariableNameCollision,
62  };
63 
64  ExpressionParserError(gd::ExpressionParserError::ErrorType type_,
65  const gd::String &message_,
66  const ExpressionParserLocation &location_,
67  const gd::String &actualValue_ = "",
68  const gd::String &objectName_ = "")
69  : type(type_), message(message_), location(location_),
70  actualValue(actualValue_), objectName(objectName_){};
71  ExpressionParserError(gd::ExpressionParserError::ErrorType type_,
72  const gd::String &message_, size_t position_)
73  : type(type_), message(message_), location(position_){};
74  ExpressionParserError(gd::ExpressionParserError::ErrorType type_,
75  const gd::String &message_, size_t startPosition_,
76  size_t endPosition_)
77  : type(type_), message(message_),
78  location(startPosition_, endPosition_){};
79  virtual ~ExpressionParserError(){};
80 
81  gd::ExpressionParserError::ErrorType GetType() { return type; }
82  const gd::String &GetMessage() { return message; }
83  const gd::String &GetObjectName() { return objectName; }
84  const gd::String &GetActualValue() { return actualValue; }
85  size_t GetStartPosition() { return location.GetStartPosition(); }
86  size_t GetEndPosition() { return location.GetEndPosition(); }
87 
88 private:
89  gd::ExpressionParserError::ErrorType type;
90  gd::String message;
91  ExpressionParserLocation location;
92  gd::String objectName;
93  gd::String actualValue;
94 };
95 
100 struct GD_CORE_API ExpressionNode {
101  ExpressionNode() : parent(nullptr) {};
102  virtual ~ExpressionNode(){};
103  virtual void Visit(ExpressionParser2NodeWorker &worker){};
104 
105  std::unique_ptr<ExpressionParserError> diagnostic;
112  ExpressionNode *parent;
113 };
114 
115 struct GD_CORE_API SubExpressionNode : public ExpressionNode {
116  SubExpressionNode(std::unique_ptr<ExpressionNode> expression_)
117  : ExpressionNode(), expression(std::move(expression_)){};
118  virtual ~SubExpressionNode(){};
119  virtual void Visit(ExpressionParser2NodeWorker &worker) {
120  worker.OnVisitSubExpressionNode(*this);
121  };
122 
123  std::unique_ptr<ExpressionNode> expression;
124 };
125 
129 struct GD_CORE_API OperatorNode : public ExpressionNode {
130  OperatorNode(gd::String::value_type op_)
131  : ExpressionNode(), op(op_){};
132  virtual ~OperatorNode(){};
133  virtual void Visit(ExpressionParser2NodeWorker &worker) {
134  worker.OnVisitOperatorNode(*this);
135  };
136 
137  std::unique_ptr<ExpressionNode> leftHandSide;
138  std::unique_ptr<ExpressionNode> rightHandSide;
139  gd::String::value_type op;
140 };
141 
145 struct GD_CORE_API UnaryOperatorNode : public ExpressionNode {
146  UnaryOperatorNode(gd::String::value_type op_)
147  : ExpressionNode(), op(op_){};
148  virtual ~UnaryOperatorNode(){};
149  virtual void Visit(ExpressionParser2NodeWorker &worker) {
150  worker.OnVisitUnaryOperatorNode(*this);
151  };
152 
153  std::unique_ptr<ExpressionNode> factor;
154  gd::String::value_type op;
155 };
156 
161 struct GD_CORE_API NumberNode : public ExpressionNode {
162  NumberNode(const gd::String &number_)
163  : ExpressionNode(), number(number_){};
164  virtual ~NumberNode(){};
165  virtual void Visit(ExpressionParser2NodeWorker &worker) {
166  worker.OnVisitNumberNode(*this);
167  };
168 
169  //
170  gd::String number;
171 };
172 
177 struct GD_CORE_API TextNode : public ExpressionNode {
178  TextNode(const gd::String &text_) : ExpressionNode(), text(text_){};
179  virtual ~TextNode(){};
180  virtual void Visit(ExpressionParser2NodeWorker &worker) {
181  worker.OnVisitTextNode(*this);
182  };
183 
184  gd::String text;
185 };
186 
188  : public ExpressionNode {
190  : ExpressionNode(){};
191 };
192 
203 struct GD_CORE_API IdentifierNode
206  const gd::String &identifierName_)
208  identifierName(identifierName_),
209  childIdentifierName(""){};
211  const gd::String &identifierName_,
212  const gd::String &childIdentifierName_)
214  identifierName(identifierName_),
215  childIdentifierName(childIdentifierName_){};
216  virtual ~IdentifierNode(){};
217  virtual void Visit(ExpressionParser2NodeWorker &worker) {
218  worker.OnVisitIdentifierNode(*this);
219  };
220 
223 
224 
232 };
233 
239  void Visit(ExpressionParser2NodeWorker &worker) override{};
240 };
241 
244 
245  std::unique_ptr<VariableAccessorOrVariableBracketAccessorNode> child;
246 };
247 
262  VariableNode(const gd::String &name_)
264  virtual ~VariableNode(){};
265  virtual void Visit(ExpressionParser2NodeWorker &worker) {
266  worker.OnVisitVariableNode(*this);
267  };
268 
269  gd::String name;
270 
271  std::unique_ptr<VariableAccessorOrVariableBracketAccessorNode>
272  child; // Can be nullptr if no accessor
273 
274  ExpressionParserLocation nameLocation;
275 };
276 
281 struct GD_CORE_API VariableAccessorNode
283  VariableAccessorNode(const gd::String &name_)
285  virtual ~VariableAccessorNode(){};
286  virtual void Visit(ExpressionParser2NodeWorker &worker) {
287  worker.OnVisitVariableAccessorNode(*this);
288  };
289 
290  gd::String name;
291  ExpressionParserLocation nameLocation;
292  ExpressionParserLocation dotLocation;
293 };
294 
299 struct GD_CORE_API VariableBracketAccessorNode
301  VariableBracketAccessorNode(std::unique_ptr<ExpressionNode> expression_)
302  : VariableAccessorOrVariableBracketAccessorNode(), expression(std::move(expression_)){};
303  virtual ~VariableBracketAccessorNode(){};
304  virtual void Visit(ExpressionParser2NodeWorker &worker) {
305  worker.OnVisitVariableBracketAccessorNode(*this);
306  };
307 
308  std::unique_ptr<ExpressionNode> expression;
309 };
310 
320 struct GD_CORE_API ObjectFunctionNameNode
322  ObjectFunctionNameNode(const gd::String &objectName_,
323  const gd::String &objectFunctionOrBehaviorName_)
325  objectName(objectName_),
326  objectFunctionOrBehaviorName(objectFunctionOrBehaviorName_) {}
327  ObjectFunctionNameNode(const gd::String &objectName_,
328  const gd::String &behaviorName_,
329  const gd::String &behaviorFunctionName_)
331  objectName(objectName_),
332  objectFunctionOrBehaviorName(behaviorName_),
333  behaviorFunctionName(behaviorFunctionName_) {}
334  virtual ~ObjectFunctionNameNode(){};
335  virtual void Visit(ExpressionParser2NodeWorker &worker) {
336  worker.OnVisitObjectFunctionNameNode(*this);
337  };
338 
339  gd::String objectName;
346 
363 };
364 
373  FunctionCallNode(const gd::String &functionName_)
375  functionName(functionName_){};
376 
378  FunctionCallNode(const gd::String &objectName_,
379  const gd::String &functionName_)
381  objectName(objectName_),
382  functionName(functionName_){};
383 
385  FunctionCallNode(const gd::String &objectName_,
386  const gd::String &behaviorName_,
387  const gd::String &functionName_)
389  objectName(objectName_),
390  behaviorName(behaviorName_),
391  functionName(functionName_){};
392  virtual ~FunctionCallNode(){};
393  virtual void Visit(ExpressionParser2NodeWorker &worker) {
394  worker.OnVisitFunctionCallNode(*this);
395  };
396 
397  gd::String objectName;
398  gd::String behaviorName;
399  std::vector<std::unique_ptr<ExpressionNode>> parameters;
400  gd::String functionName;
401 
402  ExpressionParserLocation
417 };
418 
424  EmptyNode(const gd::String &text_ = "")
426  virtual ~EmptyNode(){};
427  virtual void Visit(ExpressionParser2NodeWorker &worker) {
428  worker.OnVisitEmptyNode(*this);
429  };
430 
431  gd::String text;
432 };
433 
434 } // namespace gd
The interface for any worker class ("visitor" pattern) that want to interact with the nodes of a pars...
Definition: ExpressionParser2NodeWorker.h:36
String represents an UTF8 encoded string.
Definition: String.h:33
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
ExpressionParserLocation location
Definition: ExpressionParser2Node.h:106
An error that can be attached to a gd::ExpressionNode.
Definition: ExpressionParser2Node.h:46
Definition: ExpressionParser2Node.h:25
A function call node (either free function, object function or object behavior function)....
Definition: ExpressionParser2Node.h:371
FunctionCallNode(const gd::String &objectName_, const gd::String &functionName_)
Construct an object function call node.
Definition: ExpressionParser2Node.h:378
ExpressionParserLocation objectNameLocation
Location of the object name, if any.
Definition: ExpressionParser2Node.h:405
FunctionCallNode(const gd::String &functionName_)
Construct a free function call node.
Definition: ExpressionParser2Node.h:373
FunctionCallNode(const gd::String &objectName_, const gd::String &behaviorName_, const gd::String &functionName_)
Construct a behavior function call node.
Definition: ExpressionParser2Node.h:385
ExpressionParserLocation objectNameDotLocation
Location of the "." after the object name.
Definition: ExpressionParser2Node.h:407
ExpressionParserLocation openingParenthesisLocation
Location of the "(".
Definition: ExpressionParser2Node.h:414
ExpressionParserLocation functionNameLocation
Location of the function name.
Definition: ExpressionParser2Node.h:403
ExpressionParserLocation behaviorNameNamespaceSeparatorLocation
Definition: ExpressionParser2Node.h:411
ExpressionParserLocation behaviorNameLocation
Location of the behavior name, if any.
Definition: ExpressionParser2Node.h:409
ExpressionParserLocation closingParenthesisLocation
Location of the ")".
Definition: ExpressionParser2Node.h:416
Definition: ExpressionParser2Node.h:235
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
ExpressionParserLocation childIdentifierNameLocation
Definition: ExpressionParser2Node.h:229
ExpressionParserLocation identifierNameLocation
Location of the object or variable name.
Definition: ExpressionParser2Node.h:226
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
ExpressionParserLocation behaviorNameNamespaceSeparatorLocation
Definition: ExpressionParser2Node.h:358
gd::String objectFunctionOrBehaviorName
Definition: ExpressionParser2Node.h:340
ExpressionParserLocation objectNameLocation
Location of the object name.
Definition: ExpressionParser2Node.h:348
ExpressionParserLocation behaviorFunctionNameLocation
Definition: ExpressionParser2Node.h:360
ExpressionParserLocation objectNameDotLocation
Location of the "." after the object name.
Definition: ExpressionParser2Node.h:350
gd::String behaviorFunctionName
Definition: ExpressionParser2Node.h:343
ExpressionParserLocation objectFunctionOrBehaviorNameLocation
Definition: ExpressionParser2Node.h:351
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
Definition: ExpressionParser2Node.h:242
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