This topic explains the process of creating a custom language.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
Once a grammar definition is complete you can use it to parse documents which must conform to that grammatical structure. Here are the steps for doing this:
create a Grammar instance from the EBNF grammar definition or construct a Grammar instance with code
create a custom language class, which derives from LanguageBase and owns the Grammar instance
create a TextDocument and set its Language property to an instance of the custom language class
There are two ways to create a custom language from a grammar:
using the CustomLanguage class
generating a language class
You can create a custom language for a grammar by passing the Grammar
instance to the constructor of the CustomLanguage
class, which is derived from LanguageBase
. This will create a CustomLanguage
instance specifically built to parse documents based on the provided Grammar
instance.
The following table contains the members that are publicly exposed on the CustomLanguage
class:
You can create a custom language from a grammar by generating a LanguageBase
-derived class specifically built to parse documents with a specific grammar definition. Creating a CustomLanguage
can be thought of as dynamic creation of a language whereas generating a language class can be thought of as pre-compiling a language. Building the lexical and syntax analyzers can be slow for complex grammars. If the CustomLanguage
approach is used, that building phase needs to happen the first time a document is parsed for each run of the application. This may be acceptable for simple languages or when debugging a grammar definition on a developer’s machine, but for a complex language being used on a user’s machine, it may not be. By using the language generation approach, the lexical and syntax analyzers are built once on the developer’s machine. Then information about how to reconstruct the analyzers and Grammar
is written out to a generated class file. Each class file generated derives from LanguageBase
, has a static property named Instance returning a singleton instance of itself, and a public constructor in case two copies of the language need to be created with different services in their ServicesManager
. In addition, the generated class has a Grammar
property which returns an identical copy of the Grammar
used to generate the language.
One of the options for generating the language class is to give it the “partial” modifier. This allows you to create another file for the derived language so you can override certain members on LanguageBase
or add your own. The members that can be overridden on LanguageBase
are as follows
You can generate a language by using the LanguageGenerator class in the Infragistics.Documents.Parsing namespace. This class has the following static methods:
The LanguageGenerationParams class used as argument in the GenerateClass
methods has several options to customize the language generation process. They are as follows:
The following topics provide additional information related to this topic.