MIRA
XMLMacroProcessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2025 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_XML_MACROS_XML_MACRO_PROCESSOR_H_
48 #define _MIRA_XML_MACROS_XML_MACRO_PROCESSOR_H_
49 
50 #include <deque>
51 #include <stack>
52 #include <unordered_map>
53 
54 #include <xml/macros/IMacro.h>
55 #include <xml/macros/Scope.h>
56 #include <xml/macros/Types.h>
57 
58 namespace mira {
59 
60 class XMLDomPreprocessor;
61 
63 {
64 public:
65  // The `macro` namespace used for bultin functions.
67 
68 public:
70  // API for XMLDomPreprocessor
72 
74 
76  {
77  return *mPreprocessor;
78  }
79 
85  XMLNode expand(XMLNode ioNode);
86 
87  bool isMacro(const XMLDom::const_iterator& node);
88 
95 
101  void onDocumentLeft(const std::string& targetNamespace = "");
102 
103 public:
105  // Internal API for macro processing
107 
109 
110  XMLNode invokeMacro(const std::string& name, XMLNode node);
111 
113  {
114  std::swap(mCurrentScope, scope);
115  return scope;
116  }
117 
119  {
120  assert(mCurrentScope != nullptr);
121  return *mCurrentScope;
122  }
123 
124  [[nodiscard]] const xmlmacros::Scope& getCurrentScope() const
125  {
126  assert(mCurrentScope != nullptr);
127  return *mCurrentScope;
128  }
129 
131  {
132  return mCurrentScope;
133  }
134 
136  {
137  return mGlobalScope;
138  }
139 
143  std::string coerceToString(const std::string& name);
144 
146 
147  void popInvocation();
148 
149 public:
150  static std::tuple<xmlmacros::Identifier, std::string> getIdentifier(XMLDom::const_iterator node)
151  {
152  std::string nodeName = *node;
153  auto dotPos = nodeName.find('.');
154 
155  std::string name = dotPos == std::string::npos ?
156  std::move(nodeName) :
157  nodeName.substr(0, dotPos);
158 
159  std::string selector = dotPos == std::string::npos ?
160  std::string() :
161  nodeName.substr(dotPos + 1);
162 
163  return {
164  {node.nameSpace().prefix, std::move(name)},
165  std::move(selector)
166  };
167  }
168 
169  static xmlmacros::Identifier makeIdentifier(const std::string& name)
170  {
171  return {NAMESPACE.prefix, name};
172  }
173 
174 protected:
179 
184 
186  std::stack<xmlmacros::ScopePtr> mImportStack;
187 
188 protected:
190  const static uint MAX_INVOKE_DEPTH = 1000;
191 
193  std::vector<xmlmacros::SourceInfo> mInvokeStack;
194 
195 protected:
197 
198 protected:
200 
202 
203 }; // class XMLMacroProcessor
204 
205 } // namespace mira
206 
207 #endif // _MIRA_XML_MACROS_XML_MACRO_PROCESSOR_H_
void pushInvocation(xmlmacros::SourceInfo &&)
XMLDomPreprocessor * mPreprocessor
Definition: XMLMacroProcessor.h:196
Definition: Scope.h:57
void onDocumentEntered(XMLDom::const_iterator root, XMLDom::iterator ioDocument)
To be called before the contents of a document are processed.
void onDocumentLeft(const std::string &targetNamespace="")
To be called after an included document has been processed.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
XMLNode expand(XMLNode ioNode)
Expand a macro.
static const uint MAX_INVOKE_DEPTH
Used to prevent infinite recursion.
Definition: XMLMacroProcessor.h:190
xmlmacros::MacroPtr mDefineHandler
Definition: XMLMacroProcessor.h:199
xmlmacros::ScopePtr mCurrentScope
Current active definition scope.
Definition: XMLMacroProcessor.h:183
std::vector< xmlmacros::SourceInfo > mInvokeStack
Keeps track of the invoke-path of macros for error messages.
Definition: XMLMacroProcessor.h:193
const xmlmacros::Scope & getCurrentScope() const
Definition: XMLMacroProcessor.h:124
NameSpace nameSpace() const
Return the namespace information of the node.
Definition: Types.h:67
xmlmacros::MacroPtr mInlineImportHandler
Definition: XMLMacroProcessor.h:201
std::string coerceToString(const std::string &name)
xmlmacros::ScopePtr getGlobalScopePtr() const
Definition: XMLMacroProcessor.h:135
static const XMLDom::NameSpace NAMESPACE
Definition: XMLMacroProcessor.h:66
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:763
XMLNode invokeMacro(const std::string &name, XMLNode node)
bool isMacro(const XMLDom::const_iterator &node)
Namespace information of a node.
Definition: XMLDom.h:90
static xmlmacros::Identifier makeIdentifier(const std::string &name)
Definition: XMLMacroProcessor.h:169
Variable scope of the macro processor.
Definition: Scope.h:76
static std::tuple< xmlmacros::Identifier, std::string > getIdentifier(XMLDom::const_iterator node)
Definition: XMLMacroProcessor.h:150
std::stack< xmlmacros::ScopePtr > mImportStack
Scopes of higher-up documents.
Definition: XMLMacroProcessor.h:186
Data structure for storing macro definitions.
xmlmacros::ScopePtr mGlobalScope
Global scope of the macro processor.
Definition: XMLMacroProcessor.h:178
XMLNode processInPlace(XMLNode ioNode)
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:850
xmlmacros::Scope & getCurrentScope()
Definition: XMLMacroProcessor.h:118
xmlmacros::ScopePtr swapScope(xmlmacros::ScopePtr scope)
Definition: XMLMacroProcessor.h:112
xmlmacros::ScopePtr getCurrentScopePtr() const
Definition: XMLMacroProcessor.h:130
Preprocesses XML documents and resolves all special tags like , <if>, <warning> and so on...
Definition: XMLDomPreprocessor.h:109
Definition: XMLMacroProcessor.h:62
XMLMacroProcessor(XMLDomPreprocessor &)
std::string prefix
Definition: XMLDom.h:97
Scope::Ptr ScopePtr
Definition: Scope.h:155
Interface for implementing XML macros.
std::shared_ptr< IMacro > MacroPtr
Definition: IMacro.h:99
XMLDomPreprocessor & getPreprocessor()
Definition: XMLMacroProcessor.h:75
const_sibling_iterator find(const std::string &name, std::size_t nth=0) const
Find the nth subnode with a given name.