23 #include <fmt/format.h>
27 #include <antares/expressions/nodes/NodesForwardDeclaration.h>
28 #include <antares/expressions/visitors/NodeVisitor.h>
29 #include <antares/io/inputs/model-converter/ForbiddenNodes.h>
31 namespace Antares::IO::Inputs::ModelConverter
37 [[nodiscard]] std::string name()
const override;
59 template<Expressions::Nodes::FunctionNodeType func>
60 void checkConsistencyWithParents(
const std::string& childName)
const;
62 void checkConsistencyWithParents(
const std::string& childName)
const;
64 template<
class Parent>
65 void checkChildren(
const std::string& parentName,
66 const std::vector<Expressions::Nodes::Node*>& children,
67 bool validateConsistencyWithParents);
68 template<Expressions::Nodes::FunctionNodeType func>
69 void checkChildren(
const std::string& parentName,
70 const std::vector<Expressions::Nodes::Node*>& children,
71 bool validateConsistencyWithParents);
72 template<
class NodeType>
73 void handleComparisonNode(
const std::string& op,
74 const std::vector<Expressions::Nodes::Node*>& children);
79 std::vector<std::pair<std::string, std::type_index>> parentsStack_;
80 const std::string& expression_;
85 std::string expression;
86 std::string childName;
87 std::optional<std::string> parentName;
95 if (context.parentName.has_value())
97 return fmt::format(
"'{}' is not allowed to contain '{}' in this context '{}'",
98 context.parentName.value(),
102 return fmt::format(
"'{}' is not allowed in this context '{}'",
108 invalid_argument(BuildMessage(context))
113 template<
typename Child>
114 void NodeChecker::checkConsistencyWithParents(
const std::string& childName)
const
116 if (forbid_.isForbidden<Child>())
119 {.expression = expression_, .childName = childName, .parentName = std::nullopt});
121 for (
const auto& [parentName, typeIndex]: std::ranges::reverse_view(parentsStack_))
123 if (forbid_.isForbiddenFor<Child>(typeIndex))
125 throw BadContextComposition(
126 {.expression = expression_, .childName = childName, .parentName = parentName});
131 template<Expressions::Nodes::FunctionNodeType func>
132 void NodeChecker::checkConsistencyWithParents(
const std::string& childName)
const
134 if (forbid_.isForbidden<func>())
136 throw BadContextComposition(
137 {.expression = expression_, .childName = childName, .parentName = std::nullopt});
139 for (
const auto& [parentName, typeIndex]: std::ranges::reverse_view(parentsStack_))
141 if (forbid_.isForbiddenFor<func>(typeIndex))
143 throw BadContextComposition(
144 {.expression = expression_, .childName = childName, .parentName = parentName});
149 template<
typename Parent>
150 void NodeChecker::checkChildren(
const std::string& parentName,
151 const std::vector<Expressions::Nodes::Node*>& children,
152 bool validateConsistencyWithParents)
154 if (validateConsistencyWithParents)
156 checkConsistencyWithParents<Parent>(parentName);
158 parentsStack_.emplace_back(parentName,
typeid(Parent));
160 for (
const auto* child: children)
164 parentsStack_.pop_back();
167 template<Expressions::Nodes::FunctionNodeType func>
168 void NodeChecker::checkChildren(
const std::string& parentName,
169 const std::vector<Expressions::Nodes::Node*>& children,
170 bool validateConsistencyWithParents)
172 if (validateConsistencyWithParents)
174 checkConsistencyWithParents<func>(parentName);
176 parentsStack_.emplace_back(
178 typeid(std::integral_constant<Expressions::Nodes::FunctionNodeType, func>));
180 for (
const auto* child: children)
184 parentsStack_.pop_back();
187 template<
typename NodeType>
188 void NodeChecker::handleComparisonNode(
const std::string& op,
189 const std::vector<Expressions::Nodes::Node*>& children)
191 checkChildren<NodeType>(
"expression with " + op, children,
true);
Represents a AllTimeSumNode node in a syntax tree.
Definition: AllTimeSumNode.h:31
Represents a division node in a syntax tree.
Definition: DivisionNode.h:31
Represents an equality comparison node in a syntax tree.
Definition: EqualNode.h:31
AST node representing a function expression (max, min, pow, ...).
Definition: FunctionNode.h:63
Represents a greater than or equal comparison node in a syntax tree.
Definition: GreaterThanOrEqualNode.h:31
Represents a less than or equal comparison node in a syntax tree.
Definition: LessThanOrEqualNode.h:31
Represents a literal node in a syntax tree, storing a double value.
Definition: LiteralNode.h:11
Represents a multiplication node in a syntax tree.
Definition: MultiplicationNode.h:31
Represents a negation node in a syntax tree.
Definition: NegationNode.h:31
Represents a parameter node in a syntax tree, storing a string value.
Definition: ParameterNode.h:14
Represents a port field node in a syntax tree.
Definition: PortFieldNode.h:32
Represents a port field node where the expression is a sum.
Definition: PortFieldSumNode.h:32
Represents a subtraction node in a syntax tree.
Definition: SubtractionNode.h:31
Definition: TimeIndexNode.h:28
Definition: TimeShiftNode.h:29
Definition: TimeSumNode.h:28
Represents a variable node in a syntax tree, storing a string value.
Definition: VariableNode.h:20
Definition: NodeVisitor.h:76
void dispatch(const Nodes::Node *node, Args... args)
Definition: NodeVisitor.h:94
Definition: NodeChecker.h:91