Plasma Engine  2.0
Loading...
Searching...
No Matches
OpenDdlParser.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Containers/DynamicArray.h>
5#include <Foundation/Containers/HybridArray.h>
6#include <Foundation/IO/Stream.h>
7
9
11enum class plOpenDdlPrimitiveType
12{
13 Bool,
14 Int8,
15 Int16,
16 Int32,
17 Int64,
18 UInt8,
19 UInt16,
20 UInt32,
21 UInt64,
22 // Half, // Currently not supported
23 Float,
24 Double,
25 String,
26 // Ref, // Currently not supported
27 // Type // Currently not supported
28 Custom
29};
30
34class PL_FOUNDATION_DLL plOpenDdlParser
35{
36public:
38 virtual ~plOpenDdlParser() = default;
39
41 bool HadFatalParsingError() const { return m_bHadFatalParsingError; } // [tested]
42
43protected:
45 void SetLogInterface(plLogInterface* pLog) { m_pLogInterface = pLog; }
46
52 void SetCacheSize(plUInt32 uiSizeInKB);
53
55 void SetInputStream(plStreamReader& stream, plUInt32 uiFirstLineOffset = 0); // [tested]
56
60 bool ContinueParsing(); // [tested]
61
63 plResult ParseAll(); // [tested]
64
66 void SkipRestOfObject();
67
69 void StopParsing();
70
72 void ParsingError(plStringView sMessage, bool bFatal);
73
74 plLogInterface* m_pLogInterface;
75
76protected:
83 virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn) {}
84
86 virtual void OnBeginObject(plStringView sType, plStringView sName, bool bGlobalName) = 0;
87
89 virtual void OnEndObject() = 0;
90
92 virtual void OnBeginPrimitiveList(plOpenDdlPrimitiveType type, plStringView sName, bool bGlobalName) = 0;
93
95 virtual void OnEndPrimitiveList() = 0;
96
98 // virtual void OnBeginPrimitiveArrayList(plOpenDdlPrimitiveType type, plUInt32 uiGroupSize) = 0;
99 // virtual void OnEndPrimitiveArrayList() = 0;
100
102 virtual void OnPrimitiveBool(plUInt32 count, const bool* pData, bool bThisIsAll) = 0;
103
105 virtual void OnPrimitiveInt8(plUInt32 count, const plInt8* pData, bool bThisIsAll) = 0;
107 virtual void OnPrimitiveInt16(plUInt32 count, const plInt16* pData, bool bThisIsAll) = 0;
109 virtual void OnPrimitiveInt32(plUInt32 count, const plInt32* pData, bool bThisIsAll) = 0;
111 virtual void OnPrimitiveInt64(plUInt32 count, const plInt64* pData, bool bThisIsAll) = 0;
112
114 virtual void OnPrimitiveUInt8(plUInt32 count, const plUInt8* pData, bool bThisIsAll) = 0;
116 virtual void OnPrimitiveUInt16(plUInt32 count, const plUInt16* pData, bool bThisIsAll) = 0;
118 virtual void OnPrimitiveUInt32(plUInt32 count, const plUInt32* pData, bool bThisIsAll) = 0;
120 virtual void OnPrimitiveUInt64(plUInt32 count, const plUInt64* pData, bool bThisIsAll) = 0;
121
123 virtual void OnPrimitiveFloat(plUInt32 count, const float* pData, bool bThisIsAll) = 0;
125 virtual void OnPrimitiveDouble(plUInt32 count, const double* pData, bool bThisIsAll) = 0;
126
128 virtual void OnPrimitiveString(plUInt32 count, const plStringView* pData, bool bThisIsAll) = 0;
129
130private:
131 enum State
132 {
133 Finished,
134 Idle,
135 ReadingBool,
136 ReadingInt8,
137 ReadingInt16,
138 ReadingInt32,
139 ReadingInt64,
140 ReadingUInt8,
141 ReadingUInt16,
142 ReadingUInt32,
143 ReadingUInt64,
144 ReadingFloat,
145 ReadingDouble,
146 ReadingString,
147 };
148
149 struct DdlState
150 {
151 DdlState()
152 : m_State(Idle)
153 {
154 }
155 DdlState(State s)
156 : m_State(s)
157 {
158 }
159
160 State m_State;
161 };
162
163 void ReadNextByte();
164 bool ReadCharacter();
165 bool ReadCharacterSkipComments();
166 void SkipWhitespace();
167 void ContinueIdle();
168 void ReadIdentifier(plUInt8* szString, plUInt32& count);
169 void ReadString();
170 void ReadWord();
171 plUInt64 ReadDecimalLiteral();
172 void PurgeCachedPrimitives(bool bThisIsAll);
173 bool ContinuePrimitiveList();
174 void ContinueString();
175 void SkipString();
176 void ContinueBool();
177 void ContinueInt();
178 void ContinueFloat();
179
180 void ReadDecimalFloat();
181 void ReadHexString();
182
183 plHybridArray<DdlState, 32> m_StateStack;
184 plStreamReader* m_pInput;
186
187 static constexpr plUInt32 s_uiMaxIdentifierLength = 64;
188
189 plUInt8 m_uiCurByte;
190 plUInt8 m_uiNextByte;
191 plUInt32 m_uiCurLine;
192 plUInt32 m_uiCurColumn;
193 bool m_bSkippingMode;
194 bool m_bHadFatalParsingError;
195 plUInt8 m_szIdentifierType[s_uiMaxIdentifierLength];
196 plUInt8 m_szIdentifierName[s_uiMaxIdentifierLength];
197 plDynamicArray<plUInt8> m_TempString;
198 plUInt32 m_uiTempStringLength;
199
200 plUInt32 m_uiNumCachedPrimitives;
201 bool* m_pBoolCache;
202 plInt8* m_pInt8Cache;
203 plInt16* m_pInt16Cache;
204 plInt32* m_pInt32Cache;
205 plInt64* m_pInt64Cache;
206 plUInt8* m_pUInt8Cache;
207 plUInt16* m_pUInt16Cache;
208 plUInt32* m_pUInt32Cache;
209 plUInt64* m_pUInt64Cache;
210 float* m_pFloatCache;
211 double* m_pDoubleCache;
212};
Definition DynamicArray.h:81
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Base class for all logging classes.
Definition Log.h:77
A low level parser for the OpenDDL format. It can incrementally parse the structure,...
Definition OpenDdlParser.h:35
virtual void OnPrimitiveUInt16(plUInt32 count, const plUInt16 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveDouble(plUInt32 count, const double *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnEndPrimitiveList()=0
Called when the end of a primitive object is encountered.
void SetLogInterface(plLogInterface *pLog)
Sets an plLogInterface through which errors and warnings are reported.
Definition OpenDdlParser.h:45
virtual void OnPrimitiveInt32(plUInt32 count, const plInt32 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn)
Called when something unexpected is encountered in the document.
Definition OpenDdlParser.h:83
virtual void OnBeginPrimitiveList(plOpenDdlPrimitiveType type, plStringView sName, bool bGlobalName)=0
Called when a new primitive object is encountered.
virtual void OnPrimitiveUInt32(plUInt32 count, const plUInt32 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveInt16(plUInt32 count, const plInt16 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveBool(plUInt32 count, const bool *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveInt64(plUInt32 count, const plInt64 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveFloat(plUInt32 count, const float *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveUInt8(plUInt32 count, const plUInt8 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveUInt64(plUInt32 count, const plUInt64 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnEndObject()=0
Called when the end of an object is encountered.
virtual void OnBeginObject(plStringView sType, plStringView sName, bool bGlobalName)=0
Called when a new object is encountered.
virtual void OnPrimitiveString(plUInt32 count, const plStringView *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
virtual void OnPrimitiveInt8(plUInt32 count, const plInt8 *pData, bool bThisIsAll)=0
Called when data for a primitive type is available. More than one value may be reported at a time.
bool HadFatalParsingError() const
Whether an error occurred during parsing that resulted in cancellation of further parsing.
Definition OpenDdlParser.h:41
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54