Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement