Created
September 11, 2013 23:45
-
-
Save zakdances/6531363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyParser.H | |
#import <Foundation/Foundation.h> | |
@class PKParser; | |
@interface MyParser : NSObject { | |
__strong NSString *scssGrammar; | |
__strong PKParser *scssParser; | |
} | |
+ (MyParser *)sharedParser; | |
- (void)parseTestStringToSCSS; | |
@end | |
// MyParser.m | |
#import "MyParser.h" | |
#import <ParseKit/ParseKit.h> | |
@implementation MyParser | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Add your subclass-specific initialization here. | |
// Load my grammer from an external file | |
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"scss" withExtension:@"grammar"]; | |
NSError *err = nil; | |
scssGrammar = [NSString stringWithContentsOfFile:fileURL.path encoding:NSUTF8StringEncoding error:&err]; | |
if (err) { | |
NSLog(@"%@", err); | |
[[NSAlert alertWithError:err] runModal]; | |
return; | |
} | |
scssParser = [[PKParserFactory factory] parserFromGrammar:scssGrammar assembler:self error:nil]; | |
} | |
return self; | |
} | |
+ (MyParser *)sharedParser | |
{ | |
static MyParser *sharedParser; | |
@synchronized(self) | |
{ | |
if (!sharedParser) | |
sharedParser = [self new]; | |
return sharedParser; | |
} | |
} | |
- (void)parseTestStringToSCSS | |
{ | |
NSString *scss = @".myClass { > content {} } " | |
".myClass2 { color: #444444; }"; | |
NSError *err = nil; | |
[scssParser parse:scss error:&err]; | |
if (err) { | |
NSLog(@"%@", err); | |
[[NSAlert alertWithError:err] runModal]; | |
return; | |
} | |
} | |
- (void)parser:(PKParser *)p didMatchRuleset:(PKAssembly *)a | |
{ | |
NSLog(@"ruleset: %@", a.stack); | |
} | |
- (void)parser:(PKParser *)p didMatchSelector:(PKAssembly *)a | |
{ | |
NSLog(@"selector: %@", a.stack); | |
} | |
- (void)parser:(PKParser *)p didMatchProperty:(PKAssembly *)a | |
{ | |
// NSLog(@"properties: %@", a.stack); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment