Skip to content

Commit

Permalink
Some further optimisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
beelsebob committed Aug 23, 2012
1 parent d4e0e5f commit 39c3e31
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CoreParse/Grammar/CPGrammarInternal.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ - (NSSet *)lr0GotoKernelWithItems:(NSSet *)i symbol:(CPGrammarSymbol *)symbol
{ {
return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop) return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop)
{ {
return [symbol isEqual:[item nextSymbol]]; return [symbol isEqualToGrammarSymbol:[item nextSymbol]];
}] }]
map:^ id (CPItem *item) map:^ id (CPItem *item)
{ {
Expand Down Expand Up @@ -161,7 +161,7 @@ - (NSSet *)lr1GotoKernelWithItems:(NSSet *)i symbol:(CPGrammarSymbol *)symbol
{ {
return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop) return [[i objectsPassingTest:^ BOOL (CPItem *item, BOOL *stop)
{ {
return [symbol isEqual:[item nextSymbol]]; return [symbol isEqualToGrammarSymbol:[item nextSymbol]];
}] }]
map:^ id (CPItem *item) map:^ id (CPItem *item)
{ {
Expand Down
7 changes: 7 additions & 0 deletions CoreParse/Grammar/CPGrammarSymbol.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
*/ */
@property (readwrite, assign, getter=isTerminal) BOOL terminal; @property (readwrite, assign, getter=isTerminal) BOOL terminal;


/**
* Determines whether the grammar symbol is equal to another.
* @param object The other grammar symbol to compare.
* @return Whether the two symbols are equal.
*/
- (BOOL)isEqualToGrammarSymbol:(CPGrammarSymbol *)object;

@end @end


@interface NSObject (CPGrammarSymbol) @interface NSObject (CPGrammarSymbol)
Expand Down
5 changes: 5 additions & 0 deletions CoreParse/Grammar/CPGrammarSymbol.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ - (BOOL)isEqual:(id)object
[((CPGrammarSymbol *)object)->name isEqualToString:name]); [((CPGrammarSymbol *)object)->name isEqualToString:name]);
} }


- (BOOL)isEqualToGrammarSymbol:(CPGrammarSymbol *)object
{
return (object != nil && object->terminal == terminal && [object->name isEqualToString:name]);
}

- (NSUInteger)hash - (NSUInteger)hash
{ {
return [[self name] hash]; return [[self name] hash];
Expand Down
2 changes: 1 addition & 1 deletion CoreParse/Parsers/CPShiftReduceParsers/CPItem.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (BOOL)isEqual:(id)object


- (BOOL)isEqualToItem:(CPItem *)item - (BOOL)isEqualToItem:(CPItem *)item
{ {
return item->position == position && item->rule == rule; return item != nil && item->position == position && item->rule == rule;
} }


- (NSUInteger)hash - (NSUInteger)hash
Expand Down
2 changes: 1 addition & 1 deletion CoreParse/Parsers/CPShiftReduceParsers/CPLR1Item.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (BOOL)isEqual:(id)object
{ {
return ([object isLR1Item] && return ([object isLR1Item] &&
[super isEqualToItem:(CPLR1Item *)object] && [super isEqualToItem:(CPLR1Item *)object] &&
[((CPLR1Item *)object)->terminal isEqual:terminal]); [((CPLR1Item *)object)->terminal isEqualToGrammarSymbol:terminal]);
} }


- (NSUInteger)hash - (NSUInteger)hash
Expand Down
2 changes: 2 additions & 0 deletions CoreParse/Parsers/CPShiftReduceParsers/CPShiftReduceAction.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@


- (NSString *)descriptionWithGrammar:(CPGrammar *)g; - (NSString *)descriptionWithGrammar:(CPGrammar *)g;


- (BOOL)isEqualToShiftReduceAction:(CPShiftReduceAction *)object;

@end @end


@interface NSObject (CPIsShiftReduceAction) @interface NSObject (CPIsShiftReduceAction)
Expand Down
18 changes: 18 additions & 0 deletions CoreParse/Parsers/CPShiftReduceParsers/CPShiftReduceAction.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ - (BOOL)isEqual:(id)object
return NO; return NO;
} }


- (BOOL)isEqualToShiftReduceAction:(CPShiftReduceAction *)object
{
if (object != nil && object->type == type)
{
switch (type)
{
case kActionTypeShift:
return [object newState] == details.shift;
case kActionTypeReduce:
return [object reductionRule] == details.reductionRule;
case kActionTypeAccept:
return YES;
}
}

return NO;
}

- (NSString *)description - (NSString *)description
{ {
switch (type) switch (type)
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


#import "CPItem.h" #import "CPItem.h"
#import "CPGrammarSymbol.h" #import "CPGrammarSymbol.h"
#import "CPShiftReduceAction.h"


@implementation CPShiftReduceActionTable @implementation CPShiftReduceActionTable
{ {
Expand Down Expand Up @@ -74,7 +75,7 @@ - (void)dealloc
- (BOOL)setAction:(CPShiftReduceAction *)action forState:(NSUInteger)state name:(NSString *)token - (BOOL)setAction:(CPShiftReduceAction *)action forState:(NSUInteger)state name:(NSString *)token
{ {
NSMutableDictionary *row = table[state]; NSMutableDictionary *row = table[state];
if (nil != [row objectForKey:token] && ![[row objectForKey:token] isEqual:action]) if (nil != [row objectForKey:token] && ![[row objectForKey:token] isEqualToShiftReduceAction:action])
{ {
return NO; return NO;
} }
Expand Down

0 comments on commit 39c3e31

Please sign in to comment.