WSF
WsfParseGrammar.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 WSFPARSEGRAMMAR_HPP
16#define WSFPARSEGRAMMAR_HPP
17
18#include "wsf_parser_export.h"
19
20#include "UtCast.hpp"
21#include "UtReferenceCount.hpp"
22#include "UtTextDocument.hpp"
23class WsfParser;
24class WsfParseNode;
25#include <string>
26#include <vector>
27
28// Implements copy-on-write
29template<class T>
31{
32public:
36 : mObjectPtr(nullptr)
37 , mCountPtr(nullptr)
38 {
39 }
40
43 explicit UtSharablePointer(T* aObjectPtr)
44 : mObjectPtr(aObjectPtr)
45 , mCountPtr(nullptr)
46 {
47 if (mObjectPtr != nullptr)
48 {
49 mCountPtr = new UtReferenceCount;
50 mCountPtr->AddStrongRef();
51 }
52 }
53
56 : mObjectPtr(aSrc.mObjectPtr)
57 , mCountPtr(aSrc.mCountPtr)
58 {
59 if (mCountPtr != nullptr)
60 {
61 mCountPtr->AddStrongRef();
62 }
63 }
64
67 ~UtSharablePointer() { ReleaseReference(); }
68
75 {
76 if (this != &aRhs)
77 {
78 ReleaseReference();
79 mObjectPtr = aRhs.mObjectPtr;
80 mCountPtr = aRhs.mCountPtr;
81 // Increment reference count if this is a non-null reference
82 if (mCountPtr != nullptr)
83 {
84 mCountPtr->AddStrongRef();
85 }
86 }
87 return *this;
88 }
89
91 bool IsNull() const { return mObjectPtr == nullptr; }
92
94 bool operator==(const UtSharablePointer<T>& aRHS) const { return mCountPtr == aRHS.mCountPtr; }
95
97 bool operator!=(const UtSharablePointer<T>& aRHS) const { return mCountPtr != aRHS.mCountPtr; }
98
100 bool operator<(const UtSharablePointer<T>& aRHS) const { return mCountPtr < aRHS.mCountPtr; }
101
102 const T* Read()
103 {
104 if (mCountPtr && mCountPtr->IsValid())
105 {
106 return mObjectPtr;
107 }
108 return nullptr;
109 }
110 T* Write()
111 {
112 if (mCountPtr && mCountPtr->IsValid())
113 {
114 if (mCountPtr->GetStrongCount() == 1)
115 {
116 return mObjectPtr;
117 }
118 else
119 {
120 mObjectPtr = new T(*mObjectPtr);
121 mCountPtr = new UtReferenceCount(1);
122 }
123 }
124 return nullptr;
125 }
126
128 {
129 std::swap(mObjectPtr, aRhs.mObjectPtr);
130 std::swap(mCountPtr, aRhs.mCountPtr);
131 }
132
133private:
136 void ReleaseReference()
137 {
138 if (mCountPtr != nullptr)
139 {
140 if (mCountPtr->RemoveStrongRef())
141 {
142 delete mObjectPtr;
143 mObjectPtr = nullptr;
144 }
145 mCountPtr = nullptr;
146 }
147 }
148
149 T* mObjectPtr;
150 UtReferenceCount* mCountPtr;
151};
152
153namespace std
154{
155template<typename T>
157{
158 lhs.Swap(rhs);
159}
160} // namespace std
161
162namespace WsfGrammar
163{
164
165// The 'M' namespace defines a very simple structure to hold the result of parsing the grammar
166// Once all the data is read, it is processed further.
167namespace M
168{
169
181
188
189// Represents an element of the grammar file
190// Expression:
191// (<type> <words>*)
192// Sequence:
193// { <words> }
194// Alternate
195// sequence | sequence | sequence ( each sequence is a word())
196// Literal:
197// "text" or text
198// Action:
199// [word;word;...]
200
201class WSF_PARSER_EXPORT Expr
202{
203public:
206 , mFlags(0)
207 {
208 }
209 ~Expr() = default;
210 Expr(const Expr& aSrc);
211 Expr& operator=(const Expr& aRhs);
212 static Expr Literal(const std::string& aText)
213 {
214 Expr lit;
215 lit.SetLiteral(aText);
216 return lit;
217 }
218
219 void Clear()
220 {
221 if (!mWords.IsNull())
222 {
223 mWords.Write()->clear();
224 }
225 }
226 void SetLiteral(const std::string& aLiteral)
227 {
228 mText = aLiteral;
230 }
231
232 void Swap(Expr& aRhs);
233 // adds a subexpression efficiently by taking ownership of the input
234 void PushMove(Expr& aExp)
235 {
236 NeedWords();
237 ExprList* wordList = mWords.Write();
238 wordList->push_back(Expr());
239 aExp.Swap(wordList->back());
240 }
241 Expr& operator+=(const Expr& aExp)
242 {
243 NeedWords();
244 mWords.Write()->push_back(aExp);
245 return *this;
246 }
247 size_t WordCount() const { return mWords.IsNull() ? 0 : mWords.Read()->size(); }
248 const Expr& GetWord(size_t aIndex) const;
249 Expr& GetWord(size_t aIndex);
250
251 std::vector<Expr>& words()
252 {
253 NeedWords();
254 return *mWords.Write();
255 }
256 const std::vector<Expr>& words() const
257 {
258 NeedWords();
259 return *mWords.Read();
260 }
262 // Expression type
263 std::string mType;
264 // parameter name if this is a named parameter
265 std::string mLabel;
266 // Used for single-word expressions / aka literals
267 // Used for actions -- name of function called
268 std::string mText;
269
270private:
271 void NeedWords() const;
272 using ExprList = std::vector<Expr>;
273 mutable UtSharablePointer<ExprList> mWords;
274
275public:
277};
278
279
280} // namespace M
281} // namespace WsfGrammar
282
283#endif
Definition WsfParseGrammar.hpp:31
UtSharablePointer< T > & operator=(const UtSharablePointer< T > &aRhs)
Definition WsfParseGrammar.hpp:74
~UtSharablePointer()
Definition WsfParseGrammar.hpp:67
UtSharablePointer()
Definition WsfParseGrammar.hpp:35
void Swap(UtSharablePointer< T > &aRhs)
Definition WsfParseGrammar.hpp:127
bool operator!=(const UtSharablePointer< T > &aRHS) const
Returns false if both point to the same object.
Definition WsfParseGrammar.hpp:97
const T * Read()
Definition WsfParseGrammar.hpp:102
T * Write()
Definition WsfParseGrammar.hpp:110
bool operator<(const UtSharablePointer< T > &aRHS) const
Provides strict weak ordering based on what the UtSharedPtr is referencing.
Definition WsfParseGrammar.hpp:100
UtSharablePointer(const UtSharablePointer< T > &aSrc)
Create a copy of shared object pointer.
Definition WsfParseGrammar.hpp:55
bool operator==(const UtSharablePointer< T > &aRHS) const
Returns true if both point to the same object.
Definition WsfParseGrammar.hpp:94
UtSharablePointer(T *aObjectPtr)
Definition WsfParseGrammar.hpp:43
bool IsNull() const
Return 'true' if the pointer to the object is null.
Definition WsfParseGrammar.hpp:91
Definition WsfParseGrammar.hpp:202
void Clear()
Definition WsfParseGrammar.hpp:219
Expr & operator=(const Expr &aRhs)
Definition WsfParseGrammar.cpp:31
std::vector< Expr > & words()
Definition WsfParseGrammar.hpp:251
void Swap(Expr &aRhs)
Definition WsfParseGrammar.cpp:21
std::string mLabel
Definition WsfParseGrammar.hpp:265
int mFlags
Definition WsfParseGrammar.hpp:276
std::string mType
Definition WsfParseGrammar.hpp:263
Expr & operator+=(const Expr &aExp)
Definition WsfParseGrammar.hpp:241
size_t WordCount() const
Definition WsfParseGrammar.hpp:247
static Expr Literal(const std::string &aText)
Definition WsfParseGrammar.hpp:212
Expr()
Definition WsfParseGrammar.hpp:204
void PushMove(Expr &aExp)
Definition WsfParseGrammar.hpp:234
const std::vector< Expr > & words() const
Definition WsfParseGrammar.hpp:256
std::string mText
Definition WsfParseGrammar.hpp:268
void SetLiteral(const std::string &aLiteral)
Definition WsfParseGrammar.hpp:226
RuleType mRuleType
Definition WsfParseGrammar.hpp:261
Definition WsfParseNode.hpp:61
Definition WsfParser.hpp:36
Definition WsfParseGrammar.cpp:20
RuleType
Definition WsfParseGrammar.hpp:171
@ cRT_ACTION_CALL
Definition WsfParseGrammar.hpp:179
@ cRT_ALTERNATE
Definition WsfParseGrammar.hpp:173
@ cRT_LITERAL
Definition WsfParseGrammar.hpp:176
@ cRT_SEQUENCE
Definition WsfParseGrammar.hpp:172
@ cRT_ROOT
Definition WsfParseGrammar.hpp:177
@ cRT_ACTION
Definition WsfParseGrammar.hpp:178
@ cRT_EXPRESSION
Definition WsfParseGrammar.hpp:175
Flags
Definition WsfParseGrammar.hpp:183
@ cRF_RECURRENCE_QUESTION
Definition WsfParseGrammar.hpp:186
@ cRF_RECURRENCE_PLUS
Definition WsfParseGrammar.hpp:185
@ cRF_RECURRENCE_STAR
Definition WsfParseGrammar.hpp:184
Definition WsfParseGrammar.cpp:18
Template specialization for wsf::comm::Address. Allows usage of Address in std::unordered_map/set.
Definition WsfCommAddress.hpp:185
void swap(WsfRoute &aLhs, WsfRoute &aRhs)
Definition WsfRoute.hpp:294
Copyrights Multiple, All Rights Reserved