Browse files

Added delegate callback for failing parses, Fixes #5

  • Loading branch information...
1 parent a633c94 commit 1ccc6bd3b4c3e3ddd89385d5c55dcb225c20699a @beelsebob beelsebob committed Feb 5, 2012
View
2 CoreParse.xcodeproj/project.pbxproj
@@ -182,7 +182,7 @@
1F0E8916130462F300537D04 /* CoreParseTests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CoreParseTests-Prefix.pch"; sourceTree = "<group>"; };
1F0E8917130462F300537D04 /* CoreParseTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CoreParseTests.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
1F0E8919130462F300537D04 /* CoreParseTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = CoreParseTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
- 1F0E8934130463D000537D04 /* CoreParse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CoreParse.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
+ 1F0E8934130463D000537D04 /* CoreParse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CoreParse.h; sourceTree = "<group>"; };
1F0E8937130463E900537D04 /* CPTokeniser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CPTokeniser.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
1F0E8938130463E900537D04 /* CPTokeniser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = CPTokeniser.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
1F0E8941130466CF00537D04 /* CPTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CPTokenStream.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
View
10 CoreParse/Parsers/CPParser.h
@@ -52,6 +52,16 @@
*/
- (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree;
+@optional
+
+/**
+ * Called when the parser encounters a token for which it can not shift, reduce or accept.
+ *
+ * @param parser The parser which produced the syntax tree.
+ * @param inputStream The input stream containing the token the parser could not cope with.
+ */
+- (void)parser:(CPParser *)parser didEncounterErrorOnInput:(CPTokenStream *)inputStream;
+
@end
/**
View
9 CoreParse/Parsers/CPShiftReduceParser.m
@@ -151,7 +151,14 @@ - (id)parse:(CPTokenStream *)tokenStream
- (void)error:(CPTokenStream *)tokenStream
{
- NSLog(@"Parse error on input %@", tokenStream);
+ if ([[self delegate] respondsToSelector:@selector(parser:didEncounterErrorOnInput:)])
+ {
+ [[self delegate] parser:self didEncounterErrorOnInput:tokenStream];
+ }
+ else
+ {
+ NSLog(@"Parse error on input %@", tokenStream);
+ }
}
- (CPShiftReduceAction *)actionForState:(NSUInteger)state token:(CPToken *)token
View
4 CoreParseTests/CPTestErrorHandlingDelegate.h
@@ -10,6 +10,8 @@
#import "CoreParse.h"
-@interface CPTestErrorHandlingDelegate : NSObject <CPTokeniserDelegate>
+@interface CPTestErrorHandlingDelegate : NSObject <CPTokeniserDelegate, CPParserDelegate>
+
+@property (readwrite, assign) BOOL hasEncounteredError;
@end
View
12 CoreParseTests/CPTestErrorHandlingDelegate.m
@@ -10,6 +10,8 @@
@implementation CPTestErrorHandlingDelegate
+@synthesize hasEncounteredError;
+
- (BOOL)tokeniser:(CPTokeniser *)tokeniser shouldConsumeToken:(CPToken *)token
{
return YES;
@@ -27,4 +29,14 @@ - (NSUInteger)tokeniser:(CPTokeniser *)tokeniser didNotFindTokenOnInput:(NSStrin
return nextSlashStar.location;
}
+- (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree
+{
+ return syntaxTree;
+}
+
+- (void)parser:(CPParser *)parser didEncounterErrorOnInput:(CPTokenStream *)inputStream
+{
+ hasEncounteredError = YES;
+}
+
@end
View
15 CoreParseTests/CoreParseTests.m
@@ -714,5 +714,20 @@ - (void)testEncodingAndDecodingOfParsers
[self tearDownMapCSS];
}
+
+- (void)testParserErrors
+{
+ CPTokeniser *tokeniser = [[[CPTokeniser alloc] init] autorelease];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"a"]];
+ [tokeniser addTokenRecogniser:[CPKeywordRecogniser recogniserForKeyword:@"b"]];
+ CPTokenStream *tokenStream = [tokeniser tokenise:@"baab"];
+ NSString *starGrammarString = @"A ::= 'b''a'*;";
+ CPGrammar *starGrammar = [CPGrammar grammarWithStart:@"A" backusNaurForm:starGrammarString];
+ CPParser *starParser = [CPLALR1Parser parserWithGrammar:starGrammar];
+ CPTestErrorHandlingDelegate *errorDelegate = [[[CPTestErrorHandlingDelegate alloc] init] autorelease];
+ [starParser setDelegate:errorDelegate];
+ [starParser parse:tokenStream];
+ STAssertTrue([errorDelegate hasEncounteredError], @"Error did not get reported to delegate", nil);
+}
@end

0 comments on commit 1ccc6bd

Please sign in to comment.