Skip to content

Instantly share code, notes, and snippets.

@zakdances
Created September 11, 2013 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakdances/6531363 to your computer and use it in GitHub Desktop.
Save zakdances/6531363 to your computer and use it in GitHub Desktop.
// 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