Inherits from NSObject
Declared in GMLanguage.h
Companion guide LanguageReference

Overview

GMLanguage is a class that loads a language used by the other components in this kit. Typically languages are stored in plist files with the extension .language and the language name as the name of the file.

Languages are (currently) simple dictionaries, so it is easy to build them programatically. It is recomended to pass them through languageWithDictionary:, as this will do some processing steps and also allows for using more literals rather than allocating objects yourself (as some strings will be automatically compiled into regular expression objects).

Tasks

Creating a language from a filesystem representation

Creating a language programatically

Class Methods

languageAtPath:

Loads a language plist from a given path.

+ (NSDictionary *)languageAtPath:(NSString *)path

Parameters

path

The accessible filesystem path where to find the language declaration.

Return Value

Returns a new language dictionary instance if the file was found and parsed properly, otherwise nil.

Declared In

GMLanguage.h

languageAtURL:

Loads a language plist from a given URL.

+ (NSDictionary *)languageAtURL:(NSURL *)url

Parameters

url

The accessible url where to find the language declaration.

Return Value

Returns a new language dictionary instance if the file was found and parsed properly, otherwise nil.

Declared In

GMLanguage.h

languageFromBundleWithName:

Loads a language plist from the application bundle.

+ (NSDictionary *)languageFromBundleWithName:(NSString *)name

Parameters

name

The name of the language.

Return Value

Returns a new language dictionary instance if the file was found and parsed properly, otherwise nil.

Discussion

This is a useful shorthand method when using the provided language bundlers as these will by default reside in the applications resource folder and have the .language extension. This method thus allows to find such bundles simply by their language name.

Declared In

GMLanguage.h

languageWithDictionary:

Processes a dictionary, turning certain strings into regular expressions and also turning arrays of dictionaries into ordered dictionaries.

+ (NSDictionary *)languageWithDictionary:(NSDictionary *)dict

Parameters

dict

The language definition dictionary.

Return Value

Returns a new language dictionary.

Discussion

Example of creating a language programatically:

 NSDictionary *css = @{
    @"grammar": @[
      @{@"comment": @"/\/\*[\w\W]*?\*\//"},
      @{@"atrule": @{
          @"pattern": @"/@[\w-]+?.*?(;|(?=\s*\{))/i",
          @"inside": @[
              @{@"punctuation": @"/[;:]/g"}
          ]
      }}
    ]
 };
 NSDictionary *cssLanguage = [GMLanguage languageWithDictionary: css];

Which would be roughly equivalent to doing (although this is implementation specific and may change):

 NSError *err;
 GMOrderedDictionary *grammar = [GMOrderedDictionary dictionary];
 [grammar setObject: [NSRegularExpression regularExpressionWithPattern: @"/\*[\w\W]*?\*\/" 
                                          options: 0 error: &err] forKey: @"comment"];
 [grammar setObject: @{@"pattern": [NSRegularExpression regularExpressionWithPattern: @"@[\w-]+?.*?(;|(?=\s*\{))" 
                                                        options: NSRegularExpressionCaseInsensitive error: &err] 
 // (I think you get the idea...)

 NSDictionary *cssLanguage = @{
    @"grammar" : grammar;
 }

Declared In

GMLanguage.h