16
16
#ifndef _APL_EASING_GRAMMAR_H
17
17
#define _APL_EASING_GRAMMAR_H
18
18
19
- #include < tao/pegtl.hpp>
20
- #include < tao/pegtl/contrib/abnf.hpp>
21
-
22
19
#include " apl/animation/coreeasing.h"
20
+ #include " apl/datagrammar/grammarpolyfill.h"
23
21
#include " apl/utils/log.h"
24
22
#include " apl/utils/stringfunctions.h"
25
23
@@ -86,7 +84,7 @@ struct action
86
84
: nothing< Rule > {
87
85
};
88
86
89
- struct easing_state
87
+ struct easing_state : fail_state
90
88
{
91
89
float lastTime = 0 ;
92
90
size_t startIndex = 0 ;
@@ -118,14 +116,18 @@ template<> struct action< path >
118
116
template < typename Input >
119
117
static void apply (const Input& in, easing_state& state) {
120
118
auto argCount = state.args .size () - state.startIndex ;
121
- if (argCount % 2 == 1 )
122
- throw parse_error (" Path easing function needs an even number of arguments" , in);
119
+ if (argCount % 2 == 1 ) {
120
+ state.fail (" Path easing function needs an even number of arguments" , in);
121
+ return ;
122
+ }
123
123
124
124
// Push each linear segment. Check to ensure time is incrementing
125
125
for (auto offset = state.startIndex ; offset < state.args .size () ; offset += 2 ) {
126
126
auto time = state.args .at (offset);
127
- if (time <= state.lastTime || time >= 1 )
128
- throw parse_error (" Path easing function needs ordered array of segments" , in);
127
+ if (time <= state.lastTime || time >= 1 ) {
128
+ state.fail (" Path easing function needs ordered array of segments" , in);
129
+ return ;
130
+ }
129
131
state.lastTime = time ;
130
132
state.segments .emplace_back (EasingSegment (kLinearSegment , offset));
131
133
}
@@ -153,8 +155,10 @@ template<> struct action< cubicbezier >
153
155
template < typename Input >
154
156
static void apply (const Input& in, easing_state& state) {
155
157
auto argCount = state.args .size () - state.startIndex ;
156
- if (argCount != 4 )
157
- throw parse_error (" Cubic-bezier easing function requires 4 arguments" , in);
158
+ if (argCount != 4 ) {
159
+ state.fail (" Cubic-bezier easing function requires 4 arguments" , in);
160
+ return ;
161
+ }
158
162
159
163
// Add a final segment at (1,1)
160
164
state.segments .emplace_back (EasingSegment (kEndSegment , state.args .size ()));
@@ -176,12 +180,16 @@ template<> struct action< end >
176
180
template < typename Input >
177
181
static void apply (const Input& in, easing_state& state) {
178
182
auto argCount = state.args .size () - state.startIndex ;
179
- if (argCount != 2 )
180
- throw parse_error (" End easing function segment requires 2 arguments" , in);
183
+ if (argCount != 2 ) {
184
+ state.fail (" End easing function segment requires 2 arguments" , in);
185
+ return ;
186
+ }
181
187
182
188
auto time = state.args .at (state.startIndex );
183
- if (time <= state.lastTime && state.startIndex > 0 )
184
- throw parse_error (" End easing function segment cannot start at this time" , in);
189
+ if (time <= state.lastTime && state.startIndex > 0 ) {
190
+ state.fail (" End easing function segment cannot start at this time" , in);
191
+ return ;
192
+ }
185
193
186
194
state.lastTime = time ;
187
195
state.segments .emplace_back (EasingSegment (kEndSegment , state.startIndex ));
@@ -201,12 +209,16 @@ template<> struct action< line >
201
209
template < typename Input >
202
210
static void apply (const Input& in, easing_state& state) {
203
211
auto argCount = state.args .size () - state.startIndex ;
204
- if (argCount != 2 )
205
- throw parse_error (" Line easing function segment requires 2 arguments" , in);
212
+ if (argCount != 2 ) {
213
+ state.fail (" Line easing function segment requires 2 arguments" , in);
214
+ return ;
215
+ }
206
216
207
217
auto time = state.args .at (state.startIndex );
208
- if (time <= state.lastTime && state.startIndex > 0 )
209
- throw parse_error (" Line easing function segment cannot start at this time" , in);
218
+ if (time <= state.lastTime && state.startIndex > 0 ) {
219
+ state.fail (" Line easing function segment cannot start at this time" , in);
220
+ return ;
221
+ }
210
222
211
223
state.lastTime = time ;
212
224
state.segments .emplace_back (EasingSegment (kLinearSegment , state.startIndex ));
@@ -226,12 +238,16 @@ template<> struct action< curve >
226
238
template < typename Input >
227
239
static void apply (const Input& in, easing_state& state) {
228
240
auto argCount = state.args .size () - state.startIndex ;
229
- if (argCount != 6 )
230
- throw parse_error (" Curve easing function segment requires 6 arguments" , in);
241
+ if (argCount != 6 ) {
242
+ state.fail (" Curve easing function segment requires 6 arguments" , in);
243
+ return ;
244
+ }
231
245
232
246
auto time = state.args .at (state.startIndex );
233
- if (time <= state.lastTime && state.startIndex > 0 )
234
- throw parse_error (" Curve easing function segment cannot start at this time" , in);
247
+ if (time <= state.lastTime && state.startIndex > 0 ) {
248
+ state.fail (" Curve easing function segment cannot start at this time" , in);
249
+ return ;
250
+ }
235
251
236
252
state.lastTime = time ;
237
253
state.segments .emplace_back (EasingSegment (kCurveSegment , state.startIndex ));
@@ -246,16 +262,22 @@ template<> struct action< spatial >
246
262
assert (state.startIndex == 0 );
247
263
248
264
auto argCount = state.args .size ();
249
- if (argCount != 2 )
250
- throw parse_error (" Wrong number of arguments to spatial" , in);
265
+ if (argCount != 2 ) {
266
+ state.fail (" Wrong number of arguments to spatial" , in);
267
+ return ;
268
+ }
251
269
252
270
auto dof = static_cast <int >(state.args .at (0 ));
253
- if (dof < 2 )
254
- throw parse_error (" invalid number of indices in spatial segment" , in);
271
+ if (dof < 2 ) {
272
+ state.fail (" invalid number of indices in spatial segment" , in);
273
+ return ;
274
+ }
255
275
256
276
auto index = static_cast <int >(state.args .at (1 ));
257
- if (index < 0 || index >= dof)
258
- throw parse_error (" select index out of range in spatial segment" , in);
277
+ if (index < 0 || index >= dof) {
278
+ state.fail (" select index out of range in spatial segment" , in);
279
+ return ;
280
+ }
259
281
}
260
282
};
261
283
@@ -274,13 +296,17 @@ template<> struct action< send >
274
296
auto argCount = state.args .size () - state.startIndex ;
275
297
276
298
// Time, pcount for value
277
- auto dof = ::abs (static_cast <int >(state.args [0 ]));
278
- if (argCount != 1 + dof)
279
- throw parse_error (" Wrong number of arguments to send" , in);
299
+ auto dof = std::abs (static_cast <int >(state.args [0 ]));
300
+ if (argCount != 1 + dof) {
301
+ state.fail (" Wrong number of arguments to send" , in);
302
+ return ;
303
+ }
280
304
281
305
auto time = state.args .at (state.startIndex );
282
- if (time <= state.lastTime && !state.segments .empty ())
283
- throw parse_error (" send easing function segment cannot start at this time" , in);
306
+ if (time <= state.lastTime && !state.segments .empty ()) {
307
+ state.fail (" send easing function segment cannot start at this time" , in);
308
+ return ;
309
+ }
284
310
285
311
state.lastTime = time ;
286
312
state.segments .emplace_back (EasingSegment (kSEndSegment , state.startIndex ));
@@ -302,13 +328,17 @@ template<> struct action< scurve >
302
328
auto argCount = state.args .size () - state.startIndex ;
303
329
304
330
// Time, pcount * 3 for value, tin, tout, 4 for the time curve =
305
- auto dof = ::abs (static_cast <int >(state.args [0 ]));
306
- if (argCount != 5 + dof * 3 )
307
- throw parse_error (" Wrong number of arguments to scurve" , in);
331
+ auto dof = std::abs (static_cast <int >(state.args [0 ]));
332
+ if (argCount != 5 + dof * 3 ) {
333
+ state.fail (" Wrong number of arguments to scurve" , in);
334
+ return ;
335
+ }
308
336
309
337
auto time = state.args .at (state.startIndex );
310
- if (time <= state.lastTime && !state.segments .empty ())
311
- throw parse_error (" scurve easing function segment cannot start at this time" , in);
338
+ if (time <= state.lastTime && !state.segments .empty ()) {
339
+ state.fail (" scurve easing function segment cannot start at this time" , in);
340
+ return ;
341
+ }
312
342
313
343
state.lastTime = time ;
314
344
state.segments .emplace_back (EasingSegment (kSCurveSegment , state.startIndex ));
0 commit comments