Permalink
Browse files

Merge pull request #21 from aspitz/master

conformsToProtocol switch
  • Loading branch information...
2 parents 5e3afaa + 376212f commit 527833b689460dafa510e644df6c53be3cc96373 @beelsebob committed Oct 6, 2012
@@ -75,7 +75,7 @@ - (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree
{
NSString *name = [(CPIdentifierToken *)[children objectAtIndex:0] identifier];
Class c = NSClassFromString(name);
- CPRule *rule = nil == c || !class_conformsToProtocol(c, @protocol(CPParseResult)) ? [CPRule ruleWithName:name rightHandSideElements:rhs] : [CPRule ruleWithName:name rightHandSideElements:rhs representitiveClass:c];
+ CPRule *rule = nil == c || ![c conformsToProtocol:@protocol(CPParseResult)] ? [CPRule ruleWithName:name rightHandSideElements:rhs] : [CPRule ruleWithName:name rightHandSideElements:rhs representitiveClass:c];
[rules addObject:rule];
}
return rules;
@@ -889,4 +889,23 @@ - (void)testErrorRecovery
STAssertEquals([result intValue], 45, @"Parsed expression had incorrect value", nil);
}
+- (void)testConformsToProtocol
+{
+ CPTokeniser *tokeniser = [[[CPTokeniser alloc] init] autorelease];
+ [tokeniser addTokenRecogniser:[CPNumberRecogniser integerRecogniser]];
+ [tokeniser addTokenRecogniser:[CPWhiteSpaceRecogniser whiteSpaceRecogniser]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"+"]];
+ [tokeniser setDelegate:[[[CPTestWhiteSpaceIgnoringDelegate alloc] init] autorelease]];
+ CPTokenStream *tokenStream = [tokeniser tokenise:@"5 + 9 + 2 + 7"];
+
+ NSString *testGrammar =
+ @"Expression2 ::= <Term2> | <Expression2> '+' <Term2>;"
+ @"Term2 ::= 'Number';";
+ CPGrammar *grammar = [CPGrammar grammarWithStart:@"Expression2" backusNaurForm:testGrammar error:NULL];
+ CPParser *parser = [CPSLRParser parserWithGrammar:grammar];
+ Expression *e = [parser parse:tokenStream];
+
+ STAssertEquals([e value], 23.0f, @"'conformToProtocol' failed", [e value]);
+}
+
@end
@@ -0,0 +1,16 @@
+//
+// Expression2.h
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <CoreParse/CoreParse.h>
+
+@interface Expression2 : NSObject <CPParseResult>
+
+@property (readwrite,assign) float value;
+
+@end
@@ -0,0 +1,41 @@
+//
+// Expression2.m
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "Expression2.h"
+#import "Term2.h"
+
+@implementation Expression2
+
+@synthesize value;
+
+- (id)initWithSyntaxTree:(CPSyntaxTree *)syntaxTree{
+ self = [self init];
+
+ if (nil != self){
+ NSArray *components = [syntaxTree children];
+ if ([components count] == 1){
+ NSObject *term2 = [components objectAtIndex:0];
+ if ([term2 isMemberOfClass:[Term2 class]]){
+ self.value = [(Term2 *)term2 value];
+ } else {
+ self.value = -1;
+ }
+ } else {
+ NSObject *term2 = [components objectAtIndex:2];
+ if ([term2 isMemberOfClass:[Term2 class]]){
+ self.value = [(Expression2 *)[components objectAtIndex:0] value] + [(Term2 *)term2 value];
+ } else {
+ self.value = -1;
+ }
+ }
+ }
+
+ return self;
+}
+
+@end
@@ -0,0 +1,14 @@
+//
+// RuleBase.h
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <CoreParse/CoreParse.h>
+
+@interface RuleBase : NSObject <CPParseResult>
+
+@end
@@ -0,0 +1,21 @@
+//
+// RuleBase.m
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "RuleBase.h"
+
+@implementation RuleBase
+
+- (id)initWithSyntaxTree:(CPSyntaxTree *)syntaxTree{
+ self = [super init];
+ if (nil != self){
+ }
+
+ return self;
+}
+
+@end
@@ -0,0 +1,15 @@
+//
+// Term2.h
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "RuleBase.h"
+
+@interface Term2 : RuleBase
+
+@property (readwrite,assign) float value;
+
+@end
@@ -0,0 +1,24 @@
+//
+// Term2.m
+// CoreParse
+//
+// Created by Ayal Spitz on 10/4/12.
+// Copyright (c) 2012 In The Beginning... All rights reserved.
+//
+
+#import "Term2.h"
+
+@implementation Term2
+
+@synthesize value;
+
+- (id)initWithSyntaxTree:(CPSyntaxTree *)syntaxTree{
+ self = [super init];
+ if (nil != self){
+ [self setValue:[[(CPNumberToken *)[[syntaxTree children] objectAtIndex:0] number] floatValue]];
+ }
+
+ return self;
+}
+
+@end

0 comments on commit 527833b

Please sign in to comment.