2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/builder/json-parser-state.h>
37 // true if character represent a digit
38 inline bool IsDigit(char c)
40 return (c >= '0' && c <= '9');
43 // convert string to integer
44 bool StringToInteger(const char *first, const char *last, int& out)
54 else if (*first == '+')
60 // json error for int starting with zero
61 if( 0 == (*first - '0') && (first+1 != last))
67 for (; first != last && IsDigit(*first); ++first)
69 result = 10 * result + (*first - '0');
83 // convert hexadecimal string to unsigned integer
84 bool HexStringToUnsignedInteger(const char *first, const char *last, unsigned int& out)
86 unsigned int result = 0;
87 for (; first != last; ++first)
94 else if (*first >= 'a' && *first <= 'f')
96 digit = *first - 'a' + 10;
98 else if (*first >= 'A' && *first <= 'F')
100 digit = *first - 'A' + 10;
106 result = 16 * result + digit;
120 // convert string to floating point
121 bool StringToFloat(const char* first, const char* last, float& out)
132 else if (*first == '+')
140 for (; first != last && IsDigit(*first); ++first)
142 result = 10 * result + (*first - '0');
146 if (first != last && *first == '.')
150 float inv_base = 0.1f;
151 for (; first != last && IsDigit(*first); ++first)
153 result += (*first - '0') * inv_base;
158 // result w\o exponent
162 bool exponent_negative = false;
164 if (first != last && (*first == 'e' || *first == 'E'))
170 exponent_negative = true;
173 else if (*first == '+')
178 if(first == last || !IsDigit(*first))
183 for (; first != last && IsDigit(*first); ++first)
185 exponent = 10 * exponent + (*first - '0');
191 float power_of_ten = 10;
192 for (; exponent > 1; exponent--)
197 if (exponent_negative)
199 result /= power_of_ten;
203 result *= power_of_ten;
220 bool IsNumber(char c)
250 JsonParserState::JsonParserState(TreeNode* _root)
251 : mRoot(_root), mCurrent(_root),
252 mErrorDescription(NULL), mErrorNewLine(0), mErrorColumn(0), mErrorPosition(0),
253 mNumberOfParsedChars(0), mNumberOfCreatedNodes(0), mFirstParse(false),
262 TreeNode* JsonParserState::CreateNewNode(const char* name, TreeNode::NodeType type)
264 TreeNode* node = NULL;
266 node = TreeNodeManipulator::NewTreeNode();
267 TreeNodeManipulator modifyNew(node);
268 modifyNew.SetType(type);
269 modifyNew.SetName(name);
273 mCurrent = TreeNodeManipulator(mRoot);
277 mCurrent.AddChild(node);
278 mCurrent = modifyNew;
281 ++mNumberOfCreatedNodes;
287 TreeNode* JsonParserState::NewNode(const char* name, TreeNode::NodeType type)
289 TreeNode* node = NULL;
293 node = CreateNewNode(name, type);
301 const TreeNode* found = mCurrent.GetChild(name);
304 node = const_cast<TreeNode*>(found);
310 if( mCurrent.GetParent() == NULL )
318 // walk tree and deallocate children as were replacing this node
319 TreeNodeManipulator modify(node);
321 modify.SetName(name);
323 // Set the type of the existing node.
324 // Where the new type is different, then any children of this node will
326 // When the type is an array of numbers, then this will also remove any children
327 // When the type is an object or other array, then the children will not be removed,
328 // but will instead follow these replace rules.
329 modify.SetType(type);
335 // if not found then create new
336 node = CreateNewNode(name, type);
343 TreeNode* JsonParserState::GetRoot()
348 bool JsonParserState::Error(const char* description)
350 mErrorDescription = description;
354 bool JsonParserState::ParseWhiteSpace()
356 bool c_comment = false;
357 bool cpp_comment = false;
390 continue; // rather than carry on as comments may be back to back
393 else if( !c_comment && (c == '/' && nextChar == '/') )
400 if( c == '*' && nextChar == '/' )
407 else if( !cpp_comment && (c == '/' && nextChar == '*') )
412 if( ! (c_comment || cpp_comment) )
414 if( ! (c == '\x20' || c == '\x9' || c == '\xD' || c == '\xA' ) )
420 if( AdvanceEnded(1) )
430 bool JsonParserState::ParseSymbol(const std::string& symbol)
432 if( AtLeast( symbol.size() ) )
434 for(int i = 0; i < static_cast<int>( symbol.size() ); ++i)
436 if(*mIter != symbol[i])
450 bool JsonParserState::ParseTrue()
452 if( ParseSymbol("true") )
454 mCurrent.SetInteger(1);
455 mCurrent.SetType(TreeNode::BOOLEAN);
460 return Error("Unexpected character; expected symbol ie 'true'");
464 bool JsonParserState::ParseFalse()
466 if( ParseSymbol("false") )
468 mCurrent.SetInteger(0);
469 mCurrent.SetType(TreeNode::BOOLEAN);
474 return Error("Unexpected character; expected symbol ie 'false'");
478 bool JsonParserState::ParseNULL()
480 if( ParseSymbol("null") )
482 mCurrent.SetType(TreeNode::IS_NULL);
487 return Error("Unexpected character; expected symbol ie 'null'");
491 bool JsonParserState::ParseNumber()
493 mCurrent.SetType( TreeNode::INTEGER );
495 VectorCharIter first = mIter;
498 if( !(c == '-' || IsNumber(c) ) )
500 return Error("Number must start with '-' or 0-9");
503 while ( IsNumber(c) || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-' )
505 if (c == '.' || c == 'e' || c == 'E')
507 mCurrent.SetType( TreeNode::FLOAT );
513 if( mCurrent.GetType() == TreeNode::INTEGER )
516 if( StringToInteger(&(*first), &(*mIter), i ) )
518 mCurrent.SetInteger(i);
522 return Error("Bad integer number");
526 if(mCurrent.GetType() == TreeNode::FLOAT)
529 if( StringToFloat(&(*first), &(*mIter), f) )
531 mCurrent.SetFloat(f);
535 return Error("Bad float number");
539 return (mCurrent.GetType() == TreeNode::INTEGER) || (mCurrent.GetType() == TreeNode::FLOAT);
542 char* JsonParserState::EncodeString()
544 int substitution = 0;
545 VectorCharIter first = mIter;
546 VectorCharIter last = mIter;
550 if (static_cast<unsigned char>(*mIter) < '\x20')
552 static_cast<void>( Error("Control characters not allowed in strings") );
555 else if (*mIter == '\\' && AtLeast(2))
601 unsigned int codepoint;
604 static_cast<void>( Error("Bad unicode codepoint; not enough characters") );
607 if ( !HexStringToUnsignedInteger(&(*(mIter + 2)), &(*(mIter + 6)), codepoint) )
609 static_cast<void>( Error("Bad unicode codepoint") );
613 if (codepoint <= 0x7F)
615 *last = (char)codepoint;
617 else if (codepoint <= 0x7FF)
619 *last++ = (char)(0xC0 | (codepoint >> 6));
620 *last = (char)(0x80 | (codepoint & 0x3F));
622 else if (codepoint <= 0xFFFF)
624 *last++ = (char)(0xE0 | (codepoint >> 12));
625 *last++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
626 *last = (char)(0x80 | (codepoint & 0x3F));
631 } // case 'u' unicode
635 static_cast<void>( Error("Unrecognized escape sequence") );
643 else if (*mIter == '{')
645 if((0 == substitution) && (*last != '\\'))
652 else if (*mIter == '}')
661 else if (*mIter == '"')
675 mNumberOfParsedChars += last - first;
676 mNumberOfParsedChars += 1 ; // null terminator
678 mCurrent.SetSubstitution( substitution > 1 );
685 bool JsonParserState::ParseJson(VectorChar& source)
689 if( 0 == source.size() )
691 return Error("Empty source buffer to parse");
694 mIter = source.begin();
698 char currentChar = 0;
699 char lastCharacter = 0;
701 if( !ParseWhiteSpace() )
708 lastCharacter = currentChar;
709 currentChar = Char();
715 if( '{' == currentChar )
717 NewNode(name, TreeNode::OBJECT);
718 mState = STATE_OBJECT;
720 else if( '[' == currentChar )
722 NewNode(name, TreeNode::ARRAY);
723 mState = STATE_VALUE;
727 return Error("Json must start with object {} or array []");
730 AdvanceSkipWhiteSpace(1);
735 if( '}' == currentChar )
737 if(',' == lastCharacter)
739 return Error("Unexpected comma");
746 mState = STATE_VALUE;
748 else if ( '"' == currentChar )
754 return Error("Unexpected character");
757 AdvanceSkipWhiteSpace(1);
762 name = EncodeString();
767 if( !ParseWhiteSpace() )
773 return Error("Expected ':'");
775 if( !ParseWhiteSpace() )
779 mState = STATE_VALUE;
781 AdvanceSkipWhiteSpace(1);
786 if( '"' == currentChar )
789 NewNode(name, TreeNode::STRING);
790 if( char* value = EncodeString() )
792 mCurrent.SetString(value);
802 AdvanceSkipWhiteSpace(0);
804 else if( IsNumber(currentChar) || currentChar == '-' )
806 NewNode(name, TreeNode::IS_NULL);
815 AdvanceSkipWhiteSpace(0);
817 else if( '{' == currentChar )
819 if( '}' == lastCharacter )
821 return Error("Expected a comma");
825 NewNode(name, TreeNode::OBJECT);
826 mState = STATE_OBJECT;
827 AdvanceSkipWhiteSpace(1);
830 else if( '}' == currentChar )
832 if(',' == lastCharacter)
834 return Error("Expected another value");
837 if(mCurrent.GetType() != TreeNode::OBJECT)
839 return Error("Mismatched array definition");
842 if(mCurrent.GetParent() == NULL)
853 AdvanceSkipWhiteSpace(1);
855 else if( '[' == currentChar )
857 NewNode(name, TreeNode::ARRAY);
858 mState = STATE_VALUE;
859 AdvanceSkipWhiteSpace(1);
861 else if( ']' == currentChar )
863 if(',' == lastCharacter)
865 return Error("Expected a value");
868 if(mCurrent.GetType() != TreeNode::ARRAY)
870 return Error("Mismatched braces in object definition");
873 if(mCurrent.GetParent() == NULL)
884 AdvanceSkipWhiteSpace(1);
886 else if( 't' == currentChar )
888 NewNode(name, TreeNode::BOOLEAN);
897 AdvanceSkipWhiteSpace(0);
899 else if( 'n' == currentChar )
901 NewNode(name, TreeNode::IS_NULL);
910 AdvanceSkipWhiteSpace(0);
912 else if( 'f' == currentChar)
914 NewNode(name, TreeNode::BOOLEAN);
923 AdvanceSkipWhiteSpace(0);
925 else if( ',' == currentChar )
927 if( 0 == mCurrent.Size() )
929 return Error("Missing Value");
932 if(mCurrent.GetType() == TreeNode::OBJECT)
934 mState = STATE_OBJECT; // to get '"' in '"key":val'
936 else if(mCurrent.GetType() == TreeNode::ARRAY)
938 mState = STATE_VALUE; // array so just get next value
942 return Error("Unexpected character");
944 AdvanceSkipWhiteSpace(1);
948 return Error("Unexpected character");
954 } // case STATE_VALUE
957 return Error("Unexpected character. Json must have one object or array at its root");
965 if( mState != STATE_END )
967 return Error("Unexpected termination character");
970 mIter = source.end();
977 void JsonParserState::Reset()
979 mCurrent = TreeNodeManipulator(mRoot);
981 mErrorDescription = NULL;
988 } // namespace Internal
990 } // namespace Toolkit