forked from mz2/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathExampleCustomParserWithComposition.m
66 lines (50 loc) · 1.8 KB
/
ExampleCustomParserWithComposition.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)parseForClient:(id<MGSSyntaxParserClient>)client
{
/* Color as a command every word starting with NS */
NSRange realRange = [objcParser parseForClient:client];
[client.stringToParse 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 ([MGSSyntaxGroupString isEqual:g])
return;
if ([substring length] > 0 && [substring hasPrefix:@"NS"]) {
[client setGroup:MGSSyntaxGroupCommand forTokenInRange:substringRange atomic:YES];
}
}];
return realRange;
}
@end