-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocParameter.m
264 lines (220 loc) · 5.73 KB
/
DocParameter.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
/*
Copyright (C) 2008 Nicolas Roard
Author: Nicolas Roard
Author: Quentin Mathe <[email protected]>
Date: June 2008
License: Modified BSD (see COPYING)
*/
#import "DocParameter.h"
#import "DocIndex.h"
#import "DocHTMLElement.h"
@implementation DocParameter
@synthesize name, type, description, typePrefix, className, protocolName, typeSuffix;
- (id) initWithName: (NSString *) aName type: (NSString *) aType
{
SUPERINIT;
[self setName: aName];
[self setType: aType];
return self;
}
- (void) dealloc
{
name = nil;
type = nil;
description = nil;
typePrefix = nil;
className = nil;
protocolName = nil;
typeSuffix = nil;
}
+ (id) parameterWithName: (NSString *) aName type: (NSString *) aType
{
return [[DocParameter alloc] initWithName: aName type: aType];
}
- (void) setName: (NSString *) aName
{
name = aName;
}
static inline BOOL isDelimiter(char c)
{
return (c == ' ' || c == '*' || c == '<' || c == '>');
}
static inline unsigned int parseTypePrefix(char *rawType, unsigned int length, NSString __strong **typePrefix)
{
char *symbol = calloc(sizeof(char), length);;
int i;
BOOL foundProtocolOrClassName = NO;
for (i = 0; i < length; i++)
{
if (rawType[i] == '*')
break;
if (rawType[i] == '<' || isupper(rawType[i]))
{
foundProtocolOrClassName = YES;
break;
}
symbol[i] = rawType[i];
}
if (foundProtocolOrClassName)
{
*typePrefix = [[NSString alloc] initWithBytes: (const void *)symbol length: i encoding: NSUTF8StringEncoding];
free(symbol);
return i;
}
free(symbol);
return 0;
}
static inline unsigned int parseClassOrProtocolName(char *rawType, unsigned int length, __strong NSString **classOrProtocolName)
{
char *symbol = calloc(sizeof(char), length);
int i;
for (i = 0; i < length; i++)
{
if (isDelimiter(rawType[i]))
break;
symbol[i] = rawType[i];
}
*classOrProtocolName = [[NSString alloc] initWithBytes: (const void *)symbol length: i encoding: NSUTF8StringEncoding];
free(symbol);
return i;
}
static inline unsigned int parseTypeSuffix(char *rawType, unsigned int length, NSString __strong **typeSuffix)
{
char *suffix = calloc(sizeof(char), length);
int i;
for (i = 0; i < length; i++)
{
suffix[i] = rawType[i];
}
*typeSuffix = [[NSString alloc] initWithBytes: (const void *)suffix length: i encoding: NSUTF8StringEncoding];
free(suffix);
return i;
}
/** We support parsing protocol and classe type such as:
<list>
<item>id<ProtocolName></item>
<item>id<ProtocolName>*</item>
<item>ClassName*</item>
<item>ClassName**</item>
<item>ClassName<ProtocolName>*</item>
<item>ClassName<ProtocolName>**</item>
</list>
These declarations can be wrapped with a prefix and suffix too, but this isn't
well tested yet. e.g. const.
We won't parse a protocol name that doesn't begin with a uppercase letter
(parsing code limitation). Doesn't matter so much given we have no way to
recognize a class name without a uppercase letter (unless we use the doc index
to check its existence, but that would make the parsing slower without
observable benefit since ObjC tradition requires class and protocol names to use
camel case). */
- (void) parseType: (NSString *)aType
{
if (aType == nil)
return;
const char *rawType = [aType UTF8String];
unsigned int i = 0;
BOOL isParsingProtocolName = NO;
while (rawType[i] != '\0')
{
char character = rawType[i];
char *unparsedText = (char *)&(rawType[i]);
if (character == '<' || character == '>')
{
/* We skip < and >. typePrefix, classOrProtocolName or typeSuffix
will never contain them.
For '<' let isupper() branch parses the protocol name on next iteration.
For '>' let else branch parses the suffix on next iteration. */
i++;
isParsingProtocolName = YES;
continue;
}
else if (isupper(character))
{
NSString __strong **classOrProtocolName = (isParsingProtocolName ? &protocolName : &className);
i += parseClassOrProtocolName(unparsedText, strlen(unparsedText), classOrProtocolName);
isParsingProtocolName = NO;
}
else if (i == 0)
{
int advancement = parseTypePrefix(unparsedText, strlen(unparsedText), &typePrefix);
if (advancement == 0)
return;
i += advancement;
}
else
{
i += parseTypeSuffix(unparsedText, strlen(unparsedText), &typeSuffix);
}
}
//NSLog(@"Parsed type as prefix %@ class %@ protocol %@ suffix %@", typePrefix, className, protocolName, typeSuffix);
}
- (void) setType: (NSString *) aType
{
type = aType;
[self parseType: aType];
}
- (DocHTMLElement *) HTMLRepresentation
{
ETAssertUnreachable();
return nil;
}
- (DocHTMLElement *) HTMLRepresentationWithParentheses: (BOOL)usesParentheses
{
DocIndex *docIndex = [DocIndex currentIndex];
BOOL isReturnParameter = ([self name] == nil);
H hParam = (isReturnParameter ? [SPAN class: @"returnType"] : [SPAN class: @"type"]);
BOOL hasContent = NO;
if (usesParentheses)
{
[hParam with: @"("];
}
if (typePrefix != nil)
{
[hParam with: typePrefix];
hasContent = YES;
}
if (className != nil)
{
if (hasContent)
{
[hParam addText: @" "];
}
[hParam with: [docIndex linkForClassName: className]];
hasContent = YES;
}
if (protocolName != nil)
{
if (hasContent)
{
[hParam addText: @" "];
}
[hParam with: @"<" and: [docIndex linkForProtocolName: protocolName] and: @">"];
hasContent = YES;
}
if (typeSuffix != nil)
{
if (hasContent)
{
[hParam addText: @" "];
}
[hParam addText: typeSuffix];
}
/* No protocol or class in the type, we insert the raw type */
if (hasContent == NO)
{
[hParam addText: type];
}
if (usesParentheses)
{
[hParam with: @")"];
}
else
{
[hParam with: @" "];
}
if (isReturnParameter)
return hParam;
return [SPAN class: @"parameter" with: hParam
and: [SPAN class: @"arg" with: [self name]]];
}
@end