-
Notifications
You must be signed in to change notification settings - Fork 2
/
GSDocParser.m
385 lines (322 loc) · 11.7 KB
/
GSDocParser.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
/*
Copyright (C) 2008 Nicolas Roard
Author: Nicolas Roard
Author: Quentin Mathe <[email protected]>
Date: June 2008
License: Modified BSD (see COPYING)
*/
#import "GSDocParser.h"
#import "DocCDataType.h"
#import "DocHeader.h"
#import "DocMacro.h"
#import "DocMethod.h"
#import "DocFunction.h"
#import "DocHTMLElement.h"
#import "DocDescriptionParser.h"
@interface NullParserDelegate : NSObject <GSDocParserDelegate>
@end
@implementation GSDocParser
- (id) init
{
return [self initWithSourceFile: nil additionalParserFiles: [NSArray array]];
}
- (id) initWithSourceFile: (NSString *)aSourceFile additionalParserFiles: (NSArray *)additionalFiles
{
NILARG_EXCEPTION_TEST(aSourceFile);
INVALIDARG_EXCEPTION_TEST(additionalFiles,
[[additionalFiles pathsMatchingExtensions: [NSArray arrayWithObject: @"igsdoc"]] count] == 1);
SUPERINIT;
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL: [NSURL fileURLWithPath: aSourceFile]];
[xmlParser setDelegate: self];
parserDelegateStack = [[NSMutableArray alloc] initWithObjects: [NSValue valueWithNonretainedObject: self], nil];
indentSpaces = @"";
indentSpaceUnit = @" ";
elementClasses = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[DocHeader class], @"head",
[NullParserDelegate class], @"ivariable",
[DocMethod class], @"method",
[DocFunction class], @"function",
[DocMacro class], @"macro",
[DocCDataType class], @"type",
[DocConstant class], @"constant",
[DocVariable class], @"variable", nil];
symbolElements = [[NSSet alloc] initWithObjects: @"head", @"class", @"protocol", @"category",
@"ivariable", @"method", @"function", @"constant", @"macro", nil];
// NOTE: ref elements are pruned. DocIndex is used instead.
// desc -> dd substitution is not added to the dictionary until we enter a
// deflist, otherwise this would intercept <desc> inside <method>, <class> etc.
substitutionElements = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"ul", @"list",
@"li", @"item", @"ol", @"enum", @"dl", @"deflist", @"dt", @"term", @"", @"ref", @"", @"uref", nil];
// NOTE: var corresponds to GSDoc var and not HTML var
etdocElements = [[NSSet alloc] initWithObjects: @"p", @"code", @"example", @"br", @"em", @"strong", @"var", @"ivar", nil];
escapedCharacters = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"<", @"<", @">", @">", nil];
content = [NSMutableString new];
indexContent = [[NSDictionary alloc] initWithContentsOfFile:
[[additionalFiles pathsMatchingExtensions: [NSArray arrayWithObject: @"igsdoc"]] lastObject]];
return self;
}
- (void) setWeaver: (id <DocWeaving>)aDocWeaver
{
/* The weaver retains the parser */
weaver = aDocWeaver;
}
- (id <DocWeaving>) weaver
{
return weaver;
}
- (void) parseAndWeave
{
[xmlParser parse];
}
- (void) newContent
{
content = [NSMutableString new];
}
- (Class) elementClassForName: (NSString *)anElementName
{
return [elementClasses objectForKey: anElementName];
}
- (id <GSDocParserDelegate>) parserDelegate
{
id parserDelegate = [parserDelegateStack lastObject];
BOOL isWeakRef = [parserDelegate isKindOfClass: [NSValue class]];
return (isWeakRef ? [parserDelegate nonretainedObjectValue] : parserDelegate);
}
- (void) increaseIndentSpaces
{
indentSpaces = [indentSpaces stringByAppendingString: indentSpaceUnit];
}
- (void) decreaseIndentSpaces
{
NSUInteger i = [indentSpaces length] - [indentSpaceUnit length];
ETAssert(i >= 0);
indentSpaces = [indentSpaces substringFromIndex: i];
}
- (void) pushParserDelegate: (id <GSDocParserDelegate>)aDelegate
{
if ([parserDelegateStack lastObject] != aDelegate)
{
[self increaseIndentSpaces];
}
[parserDelegateStack addObject: aDelegate];
}
- (void) popParserDelegate
{
id objectBeforeLast = [parserDelegateStack objectAtIndex: [parserDelegateStack count] - 2];
if ([parserDelegateStack lastObject] != objectBeforeLast)
{
[self decreaseIndentSpaces];
}
[parserDelegateStack removeObjectAtIndex: [parserDelegateStack count] - 1];
}
- (NSString *) indentSpaces
{
return indentSpaces;
}
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
//NSLog (@"%@ parse <%@>", indentSpaces, elementName);
NSString *substituedElement = [substitutionElements objectForKey: elementName];
BOOL removeMarkup = [substituedElement isEqualToString: @""];
BOOL substituteMarkup = (substituedElement != nil && removeMarkup == NO);
BOOL keepMarkup = [etdocElements containsObject: elementName];
/* (1) For GSDoc tags which have equivalent ETDoc tags, we insert their content
enclosed in equivalent ETDoc tags into our content accumulator.
The next handled element can retrieve the accumulated content. For example:
<desc><i>A boat</i> on <j>the</j> river.</desc>
if i and j are GSDoc elements equivalent to x and y in ETDoc, the
accumulated content will be:
<desc><x>A boat</x> on <y>the</y> river.</desc>
(2) For ETDoc tags, we insert them along with their content in our content accumulator.
The next handled element can retrieve the accumulated content. For example:
<desc><i>A boat<i> on <j>the</j> river.</desc>
if i and j are ETDoc elements, the accumulated content will be:
<i>A boat</i>on <j>the</j> river. */
if (removeMarkup)
{
return;
}
else if (substituteMarkup)
{
[content appendString: [NSString stringWithFormat: @"<%@>", substituedElement]];
/* Replace <desc> with <dd> inside <deflist> but not elsewhere */
if ([elementName isEqualToString: @"deflist"])
{
[substitutionElements setObject: @"dd" forKey: @"desc"];
}
return;
}
else if (keepMarkup)
{
[content appendString: [NSString stringWithFormat: @"<%@>", elementName]];
return;
}
currentAttributes = attributeDict;
id parserDelegate = [self parserDelegate];
/* When we have a parser delegate registered for the new element name,
we switch this delegate, otherwise we continue with the current one. */
if ([self elementClassForName: elementName] != nil)
{
parserDelegate = [[self elementClassForName: elementName] new];
}
[self pushParserDelegate: parserDelegate];
/* Discard previously parsed but unused content that belongs to a topmost element.
For example, we want to discard <p></p> below otherwise it gets inserted
at the end of the first arg content.
<chapter>
<heading>ETGetOptionsDictionary functions</heading>
<p></p>
<function type="NSDictionary*" name="ETGetOptionsDictionary">
<arg type="char*">optString</arg> */
if ([symbolElements containsObject: elementName])
{
[content setString: @""];
}
//NSLog(@"%@ Begin <%@>, parser %@", indentSpaces, elementName, [(id)[self parserDelegate] primitiveDescription]);
[[self parserDelegate] parser: self startElement: elementName withAttributes: attributeDict];
}
/* On Mac OS X, this method is called with '<' and '>' as foundCharacters when
the parser encounters < or >.
NSXMLParser automatically unescape common sequences such as < and >, and
no way exists to disable it. */
- (void) parser: (NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSString *escapedCharacter = [escapedCharacters objectForKey: string];
#if 0
NSString *escapedString = [string stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
escapedString = [escpaedString stringByReplacingOccurrencesOfString: @">" withString: @">"];
if ([escapedString isEqual: string] == NO || [string rangeOfString: @"<"].location != NSNotFound)
{
NSLog(@"bla");
}
#endif
[content appendString: (escapedCharacter != nil ? escapedCharacter : string)];
}
- (void) parser: (NSXMLParser *)parser
didEndElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qName
{
NSString* trimmed = [content stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *substituedElement = [substitutionElements objectForKey: elementName];
BOOL removeMarkup = [substituedElement isEqualToString: @""];
BOOL substituteMarkup = (substituedElement != nil && removeMarkup == NO);
BOOL keepMarkup = [etdocElements containsObject: elementName];
/* See comment in -parser:didStartElement:namespaceURI:qualifiedName: */
if (removeMarkup)
{
return;
}
else if (substituteMarkup)
{
[content appendString: [NSString stringWithFormat: @"</%@>", substituedElement]];
if ([elementName isEqualToString: @"deflist"])
{
[substitutionElements removeObjectForKey: @"desc"];
}
return;
}
else if (keepMarkup)
{
[content appendString: [NSString stringWithFormat: @"</%@>", elementName]];
return;
}
[[self parserDelegate] parser: self endElement: elementName withContent: trimmed];
//NSLog(@"%@ End <%@> --> %@", indentSpaces, elementName, trimmed);
[self popParserDelegate];
/* Discard the content accumulated to handle the element which ends. */
[self newContent];
currentAttributes = nil;
}
- (void) parserDidEndDocument: (NSXMLParser *)parser
{
[weaver finishWeaving];
}
- (void) parser: (GSDocParser *)parser
startElement: (NSString *)elementName
withAttributes: (NSDictionary *)attributeDict
{
/* The main parser is responsible to parse the class, category and protocol attributes */
if ([elementName isEqualToString: @"class"])
{
[weaver weaveClassNamed: [attributeDict objectForKey: @"name"]
superclassName: [attributeDict objectForKey: @"super"]];
}
else if ([elementName isEqualToString: @"category"])
{
BOOL isInformalProtocol =
[self isInformalProtocolSymbolName: [attributeDict objectForKey: @"name"]];
[weaver weaveCategoryNamed: [attributeDict objectForKey: @"name"]
className: [attributeDict objectForKey: @"class"]
isInformalProtocol: isInformalProtocol];
}
if ([elementName isEqualToString: @"protocol"])
{
[weaver weaveProtocolNamed: [attributeDict objectForKey: @"name"]];
}
}
- (void) parser: (GSDocParser *)parser
endElement: (NSString *)elementName
withContent: (NSString *)trimmedContent
{
/* When we parse a class, we parse the declared child element too */
if ([elementName isEqualToString: @"declared"])
{
ETAssert(nil != [weaver currentHeader]);
[[weaver currentHeader] setDeclaredIn: trimmedContent];
}
else if ([elementName isEqualToString: @"conform"])
{
ETAssert(nil != [weaver currentHeader]);
[[weaver currentHeader] addAdoptedProtocolName: trimmedContent];
}
else if ([elementName isEqualToString: @"desc"])
{
DocHeader *currentHeader = [weaver currentHeader];
ETAssert(nil != currentHeader);
DocMethodGroupDescriptionParser *descParser = [DocMethodGroupDescriptionParser new];
[descParser parse: trimmedContent];
[currentHeader setGroup: [descParser group]];
if (IS_NIL_OR_EMPTY_STR([descParser abstract]) == NO)
{
[currentHeader setAbstract: [descParser abstract]];
}
[currentHeader setOverview: [descParser description]];
}
}
- (NSDictionary *) currentAttributes
{
NSParameterAssert(nil != currentAttributes);
return currentAttributes;
}
- (NSString *) argTypeFromArgsAttributes: (NSDictionary *)attributeDict
{
NSString *argType = [attributeDict objectForKey: @"type"];
if (argType == nil)
return @"";
return [argType stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (BOOL) isInformalProtocolSymbolName: (NSString *)aSymbolName
{
ETAssert(indexContent != nil);
return ([[indexContent objectForKey: @"protocol"] objectForKey: aSymbolName] != nil);
}
@end
@implementation NullParserDelegate
- (void) parser: (GSDocParser *)parser
startElement: (NSString *)elementName
withAttributes: (NSDictionary *)attributeDict
{
}
- (void) parser: (GSDocParser *)parser
endElement: (NSString *)elementName
withContent: (NSString *)trimmed
{
}
@end