-
Notifications
You must be signed in to change notification settings - Fork 84
/
MGTemplateStandardMarkers.m
641 lines (558 loc) · 21.8 KB
/
MGTemplateStandardMarkers.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//
// MGTemplateStandardMarkers.m
//
// Created by Matt Gemmell on 13/05/2008.
// Copyright 2008 Instinctive Code. All rights reserved.
//
#import "MGTemplateStandardMarkers.h"
#import "MGTemplateFilter.h"
//==============================================================================
#define FOR_START @"for"
#define FOR_END @"/for"
#define FOR_TYPE_ENUMERATOR @"in" // e.g. for thing in things
#define FOR_TYPE_RANGE @"to" // e.g. for 1 to 5
#define FOR_REVERSE @"reversed"
#define FOR_LOOP_VARS @"currentLoop"
#define FOR_LOOP_CURR_INDEX @"currentIndex"
#define FOR_LOOP_START_INDEX @"startIndex"
#define FOR_LOOP_END_INDEX @"endIndex"
#define FOR_PARENT_LOOP @"parentLoop"
#define STACK_START_MARKER_RANGE @"markerRange"
#define STACK_START_REMAINING_RANGE @"remainingRange"
#define FOR_STACK_ENUMERATOR @"enumerator"
#define FOR_STACK_ENUM_VAR @"enumeratorVariable"
#define FOR_STACK_DISABLED_OUTPUT @"disabledOutput"
//==============================================================================
#define SECTION_START @"section"
#define SECTION_END @"/section"
//==============================================================================
#define IF_START @"if"
#define ELSE @"else"
#define IF_END @"/if"
#define IF_VARS @"currentIf"
#define DISABLE_OUTPUT @"shouldDisableOutput"
#define IF_ARG_TRUE @"argumentTrue"
#define IF_ELSE_SEEN @"elseEncountered"
//==============================================================================
#define NOW @"now"
//==============================================================================
#define COMMENT_START @"comment"
#define COMMENT_END @"/comment"
//==============================================================================
#define LOAD @"load"
//==============================================================================
#define CYCLE @"cycle"
#define CYCLE_INDEX @"lastIndex"
#define CYCLE_VALUES @"value"
//==============================================================================
#define SET @"set"
//==============================================================================
@implementation MGTemplateStandardMarkers
{
__weak MGTemplateEngine *engine; // weak ref
NSMutableArray *forStack;
NSMutableArray *sectionStack;
NSMutableArray *ifStack;
NSMutableArray *commentStack;
NSMutableDictionary *cycles;
}
- (id)initWithTemplateEngine:(MGTemplateEngine *)theEngine
{
if ((self = [super init])) {
engine = theEngine;
forStack = [[NSMutableArray alloc] init];
sectionStack = [[NSMutableArray alloc] init];
ifStack = [[NSMutableArray alloc] init];
commentStack = [[NSMutableArray alloc] init];
cycles = [[NSMutableDictionary alloc] init];
}
return self;
}
- (NSArray *)markers
{
return @[
FOR_START, FOR_END,
SECTION_START, SECTION_END,
IF_START, ELSE, IF_END,
NOW,
COMMENT_START, COMMENT_END,
LOAD,
CYCLE,
SET,
];
}
- (NSArray *)endMarkersForMarker:(NSString *)marker
{
if ([marker isEqualToString:FOR_START]) {
return @[FOR_END];
} else if ([marker isEqualToString:SECTION_START]) {
return @[SECTION_END];
} else if ([marker isEqualToString:IF_START]) {
return @[IF_END, ELSE];
} else if ([marker isEqualToString:COMMENT_START]) {
return @[COMMENT_END];
}
return nil;
}
- (NSObject *)markerEncountered:(NSString *)marker withArguments:(NSArray *)args inRange:(NSRange)markerRange
blockStarted:(BOOL *)blockStarted blockEnded:(BOOL *)blockEnded
outputEnabled:(BOOL *)outputEnabled nextRange:(NSRange *)nextRange
currentBlockInfo:(NSDictionary *)blockInfo newVariables:(NSDictionary * __autoreleasing *)newVariables
{
if ([marker isEqualToString:FOR_START]) {
if (args && [args count] >= 3) {
// Determine which type of loop this is.
BOOL isRange = YES;
if ([[args objectAtIndex:1] isEqualToString:FOR_TYPE_ENUMERATOR]) {
isRange = NO;
}
BOOL reversed = NO;
if ([args count] == 4 && [[args objectAtIndex:3] isEqualToString:FOR_REVERSE]) {
reversed = YES;
}
// Determine if we have acceptable parameters.
NSObject *loopEnumObject = nil;
BOOL valid = NO;
NSString *startArg = [args objectAtIndex:0];
NSString *endArg = [args objectAtIndex:2];
NSInteger startIndex = -1, endIndex = -1;
if (isRange) {
// Check to see if either the arg itself is numeric, or it corresponds to a numeric variable.
valid = [self argIsNumeric:startArg integerValue:&startIndex checkVariables:YES];
if (valid) {
valid = [self argIsNumeric:endArg integerValue:&endIndex checkVariables:YES];
if (valid) {
// Check startIndex and endIndex are sensible.
valid = (startIndex <= endIndex);
}
}
} else {
startIndex = 0;
// Check that endArg is a collection.
NSObject *obj = [engine resolveVariable:endArg];
if (obj && [obj respondsToSelector:@selector(objectEnumerator)] && [obj respondsToSelector:@selector(count)]) {
endIndex = (NSInteger)[(NSArray *)obj count];
if (endIndex > 0) {
loopEnumObject = obj;
valid = YES;
}
}
}
if (valid) {
*blockStarted = YES;
// Set up for-stack frame for this loop.
NSMutableDictionary *stackFrame = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRange:markerRange], STACK_START_MARKER_RANGE,
[NSValue valueWithRange:*nextRange], STACK_START_REMAINING_RANGE,
nil];
[forStack addObject:stackFrame];
// Set up variables for the block.
NSInteger currentIndex = (reversed) ? endIndex : startIndex;
NSMutableDictionary *loopVars = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@(startIndex), FOR_LOOP_START_INDEX,
@(endIndex), FOR_LOOP_END_INDEX,
@(currentIndex), FOR_LOOP_CURR_INDEX,
@(reversed), FOR_REVERSE,
nil];
NSMutableDictionary *blockVars = [NSMutableDictionary dictionaryWithObjectsAndKeys:
loopVars, FOR_LOOP_VARS,
nil];
// Add enumerator variable if appropriate.
if (!isRange) {
NSEnumerator *enumerator;
if (reversed && [loopEnumObject respondsToSelector:@selector(reverseObjectEnumerator)]) {
enumerator = [(NSArray *)loopEnumObject reverseObjectEnumerator];
} else {
enumerator = [(NSArray *)loopEnumObject objectEnumerator];
}
[stackFrame setObject:enumerator forKey:FOR_STACK_ENUMERATOR];
[stackFrame setObject:startArg forKey:FOR_STACK_ENUM_VAR];
[blockVars setObject:[enumerator nextObject] forKey:startArg];
}
// Add parentLoop if it exists.
if (blockInfo) {
NSDictionary *parentLoop;
parentLoop = (NSDictionary *)[engine resolveVariable:FOR_LOOP_VARS]; // in case parent loop isn't in the first parent stack-frame.
if (parentLoop) {
[loopVars setObject:parentLoop forKey:FOR_PARENT_LOOP];
}
}
*newVariables = blockVars;
} else {
// Disable output for this block.
*blockStarted = YES;
NSMutableDictionary *stackFrame = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], FOR_STACK_DISABLED_OUTPUT,
[NSValue valueWithRange:markerRange], STACK_START_MARKER_RANGE,
[NSValue valueWithRange:*nextRange], STACK_START_REMAINING_RANGE,
nil];
[forStack addObject:stackFrame];
*outputEnabled = NO;
}
}
} else if ([marker isEqualToString:FOR_END]) {
// Decide whether to loop back or terminate.
if ([self currentBlock:blockInfo matchesTopOfStack:forStack]) {
NSMutableDictionary *frame = [forStack lastObject];
// Check to see if this was a block with an invalid looping condition.
NSNumber *disabledOutput = (NSNumber *)[frame objectForKey:FOR_STACK_DISABLED_OUTPUT];
if (disabledOutput && [disabledOutput boolValue]) {
*blockEnded = YES;
[forStack removeLastObject];
*outputEnabled = ![[[forStack lastObject] objectForKey: FOR_STACK_DISABLED_OUTPUT] boolValue];
}
// This is the same loop that's on top of our stack. Check to see if we need to loop back.
BOOL loop = NO;
NSDictionary *blockVars = [blockInfo objectForKey:BLOCK_VARIABLES_KEY];
if ([blockVars count] == 0) {
*blockEnded = YES;
return nil;
}
NSMutableDictionary *loopVars = [[blockVars objectForKey:FOR_LOOP_VARS] mutableCopy];
BOOL reversed = [[loopVars objectForKey:FOR_REVERSE] boolValue];
NSEnumerator *loopEnum = [frame objectForKey:FOR_STACK_ENUMERATOR];
NSObject *newEnumValue = nil;
NSInteger currentIndex = [[loopVars objectForKey:FOR_LOOP_CURR_INDEX] integerValue];
if (loopEnum) {
// Enumerator type.
newEnumValue = [loopEnum nextObject];
if (newEnumValue) {
loop = YES;
}
} else {
// Range type.
if (reversed) {
NSInteger minIndex = [[loopVars objectForKey:FOR_LOOP_START_INDEX] integerValue];
if (currentIndex > minIndex) {
loop = YES;
}
} else {
NSInteger maxIndex = [[loopVars objectForKey:FOR_LOOP_END_INDEX] integerValue];
if (currentIndex < maxIndex) {
loop = YES;
}
}
}
if (loop) {
// Set remainingRange from stack dict
*nextRange = [[frame objectForKey:STACK_START_REMAINING_RANGE] rangeValue];
// Set new currentIndex
if (reversed) {
currentIndex--;
} else {
currentIndex++;
}
[loopVars setObject:@(currentIndex) forKey:FOR_LOOP_CURR_INDEX];
// Set new val for enumVar if specified
NSMutableDictionary *newVars = [NSMutableDictionary dictionaryWithObjectsAndKeys:
loopVars, FOR_LOOP_VARS,
nil];
if (newEnumValue) {
[newVars setObject:newEnumValue forKey:[frame objectForKey:FOR_STACK_ENUM_VAR]];
}
*newVariables = newVars;
} else {
// Don't need to do much here, since:
// 1. Each blockStack frame for a "for" has its own currentLoop dict.
// 2. Parent loop's enum-vars are still in place in the parent stack's vars.
// End block.
*blockEnded = YES;
[forStack removeLastObject];
}
// Return immediately.
return nil;
}
*blockEnded = YES;
} else if ([marker isEqualToString:SECTION_START]) {
if (args && [args count] == 1) {
*blockStarted = YES;
// Set up for-stack frame for this section.
NSMutableDictionary *stackFrame = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRange:markerRange], STACK_START_MARKER_RANGE,
nil];
[sectionStack addObject:stackFrame];
}
} else if ([marker isEqualToString:SECTION_END]) {
if ([self currentBlock:blockInfo matchesTopOfStack:sectionStack]) {
// This is the same section that's on top of our stack. Remove from stack.
[sectionStack removeLastObject];
}
*blockEnded = YES;
} else if ([marker isEqualToString:IF_START]) {
if (args && ([args count] >= 1 && [args count] <= 3)) {
*blockStarted = YES;
// Determine appropriate values for outputEnabled and for our if-stack frame.
BOOL elseEncountered = NO;
BOOL argTrue = NO;
if ([args count] == 1) {
argTrue = [self argIsTrue:[args objectAtIndex:0]];
} else if ([args count] == 2 && [[[args objectAtIndex:0] lowercaseString] isEqualToString:@"not"]) {
// e.g. if not x
argTrue = ![self argIsTrue:[args objectAtIndex:1]];
} else if ([args count] == 3) {
// Assumed to be of the form: operand comparison operand, e.g. x == y
NSString *firstArg = [args objectAtIndex:0];
NSString *secondArg = [args objectAtIndex:2];
BOOL firstTrue = [self argIsTrue:firstArg];
BOOL secondTrue = [self argIsTrue:secondArg];
NSInteger num1, num2;
BOOL firstNumeric, secondNumeric;
firstNumeric = [self argIsNumeric:firstArg integerValue:&num1 checkVariables:YES];
secondNumeric = [self argIsNumeric:secondArg integerValue:&num2 checkVariables:YES];
if (!firstNumeric) {
num1 = ([engine resolveVariable:firstArg]) ? 1 : 0;
}
if (!secondNumeric) {
num2 = ([engine resolveVariable:secondArg]) ? 1 : 0;
}
NSString *op = [[args objectAtIndex:1] lowercaseString];
if ([op isEqualToString:@"and"] || [op isEqualToString:@"&&"]) {
argTrue = (firstTrue && secondTrue);
} else if ([op isEqualToString:@"or"] || [op isEqualToString:@"||"]) {
argTrue = (firstTrue || secondTrue);
} else if ([op isEqualToString:@"="] || [op isEqualToString:@"=="]) {
argTrue = (num1 == num2);
} else if ([op isEqualToString:@"!="] || [op isEqualToString:@"<>"]) {
argTrue = (num1 != num2);
} else if ([op isEqualToString:@">"]) {
argTrue = (num1 > num2);
} else if ([op isEqualToString:@"<"]) {
argTrue = (num1 < num2);
} else if ([op isEqualToString:@">="]) {
argTrue = (num1 >= num2);
} else if ([op isEqualToString:@"<="]) {
argTrue = (num1 <= num2);
} else if ([op isEqualToString:@"\%"]) {
if (num2 == 0) {
argTrue = NO;
} else {
argTrue = ((num1 % num2) > 0);
}
} else if ([op isEqualToString:@"equalsstring"] || [op isEqualToString:@"notequalsstring"]) {
NSObject *firstVal = [engine resolveVariable:firstArg];
NSObject *secondVal = [engine resolveVariable:secondArg];
if (!firstVal) {
firstVal = firstArg;
}
if (!secondVal) {
secondVal = secondArg;
}
if (firstVal && secondVal) {
//NSLog(@"%@ %@", [NSString stringWithFormat:@"%@", firstVal], [NSString stringWithFormat:@"%@", secondVal]);
argTrue = [[NSString stringWithFormat:@"%@", firstVal] isEqualToString:[NSString stringWithFormat:@"%@", secondVal]];
if ([op isEqualToString:@"notequalsstring"]) {
argTrue = !argTrue;
}
}
} else if ([op isEqualToString:@"hasprefix"] || [op isEqualToString:@"nothasprefix"]) {
NSObject *firstVal = [engine resolveVariable:firstArg];
NSObject *secondVal = [engine resolveVariable:secondArg];
if (!firstVal) {
firstVal = firstArg;
}
if (!secondVal) {
secondVal = secondArg;
}
if (firstVal && secondVal) {
//NSLog(@"%@ %@", [NSString stringWithFormat:@"%@", firstVal], [NSString stringWithFormat:@"%@", secondVal]);
argTrue = [[NSString stringWithFormat:@"%@", firstVal] hasPrefix:[NSString stringWithFormat:@"%@", secondVal]];
if ([op isEqualToString:@"nothasprefix"]) {
argTrue = !argTrue;
}
}
}
}
BOOL shouldDisableOutput = *outputEnabled;
if (shouldDisableOutput && !argTrue) {
*outputEnabled = NO;
}
// Create variables.
NSMutableDictionary *ifVars = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:argTrue], IF_ARG_TRUE,
[NSNumber numberWithBool:shouldDisableOutput], DISABLE_OUTPUT,
[NSNumber numberWithBool:elseEncountered], IF_ELSE_SEEN,
nil];
// Set up for-stack frame for this if-statement.
NSMutableDictionary *stackFrame = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRange:markerRange], STACK_START_MARKER_RANGE,
ifVars, IF_VARS,
nil];
[ifStack addObject:stackFrame];
}
} else if ([marker isEqualToString:ELSE]) {
if ([self currentBlock:blockInfo matchesTopOfStack:ifStack]) {
NSMutableDictionary *frame = [(NSDictionary *)[ifStack lastObject] objectForKey:IF_VARS];
BOOL elseSeen = [[frame objectForKey:IF_ELSE_SEEN] boolValue];
BOOL argTrue = [[frame objectForKey:IF_ARG_TRUE] boolValue];
BOOL modifyOutput = [[frame objectForKey:DISABLE_OUTPUT] boolValue];
if (!elseSeen) {
if (modifyOutput) {
// Only make changes if we've not already seen an 'else' for this block,
// and if we're modifying output state at all.
*outputEnabled = !argTrue; // either turning it off, or turning it back on.
}
// Note that we've now seen the else marker.
[frame setObject:[NSNumber numberWithBool:YES] forKey:IF_ELSE_SEEN];
}
}
} else if ([marker isEqualToString:IF_END]) {
if ([self currentBlock:blockInfo matchesTopOfStack:ifStack]) {
NSMutableDictionary *frame = [(NSDictionary *)[ifStack lastObject] objectForKey:IF_VARS];
BOOL modifyOutput = [[frame objectForKey:DISABLE_OUTPUT] boolValue];
if (modifyOutput) {
// If we're modifying output, it was enabled when this block started.
// Thus, it should be enabled after the block ends.
// If it's already enabled, this will have no harmful effect.
*outputEnabled = YES;
}
// End block.
[ifStack removeLastObject];
*blockEnded = YES;
}
*blockEnded = YES;
} else if ([marker isEqualToString:NOW]) {
return [NSDate date];
} else if ([marker isEqualToString:COMMENT_START]) {
// Work out if we need to start a block.
if (!args || [args count] == 0) {
*blockStarted = YES;
// Determine appropriate values for outputEnabled and for our stack frame.
BOOL shouldDisableOutput = *outputEnabled;
if (shouldDisableOutput) {
*outputEnabled = NO;
}
// Set up for-stack frame for this if-statement.
NSMutableDictionary *stackFrame = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRange:markerRange], STACK_START_MARKER_RANGE,
[NSNumber numberWithBool:shouldDisableOutput], DISABLE_OUTPUT,
nil];
[commentStack addObject:stackFrame];
}
} else if ([marker isEqualToString:COMMENT_END]) {
// Check this is block on top of stack.
if ([self currentBlock:blockInfo matchesTopOfStack:commentStack]) {
NSMutableDictionary *frame = [commentStack lastObject];
BOOL modifyOutput = [[frame objectForKey:DISABLE_OUTPUT] boolValue];
if (modifyOutput) {
// If we're modifying output, it was enabled when this block started.
// Thus, it should be enabled after the block ends.
// If it's already enabled, this will have no harmful effect.
*outputEnabled = YES;
}
// End block.
[commentStack removeLastObject];
*blockEnded = YES;
}
*blockEnded = YES;
} else if ([marker isEqualToString:LOAD]) {
if (args && [args count] > 0) {
for (NSString *className in args) {
Class class = NSClassFromString(className);
if (class && [(id)class isKindOfClass:[NSObject class]]) {
if ([class conformsToProtocol:@protocol(MGTemplateFilter)]) {
// Instantiate and load filter.
NSObject <MGTemplateFilter> *obj = [[class alloc] init];
[engine loadFilter:obj];
} else if ([class conformsToProtocol:@protocol(MGTemplateMarker)]) {
// Instantiate and load marker.
NSObject <MGTemplateMarker> *obj = [[class alloc] initWithTemplateEngine:engine];
[engine loadMarker:obj];
}
}
}
}
} else if ([marker isEqualToString:CYCLE]) {
if (args && [args count] > 0) {
// Check to see if it's an existing cycle.
NSString *rangeKey = NSStringFromRange(markerRange);
NSMutableDictionary *cycle = [cycles objectForKey:rangeKey];
if (cycle) {
NSArray *vals = [cycle objectForKey:CYCLE_VALUES];
NSUInteger currIndex = [[cycle objectForKey:CYCLE_INDEX] unsignedIntegerValue];
currIndex++;
if (currIndex >= [vals count]) {
currIndex = 0;
}
[cycle setObject:@(currIndex) forKey:CYCLE_INDEX];
return [vals objectAtIndex:(NSUInteger)currIndex];
} else {
// New cycle. Create and output appropriately.
cycle = [NSMutableDictionary dictionaryWithCapacity:2];
[cycle setObject:[NSNumber numberWithInt:0] forKey:CYCLE_INDEX];
[cycle setObject:args forKey:CYCLE_VALUES];
[cycles setObject:cycle forKey:rangeKey];
return [args objectAtIndex:0];
}
}
} else if ([marker isEqualToString:SET]) {
if (args && [args count] == 2 && *outputEnabled) {
// Set variable arg1 to value arg2.
NSDictionary *newVar = [NSDictionary dictionaryWithObject:[args objectAtIndex:1]
forKey:[args objectAtIndex:0]];
if (newVar) {
*newVariables = newVar;
}
}
}
return nil;
}
- (BOOL)currentBlock:(NSDictionary *)blockInfo matchesTopOfStack:(NSMutableArray *)stack
{
if (blockInfo && [stack count] > 0) { // end-tag should always have blockInfo, and correspond to a stack frame.
NSDictionary *frame = [stack lastObject];
NSRange stackSectionRange = [[frame objectForKey:STACK_START_MARKER_RANGE] rangeValue];
NSRange thisSectionRange = [[blockInfo objectForKey:BLOCK_START_MARKER_RANGE_KEY] rangeValue];
if (NSEqualRanges(stackSectionRange, thisSectionRange)) {
return YES;
}
}
return NO;
}
- (BOOL)argIsTrue:(NSString *)arg
{
BOOL argTrue = NO;
if (arg) {
NSObject *val = [engine resolveVariable:arg];
if (val) {
if ([val isKindOfClass:[NSNumber class]]) {
argTrue = [(NSNumber *)val boolValue];
} else {
argTrue = YES;
}
}
}
return argTrue;
}
- (BOOL)argIsNumeric:(NSString *)arg integerValue:(NSInteger *)val checkVariables:(BOOL)checkVars
{
BOOL numeric = NO;
NSInteger value = 0;
if (arg && [arg length] > 0) {
if ([[arg substringToIndex:1] isEqualToString:@"0"] || [arg integerValue] != 0) {
numeric = YES;
value = [arg integerValue];
} else if (checkVars) {
// Check to see if arg is a variable with an integerValue.
NSObject *argObj = [engine resolveVariable:arg];
NSString *argStr = [NSString stringWithFormat:@"%@", argObj];
if (argObj && [argObj respondsToSelector:@selector(integerValue)] &&
[self argIsNumeric:argStr integerValue:&value checkVariables:NO]) { // avoid recursion
numeric = YES;
}
}
}
if (val) {
*val = value;
}
return numeric;
}
- (void)engineFinishedProcessingTemplate
{
// Clean up stacks etc.
forStack = [[NSMutableArray alloc] init];
sectionStack = [[NSMutableArray alloc] init];
ifStack = [[NSMutableArray alloc] init];
commentStack = [[NSMutableArray alloc] init];
cycles = [[NSMutableDictionary alloc] init];
}
@end