Want more features on Pastebin? Sign Up, it's FREE!
Guest

MyParser

By: a guest on Sep 11th, 2013  |  syntax: Objective C  |  size: 1.95 KB  |  views: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // MyParser.H
  2.  
  3. #import <Foundation/Foundation.h>
  4. @class PKParser;
  5.  
  6. @interface MyParser : NSObject {
  7.     __strong NSString *scssGrammar;
  8.     __strong PKParser *scssParser;
  9. }
  10.  
  11.  
  12. + (MyParser *)sharedParser;
  13.  
  14. - (void)parseTestStringToSCSS;
  15.  
  16. @end
  17.  
  18. // MyParser.m
  19.  
  20. #import "MyParser.h"
  21. #import <ParseKit/ParseKit.h>
  22.  
  23. @implementation MyParser
  24.  
  25. - (id)init
  26. {
  27.     self = [super init];
  28.     if (self) {
  29.         // Add your subclass-specific initialization here.
  30.  
  31.         // Load my grammer from an external file
  32.         NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"scss" withExtension:@"grammar"];
  33.         NSError *err = nil;
  34.        
  35.         scssGrammar = [NSString stringWithContentsOfFile:fileURL.path encoding:NSUTF8StringEncoding error:&err];
  36.  
  37.         if (err) {
  38.             NSLog(@"%@", err);
  39.             [[NSAlert alertWithError:err] runModal];
  40.             return;
  41.         }
  42.        
  43.  
  44.         scssParser = [[PKParserFactory factory] parserFromGrammar:scssGrammar assembler:self error:nil];
  45.     }
  46.     return self;
  47. }
  48.  
  49. + (MyParser *)sharedParser
  50. {
  51.     static MyParser *sharedParser;
  52.    
  53.     @synchronized(self)
  54.     {
  55.         if (!sharedParser)
  56.             sharedParser = [self new];
  57.         return sharedParser;
  58.     }
  59. }
  60.  
  61.  
  62.  
  63. - (void)parseTestStringToSCSS
  64. {
  65.  
  66.     NSString *scss = @".myClass { > content {} } "
  67.                       ".myClass2 { color: #444444; }";
  68.    
  69.     NSError *err = nil;
  70.     [scssParser parse:scss error:&err];
  71.     if (err) {
  72.         NSLog(@"%@", err);
  73.         [[NSAlert alertWithError:err] runModal];
  74.         return;
  75.     }
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. - (void)parser:(PKParser *)p didMatchRuleset:(PKAssembly *)a
  83. {
  84.     NSLog(@"ruleset: %@", a.stack);
  85. }
  86.  
  87. - (void)parser:(PKParser *)p didMatchSelector:(PKAssembly *)a
  88. {
  89.     NSLog(@"selector: %@", a.stack);
  90. }
  91.  
  92. - (void)parser:(PKParser *)p didMatchProperty:(PKAssembly *)a
  93. {
  94. //    NSLog(@"properties: %@", a.stack);
  95. }
  96.  
  97.  
  98.  
  99. @end
clone this paste RAW Paste Data