-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_MiscMergeElseifCommand.m
executable file
·81 lines (72 loc) · 2.54 KB
/
_MiscMergeElseifCommand.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
//
// _MiscMergeElseifCommand.m
//
// Written by Don Yacktman and Carl Lindberg
//
// Copyright 2001-2004 by Don Yacktman and Carl Lindberg.
// All rights reserved.
//
// This notice may not be removed from this source code.
//
// This header is included in the MiscKit by permission from the author
// and its use is governed by the MiscKit license, found in the file
// "License.rtf" in the MiscKit distribution. Please refer to that file
// for a list of all applicable permissions and restrictions.
//
#import "_MiscMergeElseifCommand.h"
//#import <Foundation/NSUtilities.h>
#import <stdlib.h> //for NULL on OSXPB
#import "NSScanner+MiscMerge.h"
#import "MiscMergeCommandBlock.h"
@implementation _MiscMergeElseifCommand
- (BOOL)parseFromScanner:(NSScanner *)aScanner template:(MiscMergeTemplate *)template
{
MiscMergeCommandBlock *parentIfBlock = [template currentCommandBlock];
_MiscMergeIfCommand *parentIfCommand = [parentIfBlock owner];
if (![parentIfCommand isKindOfCommandClass:@"If"])
{
[template reportParseError:@"Mismatched elseif command"];
}
else
{
/*
* This is a tad messy. We are already included in the parent if's
* "true" block, so we need to remove it from there, apply an
* "else", then add ourselves to the "else" block. This way
* we convert the "elseif" to a nested "if" inside the parent's else
* block.
*/
[parentIfBlock removeCommand:self];
[parentIfCommand handleElseInTemplate:template];
[[template currentCommandBlock] addCommand:self];
}
/*
* Scan past the "else", so we can treat the rest as a normal if
* statement. A bit of a hack, but it works.
*/
[aScanner scanString:@"else" intoString:NULL];
return [super parseFromScanner:aScanner template:template];
}
/*
* Override to pop our block, and pop our parent's block as well (since we
* are nested inside another if statement). If parent is another elseif
* command, then the process will repeat until we get to the original if
* command.
*/
- (void)handleEndifInTemplate:(MiscMergeTemplate *)template
{
_MiscMergeIfCommand *parentIfCommand;
// first pop our command block
[super handleEndifInTemplate:template];
// then pop our parent's block
parentIfCommand = [[template currentCommandBlock] owner];
if (![parentIfCommand isKindOfCommandClass:@"If"])
{
[template reportParseError:@"Mismatched endif command"];
}
else
{
[parentIfCommand handleEndifInTemplate:template];
}
}
@end