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.
21 #include <dali/public-api/common/dali-common.h>
23 #include <dali-toolkit/public-api/builder/tree-node.h>
25 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
27 namespace Dali DALI_IMPORT_API
33 namespace Internal DALI_INTERNAL
37 * A safer std::advance()
39 template <typename IteratorType,typename EndIteratorType>
40 inline int AdvanceIter(IteratorType& iter, EndIteratorType& end, int n)
42 for(int i =0; i < n; ++i)
54 * Maintains parser state machine
56 * If a NULL root node is passed in the constructor then a faster non merging parse is performed (the first pass).
57 * Otherwise the json tree is merged (and requires slower searching)
64 * @param tree Tree to start with, pass NULL if no existing tree
66 explicit JsonParserState(TreeNode* tree);
70 * The source is modified in place
71 * @param source The vector buffer to parse
72 * @return true if parsed successfully
74 bool ParseJson(VectorChar& source);
78 * @return The root TreeNode
83 * Get the error description of the last parse
84 * @return The error description or NULL if no error
86 const char* GetErrorDescription() { return mErrorDescription; }
89 * Get the error line number
90 * @return The line number of the error
92 int GetErrorLineNumber() { return mErrorNewLine; }
95 * Get the error column
96 * @return The error column
98 int GetErrorColumn() { return mErrorColumn; }
101 * Get the error position
102 * @return The error position
104 int GetErrorPosition() { return mErrorPosition; }
107 * Get the size of the string data that has been parsed
108 * @return The size of string data
110 int GetParsedStringSize() { return mNumberOfParsedChars; };
113 * Get the number of nodes created
114 * @return The number of nodes
116 int GetCreatedNodeCount() { return mNumberOfCreatedNodes; };
119 VectorCharIter mIter; ///< Current position
120 VectorCharIter mStart; ///< Start position
121 VectorCharIter mEnd; ///< End of buffer being parsed
122 TreeNode* mRoot; ///< Root node created
123 TreeNodeManipulator mCurrent; ///< The Current modifiable node
124 const char* mErrorDescription; ///< The error description if set
125 int mErrorNewLine; ///< The error line number
126 int mErrorColumn; ///< The error column
127 int mErrorPosition; ///< The error position
128 int mNumberOfParsedChars; ///< The size of string data
129 int mNumberOfCreatedNodes; ///< The number of nodes created
130 bool mFirstParse; ///< Flag if first parse
133 * The current parse state
146 // inhibited copy construct and assignment
147 JsonParserState(const JsonParserState&);
148 const JsonParserState& operator=(const JsonParserState&);
151 * Parse over white space
152 * Increments the current position
153 * @return true if no parse errors
155 bool ParseWhiteSpace();
158 * Parse over a number, setting the current node if found
159 * Increments the current position. Sets error data if parse error.
160 * @return true if found, false if parse error
165 * Parse over a symbol
166 * Increments the current position. Sets error data if parse error.
167 * @return true if found, false if parse error
169 bool ParseSymbol(const std::string& symbol);
172 * Parse over 'true' symbol, setting the current node if found
173 * Increments the current position. Sets error data if parse error.
174 * @return true if found, false if parse error
179 * Parse over 'false' symbol, setting the current node if found
180 * Increments the current position. Sets error data if parse error.
181 * @return true if found, false if parse error
186 * Parse over 'null' symbol, setting the current node if found
187 * Increments the current position. Sets error data if parse error.
188 * @return true if found, false if parse error
193 * Parse over a string from the current position and insert escaped
194 * control characters in place in the string and a null terminator.
195 * This function works from and modifes the current buffer position.
196 * @return the start of the null terminated string
198 char* EncodeString();
201 * Create a new node with name and type
203 TreeNode* CreateNewNode(const char* name, TreeNode::NodeType type);
206 * Create a new node if first parse, else check if the node already
207 * exists and set it to a new type
209 TreeNode* NewNode(const char* name, TreeNode::NodeType type);
212 * Set error meta data
213 * @returns always false.
215 bool Error(const char* description);
218 * Reset state for another parse
223 * Set current to its parent
224 * @return true if we had a parent, false and error otherwise
226 inline bool UpToParent()
228 if(NULL == mCurrent.GetParent())
230 return Error("Attempt to walk up above root");
232 mCurrent = TreeNodeManipulator( mCurrent.GetParent() );
237 * Get the current character
245 * @return True if there are at least n character left
247 inline bool AtLeast(int n)
249 // The standard suggests vector.end() can be decremented as
250 // iter v.back() { *--v.end() }
251 // (ISO/IEC 14882:2003 C++ Standard 23.1.1/12 – Sequences)
252 return (mEnd - mIter) > n;
256 * @return True if at the end of the data to parse
260 return mEnd == mIter;
264 * Advance current position by n characters or stop at mEnd
266 inline void Advance(int n)
268 int c = AdvanceIter(mIter, mEnd, n);
274 * Advance by n charaters and return true if we reached the end
276 inline bool AdvanceEnded(int n)
278 int c = AdvanceIter(mIter, mEnd, n);
281 return mEnd == mIter;
285 * Advance by at least n characters (stopping at mEnd) and skip any whitespace after n.
287 inline void AdvanceSkipWhiteSpace(int n)
289 int c = AdvanceIter(mIter, mEnd, n);
292 static_cast<void>( ParseWhiteSpace() );
296 * Increment new line counters
298 inline void NewLine()
307 } // namespace Internal
309 } // namespace Toolkit