summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomguy3 <randomguy3@7c02a99a-9b00-45e3-bf44-6f3dd7fddb64>2012-03-10 16:16:25 +0000
committerrandomguy3 <randomguy3@7c02a99a-9b00-45e3-bf44-6f3dd7fddb64>2012-03-10 16:16:25 +0000
commit84d4bc6f0f38e237f2a40faaf75e921211ab0ac6 (patch)
treed3a644fed47ea2b24cf6c9a50480bd766f036bc9
parent4f15081a2cdf33c1ec6ba8c3c90ae81a7d4b42e8 (diff)
Better parse error reporting
git-svn-id: https://tikzit.svn.sourceforge.net/svnroot/tikzit/trunk@424 7c02a99a-9b00-45e3-bf44-6f3dd7fddb64
-rw-r--r--tikzit/src/common/NSError+Tikzit.h3
-rw-r--r--tikzit/src/common/TikzGraphAssembler.h4
-rw-r--r--tikzit/src/common/TikzGraphAssembler.m15
-rw-r--r--tikzit/src/gtk/MainWindow.m12
-rw-r--r--tikzit/src/gtk/TikzDocument.h7
-rw-r--r--tikzit/src/gtk/TikzDocument.m82
6 files changed, 96 insertions, 27 deletions
diff --git a/tikzit/src/common/NSError+Tikzit.h b/tikzit/src/common/NSError+Tikzit.h
index a82db4d..0f45fba 100644
--- a/tikzit/src/common/NSError+Tikzit.h
+++ b/tikzit/src/common/NSError+Tikzit.h
@@ -25,7 +25,8 @@ enum {
TZ_ERR_BADFORMAT,
TZ_ERR_IO,
TZ_ERR_TOOL_FAILED,
- TZ_ERR_NOTDIRECTORY
+ TZ_ERR_NOTDIRECTORY,
+ TZ_ERR_PARSE
};
NSString* const TZToolOutputErrorKey;
diff --git a/tikzit/src/common/TikzGraphAssembler.h b/tikzit/src/common/TikzGraphAssembler.h
index e976405..1b006dd 100644
--- a/tikzit/src/common/TikzGraphAssembler.h
+++ b/tikzit/src/common/TikzGraphAssembler.h
@@ -29,13 +29,14 @@
Node *currentNode;
Edge *currentEdge;
NSMutableDictionary *nodeMap;
+ NSError *lastError;
}
@property (readonly) Graph *graph;
@property (readonly) GraphElementData *data;
@property (readonly) Node *currentNode;
@property (readonly) Edge *currentEdge;
-
+@property (readonly) NSError *lastError;
- (BOOL)parseTikz:(NSString*)tikz;
- (BOOL)parseTikz:(NSString*)tikz forGraph:(Graph*)gr;
@@ -48,6 +49,7 @@
- (void)finishEdge;
- (void)invalidate;
+- (void)invalidateWithError:(NSError*)error;
+ (void)setup;
+ (TikzGraphAssembler*)currentAssembler;
diff --git a/tikzit/src/common/TikzGraphAssembler.m b/tikzit/src/common/TikzGraphAssembler.m
index 5354710..5e60b05 100644
--- a/tikzit/src/common/TikzGraphAssembler.m
+++ b/tikzit/src/common/TikzGraphAssembler.m
@@ -22,6 +22,7 @@
//
#import "TikzGraphAssembler.h"
+#import "NSError+Tikzit.h"
extern int yyparse(void);
extern int yylex(void);
@@ -36,7 +37,10 @@ static id currentAssembler = nil;
void yyerror(const char *str) {
NSLog(@"Parse error: %s", str);
if (currentAssembler != nil) {
- [currentAssembler invalidate];
+ NSError *error = [NSError
+ errorWithMessage:[NSString stringWithCString:str]
+ code:TZ_ERR_PARSE];
+ [currentAssembler invalidateWithError:error];
}
}
@@ -56,6 +60,7 @@ int yywrap() {
}
- (Graph*)graph { return graph; }
+- (NSError*)lastError { return lastError; }
- (GraphElementData *)data {
if (currentNode != nil) {
@@ -95,6 +100,7 @@ int yywrap() {
yylex_destroy();
[nodeMap release];
+ nodeMap = nil;
currentAssembler = nil;
[pool drain];
@@ -144,12 +150,19 @@ int yywrap() {
- (void)dealloc {
[graph release];
+ [lastError release];
[super dealloc];
}
- (void)invalidate {
[graph release];
graph = nil;
+ lastError = nil;
+}
+
+- (void)invalidateWithError:(NSError*)error {
+ [self invalidate];
+ lastError = [error retain];
}
+ (void)setup {
diff --git a/tikzit/src/gtk/MainWindow.m b/tikzit/src/gtk/MainWindow.m
index 6506125..0043137 100644
--- a/tikzit/src/gtk/MainWindow.m
+++ b/tikzit/src/gtk/MainWindow.m
@@ -521,7 +521,7 @@ static void update_paste_action (GtkClipboard *clipboard, GdkEvent *event, GtkAc
gtk_text_buffer_get_bounds (tikzBuffer, &start, &end);
gchar *text = gtk_text_buffer_get_text (tikzBuffer, &start, &end, FALSE);
- BOOL success = [document setTikz:[NSString stringWithUTF8String:text]];
+ BOOL success = [document updateTikz:[NSString stringWithUTF8String:text] error:NULL];
[self _setHasParseError:!success];
g_free (text);
@@ -766,12 +766,18 @@ static void update_paste_action (GtkClipboard *clipboard, GdkEvent *event, GtkAc
- (void) _forceLoadDocumentFromFile:(NSString*)path {
NSError *error = nil;
- TikzDocument *d = [TikzDocument documentFromFile:path styleManager:styleManager error:&error];
+ TikzDocument *d = [TikzDocument documentFromFile:path
+ styleManager:styleManager
+ error:&error];
if (d != nil) {
[self _setActiveDocument:d];
[[RecentManager defaultManager] addRecentFile:path];
} else {
- [self presentError:error withMessage:@"Could not open file"];
+ if ([error code] == TZ_ERR_PARSE) {
+ [self presentError:error withMessage:@"Invalid file"];
+ } else {
+ [self presentError:error withMessage:@"Could not open file"];
+ }
[[RecentManager defaultManager] removeRecentFile:path];
}
}
diff --git a/tikzit/src/gtk/TikzDocument.h b/tikzit/src/gtk/TikzDocument.h
index 7015168..ddeaf29 100644
--- a/tikzit/src/gtk/TikzDocument.h
+++ b/tikzit/src/gtk/TikzDocument.h
@@ -49,12 +49,12 @@
+ (TikzDocument*) documentWithStyleManager:(StyleManager*)manager;
+ (TikzDocument*) documentWithGraph:(Graph*)g styleManager:(StyleManager*)manager;
-+ (TikzDocument*) documentWithTikz:(NSString*)t styleManager:(StyleManager*)manager;
++ (TikzDocument*) documentWithTikz:(NSString*)t styleManager:(StyleManager*)manager error:(NSError**)error;
+ (TikzDocument*) documentFromFile:(NSString*)path styleManager:(StyleManager*)manager error:(NSError**)error;
- (id) initWithStyleManager:(StyleManager*)manager;
- (id) initWithGraph:(Graph*)g styleManager:(StyleManager*)manager;
-- (id) initWithTikz:(NSString*)t styleManager:(StyleManager*)manager;
+- (id) initWithTikz:(NSString*)t styleManager:(StyleManager*)manager error:(NSError**)error;
- (id) initFromFile:(NSString*)path styleManager:(StyleManager*)manager error:(NSError**)error;
@property (readonly) Graph *graph;
@@ -70,7 +70,8 @@
@property (readonly) NSString *undoName;
@property (readonly) NSString *redoName;
-- (BOOL) setTikz:(NSString*)t;
+- (BOOL) validateTikz:(NSString**)tikz error:(NSError**)error;
+- (BOOL) updateTikz:(NSString*)t error:(NSError**)error;
- (Graph*) cutSelection;
- (Graph*) copySelection;
diff --git a/tikzit/src/gtk/TikzDocument.m b/tikzit/src/gtk/TikzDocument.m
index b17c3c9..a5f1d9f 100644
--- a/tikzit/src/gtk/TikzDocument.m
+++ b/tikzit/src/gtk/TikzDocument.m
@@ -41,20 +41,34 @@
@implementation TikzDocument
-+ (TikzDocument*) documentWithStyleManager:(StyleManager*)manager {
++ (TikzDocument*) documentWithStyleManager:(StyleManager*)manager
+{
return [[[TikzDocument alloc] initWithStyleManager:manager] autorelease];
}
-+ (TikzDocument*) documentWithGraph:(Graph*)g styleManager:(StyleManager*)manager {
- return [[[TikzDocument alloc] initWithGraph:g styleManager:manager] autorelease];
++ (TikzDocument*) documentWithGraph:(Graph*)g
+ styleManager:(StyleManager*)manager
+{
+ return [[[TikzDocument alloc] initWithGraph:g
+ styleManager:manager] autorelease];
}
-+ (TikzDocument*) documentWithTikz:(NSString*)t styleManager:(StyleManager*)manager {
- return [[[TikzDocument alloc] initWithTikz:t styleManager:manager] autorelease];
++ (TikzDocument*) documentWithTikz:(NSString*)t
+ styleManager:(StyleManager*)manager
+ error:(NSError**)error
+{
+ return [[[TikzDocument alloc] initWithTikz:t
+ styleManager:manager
+ error:error] autorelease];
}
-+ (TikzDocument*) documentFromFile:(NSString*)pth styleManager:(StyleManager*)manager error:(NSError**)error {
- return [[[TikzDocument alloc] initFromFile:pth styleManager:manager error:error] autorelease];
++ (TikzDocument*) documentFromFile:(NSString*)pth
+ styleManager:(StyleManager*)manager
+ error:(NSError**)error
+{
+ return [[[TikzDocument alloc] initFromFile:pth
+ styleManager:manager
+ error:error] autorelease];
}
@@ -102,12 +116,19 @@
return self;
}
-- (id) initWithTikz:(NSString*)t styleManager:(StyleManager*)manager {
+- (id) initWithTikz:(NSString*)t
+ styleManager:(StyleManager*)manager
+ error:(NSError**)error
+{
self = [self initWithStyleManager:manager];
if (self) {
[undoManager disableUndoRegistration];
- [self setTikz:t];
+ BOOL success = [self updateTikz:t error:error];
+ if (!success) {
+ [self release];
+ return nil;
+ }
[undoManager enableUndoRegistration];
hasChanges = NO;
}
@@ -115,7 +136,10 @@
return self;
}
-- (id) initFromFile:(NSString*)pth styleManager:(StyleManager*)manager error:(NSError**)error {
+- (id) initFromFile:(NSString*)pth
+ styleManager:(StyleManager*)manager
+ error:(NSError**)error
+{
NSStringEncoding enc; // we can't pass in NULL here...
NSString *t = [NSString stringWithContentsOfFile:pth
usedEncoding:&enc error:error];
@@ -124,7 +148,7 @@
return nil;
}
- self = [self initWithTikz:t styleManager:manager];
+ self = [self initWithTikz:t styleManager:manager error:error];
if (self) {
[self setPath:pth];
@@ -224,10 +248,6 @@
[[NSNotificationCenter defaultCenter] postNotificationName:@"TikzChanged" object:self];
}
-- (void) postParseError {
- [[NSNotificationCenter defaultCenter] postNotificationName:@"ParseError" object:self];
-}
-
- (void) postUndoStackChanged {
[[NSNotificationCenter defaultCenter] postNotificationName:@"UndoStackChanged" object:self];
}
@@ -236,7 +256,27 @@
return tikz;
}
-- (BOOL) setTikz:(NSString*)t {
+- (BOOL) validateTikz:(NSString**)t error:(NSError**)error {
+ if (*t == nil) {
+ return NO;
+ }
+ if (*t == tikz || [*t isEqual:tikz]) {
+ return YES;
+ }
+
+ TikzGraphAssembler *a = [TikzGraphAssembler assembler];
+ BOOL success = [a parseTikz:*t];
+ if (!success && error != NULL) {
+ *error = [a lastError];
+ if (*error == nil) {
+ *error = [NSError errorWithMessage:@"Unknown error"
+ code:TZ_ERR_PARSE];
+ }
+ }
+ return success;
+}
+
+- (BOOL) updateTikz:(NSString*)t error:(NSError**)error {
if (t == nil) {
t = [NSString string];
}
@@ -247,13 +287,19 @@
TikzGraphAssembler *a = [TikzGraphAssembler assembler];
BOOL success = [a parseTikz:t];
if (success) {
- // setTikz actually generates a graph from the tikz,
+ // updateTikz actually generates a graph from the tikz,
// and generates the final tikz from that
[self startUndoGroup];
[self setGraph:[a graph]];
[self nameAndEndUndoGroup:@"Update tikz"];
} else {
- [self postParseError];
+ if (error != NULL) {
+ *error = [a lastError];
+ if (*error == nil) {
+ *error = [NSError errorWithMessage:@"Unknown error"
+ code:TZ_ERR_PARSE];
+ }
+ }
}
return success;