GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ExpressionParser2NodePrinter.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_EXPRESSIONNODEPRINTER_H
7 #define GDCORE_EXPRESSIONNODEPRINTER_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 
32  public:
34  virtual ~ExpressionParser2NodePrinter(){};
35 
36  static gd::String PrintNode(gd::ExpressionNode& node) {
38  node.Visit(printer);
39  return printer.GetOutput();
40  }
41 
45  const gd::String& GetOutput() { return output; };
46 
47  static gd::String PrintStringLiteral(const gd::String& str) {
48  return "\"" +
49  str.FindAndReplace("\\", "\\\\").FindAndReplace("\"", "\\\"") + "\"";
50  }
51 
52  protected:
53  void OnVisitSubExpressionNode(SubExpressionNode& node) override {
54  output += "(";
55  node.expression->Visit(*this);
56  output += ")";
57  }
58  void OnVisitOperatorNode(OperatorNode& node) override {
59  node.leftHandSide->Visit(*this);
60  if (node.op == ' ') {
61  // There is no "space" operator. If it's there, it's because
62  // an operator could not be found (that's an error). Add only
63  // a whitespace between terms.
64  output += " ";
65  } else {
66  output += " ";
67  output.push_back(node.op);
68  output += " ";
69  }
70  node.rightHandSide->Visit(*this);
71  }
72  void OnVisitUnaryOperatorNode(UnaryOperatorNode& node) override {
73  output.push_back(node.op);
74  node.factor->Visit(*this);
75  }
76  void OnVisitNumberNode(NumberNode& node) override { output += node.number; }
77  void OnVisitTextNode(TextNode& node) override {
78  output += PrintStringLiteral(node.text);
79  }
80  void OnVisitVariableNode(VariableNode& node) override {
81  output += node.name;
82  if (node.child) node.child->Visit(*this);
83  }
84  void OnVisitVariableAccessorNode(VariableAccessorNode& node) override {
85  output += "." + node.name;
86  if (node.child) node.child->Visit(*this);
87  }
88  void OnVisitVariableBracketAccessorNode(
89  VariableBracketAccessorNode& node) override {
90  output += "[";
91  node.expression->Visit(*this);
92  output += "]";
93  if (node.child) node.child->Visit(*this);
94  }
95  void OnVisitIdentifierNode(IdentifierNode& node) override {
96  output += node.identifierName;
97  if (!node.childIdentifierName.empty()) {
98  output += "." + node.childIdentifierName;
99  }
100  }
101  void OnVisitObjectFunctionNameNode(ObjectFunctionNameNode& node) override {
102  if (!node.behaviorFunctionName.empty()) {
103  output += node.objectName + "." + node.objectFunctionOrBehaviorName +
104  "::" + node.behaviorFunctionName;
105  } else {
106  output += node.objectName + "." + node.objectFunctionOrBehaviorName;
107  }
108  };
109  void OnVisitFunctionCallNode(FunctionCallNode& node) override {
110  if (!node.behaviorName.empty()) {
111  output +=
112  node.objectName + "." + node.behaviorName + "::" + node.functionName;
113  } else if (!node.objectName.empty()) {
114  output += node.objectName + "." + node.functionName;
115  } else {
116  output += node.functionName;
117  }
118 
119  output += "(";
120 
121  bool isFirst = true;
122  for (auto& parameterNode : node.parameters) {
123  if (!isFirst) output += ", ";
124  parameterNode->Visit(*this);
125  isFirst = false;
126  }
127 
128  output += ")";
129  }
130  void OnVisitEmptyNode(EmptyNode& node) override { output += node.text; }
131 
132  private:
133  gd::String output;
134 };
135 
136 } // namespace gd
137 
138 #endif // GDCORE_EXPRESSIONNODEPRINTER_H
Print the expression corresponding to a set of nodes (i.e: this is doing the inverse operation of gd:...
Definition: ExpressionParser2NodePrinter.h:31
const gd::String & GetOutput()
Get the string corresponding to the expression nodes.
Definition: ExpressionParser2NodePrinter.h:45
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:31
void push_back(value_type character)
Add a character (from its codepoint) at the end of the String.
Definition: String.cpp:221
String FindAndReplace(String search, String replacement, bool all=true) const
Searches a string for a specified substring and returns a new string where all occurrences of this su...
Definition: String.cpp:411
Definition: CommonTools.h:24
The base node, from which all nodes in the tree of an expression inherits from.
Definition: ExpressionParser2Node.h:93