From 8436b5b5cb4b0d42ca4b12b7af0b5e8cc7a0832a Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 19 Nov 2016 10:51:15 -0600 Subject: [PATCH 1/4] This is for issue #52. This removes the `grammar`<->NSTextView KVO. This change also gets rid of enable/disable state for `textView` since it breaks undo history and I don't think it actually adds any value. Now uses `[self.textView string]` as the authoritative state of the view. After this change undo history is maintained after `generate` or `File`->`Save`. --- ParserGenApp/PGDocument.h | 2 +- ParserGenApp/PGDocument.m | 20 ++++++++++++-------- ParserGenApp/en.lproj/PGDocument.xib | 25 +++++++------------------ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/ParserGenApp/PGDocument.h b/ParserGenApp/PGDocument.h index 02cfbfc..8fb5464 100644 --- a/ParserGenApp/PGDocument.h +++ b/ParserGenApp/PGDocument.h @@ -30,7 +30,7 @@ @property (nonatomic, copy) NSString *destinationPath; @property (nonatomic, copy) NSString *parserName; -@property (nonatomic, copy) NSString *grammar; +//@property (nonatomic, copy) NSString *grammar; @property (nonatomic, assign) BOOL busy; @property (nonatomic, retain) NSError *error; diff --git a/ParserGenApp/PGDocument.m b/ParserGenApp/PGDocument.m index 3f5b2b6..3aa8c05 100644 --- a/ParserGenApp/PGDocument.m +++ b/ParserGenApp/PGDocument.m @@ -48,9 +48,6 @@ - (instancetype)init { self.delegatePreMatchCallbacksOn = PGParserFactoryDelegateCallbacksOnNone; self.delegatePostMatchCallbacksOn = PGParserFactoryDelegateCallbacksOnAll; - - NSString *path = [[NSBundle mainBundle] pathForResource:@"expression" ofType:@"grammar"]; - self.grammar = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; } return self; } @@ -59,7 +56,6 @@ - (instancetype)init { - (void)dealloc { self.destinationPath = nil; self.parserName = nil; - self.grammar = nil; self.textView = nil; @@ -86,6 +82,13 @@ - (NSString *)windowNibName { - (void)windowControllerDidLoadNib:(NSWindowController *)wc { [super windowControllerDidLoadNib:wc]; + NSString *path = [[NSBundle mainBundle] pathForResource:@"expression" ofType:@"grammar"]; + NSString *grammarVal = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + if (grammarVal) { + [self.textView setString:grammarVal]; + } + + // Since maverics the check boxes in interface builder do not work to // turn off smart substitution on dashes and quotes [_textView setAutomaticDashSubstitutionEnabled:NO]; @@ -106,7 +109,8 @@ - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError { NSMutableDictionary *tab = [NSMutableDictionary dictionaryWithCapacity:9]; if (_destinationPath) tab[@"destinationPath"] = _destinationPath; - if (_grammar) tab[@"grammar"] = _grammar; + NSString* grammarVal = [self.textView string]; + if (grammarVal) tab[@"grammar"] = grammarVal; if (_parserName) tab[@"parserName"] = _parserName; tab[@"enableARC"] = @(_enableARC); tab[@"enableHybridDFA"] = @(_enableHybridDFA); @@ -129,7 +133,7 @@ - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError * //NSLog(@"%@", tab); self.destinationPath = tab[@"destinationPath"]; - self.grammar = tab[@"grammar"]; + [self.textView setString:tab[@"grammar"]]; self.parserName = tab[@"parserName"]; self.enableARC = [tab[@"enableARC"] boolValue]; self.enableHybridDFA = [tab[@"enableHybridDFA"] boolValue]; @@ -148,7 +152,7 @@ - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError * - (IBAction)generate:(id)sender { NSString *destPath = [[_destinationPath copy] autorelease]; NSString *parserName = [[_parserName copy] autorelease]; - NSString *grammar = [[_grammar copy] autorelease]; + NSString *grammar = [self.textView string]; if (![destPath length] || ![parserName length] || ![grammar length]) { NSBeep(); @@ -230,7 +234,7 @@ - (IBAction)reveal:(id)sender { - (void)generateWithDestinationPath:(NSString *)destPath parserName:(NSString *)parserName grammar:(NSString *)grammar { NSError *err = nil; - self.root = (id)[_factory ASTFromGrammar:_grammar error:&err]; + self.root = (id)[_factory ASTFromGrammar:grammar error:&err]; if (err) { self.error = err; goto done; diff --git a/ParserGenApp/en.lproj/PGDocument.xib b/ParserGenApp/en.lproj/PGDocument.xib index 59a5bee..b3ee1dc 100644 --- a/ParserGenApp/en.lproj/PGDocument.xib +++ b/ParserGenApp/en.lproj/PGDocument.xib @@ -1,8 +1,9 @@ - + - + + @@ -57,29 +58,17 @@ - + - + - + - - - - - NSNegateBoolean - - - - - - - @@ -91,7 +80,7 @@ - + From 09dc5bf47370cb18fe0697cd24a8a095f64a982f Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 19 Nov 2016 10:58:22 -0600 Subject: [PATCH 2/4] Removes Busy from code. --- ParserGenApp/PGDocument.h | 2 -- ParserGenApp/PGDocument.m | 2 -- 2 files changed, 4 deletions(-) diff --git a/ParserGenApp/PGDocument.h b/ParserGenApp/PGDocument.h index 8fb5464..b36062f 100644 --- a/ParserGenApp/PGDocument.h +++ b/ParserGenApp/PGDocument.h @@ -30,8 +30,6 @@ @property (nonatomic, copy) NSString *destinationPath; @property (nonatomic, copy) NSString *parserName; -//@property (nonatomic, copy) NSString *grammar; -@property (nonatomic, assign) BOOL busy; @property (nonatomic, retain) NSError *error; @property (nonatomic, assign) BOOL enableARC; diff --git a/ParserGenApp/PGDocument.m b/ParserGenApp/PGDocument.m index 3aa8c05..9f24d13 100644 --- a/ParserGenApp/PGDocument.m +++ b/ParserGenApp/PGDocument.m @@ -159,7 +159,6 @@ - (IBAction)generate:(id)sender { return; } - self.busy = YES; self.error = nil; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @@ -304,7 +303,6 @@ - (void)done { [[NSSound soundNamed:@"Hero"] play]; } - self.busy = NO; [self focusTextView]; } From 2380bf74b0eaf3bc7ce126643edfb73a62262aa3 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 19 Nov 2016 13:26:50 -0600 Subject: [PATCH 3/4] Have to leave unused variable for backward compatibility with saved documents. --- ParserGenApp/PGDocument.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ParserGenApp/PGDocument.h b/ParserGenApp/PGDocument.h index b36062f..4103e08 100644 --- a/ParserGenApp/PGDocument.h +++ b/ParserGenApp/PGDocument.h @@ -39,5 +39,10 @@ @property (nonatomic, assign) NSInteger delegatePreMatchCallbacksOn; @property (nonatomic, assign) NSInteger delegatePostMatchCallbacksOn; +// These two are here for backwards compatibiltly with old saved files +@property (nonatomic, copy) NSString *grammar; +@property (nonatomic, assign) BOOL busy; + + @property (nonatomic, retain) IBOutlet NSTextView *textView; @end From 222ca33c0db72cbb73f5391a71edcbe2168e91a6 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 21 Nov 2016 15:26:37 -0600 Subject: [PATCH 4/4] Fixes bug with loading saved files. --- ParserGenApp/PGDocument.m | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ParserGenApp/PGDocument.m b/ParserGenApp/PGDocument.m index 9f24d13..31cc37a 100644 --- a/ParserGenApp/PGDocument.m +++ b/ParserGenApp/PGDocument.m @@ -28,6 +28,8 @@ @interface PGDocument () @property (nonatomic, retain) PGParserFactory *factory; @property (nonatomic, retain) PGRootNode *root; @property (nonatomic, retain) PGParserGenVisitor *visitor; + +@property (nonatomic, retain) NSString* loadGrammar; @end @implementation PGDocument @@ -96,6 +98,12 @@ - (void)windowControllerDidLoadNib:(NSWindowController *)wc { [_textView setFont:[NSFont fontWithName:@"Monaco" size:12.0]]; [self focusTextView]; + + if (self.loadGrammar != nil) { + [self.textView setString: self.loadGrammar]; + self.loadGrammar = nil; + } + } @@ -133,7 +141,11 @@ - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError * //NSLog(@"%@", tab); self.destinationPath = tab[@"destinationPath"]; - [self.textView setString:tab[@"grammar"]]; + if (self.textView != nil) { + [self.textView setString:tab[@"grammar"]]; + } else { + self.loadGrammar = tab[@"grammar"]; + } self.parserName = tab[@"parserName"]; self.enableARC = [tab[@"enableARC"] boolValue]; self.enableHybridDFA = [tab[@"enableHybridDFA"] boolValue];