-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocDeclarationReorderer.m
205 lines (171 loc) · 5.55 KB
/
DocDeclarationReorderer.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
/*
Copyright (C) 2010 Quentin Mathe
Author: Quentin Mathe <quentin.mathe@gmail.com>
Date: December 2010
License: Modified BSD (see COPYING)
*/
#import "DocDeclarationReorderer.h"
#import "DocHeader.h"
#import "DocIndex.h"
#import "DocMethod.h"
#import "DocTOCPage.h"
#import "DocPage.h"
static NSString *root = @"root";
@implementation DocDeclarationReorderer
- (id) initWithWeaver: (id <DocWeaving>)aWeaver
orderedSymbols: (NSDictionary *)symbolArraysByKind
{
NILARG_EXCEPTION_TEST(aWeaver);
NILARG_EXCEPTION_TEST(symbolArraysByKind);
SUPERINIT;
weaver = aWeaver;
orderedSymbols = symbolArraysByKind;
accumulatedDocElements =
[[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSMutableArray array], root, nil];
currentConstructName = root;
return self;
}
- (void) dealloc
{
weaver = nil;
orderedSymbols = nil;
accumulatedDocElements = nil;
}
// FIXME: Forwarding seems to be broken with -performSelector:withObject:
- (id) forwardingTargetForSelector: (SEL)aSelector
{
return weaver;
}
- (void) forwardInvocation: (NSInvocation *)anInv
{
[anInv invokeWithTarget: weaver];
}
- (NSArray *) orderedSymbolsWithinConstructNamed: (NSString *)aConstructName
{
return [orderedSymbols objectForKey: aConstructName];
}
- (void) weaveAccumulatedDocElementsForConstructNamed: (NSString *)aConstructName
{
NSArray *docElements = [accumulatedDocElements objectForKey: aConstructName];
NSArray *docElementSymbols = [docElements valueForKey: @"refMarkup"];
NSString *currentTaskUnit = nil;
ETAssert([docElements count] == [[self orderedSymbolsWithinConstructNamed: aConstructName] count]);
for (NSString *symbol in [self orderedSymbolsWithinConstructNamed: aConstructName])
{
NSInteger symbolIndex = [docElementSymbols indexOfObject: symbol];
if (NSNotFound == symbolIndex)
{
continue;
//FIXME: Do some sane thing if the symbol wasn't found.
}
DocMethod *docElement = [docElements objectAtIndex: symbolIndex];
BOOL hasDefaultTask = [[docElement task] isEqualToString: [[docElement class] defaultTask]];
currentTaskUnit = ([docElement taskUnit] != nil ? [docElement taskUnit] : currentTaskUnit);
if (currentTaskUnit != nil && hasDefaultTask)
{
[docElement setTask: currentTaskUnit];
}
[weaver weaveMethod: docElement];
}
}
- (void) resetAccumulatedDocElements
{
accumulatedDocElements = [NSMutableDictionary dictionaryWithObject:
[accumulatedDocElements objectForKey: root] forKey: root];
}
- (void) flushAccumulatedDocElementsForConstructNamed: (NSString *)aName
{
if (currentConstructName == nil)
return;
[self weaveAccumulatedDocElementsForConstructNamed: aName];
[self resetAccumulatedDocElements];
}
- (void) accumulateDocElement: (DocElement *)anElement
{
NSMutableArray *elements = [accumulatedDocElements objectForKey: currentConstructName];
if (elements == nil)
{
elements = [NSMutableArray array];
[accumulatedDocElements setObject: elements forKey: currentConstructName];
}
[elements addObject: anElement];
}
- (void) weaveHeader: (DocHeader *)aHeader
{
currentConstructName = root;
[weaver weaveHeader: aHeader];
}
- (void) weaveClassNamed: (NSString *)aClassName
superclassName: (NSString *)aSuperclassName
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = aClassName;
[weaver weaveClassNamed: aClassName superclassName: aSuperclassName];
}
- (void) weaveProtocolNamed: (NSString *)aProtocolName
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
NSString *symbol = [NSString stringWithFormat: @"(%@)", aProtocolName];
currentConstructName = symbol;
[weaver weaveProtocolNamed: aProtocolName];
}
- (void) weaveCategoryNamed: (NSString *)aCategoryName
className: (NSString *)aClassName
isInformalProtocol: (BOOL)isInformalProtocol
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
NSString *symbol = [NSString stringWithFormat: @"%@(%@)", aClassName, aCategoryName];
currentConstructName = symbol;
[weaver weaveCategoryNamed: aCategoryName className: aClassName isInformalProtocol: isInformalProtocol];
}
- (void) weaveMethod: (DocMethod *)aMethod
{
[self accumulateDocElement: aMethod];
//[weaver weaveMethod: aMethod];
}
- (void) weaveFunction: (DocFunction *)aFunction
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveFunction: aFunction];
}
- (void) weaveMacro: (DocMacro *)aMacro
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveMacro: aMacro];
}
- (void) weaveConstant: (DocConstant *)aConstant
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveConstant: aConstant];
}
- (void) weaveOtherDataType: (DocCDataType *)aDataType
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveOtherDataType: aDataType];
}
- (void) finishWeaving
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
}
- (DocHeader *) currentHeader
{
return [weaver currentHeader];
}
- (void) weaveIVar:(DocIVar *)anIVar
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveIVar: anIVar];
}
- (void) weaveProperty:(DocProperty *)aProperty
{
[self flushAccumulatedDocElementsForConstructNamed: currentConstructName];
currentConstructName = root;
[weaver weaveProperty: aProperty];
}
@end