Skip to content

Commit

Permalink
Initial project add.
Browse files Browse the repository at this point in the history
  • Loading branch information
itod committed Mar 10, 2014
1 parent e4e9d3e commit d6db835
Show file tree
Hide file tree
Showing 918 changed files with 121,367 additions and 0 deletions.
5,387 changes: 5,387 additions & 0 deletions PEGKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions PEGKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions ParserGenApp/PGDocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// PGDocument.h
// ParserGenApp
//
// Created by Todd Ditchendorf on 4/15/13.
//
//

#import <Cocoa/Cocoa.h>

@interface PGDocument : NSDocument

- (IBAction)generate:(id)sender;
- (IBAction)browse:(id)sender;
- (IBAction)reveal:(id)sender;

@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;
@property (nonatomic, assign) BOOL enableHybridDFA;
@property (nonatomic, assign) BOOL enableMemoization;
@property (nonatomic, assign) BOOL enableAutomaticErrorRecovery;
@property (nonatomic, assign) NSInteger preassemblerSettingBehavior;
@property (nonatomic, assign) NSInteger assemblerSettingBehavior;

@property (nonatomic, retain) IBOutlet NSTextView *textView;
@end
280 changes: 280 additions & 0 deletions ParserGenApp/PGDocument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
//
// PGDocument.m
// ParserGenApp
//
// Created by Todd Ditchendorf on 4/15/13.
//
//

#import "PGDocument.h"
#import <ParseKit/ParseKit.h>
#import "PKSParserGenVisitor.h"

@interface PGDocument ()
@property (nonatomic, retain) PKParserFactory *factory;
@property (nonatomic, retain) PKRootNode *root;
@property (nonatomic, retain) PKSParserGenVisitor *visitor;
@end

@implementation PGDocument

- (id)init {
self = [super init];
if (self) {
self.factory = [PKParserFactory factory];
_factory.collectTokenKinds = YES;

self.enableARC = YES;
self.enableHybridDFA = YES;
self.enableMemoization = YES;
self.enableAutomaticErrorRecovery = NO;

self.destinationPath = [@"~/Desktop" stringByExpandingTildeInPath];
self.parserName = @"ExpressionParser";

self.preassemblerSettingBehavior = PKParserFactoryAssemblerSettingBehaviorNone;
self.assemblerSettingBehavior = PKParserFactoryAssemblerSettingBehaviorAll;

NSString *path = [[NSBundle mainBundle] pathForResource:@"expression" ofType:@"grammar"];
self.grammar = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}
return self;
}


- (void)dealloc {
self.destinationPath = nil;
self.parserName = nil;
self.grammar = nil;

self.textView = nil;

self.factory = nil;
self.root = nil;
self.visitor = nil;
[super dealloc];
}


- (void)awakeFromNib {

}


#pragma mark -
#pragma mark NSDocument

- (NSString *)windowNibName {
return @"PGDocument";
}


- (void)windowControllerDidLoadNib:(NSWindowController *)wc {
[super windowControllerDidLoadNib:wc];

[_textView setFont:[NSFont fontWithName:@"Monaco" size:12.0]];
}


+ (BOOL)autosavesInPlace {
return YES;
}


- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
NSAssert([[NSThread currentThread] isMainThread], @"");
NSMutableDictionary *tab = [NSMutableDictionary dictionaryWithCapacity:9];

if (_destinationPath) tab[@"destinationPath"] = _destinationPath;
if (_grammar) tab[@"grammar"] = _grammar;
if (_parserName) tab[@"parserName"] = _parserName;
tab[@"enableARC"] = @(_enableARC);
tab[@"enableHybridDFA"] = @(_enableHybridDFA);
tab[@"enableMemoization"] = @(_enableMemoization);
tab[@"enableAutomaticErrorRecovery"] = @(_enableAutomaticErrorRecovery);
tab[@"preassemblerSettingBehavior"] = @(_preassemblerSettingBehavior);
tab[@"assemblerSettingBehavior"] = @(_assemblerSettingBehavior);

//NSLog(@"%@", tab);

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:tab];
return data;
}


- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
NSAssert([[NSThread currentThread] isMainThread], @"");
NSDictionary *tab = [NSKeyedUnarchiver unarchiveObjectWithData:data];

//NSLog(@"%@", tab);

self.destinationPath = tab[@"destinationPath"];
self.grammar = tab[@"grammar"];
self.parserName = tab[@"parserName"];
self.enableARC = [tab[@"enableARC"] boolValue];
self.enableHybridDFA = [tab[@"enableHybridDFA"] boolValue];
self.enableMemoization = [tab[@"enableMemoization"] boolValue];
self.enableAutomaticErrorRecovery = [tab[@"enableAutomaticErrorRecovery"] boolValue];
self.preassemblerSettingBehavior = [tab[@"preassemblerSettingBehavior"] integerValue];
self.assemblerSettingBehavior = [tab[@"assemblerSettingBehavior"] integerValue];

return YES;
}


#pragma mark -
#pragma mark Actions

- (IBAction)generate:(id)sender {
NSString *destPath = [[_destinationPath copy] autorelease];
NSString *parserName = [[_parserName copy] autorelease];
NSString *grammar = [[_grammar copy] autorelease];

if (![destPath length] || ![parserName length] || ![grammar length]) {
NSBeep();
return;
}

self.busy = YES;
self.error = nil;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self generateWithDestinationPath:destPath parserName:parserName grammar:grammar];
});
}


- (IBAction)browse:(id)sender {
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSWindow *win = [[[self windowControllers] lastObject] window];

NSString *path = nil;

if (_destinationPath) {
path = _destinationPath;

BOOL isDir;
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] || !isDir) {
path = nil;
}
}

if (path) {
NSURL *pathURL = [NSURL fileURLWithPath:path];
[panel setDirectoryURL:pathURL];
}

[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:YES];
[panel setCanCreateDirectories:YES];
[panel setCanChooseFiles:NO];

[panel beginSheetModalForWindow:win completionHandler:^(NSInteger result) {
if (NSOKButton == result) {
NSString *path = [[panel URL] relativePath];
self.destinationPath = path;

[self updateChangeCount:NSChangeDone];
}
}];
}


- (IBAction)reveal:(id)sender {
NSString *path = _destinationPath;

BOOL isDir;
NSFileManager *mgr = [NSFileManager defaultManager];
while ([path length] && ![mgr fileExistsAtPath:path isDirectory:&isDir]) {
path = [path stringByDeletingLastPathComponent];
}

NSString *filename = self.parserName;
if (![filename hasSuffix:@"Parser"]) {
filename = [NSString stringWithFormat:@"%@Parser.m", filename];
}

path = [path stringByAppendingPathComponent:filename];

if ([path length]) {
[[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:nil];
}
}


#pragma mark -
#pragma mark Private


- (void)generateWithDestinationPath:(NSString *)destPath parserName:(NSString *)parserName grammar:(NSString *)grammar {
NSError *err = nil;
self.root = (id)[_factory ASTFromGrammar:_grammar error:&err];
if (err) {
self.error = err;
goto done;
}

NSString *className = self.parserName;
if (![className hasSuffix:@"Parser"]) {
className = [NSString stringWithFormat:@"%@Parser", className];
}

_root.grammarName = self.parserName;

self.visitor = [[[PKSParserGenVisitor alloc] init] autorelease];
_visitor.enableARC = _enableARC;
_visitor.enableHybridDFA = _enableHybridDFA; //NSAssert(_enableHybridDFA, @"");
_visitor.enableMemoization = _enableMemoization;
_visitor.enableAutomaticErrorRecovery = _enableAutomaticErrorRecovery;
_visitor.preassemblerSettingBehavior = _preassemblerSettingBehavior;
_visitor.assemblerSettingBehavior = _assemblerSettingBehavior;

@try {
[_root visit:_visitor];
}
@catch (NSException *ex) {
id userInfo = @{NSLocalizedFailureReasonErrorKey: [ex reason]};
NSError *err = [NSError errorWithDomain:[ex name] code:0 userInfo:userInfo];
self.error = err;
goto done;
}

NSString *path = [[NSString stringWithFormat:@"%@/%@.h", destPath, className] stringByExpandingTildeInPath];
err = nil;
if (![_visitor.interfaceOutputString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err]) {
NSLog(@"%@", err);
}

path = [[NSString stringWithFormat:@"%@/%@.m", destPath, className] stringByExpandingTildeInPath];
err = nil;
if (![_visitor.implementationOutputString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err]) {
NSLog(@"%@", err);
}

done:
dispatch_async(dispatch_get_main_queue(), ^(void){
[self done];
});
}


- (void)done {
if (_error) {
[[NSSound soundNamed:@"Basso"] play];
[self displayError:_error];
} else {
[[NSSound soundNamed:@"Hero"] play];
}

self.busy = NO;
}


- (void)displayError:(NSError *)error {
NSString *title = NSLocalizedString(@"Error parsing grammar", @"");
NSString *msg = [error localizedFailureReason];
NSString *defaultButton = NSLocalizedString(@"OK", @"");
NSRunAlertPanel(title, msg, defaultButton, nil, nil);
}

@end
53 changes: 53 additions & 0 deletions ParserGenApp/ParserGenApp-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>parsergen</string>
</array>
<key>CFBundleTypeIconFile</key>
<string></string>
<key>CFBundleTypeName</key>
<string>DocumentType</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>????</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>NSDocumentClass</key>
<string>PGDocument</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.parsekit.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
7 changes: 7 additions & 0 deletions ParserGenApp/ParserGenApp-Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'ParserGenApp' target in the 'ParserGenApp' project
//

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
Loading

0 comments on commit d6db835

Please sign in to comment.