Skip to content

Commit

Permalink
Add another example parser to Fragaria Simple that mirror what could …
Browse files Browse the repository at this point in the history
…formerly be done through SMLSyntaxColouringDelegate.
  • Loading branch information
shysaur committed Dec 28, 2018
1 parent da82aa1 commit ddba7da
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
8 changes: 1 addition & 7 deletions Applications/Fragaria Simple/ExampleCustomParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ NS_ASSUME_NONNULL_BEGIN
* depending on the language, and a single parser factory which
* initializes each parser according to the language.
* See the implementation of MGSClassicFragariaParserFactory for an
* example of this use case.
*
* The most complex use-case for Fragaria parsers is when you want to modify
* the output of an existing parser.
* This case can be implemented through composition of a custom parser with the
* new parser. */
* example of this use case. */

@interface ExampleCustomParser : MGSSyntaxParser <MGSParserFactory>

@end


NS_ASSUME_NONNULL_END
20 changes: 20 additions & 0 deletions Applications/Fragaria Simple/ExampleCustomParserWithComposition.h
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 Applications/Fragaria Simple/ExampleCustomParserWithComposition.m
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
6 changes: 4 additions & 2 deletions Applications/Fragaria Simple/FragariaAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <Fragaria/Fragaria.h>
#import "MGSSimpleBreakpointDelegate.h"
#import "ExampleCustomParser.h"
#import "ExampleCustomParserWithComposition.h"


@implementation FragariaAppDelegate {
Expand All @@ -27,10 +28,11 @@ @implementation FragariaAppDelegate {
*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
/* Register a custom parser factory.
* The parser will appear in the list of languages, toghether with the
/* Register our custom parser factories.
* The custom parsers will appear in the list of languages, toghether with the
* other standard ones. */
[[MGSSyntaxController sharedInstance] registerParserFactory:[[ExampleCustomParser alloc] init]];
[[MGSSyntaxController sharedInstance] registerParserFactory:[[ExampleCustomParserWithComposition alloc] init]];

// define initial object configuration
//
Expand Down

0 comments on commit ddba7da

Please sign in to comment.