1 #ifndef __DALI_JSON_PARSE_STATE_H__
2 #define __DALI_JSON_PARSE_STATE_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
20 #include <dali/public-api/common/dali-common.h>
22 #include <dali-toolkit/public-api/builder/tree-node.h>
24 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
26 namespace Dali DALI_IMPORT_API
32 namespace Internal DALI_INTERNAL
36 * A safer std::advance()
38 template <typename IteratorType,typename EndIteratorType>
39 inline int AdvanceIter(IteratorType& iter, EndIteratorType& end, int n)
41 for(int i =0; i < n; ++i)
53 * Maintains parser state machine
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)
63 * @param tree Tree to start with, pass NULL if no existing tree
65 explicit JsonParserState(TreeNode* tree);
69 * The source is modified in place
70 * @param source The vector buffer to parse
71 * @return true if parsed successfully
73 bool ParseJson(VectorChar& source);
77 * @return The root TreeNode
82 * Get the error description of the last parse
83 * @return The error description or NULL if no error
85 const char* GetErrorDescription() { return mErrorDescription; }
88 * Get the error line number
89 * @return The line number of the error
91 int GetErrorLineNumber() { return mErrorNewLine; }
94 * Get the error column
95 * @return The error column
97 int GetErrorColumn() { return mErrorColumn; }
100 * Get the error position
101 * @return The error position
103 int GetErrorPosition() { return mErrorPosition; }
106 * Get the size of the string data that has been parsed
107 * @return The size of string data
109 int GetParsedStringSize() { return mNumberOfParsedChars; };
112 * Get the number of nodes created
113 * @return The number of nodes
115 int GetCreatedNodeCount() { return mNumberOfCreatedNodes; };
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
132 * The current parse state
145 // inhibited copy construct and assignment
146 JsonParserState(const JsonParserState&);
147 const JsonParserState& operator=(const JsonParserState&);
150 * Parse over white space
151 * Increments the current position
152 * @return true if no parse errors
154 bool ParseWhiteSpace();
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
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
168 bool ParseSymbol(const std::string& symbol);
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
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
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
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
197 char* EncodeString();
200 * Create a new node with name and type
202 TreeNode* CreateNewNode(const char* name, TreeNode::NodeType type);
205 * Create a new node if first parse, else check if the node already
206 * exists and set it to a new type
208 TreeNode* NewNode(const char* name, TreeNode::NodeType type);
211 * Set error meta data
212 * @returns always false.
214 bool Error(const char* description);
217 * Reset state for another parse
222 * Set current to its parent
223 * @return true if we had a parent, false and error otherwise
225 inline bool UpToParent()
227 if(NULL == mCurrent.GetParent())
229 return Error("Attempt to walk up above root");
231 mCurrent = TreeNodeManipulator( mCurrent.GetParent() );
236 * Get the current character
244 * @return True if there are at least n character left
246 inline bool AtLeast(int n)
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;
255 * @return True if at the end of the data to parse
259 return mEnd == mIter;
263 * Advance current position by n characters or stop at mEnd
265 inline void Advance(int n)
267 int c = AdvanceIter(mIter, mEnd, n);
273 * Advance by n charaters and return true if we reached the end
275 inline bool AdvanceEnded(int n)
277 int c = AdvanceIter(mIter, mEnd, n);
280 return mEnd == mIter;
284 * Advance by at least n characters (stopping at mEnd) and skip any whitespace after n.
286 inline void AdvanceSkipWhiteSpace(int n)
288 int c = AdvanceIter(mIter, mEnd, n);
291 static_cast<void>( ParseWhiteSpace() );
295 * Increment new line counters
297 inline void NewLine()
306 } // namespace Internal
308 } // namespace Toolkit