Revert "[SRUK] (StyleManager) Create a style manager"
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / builder / json-parser-state.h
1 #ifndef __DALI_JSON_PARSE_STATE_H__
2 #define __DALI_JSON_PARSE_STATE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 #include <dali/public-api/common/dali-common.h>
21
22 #include <dali-toolkit/public-api/builder/tree-node.h>
23
24 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
25
26 namespace Dali DALI_IMPORT_API
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal DALI_INTERNAL
33 {
34
35 /*
36  * A safer std::advance()
37  */
38 template <typename IteratorType,typename EndIteratorType>
39 inline int AdvanceIter(IteratorType& iter, EndIteratorType& end, int n)
40 {
41   for(int i =0; i < n; ++i)
42   {
43     if(iter == end)
44     {
45       return n - i;
46     }
47     ++iter;
48   }
49   return n;
50 }
51
52 /*
53  * Maintains parser state machine
54  *
55  * If a NULL root node is passed in the constructor then a faster non merging parse is performed (the first pass).
56  * Otherwise the json tree is merged (and requires slower searching)
57  */
58 class JsonParserState
59 {
60 public:
61   /*
62    * Constructor
63    * @param tree Tree to start with, pass NULL if no existing tree
64    */
65   explicit JsonParserState(TreeNode* tree);
66
67   /*
68    * Parse json source
69    * The source is modified in place
70    * @param source The vector buffer to parse
71    * @return true if parsed successfully
72    */
73   bool ParseJson(VectorChar& source);
74
75   /*
76    * Get the root node
77    * @return The root TreeNode
78    */
79   TreeNode* GetRoot();
80
81   /*
82    * Get the error description of the last parse
83    * @return The error description or NULL if no error
84    */
85   const char* GetErrorDescription() { return mErrorDescription; }
86
87   /*
88    * Get the error line number
89    * @return The line number of the error
90    */
91   int GetErrorLineNumber() { return mErrorNewLine; }
92
93   /*
94    * Get the error column
95    * @return The error column
96    */
97   int GetErrorColumn() { return mErrorColumn; }
98
99   /*
100    * Get the error position
101    * @return The error position
102    */
103   int GetErrorPosition() { return mErrorPosition; }
104
105   /*
106    * Get the size of the string data that has been parsed
107    * @return The size of string data
108    */
109   int GetParsedStringSize() { return mNumberOfParsedChars; };
110
111   /*
112    * Get the number of nodes created
113    * @return The number of nodes
114    */
115   int GetCreatedNodeCount() { return mNumberOfCreatedNodes; };
116
117 private:
118   VectorCharIter mIter;                ///< Current position
119   VectorCharIter mStart;               ///< Start position
120   VectorCharIter mEnd;                 ///< End of buffer being parsed
121   TreeNode* mRoot;                     ///< Root node created
122   TreeNodeManipulator mCurrent;        ///< The Current modifiable node
123   const char* mErrorDescription;       ///< The error description if set
124   int mErrorNewLine;                   ///< The error line number
125   int mErrorColumn;                    ///< The error column
126   int mErrorPosition;                  ///< The error position
127   int mNumberOfParsedChars;            ///< The size of string data
128   int mNumberOfCreatedNodes;           ///< The number of nodes created
129   bool mFirstParse;                    ///< Flag if first parse
130
131   /*
132    * The current parse state
133    */
134   enum State
135   {
136     STATE_START,
137     STATE_OBJECT,
138     STATE_KEY,
139     STATE_VALUE,
140     STATE_END,
141   };
142
143   State mState;
144
145   // inhibited copy construct and assignment
146   JsonParserState(const JsonParserState&);
147   const JsonParserState& operator=(const JsonParserState&);
148
149   /*
150    * Parse over white space
151    * Increments the current position
152    * @return true if no parse errors
153    */
154   bool ParseWhiteSpace();
155
156   /*
157    * Parse over a number, setting the current node if found
158    * Increments the current position. Sets error data if parse error.
159    * @return true if found, false if parse error
160    */
161   bool ParseNumber();
162
163   /*
164    * Parse over a symbol
165    * Increments the current position. Sets error data if parse error.
166    * @return true if found, false if parse error
167    */
168   bool ParseSymbol(const std::string& symbol);
169
170   /*
171    * Parse over 'true' symbol, setting the current node if found
172    * Increments the current position. Sets error data if parse error.
173    * @return true if found, false if parse error
174    */
175   bool ParseTrue();
176
177   /*
178    * Parse over 'false' symbol, setting the current node if found
179    * Increments the current position. Sets error data if parse error.
180    * @return true if found, false if parse error
181    */
182   bool ParseFalse();
183
184   /*
185    * Parse over 'null' symbol, setting the current node if found
186    * Increments the current position. Sets error data if parse error.
187    * @return true if found, false if parse error
188    */
189   bool ParseNULL();
190
191   /*
192    * Parse over a string from the current position and insert escaped
193    * control characters in place in the string and a null terminator.
194    * This function works from and modifes the current buffer position.
195    * @return the start of the null terminated string
196    */
197   char* EncodeString();
198
199   /*
200    * Create a new node with name and type
201    */
202   TreeNode* CreateNewNode(const char* name, TreeNode::NodeType type);
203
204   /*
205    * Create a new node if first parse, else check if the node already
206    * exists and set it to a new type
207    */
208   TreeNode* NewNode(const char* name, TreeNode::NodeType type);
209
210   /*
211    * Set error meta data
212    * @returns always false.
213    */
214   bool Error(const char* description);
215
216   /*
217    * Reset state for another parse
218    */
219   void Reset();
220
221   /*
222    * Set current to its parent
223    * @return true if we had a parent, false and error otherwise
224    */
225   inline bool UpToParent()
226   {
227     if(NULL == mCurrent.GetParent())
228     {
229       return Error("Attempt to walk up above root");
230     }
231     mCurrent = TreeNodeManipulator( mCurrent.GetParent() );
232     return true;
233   }
234
235   /*
236    * Get the current character
237    */
238   inline char Char()
239   {
240     return *mIter;
241   }
242
243   /*
244    * @return True if there are at least n character left
245    */
246   inline bool AtLeast(int n)
247   {
248     // The standard suggests vector.end() can be decremented as
249     //   iter v.back() { *--v.end() }
250     // (ISO/IEC 14882:2003 C++ Standard 23.1.1/12 – Sequences)
251     return (mEnd - mIter) > n;
252   }
253
254   /*
255    * @return True if at the end of the data to parse
256    */
257   inline bool AtEnd()
258   {
259     return mEnd == mIter;
260   }
261
262   /*
263    * Advance current position by n characters or stop at mEnd
264    */
265   inline void Advance(int n)
266   {
267     int c = AdvanceIter(mIter, mEnd, n);
268     mErrorPosition += c;
269     mErrorColumn   += c;
270   }
271
272   /*
273    * Advance by n charaters and return true if we reached the end
274    */
275   inline bool AdvanceEnded(int n)
276   {
277     int c = AdvanceIter(mIter, mEnd, n);
278     mErrorPosition += c;
279     mErrorColumn   += c;
280     return mEnd == mIter;
281   }
282
283   /*
284    * Advance by at least n characters (stopping at mEnd) and skip any whitespace after n.
285    */
286   inline void AdvanceSkipWhiteSpace(int n)
287   {
288     int c = AdvanceIter(mIter, mEnd, n);
289     mErrorPosition += c;
290     mErrorColumn   += c;
291     static_cast<void>( ParseWhiteSpace() );
292   }
293
294   /*
295    * Increment new line counters
296    */
297   inline void NewLine()
298   {
299     ++mErrorNewLine;
300     mErrorColumn = 0;
301   }
302
303 };
304
305
306 } // namespace Internal
307
308 } // namespace Toolkit
309
310 } // namespace Dali
311
312
313 #endif // header