1 /*
2  * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved.
3 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10 
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 /*
27  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
28 
29  * Permission is hereby granted, free of charge, to any person obtaining a copy
30  * of this software and associated documentation files (the "Software"), to deal
31  * in the Software without restriction, including without limitation the rights
32  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33  * copies of the Software, and to permit persons to whom the Software is
34  * furnished to do so, subject to the following conditions:
35 
36  * The above copyright notice and this permission notice shall be included in
37  * all copies or substantial portions of the Software.
38 
39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45  * SOFTWARE.
46  */
47 
48 #ifndef _TVG_LOTTIE_PARSER_HANDLER_H_
49 #define _TVG_LOTTIE_PARSER_HANDLER_H_
50 
51 #include "rapidjson/document.h"
52 #include "tvgCommon.h"
53 
54 
55 using namespace rapidjson;
56 
57 
58 struct LookaheadParserHandler
59 {
60     enum LookaheadParsingState {
61         kInit = 0,
62         kError,
63         kHasNull,
64         kHasBool,
65         kHasNumber,
66         kHasString,
67         kHasKey,
68         kEnteringObject,
69         kExitingObject,
70         kEnteringArray,
71         kExitingArray
72     };
73 
74     Value                   val;
75     LookaheadParsingState   state = kInit;
76     Reader                  reader;
77     InsituStringStream      iss;
78 
LookaheadParserHandlerLookaheadParserHandler79     LookaheadParserHandler(const char *str) : iss((char*)str)
80     {
81         reader.IterativeParseInit();
82     }
83 
NullLookaheadParserHandler84     bool Null()
85     {
86         state = kHasNull;
87         val.SetNull();
88         return true;
89     }
90 
BoolLookaheadParserHandler91     bool Bool(bool b)
92     {
93         state = kHasBool;
94         val.SetBool(b);
95         return true;
96     }
97 
IntLookaheadParserHandler98     bool Int(int i)
99     {
100         state = kHasNumber;
101         val.SetInt(i);
102         return true;
103     }
104 
UintLookaheadParserHandler105     bool Uint(unsigned u)
106     {
107         state = kHasNumber;
108         val.SetUint(u);
109         return true;
110     }
111 
Int64LookaheadParserHandler112     bool Int64(int64_t i)
113     {
114         state = kHasNumber;
115         val.SetInt64(i);
116         return true;
117     }
118 
Uint64LookaheadParserHandler119     bool Uint64(int64_t u)
120     {
121         state = kHasNumber;
122         val.SetUint64(u);
123         return true;
124     }
125 
DoubleLookaheadParserHandler126     bool Double(double d)
127     {
128         state = kHasNumber;
129         val.SetDouble(d);
130         return true;
131     }
132 
RawNumberLookaheadParserHandler133     bool RawNumber(const char *, SizeType, TVG_UNUSED bool)
134     {
135         return false;
136     }
137 
StringLookaheadParserHandler138     bool String(const char *str, SizeType length, TVG_UNUSED bool)
139     {
140         state = kHasString;
141         val.SetString(str, length);
142         return true;
143     }
144 
StartObjectLookaheadParserHandler145     bool StartObject()
146     {
147         state = kEnteringObject;
148         return true;
149     }
150 
KeyLookaheadParserHandler151     bool Key(const char *str, SizeType length, TVG_UNUSED bool)
152     {
153         state = kHasKey;
154         val.SetString(str, length);
155         return true;
156     }
157 
EndObjectLookaheadParserHandler158     bool EndObject(SizeType)
159     {
160         state = kExitingObject;
161         return true;
162     }
163 
StartArrayLookaheadParserHandler164     bool StartArray()
165     {
166         state = kEnteringArray;
167         return true;
168     }
169 
EndArrayLookaheadParserHandler170     bool EndArray(SizeType)
171     {
172         state = kExitingArray;
173         return true;
174     }
175 
ErrorLookaheadParserHandler176     void Error()
177     {
178         TVGERR("LOTTIE", "Parsing Error!");
179         state = kError;
180     }
181 
InvalidLookaheadParserHandler182     bool Invalid()
183     {
184         return state == kError;
185     }
186 
187     bool enterObject();
188     bool enterArray();
189     bool nextArrayValue();
190     int getInt();
191     float getFloat();
192     const char* getString();
193     char* getStringCopy();
194     bool getBool();
195     void getNull();
196     bool parseNext();
197     const char* nextObjectKey();
198     void skip(const char* key);
199     void skipOut(int depth);
200     int peekType();
201 };
202 
203 #endif //_TVG_LOTTIE_PARSER_HANDLER_H_
204 
205 #endif /* LV_USE_THORVG_INTERNAL */
206 
207