SynEdit 1.04 Delphi 10.1 Berlin
SynEdit 1.04 Delphi 10.1 Berlin | 2 Mb
How to create your own custom highlighter for the TSynEdit component. This document starts with a step-by-step explanation on how to create a simple highlighter, but this will be expanded to cover advanced highlighting techniques.
Preparation
Creation of a new highlighter is best started with the creation of a grammar file. This grammar file (.msg file), is then used by the program SynGen to generate a basic skeleton of the highlighter source. For most highlighters this generated source is nothing more then a point to begin with, but for simple keyword highlighters, the result is directly usable.
The layout of the grammar file, is rather straight forward (you can insert empty lines if you like. Text between { and } is considered as comment, and thus is ignored):
The first line contains the name of the highlighter class to be created (e.g. TSynSampleSyn).
The second line contains the prefix of the enumeration type to be created for the various types of tokens. The current series of SynEdit highlighters use the prefix �tk’ for this.
The third line contains either the keyword �Sensitive’ to indicate that the keywords of the highlighter are to be used case sensitive, or it will contain the keyword �IdentStart’, followed by a list of characters that can be used to make up valid identifiers.
After this the keyword �KEYS’ is in the file, after which all the keywords to be highlighted are mentioned. Each keyword starts on a new line. This section is ended by the terminator string �|<>|’.
After this the file contains the list of token types the highlighter will recognize. By default the token types Identifier and Key should always be there. Each token type starts on a new line, and the section is again terminated by a line containing �|<>|’.
After this the keyword �CHARS’ is in the file, after which all the special character handling is coded in Pascal. This is just a kind of a case statement, which will be clarified in the examples listed later in this document. This section is also terminated by a line containing �|<>|’.
Based on this .msg file, we can run the SynGen program, which will generate the basic highlighter source for us. After that we can take the generated source file, and fine tune it for our special needs.
Once we have created this file (as sample.msg), we can startup SynGen. On startup, SynGen will prompt us to select the grammar file we have just created. After that, we are presented with a four-page window, which allows us to do some customizations:
On the first page (Highlighter) we will fill in some general information about the highlighter. This contains the name of the author, a short description, and the version of the highlighter. This page also contains two checkboxes which give the user the option to have the SynEdit standard comment header (with the GPL/MPL information), and the option to have a GetKeywords function which returns all keywords being highlighted.
On the second page (Language) we can fill in the default filter (for this example we select �All files’), which can be used for the FileOpen dialogbox in Delphi. We can also fill in the name of the language being highlighted (for this example we call it �Sample’).
On the third page (Attributes), we can assign different constants to the Identifier and Reserved word tokens. We can keep the default values now. On this page we can also set which type of token should be used for non-keyword identifiers. In this example we will leave it to the default value (Identifier).
On the fourth page (Private fields), we can add our own private field declarations to the highlighter class. We don’t need this now, so we leave this page empty.
After having filled in all fields in SynGen, we can press the �Start’ button. After that SynGen will generate a Delphi unit, called sample.pas, which contains the implementation of the highlighter. You can use this highlighter in SynEdit, and it will only highlight the keyword �Hello’ in bold.
If we now want to add more keywords to this highlighter, we simply add them in the grammar file, as new lines in the KEYS section. After regenerating the Pascal unit using SynGen, we will have the additional keywords being highlighted in SynEdit.
Adding support for comments and strings
After creating a simple highlighter like this, we want to add support for comments to the highlighter. In our sample language, comments are started with a �{� (brace open) character, and closed with a �}’ character (Pascal style comments).
To achieve this we must make two modifications to our grammar file:
Add a new token kind �Comment’
Add a new section “ENCLOSEDBY” which specifies the delimiters of the comment section
Only for V.I.P
Warning! You are not allowed to view this text.