A keyword-based highlighter which can highlight keywords, text parts enclosed with specific patterns, line comment, and regular expressions.

Namespace: Sgry.Azuki.Highlighter
Assembly: Azuki (in Azuki.dll) Version: 1.7.5.21597

Syntax

C#
public class KeywordHighlighter : IHighlighter
Visual Basic
Public Class KeywordHighlighter _
	Implements IHighlighter

Remarks

KeywordHighlighter highlights keywords, enclosed parts, and regular expressions. To make basic syntax highlighter, you can create an instance and register highlighting targets, or make a child class and register highlighting targets.

KeywordHighlighter can highlight four types of text patterns.

  1. Keyword set
  2. Line highlight
  3. Enclosure
  4. Regular expression

Keyword set is a set of keywords. KeywordHighlighter searches a document for registered keywords and applies char-class associated with the keyword set. For example, you may create two keyword sets for highlighting C# source code. One of the them contains every keywords of C# and is associated with CharClass.Keyword. Another one contains every preprocessor macro keywords and is associated with CharClass.Macro. To register keyword sets, use AddKeywordSet method.

Line highlight is a feature to highlight text patterns which begins with particular pattern and continues until the end of line. This feature is designed to highlight single line comment found in many programming language. To register targets of line highlight, use AddLineHighlight method.

Enclosure is a text pattern that is enclosed with particular patterns. Typical example of enclosure type is "string literal" and "multiple line comment" found in many programming languages. To register enclosure target, use AddEnclosure method.

Regular expression is one of the most flexible and popular method to express character sequence pattern. To register a regular expression to specify highlighting targets, give AddRegex method a regular expression and a CharClass. Note that there is another overloaded method AddRegex(String, Boolean, IList<(Of <<'(CharClass>)>>)) which takes not a CharClass but a list of them. This version applies the char-classes to each group captured in every matched patterns. If you need to highlight complex patterns consisting of sub-patterns each of which should be highlighted differently, this method will be useful.

Here are some notes about highlighting with regular expressions.

  • If you need to specify preceding or following text patterns for specifying highlighting targets strictly, consider using zero-width assertions such as (?=...) and (?!...). For example, regular expression literals used in Perl may be specified as (?<!\w\s*)/([^/\\]|\\.)+/[a-z]*, which uses a negative lookbehind assertion to prevent highlighting patterns look like /.../ whose preceding non-whitespace character is a letter or digit. By the assertion, /3+2/ inside $x=/3+2/; will be highlighted but $x=1/3+2/3; will not be highlighted.
  • The back-end of this feature is System. Text. RegularExpressions. Regex, which is provided by .NET Framework. For detail of regular expression, refer to the reference manual of that class.

There is one more note about this class. KeywordHighlighter highlights numeric literals like 3.14 or 0xFFFE by default. To disable this feature, set false to HighlightsNumericLiterals property. There is no customization option for this feature so if you want to highlight numeric literals in a way different from this class's, disable this feature and define regular expressions for numeric literals by your own. For more detail of this feature, see the document of HighlightsNumericLiterals property.

Examples

Next example creates a highlighter object to highlight C# source code.

CopyC#
KeywordHighlighter kh = new KeywordHighlighter();

// Registering keyword set
kh.AddKeywordSet( new string[]{
    "abstract", "as", "base", "bool", ...
}, CharClass.Keyword );

// Registering pre-processor keywords
string[] words = new string[] {
    "define", "elif", "else", "endif", "endregion", "error", "if",
    "line", "pragma", "region", "undef", "warning"
};
AddRegex( @"^\s*#\s*(" + String.Join(@"\b|", words) + @"\b)",
          CharClass.Macro );

// Registering string literals and character literal
kh.AddEnclosure( "'", "'", CharClass.String, false, '\\' );
kh.AddEnclosure( "@\"", "\"", CharClass.String, true, '\"' );
kh.AddEnclosure( "\"", "\"", CharClass.String, false, '\\' );

// Registering comment
kh.AddEnclosure( "/**", "*/", CharClass.DocComment, true );
kh.AddEnclosure( "/*", "*/", CharClass.Comment, true );
kh.AddLineHighlight( "///", CharClass.DocComment );
kh.AddLineHighlight( "//", CharClass.Comment );

Inheritance Hierarchy

System..::..Object
  Sgry.Azuki.Highlighter..::..KeywordHighlighter

See Also