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>
36 // true if character represent a digit
37 inline bool IsDigit(char c)
39 return (c >= '0' && c <= '9');
42 // convert string to integer
43 bool StringToInteger(const char *first, const char *last, int& out)
53 else if (*first == '+')
59 // json error for int starting with zero
60 if( 0 == (*first - '0') && (first+1 != last))
66 for (; first != last && IsDigit(*first); ++first)
68 result = 10 * result + (*first - '0');
82 // convert hexadecimal string to unsigned integer
83 bool HexStringToUnsignedInteger(const char *first, const char *last, unsigned int& out)
85 unsigned int result = 0;
86 for (; first != last; ++first)
93 else if (*first >= 'a' && *first <= 'f')
95 digit = *first - 'a' + 10;
97 else if (*first >= 'A' && *first <= 'F')
99 digit = *first - 'A' + 10;
105 result = 16 * result + digit;
119 // convert string to floating point
120 bool StringToFloat(const char* first, const char* last, float& out)
131 else if (*first == '+')
139 for (; first != last && IsDigit(*first); ++first)
141 result = 10 * result + (*first - '0');
145 if (first != last && *first == '.')
149 float inv_base = 0.1f;
150 for (; first != last && IsDigit(*first); ++first)
152 result += (*first - '0') * inv_base;
157 // result w\o exponent
161 bool exponent_negative = false;
163 if (first != last && (*first == 'e' || *first == 'E'))
169 exponent_negative = true;
172 else if (*first == '+')
177 if(first == last || !IsDigit(*first))
182 for (; first != last && IsDigit(*first); ++first)
184 exponent = 10 * exponent + (*first - '0');
190 float power_of_ten = 10;
191 for (; exponent > 1; exponent--)
196 if (exponent_negative)
198 result /= power_of_ten;
202 result *= power_of_ten;
219 bool IsNumber(char c)
249 JsonParserState::JsonParserState(TreeNode* _root)
250 : mRoot(_root), mCurrent(_root),
251 mErrorDescription(NULL), mErrorNewLine(0), mErrorColumn(0), mErrorPosition(0),
252 mNumberOfParsedChars(0), mNumberOfCreatedNodes(0), mFirstParse(false),
261 TreeNode* JsonParserState::CreateNewNode(const char* name, TreeNode::NodeType type)
263 TreeNode* node = NULL;
265 node = TreeNodeManipulator::NewTreeNode();
266 TreeNodeManipulator modifyNew(node);
267 modifyNew.SetType(type);
268 modifyNew.SetName(name);
272 mCurrent = TreeNodeManipulator(mRoot);
276 mCurrent.AddChild(node);
277 mCurrent = modifyNew;
280 ++mNumberOfCreatedNodes;
286 TreeNode* JsonParserState::NewNode(const char* name, TreeNode::NodeType type)
288 TreeNode* node = NULL;
292 node = CreateNewNode(name, type);
300 const TreeNode* found = mCurrent.GetChild(name);
303 node = const_cast<TreeNode*>(found);
309 if( mCurrent.GetParent() == NULL )
317 // walk tree and deallocate children as were replacing this node
318 TreeNodeManipulator modify(node);
320 modify.SetName(name);
321 // Set the type of the existing node, this may remove children where necessary
322 // (changing from container type to value type)
323 modify.SetType(type);
329 // if not found then create new
330 node = CreateNewNode(name, type);
337 TreeNode* JsonParserState::GetRoot()
342 bool JsonParserState::Error(const char* description)
344 mErrorDescription = description;
348 bool JsonParserState::ParseWhiteSpace()
350 bool c_comment = false;
351 bool cpp_comment = false;
384 continue; // rather than carry on as comments may be back to back
387 else if( !c_comment && (c == '/' && nextChar == '/') )
394 if( c == '*' && nextChar == '/' )
401 else if( !cpp_comment && (c == '/' && nextChar == '*') )
406 if( ! (c_comment || cpp_comment) )
408 if( ! (c == '\x20' || c == '\x9' || c == '\xD' || c == '\xA' ) )
414 if( AdvanceEnded(1) )
424 bool JsonParserState::ParseSymbol(const std::string& symbol)
426 if( AtLeast( symbol.size() ) )
428 for(int i = 0; i < static_cast<int>( symbol.size() ); ++i)
430 if(*mIter != symbol[i])
444 bool JsonParserState::ParseTrue()
446 if( ParseSymbol("true") )
448 mCurrent.SetInteger(1);
449 mCurrent.SetType(TreeNode::BOOLEAN);
454 return Error("Unexpected character; expected symbol ie 'true'");
458 bool JsonParserState::ParseFalse()
460 if( ParseSymbol("false") )
462 mCurrent.SetInteger(0);
463 mCurrent.SetType(TreeNode::BOOLEAN);
468 return Error("Unexpected character; expected symbol ie 'false'");
472 bool JsonParserState::ParseNULL()
474 if( ParseSymbol("null") )
476 mCurrent.SetType(TreeNode::IS_NULL);
481 return Error("Unexpected character; expected symbol ie 'null'");
485 bool JsonParserState::ParseNumber()
487 mCurrent.SetType( TreeNode::INTEGER );
489 VectorCharIter first = mIter;
492 if( !(c == '-' || IsNumber(c) ) )
494 return Error("Number must start with '-' or 0-9");
497 while ( IsNumber(c) || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-' )
499 if (c == '.' || c == 'e' || c == 'E')
501 mCurrent.SetType( TreeNode::FLOAT );
507 if( mCurrent.GetType() == TreeNode::INTEGER )
510 if( StringToInteger(&(*first), &(*mIter), i ) )
512 mCurrent.SetInteger(i);
516 return Error("Bad integer number");
520 if(mCurrent.GetType() == TreeNode::FLOAT)
523 if( StringToFloat(&(*first), &(*mIter), f) )
525 mCurrent.SetFloat(f);
529 return Error("Bad float number");
533 return (mCurrent.GetType() == TreeNode::INTEGER) || (mCurrent.GetType() == TreeNode::FLOAT);
536 char* JsonParserState::EncodeString()
538 int substitution = 0;
539 VectorCharIter first = mIter;
540 VectorCharIter last = mIter;
544 if (static_cast<unsigned char>(*mIter) < '\x20')
546 static_cast<void>( Error("Control characters not allowed in strings") );
549 else if (*mIter == '\\' && AtLeast(2))
595 unsigned int codepoint;
598 static_cast<void>( Error("Bad unicode codepoint; not enough characters") );
601 if ( !HexStringToUnsignedInteger(&(*(mIter + 2)), &(*(mIter + 6)), codepoint) )
603 static_cast<void>( Error("Bad unicode codepoint") );
607 if (codepoint <= 0x7F)
609 *last = (char)codepoint;
611 else if (codepoint <= 0x7FF)
613 *last++ = (char)(0xC0 | (codepoint >> 6));
614 *last = (char)(0x80 | (codepoint & 0x3F));
616 else if (codepoint <= 0xFFFF)
618 *last++ = (char)(0xE0 | (codepoint >> 12));
619 *last++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
620 *last = (char)(0x80 | (codepoint & 0x3F));
625 } // case 'u' unicode
629 static_cast<void>( Error("Unrecognized escape sequence") );
637 else if (*mIter == '{')
639 if((0 == substitution) && (*last != '\\'))
646 else if (*mIter == '}')
655 else if (*mIter == '"')
669 mNumberOfParsedChars += last - first;
670 mNumberOfParsedChars += 1 ; // null terminator
672 mCurrent.SetSubstitution( substitution > 1 );
679 bool JsonParserState::ParseJson(VectorChar& source)
683 if( 0 == source.size() )
685 return Error("Empty source buffer to parse");
688 mIter = source.begin();
692 char currentChar = 0;
693 char lastCharacter = 0;
695 if( !ParseWhiteSpace() )
702 lastCharacter = currentChar;
703 currentChar = Char();
709 if( '{' == currentChar )
711 NewNode(name, TreeNode::OBJECT);
712 mState = STATE_OBJECT;
714 else if( '[' == currentChar )
716 NewNode(name, TreeNode::ARRAY);
717 mState = STATE_VALUE;
721 return Error("Json must start with object {} or array []");
724 AdvanceSkipWhiteSpace(1);
729 if( '}' == currentChar )
731 if(',' == lastCharacter)
733 return Error("Unexpected comma");
740 mState = STATE_VALUE;
742 else if ( '"' == currentChar )
748 return Error("Unexpected character");
751 AdvanceSkipWhiteSpace(1);
756 name = EncodeString();
761 if( !ParseWhiteSpace() )
767 return Error("Expected ':'");
769 if( !ParseWhiteSpace() )
773 mState = STATE_VALUE;
775 AdvanceSkipWhiteSpace(1);
780 if( '"' == currentChar )
783 NewNode(name, TreeNode::STRING);
784 if( char* value = EncodeString() )
786 mCurrent.SetString(value);
796 AdvanceSkipWhiteSpace(0);
798 else if( IsNumber(currentChar) || currentChar == '-' )
800 NewNode(name, TreeNode::IS_NULL);
809 AdvanceSkipWhiteSpace(0);
811 else if( '{' == currentChar )
813 if( '}' == lastCharacter )
815 return Error("Expected a comma");
819 NewNode(name, TreeNode::OBJECT);
820 mState = STATE_OBJECT;
821 AdvanceSkipWhiteSpace(1);
824 else if( '}' == currentChar )
826 if(',' == lastCharacter)
828 return Error("Expected another value");
831 if(mCurrent.GetType() != TreeNode::OBJECT)
833 return Error("Mismatched array definition");
836 if(mCurrent.GetParent() == NULL)
847 AdvanceSkipWhiteSpace(1);
849 else if( '[' == currentChar )
851 NewNode(name, TreeNode::ARRAY);
852 mState = STATE_VALUE;
853 AdvanceSkipWhiteSpace(1);
855 else if( ']' == currentChar )
857 if(',' == lastCharacter)
859 return Error("Expected a value");
862 if(mCurrent.GetType() != TreeNode::ARRAY)
864 return Error("Mismatched braces in object definition");
867 if(mCurrent.GetParent() == NULL)
878 AdvanceSkipWhiteSpace(1);
880 else if( 't' == currentChar )
882 NewNode(name, TreeNode::BOOLEAN);
891 AdvanceSkipWhiteSpace(0);
893 else if( 'n' == currentChar )
895 NewNode(name, TreeNode::IS_NULL);
904 AdvanceSkipWhiteSpace(0);
906 else if( 'f' == currentChar)
908 NewNode(name, TreeNode::BOOLEAN);
917 AdvanceSkipWhiteSpace(0);
919 else if( ',' == currentChar )
921 if( 0 == mCurrent.Size() )
923 return Error("Missing Value");
926 if(mCurrent.GetType() == TreeNode::OBJECT)
928 mState = STATE_OBJECT; // to get '"' in '"key":val'
930 else if(mCurrent.GetType() == TreeNode::ARRAY)
932 mState = STATE_VALUE; // array so just get next value
936 return Error("Unexpected character");
938 AdvanceSkipWhiteSpace(1);
942 return Error("Unexpected character");
948 } // case STATE_VALUE
951 return Error("Unexpected character. Json must have one object or array at its root");
959 if( mState != STATE_END )
961 return Error("Unexpected termination character");
964 mIter = source.end();
971 void JsonParserState::Reset()
973 mCurrent = TreeNodeManipulator(mRoot);
975 mErrorDescription = NULL;
982 } // namespace Internal
984 } // namespace Toolkit