Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 405 lines (356 sloc) 16.674 kb
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
//
// GBMethodData.m
// appledoc
//
// Created by Tomaz Kragelj on 26.7.10.
// Copyright (C) 2010, Gentle Bytes. All rights reserved.
//

#import "GRMustache.h"
#import "GBMethodArgument.h"
#import "GBMethodSectionData.h"
#import "GBMethodData.h"
#import "RegexKitLite.h"

@interface GBMethodData ()

- (NSString *)selectorFromAssignedData;
- (NSString *)prefixedSelectorFromAssignedData;
- (NSString *)selectorDelimiterFromAssignedData;
- (NSString *)prefixFromAssignedData;
- (BOOL)formatTypesFromArray:(NSArray *)types toArray:(NSMutableArray *)array prefix:(NSString *)prefix suffix:(NSString *)suffix;
- (NSDictionary *)formattedComponentWithValue:(NSString *)value;
- (NSDictionary *)formattedComponentWithValue:(NSString *)value style:(NSUInteger)style href:(NSString *)href;
- (NSString *)attributeValueForKey:(NSString *)key;
- (BOOL)validateMergeWith:(GBMethodData *)source;
@property (readonly) NSString *methodSelectorDelimiter;
@property (readonly) NSString *methodPrefix;


@end

#pragma mark -

@implementation GBMethodData

#pragma mark Initialization & disposal

+ (id)methodDataWithType:(GBMethodType)type result:(NSArray *)result arguments:(NSArray *)arguments {
NSParameterAssert([arguments count] >= 1);
return [[[self alloc] initWithType:type attributes:[NSArray array] result:result arguments:arguments] autorelease];
}

+ (id)propertyDataWithAttributes:(NSArray *)attributes components:(NSArray *)components {
NSParameterAssert([components count] >= 2); // At least one return and the name!
    
    // extract return type and property name
    NSString *propertyName = nil;
    NSMutableArray *results = [NSMutableArray array];
    BOOL nextComponentIsBlockPropertyName = NO;
    BOOL nextComponentIsBlockReturnComponent = NO;
BOOL nextComponentIsPropertyName = NO;
    BOOL inProtocolsList = NO;
    NSUInteger parenthesisLevel = 0;
    for (NSString *component in components) {
        if ([component isEqualToString:@"^"]) {
            [results addObject:component];
            nextComponentIsBlockPropertyName = YES;
        } else if (nextComponentIsBlockPropertyName) {
            propertyName = component;
            nextComponentIsBlockPropertyName = NO;
            nextComponentIsBlockReturnComponent = YES;
        } else if (nextComponentIsBlockReturnComponent) {
            if (parenthesisLevel > 0 || [component isEqualToString:@"("]) {
                [results addObject:component];
            }
        } else if ([component isEqualToString:@"<"]) {
            inProtocolsList = YES;
            [results addObject:component];
        } else if ([component isEqualToString:@">"]) {
            inProtocolsList = NO;
            [results addObject:component];
} else if ([component isEqualToString:@"*"]) {
[results addObject:component];
nextComponentIsPropertyName = YES;
} else if ([component isEqualToString:@"id"]) {
[results addObject:component];
        } else if ([component isMatchedByRegex:@"^[_a-zA-Z][_a-zA-Z0-9]$"]) {
if (results.count == 0 || inProtocolsList) {
                [results addObject:component];
            } else if (propertyName == nil || nextComponentIsPropertyName) {
                propertyName = component;
nextComponentIsPropertyName = NO;
            } else {
                // ignore termination macro
            }
        } else if (propertyName == nil) {
if (nextComponentIsPropertyName) {
propertyName = propertyName;
nextComponentIsPropertyName = NO;
} else {
[results addObject:component];
}
        }
        if ([component isEqualToString:@"("]) {
            ++parenthesisLevel;
        } else if ([component isEqualToString:@")"]) {
            --parenthesisLevel;
        }
    }

// In case we end up with no property name, just take the last component...
if (!propertyName) propertyName = [components lastObject];
if ([results containsObject:propertyName]) [results removeObject:propertyName];
    
GBMethodArgument *argument = [GBMethodArgument methodArgumentWithName:propertyName];
return [[[self alloc] initWithType:GBMethodTypeProperty attributes:attributes result:results arguments:[NSArray arrayWithObject:argument]] autorelease];
}

- (id)initWithType:(GBMethodType)type attributes:(NSArray *)attributes result:(NSArray *)result arguments:(NSArray *)arguments {
self = [super init];
if (self) {
_methodType = type;
_methodAttributes = [attributes retain];
_methodResultTypes = [result retain];
_methodArguments = [arguments retain];
_methodPrefix = [[self prefixFromAssignedData] retain];
_methodSelectorDelimiter = [[self selectorDelimiterFromAssignedData] retain];
_methodSelector = [[self selectorFromAssignedData] retain];
        _methodReturnType = (NSString *)self.methodResultTypes.firstObject;
_prefixedMethodSelector = [[self prefixedSelectorFromAssignedData] retain];
}
return self;
}

#pragma mark Formatted components handling

- (NSArray *)formattedComponents {
NSMutableArray *result = [NSMutableArray array];
if (self.methodType == GBMethodTypeProperty) {
// Add property keyword and space.
[result addObject:[self formattedComponentWithValue:@"@property"]];
[result addObject:[self formattedComponentWithValue:@" "]];

// Add the list of attributes.
if ([self.methodAttributes count] > 0) {
__block BOOL isSetterOrGetter = NO;
[result addObject:[self formattedComponentWithValue:@"("]];
[self.methodAttributes enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
[result addObject:[self formattedComponentWithValue:attribute]];
if ([attribute isEqualToString:@"setter"] || [attribute isEqualToString:@"getter"]) {
isSetterOrGetter = YES;
return;
}
if (isSetterOrGetter) {
if ([attribute isEqualToString:@"="]) return;
isSetterOrGetter = NO;
}
if (idx < [self.methodAttributes count]-1) {
[result addObject:[self formattedComponentWithValue:@","]];
[result addObject:[self formattedComponentWithValue:@" "]];
}
}];
[result addObject:[self formattedComponentWithValue:@")"]];
[result addObject:[self formattedComponentWithValue:@" "]];
}

// Add the list of resulting types, append space unless last component was * and property name.
if (![self formatTypesFromArray:self.methodResultTypes toArray:result prefix:nil suffix:nil]) {
[result addObject:[self formattedComponentWithValue:@" "]];
}
[result addObject:[self formattedComponentWithValue:self.methodSelector]];
} else {
// Add prefix.
[result addObject:[self formattedComponentWithValue:(self.methodType == GBMethodTypeInstance) ? @"-" : @"+"]];
[result addObject:[self formattedComponentWithValue:@" "]];

// Add return types, then append all arguments.
[self formatTypesFromArray:self.methodResultTypes toArray:result prefix:@"(" suffix:@")"];
[self.methodArguments enumerateObjectsUsingBlock:^(GBMethodArgument *argument, NSUInteger idx, BOOL *stop) {
[result addObject:[self formattedComponentWithValue:argument.argumentName]];
if (argument.isTyped) {
[result addObject:[self formattedComponentWithValue:@":"]];
[self formatTypesFromArray:argument.argumentTypes toArray:result prefix:@"(" suffix:@")"];
if (argument.argumentVar) [result addObject:[self formattedComponentWithValue:argument.argumentVar style:1 href:nil]];
if (argument.isVariableArg) {
[result addObject:[self formattedComponentWithValue:@","]];
[result addObject:[self formattedComponentWithValue:@" "]];
[result addObject:[self formattedComponentWithValue:@"..." style:1 href:nil]];
}
}
if (idx < [self.methodArguments count]-1) [result addObject:[self formattedComponentWithValue:@" "]];
}];
}
return result;
}

- (BOOL)formatTypesFromArray:(NSArray *)types toArray:(NSMutableArray *)array prefix:(NSString *)prefix suffix:(NSString *)suffix {
BOOL hasValues = [types count] > 0;
if (hasValues && prefix) [array addObject:[self formattedComponentWithValue:prefix]];

__block BOOL lastCompWasPointer = NO;
__block BOOL insideProtocol = NO;
__block BOOL appendSpace = NO;
[types enumerateObjectsUsingBlock:^(NSString *type, NSUInteger idx, BOOL *stop) {
if (appendSpace) [array addObject:[self formattedComponentWithValue:@" "]];
[array addObject:[self formattedComponentWithValue:type]];

// We should not add space after last element or after pointer.
appendSpace = YES;
BOOL isLast = (idx == [types count] - 1);
BOOL isPointer = [type isEqualToString:@"*"];
if (isLast || isPointer) appendSpace = NO;

// We should not add space between components of a protocol (i.e. id<ProtocolName> should be written without any space). Because we've alreay
if (!isLast && [[types objectAtIndex:idx+1] isEqualToString:@"<"])
insideProtocol = YES;
else if ([type isEqualToString:@">"])
insideProtocol = NO;
if (insideProtocol) appendSpace = NO;

lastCompWasPointer = isPointer;
}];

if (hasValues && suffix) [array addObject:[self formattedComponentWithValue:suffix]];
return lastCompWasPointer;
}

- (NSDictionary *)formattedComponentWithValue:(NSString *)value {
return [self formattedComponentWithValue:value style:0 href:nil];
}

- (NSDictionary *)formattedComponentWithValue:(NSString *)value style:(NSUInteger)style href:(NSString *)href {
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:3];
[result setObject:value forKey:@"value"];
if (style > 0) {
[result setObject:[NSNumber numberWithUnsignedInt:style] forKey:@"style"];
[result setObject:[GRYes yes] forKey:@"emphasized"];
}
if (href) [result setObject:href forKey:@"href"];
return result;
}

#pragma mark Helper methods

- (NSString *)selectorFromAssignedData {
NSMutableString *result = [NSMutableString string];
for (GBMethodArgument *argument in self.methodArguments) {
[result appendString:argument.argumentName];
[result appendString:self.methodSelectorDelimiter];
}
return result;
}

- (NSString *)prefixedSelectorFromAssignedData {
NSMutableString *result = [NSMutableString string];
if ([self.methodPrefix length] > 0) [result appendFormat:@"%@ ", self.methodPrefix];
[result appendString:self.methodSelector];
return result;
}

- (NSString *)selectorDelimiterFromAssignedData {
if ([self.methodArguments count] > 1 || [[self.methodArguments lastObject] isTyped]) return @":";
return @"";
}

- (NSString *)prefixFromAssignedData {
switch (self.methodType) {
case GBMethodTypeClass: return @"+";
case GBMethodTypeInstance: return @"-";
case GBMethodTypeProperty: return @"@property";
}
return @"";
}

- (NSString *)propertyGetterSelector {
if (self.methodType != GBMethodTypeProperty) return nil;
NSString *result = [self attributeValueForKey:@"getter"];
if (!result) result = self.methodSelector;
return result;
}

- (NSString *)propertySetterSelector {
if (self.methodType != GBMethodTypeProperty) return nil;
NSString *result = [self attributeValueForKey:@"setter"];
if (!result) {
NSString *firstLetter = [[self.methodSelector substringToIndex:1] uppercaseString];
NSString *theRest = [self.methodSelector substringFromIndex:1];
result = [NSString stringWithFormat:@"set%@%@:", firstLetter, theRest];
}
return result;
}

- (NSString *)propertyType {
    if (self.methodType != GBMethodTypeProperty) return nil;
    NSString *result = (NSString *)self.methodResultTypes.firstObject;
    if (!result) result = self.methodReturnType;
    return result;
}

- (NSString *)attributeValueForKey:(NSString *)key {
// Returns the value after equal sign for the given key (i.e. for attributes "getter", "=", "value", this would return "value"). Returns nil if either key isn't found or isn't followed by equal sign and/or a value.
__block NSString *result = nil;
__block BOOL foundKey = NO;
[self.methodAttributes enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
if ([attribute isEqualToString:key]) {
foundKey = YES;
return;
}
if (foundKey && ![attribute isEqualToString:@"="]) {
result = attribute;
*stop = YES;
}
}];
return result;
}

- (BOOL)validateMergeWith:(GBMethodData *)source {
// Validates merging with the given method. This method raises exception if merging is not allowed based on method types. It takes into account manual propery accessors and mutators! Note that in case class method is being matched with instance, we prevent merging - this is to allow same selectors (due to how we currently handle class/instance methods (i.e. don't distinguish between them when matching by selectors) we simply need to prevent merging taking place in such case).
if (source.methodType != self.methodType) {
GBMethodData *propertyData = nil;
GBMethodData *manualData = nil;
if (self.methodType == GBMethodTypeProperty && source.methodType == GBMethodTypeInstance) {
propertyData = self;
manualData = source;
} else if (self.methodType == GBMethodTypeInstance && source.methodType == GBMethodTypeProperty) {
propertyData = source;
manualData = self;
} else if (self.methodType == GBMethodTypeInstance && source.methodType == GBMethodTypeClass) {
return NO;
} else if (self.methodType == GBMethodTypeClass && source.methodType == GBMethodTypeInstance) {
return NO;
} else if (self.methodType == GBMethodTypeProperty && source.methodType == GBMethodTypeClass) {
return NO;
} else {
[NSException raise:@"Failed merging %@ to %@; method type doesn't match!", source, self];
}

// We should allow if the getter or setter matches and if the getter name is shared to an instance method.
if ([propertyData.propertyGetterSelector isEqualToString:manualData.methodSelector]) return YES;
if ([propertyData.propertySetterSelector isEqualToString:manualData.methodSelector]) return YES;
if (![propertyData.propertyType isEqualToString:manualData.methodReturnType]) return YES;
        [NSException raise:@"Failed merging %@ to %@; getter or setter doesn't match", source, self];
} else {
// If assertion from code below is present, it breaks cases where category declares a property which is also getter for a property from class declaration. See #184 https://github.com/tomaz/appledoc/issues/184 for details. I'm leaving the code commented for the moment to see if it also affects some other user (don't think so, but just in case).
//NSParameterAssert([source.methodSelector isEqualToString:self.methodSelector]);
}
return YES;
}

#pragma mark Overidden methods

- (void)mergeDataFromObject:(id)source {
if (!source || source == self) return;
GBLogDebug(@"%@: Merging data from %@...", self, source);
if (![self validateMergeWith:source]) return;

// Use argument var names from the method that has comment. If no method has comment, just keep deafult.
if ([source comment] && ![self comment]) {
GBLogDebug(@"%@: Checking for difference due to comment status...", self);
for (NSUInteger i=0; i<[self.methodArguments count]; i++) {
GBMethodArgument *ourArgument = [[self methodArguments] objectAtIndex:i];
GBMethodArgument *otherArgument = [[source methodArguments] objectAtIndex:i];
if (![ourArgument.argumentVar isEqualToString:otherArgument.argumentVar]) {
GBLogDebug(@"%@: Changing %ld. argument var name from %@ to %@...", self, i+1, ourArgument.argumentVar, otherArgument.argumentVar);
ourArgument.argumentVar = otherArgument.argumentVar;
}
}
}
[super mergeDataFromObject:source];
}

- (NSString *)description {
if (self.parentObject) {
switch (self.methodType) {
case GBMethodTypeClass:
case GBMethodTypeInstance:
return [NSString stringWithFormat:@"%@[%@ %@]", self.methodPrefix, self.parentObject, self.methodSelector];
case GBMethodTypeProperty:
return [NSString stringWithFormat:@"%@%@.%@", self.methodPrefix, self.parentObject, self.methodSelector];
}
}
return self.methodSelector;
}

#pragma mark Properties

- (BOOL)isInstanceMethod {
return (self.methodType == GBMethodTypeInstance);
}

- (BOOL)isClassMethod {
return (self.methodType == GBMethodTypeClass);
}

- (BOOL)isMethod {
return !self.isProperty;
}

- (BOOL)isProperty {
return (self.methodType == GBMethodTypeProperty);
}

@synthesize methodType = _methodType;
@synthesize methodAttributes = _methodAttributes;
@synthesize methodResultTypes = _methodResultTypes;
@synthesize methodArguments = _methodArguments;
@synthesize methodSelector = _methodSelector;
@synthesize methodReturnType = _methodReturnType;
@synthesize methodSelectorDelimiter = _methodSelectorDelimiter;
@synthesize methodPrefix = _methodPrefix;
@synthesize prefixedMethodSelector = _prefixedMethodSelector;
@synthesize methodSection;
@synthesize isRequired;

@end
Something went wrong with that request. Please try again.