<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.codeblocks.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jazzboy</id>
	<title>Code::Blocks - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.codeblocks.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jazzboy"/>
	<link rel="alternate" type="text/html" href="https://wiki.codeblocks.org/index.php/Special:Contributions/Jazzboy"/>
	<updated>2026-05-11T00:02:53Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>https://wiki.codeblocks.org/index.php?title=Creating_a_custom_lexer_for_Code::Blocks_editor&amp;diff=4979</id>
		<title>Creating a custom lexer for Code::Blocks editor</title>
		<link rel="alternate" type="text/html" href="https://wiki.codeblocks.org/index.php?title=Creating_a_custom_lexer_for_Code::Blocks_editor&amp;diff=4979"/>
		<updated>2007-09-21T23:09:28Z</updated>

		<summary type="html">&lt;p&gt;Jazzboy: /* Keywords */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Developer Documentation]]&lt;br /&gt;
Code::Blocks can provide syntax highlighting for any of the languages supported by Scintilla http://www.scintilla.org (the Rich Editing API used by C::B to provide syntax highlighting, code folding etc). Each language that C::B provides highlighting for has a C::B specific xml file found under &amp;lt;tt&amp;gt;sdk/resources/lexers&amp;lt;/tt&amp;gt; that specifies styles and keywords of the language (they're simple XML files with names of the form &amp;lt;tt&amp;gt;lexer_*.xml&amp;lt;/tt&amp;gt;). Adding a new language that is supported by Scintilla is as simple as adding an xml file for that language. At a minimum, the xml file must list the available set of styles for the lexical elements of a language and provide for keywords (the file will also specify a file containing sample code for editing and previewing lexer styles in Code::Blocks). Actual styles and keywords for each language can be edited by users within Code::Blocks under Settings-&amp;gt;Editors-&amp;gt;Syntax Highlighting allowing them view the results in a preview window (for the preview to display you will need to provide this sample code in a separate file also located in &amp;lt;tt&amp;gt;sdk/resources/lexers&amp;lt;/tt&amp;gt;). User customized styles and keywords are kept in the users &amp;lt;tt&amp;gt;default.conf&amp;lt;/tt&amp;gt; file (i.e. the xml file is never changed within codeblocks) allowing users to easily revert to the xml file.&lt;br /&gt;
&lt;br /&gt;
To illustrate how you might construct a new lexer xml file let's look at the pre-installed CPP lexer, whose styles and keywords are stored in &amp;lt;tt&amp;gt;lexer_cpp.xml&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==XML==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Basically this says &amp;quot;I am an XML file&amp;quot;. '''Very Important'''&lt;br /&gt;
&lt;br /&gt;
==DOCTYPE==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!DOCTYPE CodeBlocks_lexer_properties&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This needs to be in every lexer file. Code::Blocks '''will not''' load the lexer if this is not present.&lt;br /&gt;
&lt;br /&gt;
==Lexers==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;Lexer name=&amp;quot;C/C++&amp;quot;&lt;br /&gt;
        index=&amp;quot;3&amp;quot;&lt;br /&gt;
        filemasks=&amp;quot;*.c,*.cpp,*.cc,*.cxx,*.h,*.hpp,*.hh,*.hxx,*.inl&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pretty much self explanatory, except for the &amp;quot;magic&amp;quot; ''index'' number (we'll come to it in a sec).&lt;br /&gt;
&lt;br /&gt;
* '''name''' is the lexer's configuration name. This will appear in the editor's configuration dialog, in the languages drop down box (in colors editing page).&lt;br /&gt;
&lt;br /&gt;
* '''filemasks''' is a comma separated list of the extensions that this lexer should be used for. This is case-insensitive.&lt;br /&gt;
&lt;br /&gt;
* '''index''' corresponds with the &amp;lt;tt&amp;gt;wxSCI_LEX_*&amp;lt;/tt&amp;gt; constants, found in &amp;lt;tt&amp;gt;sdk/wxscintilla/include/wx/wxscintilla.h&amp;lt;/tt&amp;gt;. In this example, if you look in &amp;lt;tt&amp;gt;sdk/wxscintilla/include/wx/wxscintilla.h&amp;lt;/tt&amp;gt;, you'll see that ''index 3'' matches &amp;lt;tt&amp;gt;wxSCI_LEX_CPP&amp;lt;/tt&amp;gt;. That is the ''lexer id'' for C/C++ syntax highlighting.&lt;br /&gt;
&lt;br /&gt;
If we were building a lexer configuration for let's say, XML (random choice) we would look up the constant &amp;lt;tt&amp;gt;wxSCI_LEX_XML&amp;lt;/tt&amp;gt; which is defined to be number ''5''. So ''index=5''. Simple.&lt;br /&gt;
&lt;br /&gt;
==Styles==&lt;br /&gt;
&lt;br /&gt;
Next follows many &amp;lt;tt&amp;gt;&amp;lt;Style&amp;gt;&amp;lt;/tt&amp;gt; tags defining the different styles:&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;Style name=&amp;quot;Default&amp;quot;&lt;br /&gt;
               index=&amp;quot;0&amp;quot;&lt;br /&gt;
               fg=&amp;quot;0,0,0&amp;quot;&lt;br /&gt;
               bg=&amp;quot;255,255,255&amp;quot;&lt;br /&gt;
               bold=&amp;quot;0&amp;quot;&lt;br /&gt;
               italics=&amp;quot;0&amp;quot;&lt;br /&gt;
               underlined=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''name''' is the style's name. It appears in the editor's configuration dialog, in the colors editing page.&lt;br /&gt;
&lt;br /&gt;
* '''fg''' is the foreground color. Comma separated list of three numbers from 0 to 255. In order: red, green and blue (RGB).&lt;br /&gt;
&lt;br /&gt;
* '''bg''' is the background color.&lt;br /&gt;
&lt;br /&gt;
* '''bold''' is &amp;lt;tt&amp;gt;&amp;quot;0&amp;quot;&amp;lt;/tt&amp;gt; for disabled, &amp;lt;tt&amp;gt;&amp;quot;1&amp;quot;&amp;lt;/tt&amp;gt; for enabled.&lt;br /&gt;
&lt;br /&gt;
* '''italics''' is &amp;lt;tt&amp;gt;&amp;quot;0&amp;quot;&amp;lt;/tt&amp;gt; for disabled, &amp;lt;tt&amp;gt;&amp;quot;1&amp;quot;&amp;lt;/tt&amp;gt; for enabled.&lt;br /&gt;
&lt;br /&gt;
* '''underlined''' is &amp;lt;tt&amp;gt;&amp;quot;0&amp;quot;&amp;lt;/tt&amp;gt; for disabled, &amp;lt;tt&amp;gt;&amp;quot;1&amp;quot;&amp;lt;/tt&amp;gt; for enabled.&lt;br /&gt;
&lt;br /&gt;
You don't have to define all of these attributes. It's good to define them all for the &amp;lt;tt&amp;gt;&amp;quot;default&amp;quot;&amp;lt;/tt&amp;gt; style (all lexers have a default style), but only the attributes needed should be defined for the rest of the styles.&lt;br /&gt;
&lt;br /&gt;
* The '''index''' number in the &amp;lt;tt&amp;gt;&amp;lt;Style&amp;gt;&amp;lt;/tt&amp;gt; tags, comes from a different set of constants defined in &amp;lt;tt&amp;gt;sdk/wxscintilla/include/wx/wxscintilla.h&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
For each language supported by scintilla, there is a set of styles ''(lexical states)'' defined (these are what we're trying to configure with these files). &lt;br /&gt;
&lt;br /&gt;
For example, for C/C++ files (&amp;lt;tt&amp;gt;wxSCI_LEX_CPP&amp;lt;/tt&amp;gt;, remember?) the styles are defined as &amp;lt;tt&amp;gt;wxSCI_C_*&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For the &amp;lt;tt&amp;gt;&amp;quot;default&amp;quot;&amp;lt;/tt&amp;gt; style shown above, this would be &amp;lt;tt&amp;gt;wxSCI_C_DEFAULT&amp;lt;/tt&amp;gt; which is defined to be ''0''. Hence ''index=0'' for &amp;lt;tt&amp;gt;&amp;quot;default&amp;quot;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;Style name=&amp;quot;Comment (normal)&amp;quot;&lt;br /&gt;
               index=&amp;quot;1,2&amp;quot;&lt;br /&gt;
               fg=&amp;quot;160,160,160&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the style definition for normal comments. As you can see you can define a single style for more than one style index, in this case two: ''1'' and ''2'' (always comma separated).&lt;br /&gt;
&lt;br /&gt;
''1'' is for &amp;lt;tt&amp;gt;wxSCI_C_COMMENT&amp;lt;/tt&amp;gt; (the C comment &amp;lt;tt&amp;gt;/* */&amp;lt;/tt&amp;gt;) and ''2'' is for &amp;lt;tt&amp;gt;wxSCI_C_COMMENTLINE&amp;lt;/tt&amp;gt; (the C++ comment to end of line &amp;lt;tt&amp;gt;// &amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are some special styles defined by Code::Blocks and are available to all lexers:&lt;br /&gt;
&lt;br /&gt;
* '''index -99''' is the selected text style.&lt;br /&gt;
* '''index -98''' is the active line style (the line the caret is on).&lt;br /&gt;
* '''index -2''' is the breakpoint line style.&lt;br /&gt;
* '''index -3''' is the debugger active line style (while stepping the debugger).&lt;br /&gt;
* '''index -4''' is the compiler warning/error line style. ('''Note: this index was removed completely?''')&lt;br /&gt;
&lt;br /&gt;
==Keywords==&lt;br /&gt;
&lt;br /&gt;
Now on to the keywords.&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;Keywords&amp;gt;&lt;br /&gt;
               &amp;lt;Set index=&amp;quot;0&amp;quot;&lt;br /&gt;
                         value=&amp;quot;if int long try while and-so-on&amp;quot; /&amp;gt;&lt;br /&gt;
               &amp;lt;Set index=&amp;quot;1&amp;quot; /&amp;gt;&lt;br /&gt;
               &amp;lt;Set index=&amp;quot;2&amp;quot;&lt;br /&gt;
                              value=&amp;quot;param remarks return $ @ \ &amp;amp; &amp;lt; &amp;gt; # { } and-so-on&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;/Keywords&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are defining a lexer configuration for a language that has keywords they should be added in the &amp;lt;tt&amp;gt;&amp;lt;Keywords&amp;gt;&amp;lt;/tt&amp;gt; tag.&lt;br /&gt;
In the scintilla version used by Code::Blocks, you can setup up to 9 keyword sets (the &amp;quot;index&amp;quot; attribute). What each set does and which sets are used by a lexer, is defined by the lexer itself.&lt;br /&gt;
&lt;br /&gt;
If you open LexCPP.cxx (from the scintilla sources), you will find the following:&lt;br /&gt;
&lt;br /&gt;
 static const char * const cppWordLists[] = {&lt;br /&gt;
            &amp;quot;Primary keywords and identifiers&amp;quot;,&lt;br /&gt;
            &amp;quot;Secondary keywords and identifiers&amp;quot;,&lt;br /&gt;
            &amp;quot;Documentation comment keywords&amp;quot;,&lt;br /&gt;
            &amp;quot;Unused&amp;quot;,&lt;br /&gt;
            &amp;quot;Global classes and typedefs&amp;quot;,&lt;br /&gt;
            0,&lt;br /&gt;
        };&lt;br /&gt;
&lt;br /&gt;
This tells us which indices are valid and what each one represents:&lt;br /&gt;
&lt;br /&gt;
Index 0, &amp;quot;Primary keywords and identifiers&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Index 1, &amp;quot;Secondary keywords and identifiers&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Index 2, &amp;quot;Documentation comment keywords&amp;quot;&lt;br /&gt;
&lt;br /&gt;
(notice that index &amp;quot;3&amp;quot; is not used)&lt;br /&gt;
&lt;br /&gt;
Index 4, &amp;quot;Global classes and typedefs&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Sample Code==&lt;br /&gt;
&lt;br /&gt;
The tag left is &amp;lt;tt&amp;gt;SampleCode&amp;lt;/tt&amp;gt;. This is much pretty self explanatory:&lt;br /&gt;
&lt;br /&gt;
        &amp;lt;SampleCode value=&amp;quot;lexer_cpp.sample&amp;quot;&lt;br /&gt;
                    breakpoint_line=&amp;quot;20&amp;quot;&lt;br /&gt;
                    debug_line=&amp;quot;22&amp;quot;&lt;br /&gt;
                    error_line=&amp;quot;23&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* '''value''' is the filename of the code that will be shown in the Preview window. &lt;br /&gt;
When creating a &amp;lt;tt&amp;gt;lexer_*.sample&amp;lt;/tt&amp;gt; try to do it with simple and concise sample code (like the ones found in a typical &amp;quot;Hello world!&amp;quot;), yet include all the styles of the lexer.&lt;br /&gt;
&lt;br /&gt;
There are other optional options ('''Note: this index was removed completely?''')&lt;br /&gt;
* '''breakpoint_line''' is the number of the line in which a breakpoint line will be previewed.&lt;br /&gt;
* '''debug_line''' is the number of the line in which a debug line will be previewed.&lt;br /&gt;
* '''error_line''' is the number of the line in which an error line will be previewed.&lt;br /&gt;
&lt;br /&gt;
==Tips==&lt;br /&gt;
When writting a lexer, this can be useful:&lt;br /&gt;
* .properties files of SciTE (the official Scintilla-based text editor) [http://scintilla.sourceforge.net/SciTE.html]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Adding support for code-folding==&lt;br /&gt;
&lt;br /&gt;
'''Note: support for code-folding can't be done from the lexer files. It must be done right in the Scintilla code'''.&lt;br /&gt;
&lt;br /&gt;
See here: http://sphere.sourceforge.net/flik/docs/scintilla-folding.html&lt;br /&gt;
&lt;br /&gt;
==Adding support for a lexer not supported in Scintilla==&lt;br /&gt;
&lt;br /&gt;
'''Note: support for a lexer not supported in Scintilla is out of scope of Code::Blocks. It must be done right in the Scintilla code.'''&lt;br /&gt;
&lt;br /&gt;
Here are some instructions: http://scintilla.sourceforge.net/Lexer.txt&lt;br /&gt;
&lt;br /&gt;
* After you've written the Scintilla lexer, submit the files to the [http://sourceforge.net/tracker/?group_id=2439 Scintilla tracker], following their own [http://scintilla.sourceforge.net/SciCoding.html coding style].&lt;br /&gt;
&lt;br /&gt;
* After that, make any necesary change to wxScintilla and sumbit the files to the [http://sourceforge.net/tracker/?group_id=51305&amp;amp;atid=462818 wxScintilla tracker] or send a mail to the autor wyo@users.sourceforge.net (Otto Wyss).&lt;br /&gt;
&lt;br /&gt;
* And finishing, be sure to send all to the [http://developer.berlios.de/patch/?func=addpatch&amp;amp;group_id=5358 Code::Blocks tracker] (Category: Lexer), and announce it at the [/index.php?board=7.0 Forum] and the [[Announcement for plugins/patches|Wiki announcement for plugins/patches]].&lt;/div&gt;</summary>
		<author><name>Jazzboy</name></author>
	</entry>
</feed>