forked from mz2/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add another example parser to Fragaria Simple that mirror what could …
…formerly be done through SMLSyntaxColouringDelegate.
- Loading branch information
Showing
4 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
Applications/Fragaria Simple/ExampleCustomParserWithComposition.h
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// ExampleCustomParserWithComposition.h | ||
// Fragaria Simple | ||
// | ||
// Created by Daniele Cattaneo on 28/12/2018. | ||
// | ||
|
||
#import <Fragaria/Fragaria.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/* This parser modifies the output of an existing parser (specifically, the | ||
* standard Fragaria Objective-C parser), similarly to what could previously | ||
* be done through SMLSyntaxColouringDelegate. */ | ||
|
||
@interface ExampleCustomParserWithComposition : MGSSyntaxParser <MGSParserFactory> | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
66 changes: 66 additions & 0 deletions
66
Applications/Fragaria Simple/ExampleCustomParserWithComposition.m
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ExampleCustomParserWithComposition.m | ||
// Fragaria Simple | ||
// | ||
// Created by Daniele Cattaneo on 28/12/2018. | ||
// | ||
|
||
#import "ExampleCustomParserWithComposition.h" | ||
|
||
|
||
@implementation ExampleCustomParserWithComposition | ||
{ | ||
MGSSyntaxParser *objcParser; | ||
} | ||
|
||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
objcParser = [[MGSSyntaxController sharedInstance] parserForSyntaxDefinitionName:@"Objective-C"]; | ||
if (!objcParser) { | ||
NSLog(@"Where's my standard objc parser!?"); | ||
return nil; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
- (NSArray *)syntaxDefinitionNames | ||
{ | ||
return @[@"Objective-C Plus"]; | ||
} | ||
|
||
|
||
- (nonnull MGSSyntaxParser *)parserForSyntaxDefinitionName:(nonnull NSString *)syndef | ||
{ | ||
/* Note that calling -parserForSyntaxDefinitionName: on [MGSSyntaxController sharedInstance] | ||
* inside the implementation of -parserForSyntaxDefinitionName: on a parser factory | ||
* is fully supported, as long as it does not cause a recursion | ||
* inside your parser factory code. */ | ||
return self; | ||
} | ||
|
||
|
||
- (NSRange)parseString:(NSString *)string inRange:(NSRange)range forParserClient:(id<MGSSyntaxParserClient>)client | ||
{ | ||
/* Color as a command every word starting with NS */ | ||
NSRange realRange = [objcParser parseString:string inRange:range forParserClient:client]; | ||
|
||
[string enumerateSubstringsInRange:realRange options:NSStringEnumerationByWords usingBlock: | ||
^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) { | ||
/* Ignore whatever is inside a string */ | ||
NSString *g = [client groupOfTokenAtCharacterIndex:substringRange.location]; | ||
if ([SMLSyntaxGroupString isEqual:g]) | ||
return; | ||
|
||
if ([substring length] > 0 && [substring hasPrefix:@"NS"]) { | ||
[client setGroup:SMLSyntaxGroupCommand forTokenInRange:substringRange]; | ||
} | ||
}]; | ||
|
||
return realRange; | ||
} | ||
|
||
|
||
@end |
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