Skip to content

Create your Highlighter ​

There are three ways to get a highlighter for a language that is not in the list:

  1. TSynGeneralSyn (and TSynOmniSyn): configurable highlighters. You define keywords, comments and strings with properties, without writing code.
  2. SynGen: a generator that creates the source of a highlighter from a grammar file.
  3. Write the unit by hand, deriving from TSynCustomHighlighter (or from TSynCustomCodeFoldingHighlighter if the language has foldable blocks): the easiest way is to start from the source of a similar highlighter.

SynGen ​

SynGen is in the SynGen folder of the library (SynGen.dpr). It reads a grammar file (.msg) and generates the unit of a highlighter. For simple keyword based highlighters the result can be used as it is; for the others it is the starting point.

The grammar file ​

Text between { and } is a comment. The first lines are:

  • the name of the class of the highlighter (for example TSynSampleSyn);
  • the prefix of the enumeration of the token types (tk in all the highlighters of the library);
  • the characters that can start and compose an identifier.

Then come the sections: TOKENTYPES, KEYS, CHARS, and the ENCLOSEDBY ones for comments and strings. A sample:

text
TSynSampleSyn   {first Identifier is considered to be the Class Name }
tk              {second Identifier is considered to be the Identifier Prefix }
IdentStart '_', 'a'..'z', 'A'..'Z':: '_', '0'..'9', 'a'..'z', 'A'..'Z'::

TOKENTYPES
Identifier
Comment Style=[fsItalic]|Foreground=clNavy
Space
String Foreground=clRed
Key Style=[fsBold]
Test Background=clSilver|Foreground=clBlue|Style=[fsUnderline, fsItalic]
|><|

KEYS
Hello
World
|><|

KEYS Test
SynEdit
|><|
  • TOKENTYPES lists the kinds of tokens, each one with its default attributes (Style, Foreground, Background).
  • KEYS lists the keywords of a token type (KEYS is the Key type, KEYS Test the Test type).
  • CHARS links the characters to the procedures that read the tokens.

The full description of the format is in the document SynGen\Highlighters-HowTo.htm.

Generated unit names ​

The unit generated by SynGen has to follow the naming rule of Ethea SynEdit: SynEdit.Highlighter.<Language>. Rename the generated file and the unit clause accordingly, and add it to the packages if you want it in the library.

Demo ​

See the Highlighter demo, which uses a grammar file to build a sample highlighter.

Released under the MPL 1.1 / GPL 2 dual license.