WSF
WsfParseNode.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11// ****************************************************************************
12// Updated by Infoscitex, a DCS Company.
13// ****************************************************************************
14
15#ifndef WSFPARSENODE_HPP
16#define WSFPARSENODE_HPP
17
18#include "wsf_parser_export.h"
19
20#include <cstdlib>
21#include <vector>
22
23#include "UtLog.hpp"
24#include "UtMemoryPool.hpp"
25#include "UtStringRef.hpp"
26#include "UtTextDocument.hpp"
27#include "WsfParseAuxData.hpp"
29#include "WsfParseRule.hpp"
30
32
33class WSF_PARSER_EXPORT WsfParseFileReferenceData : public WsfParseAuxData
34{
35public:
36 WsfParseFileReferenceData(const std::string& aFilePath)
38 , mFilePath(aFilePath)
39 {
40 }
41 ~WsfParseFileReferenceData() override = default;
42 UtStringRef mFilePath;
43};
44
45class WSF_PARSER_EXPORT WsfParseDelayLoadData : public WsfParseAuxData
46{
47public:
53 size_t mLoadOrder;
54};
55
56class WsfParseNodeP;
57
60class WSF_PARSER_EXPORT WsfParseNode
61{
62public:
63 friend class WsfParseNodePool;
64 friend class WsfParseNodeP;
65
66 void Add(WsfParseNode* aChild);
68 void SetFlags(int aMask) { mFlags |= aMask; }
69 int GetFlags() const { return mFlags; }
70 void DebugPrint(ut::log::MessageStream& aStream);
71 WsfParseNode* Next(bool aRecurseDown = true) const;
72 WsfParseNode* NextLeaf(bool aRecurseDown = true) const;
73 WsfParseNode* PreviousLeaf() const;
74 WsfParseNode* Previous() const;
75 WsfParseNode* FindChildByType(const std::string& aType) const;
76 UtTextRange SubtreeRange(UtTextDocument* aSource);
77 UtTextDocumentRange SubtreeRange();
78 UtTextRange GetBoundingRange(UtTextDocument* aSource);
79 WsfParseNode* GetLeftmostValue(UtTextDocument* aSourcePtr);
80 WsfParseNode* GetRightmostValue(UtTextDocument* aSourcePtr);
81 WsfParseNode* GetLeftmostValue();
82 WsfParseNode* GetRightmostValue();
83 WsfParseNode* GetRightmostLeaf();
84 WsfParseNode* GetLeftmostLeaf();
86 // Note: the auxiliary value's memory must be managed elsewhere.
87 // The usual place is WsfParser.AddAuxiliaryValue()
88 void SetAuxiliaryValue(WsfParseAuxData* aValuePtr) { mAuxiliaryValue = aValuePtr; }
89 WsfParseNode* GetParent() const { return mParent; }
90 WsfParseNode* GetChild(size_t aIndex) const;
91 void Detatch();
92 void Replace(WsfParseNode* aNodePtr);
93
94 void ResetLinks();
95 WsfParseNode* Right() { return mRight; }
96 WsfParseNode* FarRight();
97 WsfParseNode* Left() { return mLeft; }
98 WsfParseNode* FarLeft();
99 WsfParseNode* Down() { return mDown; }
100 void InsertBefore(WsfParseNode* aNodePtr);
101 void InsertAfter(WsfParseNode* aNodePtr);
102 size_t LexicalIndex() const { return mLexicalIndex; }
103 size_t LoadOrder() const;
104 void SetLexicalIndex(size_t aLexicalIndex) { mLexicalIndex = aLexicalIndex; }
105
106 static bool GetRangeNodes(WsfParseNode* aRootNodePtr,
107 const UtTextDocumentRange& aRange,
108 const std::vector<WsfParseNode*>& aFileTransitionNodes,
109 std::vector<WsfParseNode*>& aNodes);
110 WsfParseRule* GetTopLevelRule() const;
111 void MarkPruned();
112
113 std::string GetTextValue();
114
115 void Print(std::ostream& aStream, int aIndent, bool aVerbose);
116
117 static std::string GetNodeString(WsfParseNode* aNodePtr);
118
142
144
149 const char* mType;
154 UtTextDocumentRange mValue;
155
156protected:
157 WsfParseNode(WsfParseRule* aReader, const char* aType, UtTextDocumentRange aValue = UtTextDocumentRange())
158 : mFlags()
159 , mRulePtr(aReader)
160 , mType(aType)
162 , mValue(aValue)
163 , mParent()
164 , mRight()
165 , mLeft()
166 , mDown()
167 , mLexicalIndex(ut::npos)
168 {
169 }
171 : mFlags()
172 , mRulePtr()
173 , mType()
175 , mValue()
176 , mParent()
177 , mRight()
178 , mLeft()
179 , mDown()
180 , mLexicalIndex(ut::npos)
181 {
182 }
183 ~WsfParseNode() = default;
184 WsfParseNode(const WsfParseNode&) = default; // copy constructor
185
186private:
187 WsfParseNode* NextP(WsfParseNode* aPreviousChild);
188
190 WsfParseNode* mParent;
192 WsfParseNode* mRight;
194 WsfParseNode* mLeft;
196 WsfParseNode* mDown;
198 size_t mLexicalIndex;
199};
200
201
203class WSF_PARSER_EXPORT WsfParseNodePool
204{
205 UtMemoryPool* mAlloc;
206
207 // This intermediate class merely allows access to the constructor
208 class WSF_PARSER_EXPORT WsfParseNodeP : public WsfParseNode
209 {
210 public:
211 WsfParseNodeP(WsfParseRule* aReader, const char* aType, UtTextDocumentRange aValue = UtTextDocumentRange())
212 : WsfParseNode(aReader, aType, aValue)
213 {
214 }
215 WsfParseNodeP()
216 : WsfParseNode()
217 {
218 }
219 };
220
221public:
222 WsfParseNodePool() { mAlloc = new UtMemoryPool(sizeof(WsfParseNodeP)); }
224 {
225 delete mAlloc;
226 for (size_t i = 0; i < mAuxVals.size(); ++i)
227 {
228 delete mAuxVals[i];
229 }
230 }
231 WsfParseNodePool(const WsfParseNodePool&) = default; // copy constructor
232
234 {
235#ifndef NDEBUG
236 mAlloc->DebugClear();
237#endif
238 mAlloc->Reset();
239 for (size_t i = 0; i < mAuxVals.size(); ++i)
240 {
241 delete mAuxVals[i];
242 }
243 mAuxVals.clear();
244 }
245
247 {
248 std::swap(mAlloc, aRhs.mAlloc);
250 }
251
253 WsfParseNode* NewNode(WsfParseRule* aRule, const char* aType, UtTextDocumentRange aValue = UtTextDocumentRange())
254 {
255 void* memPtr = mAlloc->Alloc();
256 if (!memPtr)
257 {
258 std::string ruleDescription = "root-commands";
259 if (aRule)
260 {
261 ruleDescription = aRule->GetRuleDescription();
262 }
263 auto logger = ut::log::error() << "Memory allocation failed on parsing.";
264 logger.AddNote() << "Rule: " << ruleDescription;
265 logger.AddNote() << "File: " << aValue.mSource->GetFilePath().GetFileName();
266 throw std::bad_alloc();
267 }
268 return new (memPtr) WsfParseNodeP(aRule, aType, aValue);
269 }
270
273 {
274 void* memPtr = mAlloc->Alloc();
275 if (!memPtr)
276 {
277 ut::log::error() << "Memory allocation failure.";
278 throw std::bad_alloc();
279 }
280 return new (memPtr) WsfParseNodeP();
281 }
282
283 void FreeNode(WsfParseNode* aNodePtr)
284 {
285 // Currently, there is no way to free a single node. This is a stub to keep track of places
286 // that nodes are no longer needed.
287 }
288
289 // Because WsfParseNode does not have a destructor, the lifetime of WsfParseAuxData objects is handled by WsfParseNodePool
290 void AddAuxiliaryValue(WsfParseAuxData* aDataPtr) { mAuxVals.push_back(aDataPtr); }
291 std::vector<WsfParseAuxData*> mAuxVals;
293};
294
295using WsfParseNodeList = std::vector<WsfParseNode*>;
296
297#endif
@ cMDB_PARSE_NODE_POOL
Definition WsfParseDebugMarkers.hpp:30
std::vector< WsfParseNode * > WsfParseNodeList
Definition WsfParseNode.hpp:295
Abstract class for encapsulating auxiliary data to be attached to a parse node.
Definition WsfParseAuxData.hpp:19
@ cFILE_REFERENCE
Definition WsfParseAuxData.hpp:24
@ cDELAY_LOAD_DATA
Definition WsfParseAuxData.hpp:25
WsfParseAuxData(DataType aType)
Definition WsfParseAuxData.hpp:28
WsfParseDelayLoadData()
Definition WsfParseNode.hpp:48
size_t mLoadOrder
Definition WsfParseNode.hpp:53
UtStringRef mFilePath
Definition WsfParseNode.hpp:42
~WsfParseFileReferenceData() override=default
WsfParseFileReferenceData(const std::string &aFilePath)
Definition WsfParseNode.hpp:36
To improve efficiency, WsfParseNodePool handles the allocation and deletion of WsfParseNode's.
Definition WsfParseNode.hpp:204
UT_MEMORY_DEBUG_MARKER(cMDB_PARSE_NODE_POOL)
void SwapPool(WsfParseNodePool &aRhs)
Definition WsfParseNode.hpp:246
WsfParseNodePool(const WsfParseNodePool &)=default
WsfParseNode * NewNode(WsfParseRule *aRule, const char *aType, UtTextDocumentRange aValue=UtTextDocumentRange())
Create a new parse node.
Definition WsfParseNode.hpp:253
void AddAuxiliaryValue(WsfParseAuxData *aDataPtr)
Definition WsfParseNode.hpp:290
~WsfParseNodePool()
Definition WsfParseNode.hpp:223
void FreeNode(WsfParseNode *aNodePtr)
Definition WsfParseNode.hpp:283
std::vector< WsfParseAuxData * > mAuxVals
Definition WsfParseNode.hpp:291
WsfParseNodePool()
Definition WsfParseNode.hpp:222
void DeleteAllNodes()
Definition WsfParseNode.hpp:233
WsfParseNode * NewNode()
Create a new parse node.
Definition WsfParseNode.hpp:272
Definition WsfParseNode.hpp:61
friend class WsfParseNodeP
Definition WsfParseNode.hpp:64
WsfParseRule * mRulePtr
The WsfParseReader object which created this node.
Definition WsfParseNode.hpp:146
int GetFlags() const
Definition WsfParseNode.hpp:69
WsfParseNode * Down()
Definition WsfParseNode.hpp:99
void Add(WsfParseNode *aChild)
Definition WsfParseNode.cpp:24
friend class WsfParseNodePool
Definition WsfParseNode.hpp:63
Flags
Definition WsfParseNode.hpp:120
@ cLOAD_TYPE_NODE
Definition WsfParseNode.hpp:127
@ cPRUNE_NODE
Definition WsfParseNode.hpp:140
@ cSCRIPT_BLOCK
Definition WsfParseNode.hpp:132
@ cDELAY_LOAD
Definition WsfParseNode.hpp:136
@ cSCRIPT_FUNCTION
Definition WsfParseNode.hpp:133
@ cBLOCK_NODE
Definition WsfParseNode.hpp:131
@ cBLOCK_END
Definition WsfParseNode.hpp:138
@ cNAMED_NODE
Definition WsfParseNode.hpp:126
@ cSCRIPT_MASK
Definition WsfParseNode.hpp:135
@ cTYPE_MASK
Definition WsfParseNode.hpp:125
@ cLAZY_TYPE_REFERENCE_NODE
Definition WsfParseNode.hpp:128
@ cLAZY_TYPENAME_NODE
Definition WsfParseNode.hpp:129
@ cLAZY_TYPE_MASK
Definition WsfParseNode.hpp:130
@ cBLOCK_START
Definition WsfParseNode.hpp:137
@ cTYPE_NAME_NODE
Definition WsfParseNode.hpp:123
@ cERROR_INCOMPLETE
Flag indicates the node is the result of a partially matched rule.
Definition WsfParseNode.hpp:122
@ cTYPE_REFERENCE_NODE
Definition WsfParseNode.hpp:124
@ cSCRIPT_VARIABLES
Definition WsfParseNode.hpp:134
void SetFlags(int aMask)
Definition WsfParseNode.hpp:68
WsfParseNode * GetParent() const
Definition WsfParseNode.hpp:89
~WsfParseNode()=default
void MarkIncomplete()
Definition WsfParseNode.hpp:67
WsfParseNode()
Definition WsfParseNode.hpp:170
void SetAuxiliaryValue(WsfParseAuxData *aValuePtr)
Definition WsfParseNode.hpp:88
WsfParseNode(WsfParseRule *aReader, const char *aType, UtTextDocumentRange aValue=UtTextDocumentRange())
Definition WsfParseNode.hpp:157
WsfParseAuxData * mAuxiliaryValue
Definition WsfParseNode.hpp:150
void SetLexicalIndex(size_t aLexicalIndex)
Definition WsfParseNode.hpp:104
WsfParseNode * Left()
Definition WsfParseNode.hpp:97
const char * mType
Definition WsfParseNode.hpp:149
WsfParseNode * Right()
Definition WsfParseNode.hpp:95
int mFlags
Bit-set of flags for the node.
Definition WsfParseNode.hpp:143
WsfParseAuxData * GetAuxiliaryValue() const
Definition WsfParseNode.hpp:85
UtTextDocumentRange mValue
Definition WsfParseNode.hpp:154
WsfParseNode(const WsfParseNode &)=default
size_t LexicalIndex() const
Definition WsfParseNode.hpp:102
Definition WsfParseRule.hpp:131
virtual std::string GetRuleDescription()
Definition WsfParseRule.hpp:219
void swap(WsfRoute &aLhs, WsfRoute &aRhs)
Definition WsfRoute.hpp:294
Definition WsfSensorErrorModel.hpp:21
Copyrights Multiple, All Rights Reserved