WSF
WsfParser.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 WSFPARSER_HPP
16#define WSFPARSER_HPP
17
18#include <list>
19
20#include "UtCallback.hpp"
21#include "UtCircularBuffer.hpp"
22#include "UtPath.hpp"
23#include "UtTextDocument.hpp"
25class WsfParseError;
26#include "wsf_parser_export.h"
27class WsfParseIndex;
28#include "WsfParseNode.hpp"
29class WsfParseRule;
32
35class WSF_PARSER_EXPORT WsfParser
36{
37public:
38 WsfParser(WsfParseSourceProvider* aSourceProviderPtr,
39 const UtPath& aWorkingDirectory,
40 WsfParseDefinitions* aDefinitionsPtr = nullptr);
41 WsfParser(const WsfParser& aRhs);
42 WsfParser& operator=(const WsfParser&) = delete;
43 ~WsfParser();
44
45 WsfParseNode* ParseFiles(const std::vector<UtPath>& aFileList, int aMaxErrors);
46
47 void PushDeferredSource(const UtPath& aFilePath);
48
49 UtTextDocument* PushSource(const std::string& aFilePath,
50 bool aForceInclude,
51 UtTextDocumentRange* aIncludeLocation = nullptr,
52 bool aSearchIncludePaths = true);
53
54 UtTextDocument* FindSource(const std::string& aFilePath, bool aSearchIncludePaths = true, bool aReadFile = true);
55
57 size_t GetWordsRead() const { return mWordsRead; }
58
62 UtTextDocumentRange ReadWord();
63
68 UtTextDocumentRange ReadTerminator(const std::string& aTerminator);
69
70 void UndoRead(size_t aWordCount = 1);
71 UtTextDocumentRange UndoReadAndGetRange(size_t aWordCount);
72 void PushToUndoStack(const UtTextDocumentRange& aRange);
73
74 void PartialMatch(UtTextDocumentRange aRange) { mPartialMatches.push_back(aRange); }
75 void AddError(WsfParseError* aErrorPtr) { mSharedPtr->mErrors.push_back(aErrorPtr); }
76 void AddError(WsfParseRule* aReader, UtTextDocumentRange& aRange);
77 void ClearPartialMatches() { mPartialMatches.clear(); }
78
79 WsfParseSourceInclude* GetCurrentInclude() { return mCurrentSourcePtr; }
80 WsfParseSourceInclude* GetInclude(UtTextDocument* aParseSourcePtr);
81
82 WsfParseIndex*& GetParseIndex() { return mParseData; }
83 WsfParseDefinitions* GetDefinitions() { return mDefinitions; }
84 void SetDefinitions(WsfParseDefinitions* aDefinitionsPtr);
85 void SwapDefinitions(WsfParseDefinitions*& aDefinitionsPtr);
86
87 void PopSource();
88 void ProcessSpecialNode(WsfParseNode* aNodePtr);
89 void Reset();
90
91 void PushTerminator(const char* aTerminatorString)
92 {
93 ++mTerminatorPrefixCheck[(unsigned char)*aTerminatorString];
94 mTerminatorList.push_back(aTerminatorString);
95 }
97 {
98 char c = mTerminatorList.back()[0];
99 --mTerminatorPrefixCheck[(unsigned char)c];
100 mTerminatorList.pop_back();
101 }
102 void SetWorkingDirectory(const UtPath& aWorkingDirectory) { mSharedPtr->mWorkingDirectory = aWorkingDirectory; }
103 const UtPath& GetWorkingDirectory() const { return mSharedPtr->mWorkingDirectory; }
104 const std::vector<WsfParseError*>& GetErrors() const { return mSharedPtr->mErrors; }
105
106 WsfParseSourceProvider* GetSourceProvider() { return mSourceProvider; }
107
108 WsfParseNode* GetCurrentSequence() { return mCurrentSequenceStack.back(); }
109 void PushSequence(WsfParseNode* aNodePtr) { mCurrentSequenceStack.push_back(aNodePtr); }
110 void PopSequence() { mCurrentSequenceStack.pop_back(); }
111
112 void DelayLoad(const std::string& aKind,
113 const std::string& aName,
114 WsfParseNode* aNodePtr,
115 ptrdiff_t aTextOffset,
116 WsfParseRule* aSequencePtr);
117
118 bool Requires(const std::string& aKind, const std::string& aName);
119
120 void ResolveDelayLoad();
121
122 bool IsDelayLoading() { return mIsDelayLoading; }
123
125
126 void DeleteAllNodes() { mSharedPtr->DeleteAllNodes(); }
127
128 void SwapPool(WsfParser& aRhs) { mSharedPtr->SwapPool(*aRhs.mSharedPtr); }
129 void SwapPool(WsfParseNodePool& aPool) { mSharedPtr->SwapPool(aPool); }
130
131 WsfParseNode* NewNode(WsfParseRule* aRule, const char* aType, UtTextDocumentRange aValue = UtTextDocumentRange())
132 {
133 return mSharedPtr->NewNode(aRule, aType, aValue);
134 }
135
136 WsfParseNode* NewNode() { return mSharedPtr->NewNode(); }
137
138 void FreeNode(WsfParseNode* aNodePtr)
139 {
140 // Currently, there is no way to free a single node. This is a stub to keep track of places
141 // that nodes are no longer needed.
142 }
143
144 void AddAuxiliaryValue(WsfParseAuxData* aDataPtr) { mSharedPtr->AddAuxiliaryValue(aDataPtr); }
145
146 void FinalizeParseTree(WsfParseNode* aRootNodePtr, std::vector<WsfParseNode*>& aFileTransitionNodes);
147
148 bool FullyExpandNodes() const { return mFullyExpandNodes; }
149 void SetFullyExpandNodes(bool aValue) { mFullyExpandNodes = aValue; }
150 void SetIgnoreIncludes(bool aIngoreIncludes) { mIgnoreIncludes = aIngoreIncludes; }
151
152private:
153 void PushSourceP(UtTextDocument* aSource, UtTextDocumentRange* aIncludeLocation = nullptr, size_t aIncludeCount = 1);
154
155 using DelayLoadId = std::pair<std::string, std::string>;
156 class DelayLoader
157 {
158 public:
159 DelayLoader()
160 : mNodePtr(nullptr)
161 , mRulePtr(nullptr)
162 , mTextOffset(0)
163 , mIncludePtr(nullptr)
164 {
165 }
166
167 WsfParseNode* mNodePtr;
168 // WsfParseSequence* mSequencePtr;
169 WsfParseRule* mRulePtr;
170 ptrdiff_t mTextOffset;
171 WsfParseSourceInclude* mIncludePtr;
172 };
173
174 using DelayLoaderMap = std::multimap<DelayLoadId, DelayLoader>;
175
176
178 class Shared : public WsfParseNodePool
179 {
180 public:
181 Shared()
182 : mDelayLoadingCount(0)
183 {
184 }
185 ~Shared();
186 void ClearErrors();
187
188 std::vector<WsfParseError*> mErrors;
189 UtPath mWorkingDirectory;
190 DelayLoaderMap mDelayLoaders;
191 std::set<DelayLoadId> mCompletedDelayLoaders;
192 std::set<DelayLoadId> mLoadingDelayLoaders;
194 std::vector<DelayLoadId> mDelayLoadOrdering;
195 size_t mDelayLoadingCount;
196 UT_MEMORY_DEBUG_MARKER(cMDB_PARSE_PARSER_SHARED);
197 };
198
199 struct SourceData
200 {
201 UtTextDocument* mSource;
202 WsfParseSourceInclude* mIncludePtr;
203 UtTextDocument::iterator mPos;
204 };
205
206 static const int cRECALL_SIZE = 100;
208 static const int cMAXIMUM_REINCLUDE_COUNT = 10;
209
221 UtTextDocumentRange ReadWordP();
222
223 Shared* mSharedPtr;
224 UtReferenceCount* mSharedReferenceCountPtr;
225
229 std::list<UtPath> mDeferredSources;
230
231 WsfParseIndex* mParseData;
232
234 size_t mWordsRead;
235
237 std::vector<SourceData> mSourceStack;
238
239 WsfParseDefinitions* mDefinitions;
240
241 std::vector<UtTextDocumentRange> mPartialMatches;
242
243 std::vector<std::pair<UtTextDocument*, WsfParseSourceInclude*>> mSourceIncludes;
244
245 WsfParseSourceInclude* mCurrentSourcePtr;
246 WsfParseSourceInclude* mRootSourcePtr;
247 WsfParseSourceProvider* mSourceProvider;
249 UtFixedCircularBuffer<UtTextDocumentRange> mRecall;
250 ptrdiff_t mRecallPosition;
252 std::vector<const char*> mTerminatorList;
253 unsigned char mTerminatorPrefixCheck[256];
254 bool mCheckForTerminator;
255
256 // UtPath mWorkingDirectory;
257 std::vector<WsfParseNode*> mCurrentSequenceStack;
258
259 bool mIsDelayLoading;
260 bool mIgnoreIncludes;
261 bool mInsidePreprocessorVariable;
265 bool mFullyExpandNodes;
266 UT_MEMORY_DEBUG_MARKER(cMDB_PARSE_PARSER);
267};
268
270{
271public:
273 UtTextDocumentRange mRange;
274};
275
276#endif
@ cMDB_PARSE_PARSER_SHARED
Definition WsfParseDebugMarkers.hpp:29
@ cMDB_PARSE_PARSER
Definition WsfParseDebugMarkers.hpp:28
Abstract class for encapsulating auxiliary data to be attached to a parse node.
Definition WsfParseAuxData.hpp:19
Definition WsfParseDefinitions.hpp:39
Definition WsfParser.hpp:270
UtTextDocumentRange mRange
Definition WsfParser.hpp:273
WsfParseRule * mReaderPtr
Definition WsfParser.hpp:272
Definition WsfParseIndex.hpp:40
To improve efficiency, WsfParseNodePool handles the allocation and deletion of WsfParseNode's.
Definition WsfParseNode.hpp:204
Definition WsfParseNode.hpp:61
Definition WsfParseRule.hpp:131
Definition WsfParseSourceInclude.hpp:31
Interface providing source files to the WsfParser.
Definition WsfParseSourceProvider.hpp:24
void SetFullyExpandNodes(bool aValue)
Definition WsfParser.hpp:149
void ClearPartialMatches()
Definition WsfParser.hpp:77
WsfParseSourceInclude * GetCurrentInclude()
Definition WsfParser.hpp:79
void PushSequence(WsfParseNode *aNodePtr)
Definition WsfParser.hpp:109
const std::vector< WsfParseError * > & GetErrors() const
Definition WsfParser.hpp:104
void FreeNode(WsfParseNode *aNodePtr)
Definition WsfParser.hpp:138
void DeleteAllNodes()
Node pool functions.
Definition WsfParser.hpp:126
WsfParseSourceProvider * GetSourceProvider()
Definition WsfParser.hpp:106
WsfParseNode * GetCurrentSequence()
Definition WsfParser.hpp:108
WsfParseNode * NewNode()
Definition WsfParser.hpp:136
void PushTerminator(const char *aTerminatorString)
Definition WsfParser.hpp:91
void PartialMatch(UtTextDocumentRange aRange)
Definition WsfParser.hpp:74
void AddAuxiliaryValue(WsfParseAuxData *aDataPtr)
Definition WsfParser.hpp:144
void PopTerminator()
Definition WsfParser.hpp:96
bool FullyExpandNodes() const
Definition WsfParser.hpp:148
void SetWorkingDirectory(const UtPath &aWorkingDirectory)
Definition WsfParser.hpp:102
void SetIgnoreIncludes(bool aIngoreIncludes)
Definition WsfParser.hpp:150
WsfParseDefinitions * GetDefinitions()
Definition WsfParser.hpp:83
void SwapPool(WsfParser &aRhs)
Definition WsfParser.hpp:128
WsfParser & operator=(const WsfParser &)=delete
void PushDeferredSource(const UtPath &aFilePath)
Definition WsfParser.cpp:719
bool IsDelayLoading()
Definition WsfParser.hpp:122
void SwapPool(WsfParseNodePool &aPool)
Definition WsfParser.hpp:129
UtTextDocument * PushSource(const std::string &aFilePath, bool aForceInclude, UtTextDocumentRange *aIncludeLocation=nullptr, bool aSearchIncludePaths=true)
Definition WsfParser.cpp:265
const UtPath & GetWorkingDirectory() const
Definition WsfParser.hpp:103
UtTextDocument * FindSource(const std::string &aFilePath, bool aSearchIncludePaths=true, bool aReadFile=true)
Definition WsfParser.cpp:209
size_t GetWordsRead() const
Returns the number of words that have been read since the last call to Reset().
Definition WsfParser.hpp:57
void PopSequence()
Definition WsfParser.hpp:110
WsfParser(WsfParseSourceProvider *aSourceProviderPtr, const UtPath &aWorkingDirectory, WsfParseDefinitions *aDefinitionsPtr=nullptr)
Definition WsfParser.cpp:37
WsfParseNode * NewNode(WsfParseRule *aRule, const char *aType, UtTextDocumentRange aValue=UtTextDocumentRange())
Definition WsfParser.hpp:131
WsfParseIndex *& GetParseIndex()
Definition WsfParser.hpp:82
WsfParseNode * ParseFiles(const std::vector< UtPath > &aFileList, int aMaxErrors)
Definition WsfParser.cpp:813
void AddError(WsfParseError *aErrorPtr)
Definition WsfParser.hpp:75
Copyrights Multiple, All Rights Reserved