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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/common/dali-common.h>
25 #include <dali-toolkit/devel-api/builder/tree-node.h>
27 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
39 * A safer std::advance()
41 template <typename IteratorType,typename EndIteratorType>
42 inline int AdvanceIter(IteratorType& iter, EndIteratorType& end, int n)
44 for(int i =0; i < n; ++i)
56 * Maintains parser state machine
58 * If a NULL root node is passed in the constructor then a faster non merging parse is performed (the first pass).
59 * Otherwise the json tree is merged (and requires slower searching)
66 * @param tree Tree to start with, pass NULL if no existing tree
68 explicit JsonParserState(TreeNode* tree);
72 * The source is modified in place
73 * @param source The vector buffer to parse
74 * @return true if parsed successfully
76 bool ParseJson(VectorChar& source);
80 * @return The root TreeNode
85 * Get the error description of the last parse
86 * @return The error description or NULL if no error
88 const char* GetErrorDescription() { return mErrorDescription; }
91 * Get the error line number
92 * @return The line number of the error
94 int GetErrorLineNumber() { return mErrorNewLine; }
97 * Get the error column
98 * @return The error column
100 int GetErrorColumn() { return mErrorColumn; }
103 * Get the error position
104 * @return The error position
106 int GetErrorPosition() { return mErrorPosition; }
109 * Get the size of the string data that has been parsed
110 * @return The size of string data
112 int GetParsedStringSize() { return mNumberOfParsedChars; };
115 * Get the number of nodes created
116 * @return The number of nodes
118 int GetCreatedNodeCount() { return mNumberOfCreatedNodes; };
121 VectorCharIter mIter; ///< Current position
122 VectorCharIter mStart; ///< Start position
123 VectorCharIter mEnd; ///< End of buffer being parsed
124 TreeNode* mRoot; ///< Root node created
125 TreeNodeManipulator mCurrent; ///< The Current modifiable node
126 const char* mErrorDescription; ///< The error description if set
127 int mErrorNewLine; ///< The error line number
128 int mErrorColumn; ///< The error column
129 int mErrorPosition; ///< The error position
130 int mNumberOfParsedChars; ///< The size of string data
131 int mNumberOfCreatedNodes; ///< The number of nodes created
132 bool mFirstParse; ///< Flag if first parse
135 * The current parse state
148 // inhibited copy construct and assignment
149 JsonParserState(const JsonParserState&);
150 const JsonParserState& operator=(const JsonParserState&);
153 * Parse over white space
154 * Increments the current position
155 * @return true if no parse errors
157 bool ParseWhiteSpace();
160 * Parse over a number, setting the current node if found
161 * Increments the current position. Sets error data if parse error.
162 * @return true if found, false if parse error
167 * Parse over a symbol
168 * Increments the current position. Sets error data if parse error.
169 * @return true if found, false if parse error
171 bool ParseSymbol(const std::string& symbol);
174 * Parse over 'true' symbol, setting the current node if found
175 * Increments the current position. Sets error data if parse error.
176 * @return true if found, false if parse error
181 * Parse over 'false' symbol, setting the current node if found
182 * Increments the current position. Sets error data if parse error.
183 * @return true if found, false if parse error
188 * Parse over 'null' symbol, setting the current node if found
189 * Increments the current position. Sets error data if parse error.
190 * @return true if found, false if parse error
195 * Parse over a string from the current position and insert escaped
196 * control characters in place in the string and a null terminator.
197 * This function works from and modifes the current buffer position.
198 * @return the start of the null terminated string
200 char* EncodeString();
203 * Create a new node with name and type
205 TreeNode* CreateNewNode(const char* name, TreeNode::NodeType type);
208 * Create a new node if first parse, else check if the node already
209 * exists and set it to a new type
211 TreeNode* NewNode(const char* name, TreeNode::NodeType type);
214 * Set error meta data
215 * @returns always false.
217 bool Error(const char* description);
220 * Reset state for another parse
225 * Set current to its parent
226 * @return true if we had a parent, false and error otherwise
228 inline bool UpToParent()
230 if(NULL == mCurrent.GetParent())
232 return Error("Attempt to walk up above root");
234 mCurrent = TreeNodeManipulator( mCurrent.GetParent() );
239 * Get the current character
247 * @return True if there are at least n character left
249 inline bool AtLeast(int n)
251 // The standard suggests vector.end() can be decremented as
252 // iter v.back() { *--v.end() }
253 // (ISO/IEC 14882:2003 C++ Standard 23.1.1/12 – Sequences)
254 return (mEnd - mIter) > n;
258 * @return True if at the end of the data to parse
262 return mEnd == mIter;
266 * Advance current position by n characters or stop at mEnd
268 inline void Advance(int n)
270 int c = AdvanceIter(mIter, mEnd, n);
276 * Advance by n charaters and return true if we reached the end
278 inline bool AdvanceEnded(int n)
280 int c = AdvanceIter(mIter, mEnd, n);
283 return mEnd == mIter;
287 * Advance by at least n characters (stopping at mEnd) and skip any whitespace after n.
289 inline void AdvanceSkipWhiteSpace(int n)
291 int c = AdvanceIter(mIter, mEnd, n);
294 static_cast<void>( ParseWhiteSpace() );
298 * Increment new line counters
300 inline void NewLine()
309 } // namespace Internal
311 } // namespace Toolkit