Can't figure out how to color code .yml files

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by Jeff Cohen
Version: 4.0.0233
I've had success writing the .xml for the dynamic lexer for a couple of different file types, but I can't figure out what I'm doing wrong for .yml files.

Quick background on .yml files if you're not familiar with them... they're similar to INI files, but instead of using brackets around section names and equal signs with key/value pairs, everything is inferred from indentation:
# comments start with a pound sign and terminate at the end of the line
   key: value
Section names must start in column 1 and end with a colon; keys must start with at least one space, end with a colon, followed by the value.

With my XML definition, comments are highlighted correctly and so are section names, but not keys (the "YmlKeyToken" definition below).

Here's a fragment of my xml (let me know if I should post more context).

Thanks in advance... I fear I'm just doing something obviously wrong but just can't see it.


<SyntaxLanguage Key="Yaml" LanguageDefinitionVersion="4.0" Secure="True"

    <!-- Highlighting Styles -->    
        <Style Key="DefaultStyle" Name="Text" ForeColor="Black" />
        <Style Key="YmlKeyStyle" ForeColor="#0000AA" />
        <Style Key="SectionStyle" ForeColor="#AA0000" />
        <Style Key="ValueStyle" ForeColor="#00AA00" />
    <Style Key="CommentDefaultStyle" ForeColor="Gray" />
    <!-- Macros -->    
        <!-- Redefine word macros to include hyphens -->
        <Macro Key="WordMacro" Value="[a-zA-Z_0-9\-]" />
        <Macro Key="NonWordMacro" Value="[^a-zA-Z_0-9\-]" />

<!-- States -->
    <!-- Code -->
    <State Key="DefaultState">
      <!-- Patterns Groups -->
        <!-- Whitespace -->
        <RegexPatternGroup TokenKey="WhitespaceToken" PatternValue="{WhitespaceMacro}+" IsWhitespace="True" />
        <!-- Line Terminators -->
        <RegexPatternGroup TokenKey="LineTerminatorToken" PatternValue="{LineTerminatorMacro}" IsWhitespace="True" />
        <!-- Key -->
        <RegexPatternGroup TokenKey="YmlKeyToken" PatternValue="^ {NonWordMacro}+ {WordMacro}+ {WhitespaceMacro}*" LookAhead="\: {WhitespaceMacro}* {WordMacro}+" Style="YmlKeyStyle" />
        <!-- Section -->
        <RegexPatternGroup TokenKey="YmlSectionToken" PatternValue="^ {WordMacro}+ {WhitespaceMacro}* \: {WhitespaceMacro}* {LineTerminatorMacro}" Style="SectionStyle" />


        <ChildState Key="CommentState" />

Comments (1)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
The best way to "debug" languages is to load them into our SDI Editor sample
using the Custom Dynamic menu item. Then move the caret around and the status
bar shows you what token is at the caret and puts an asterisk by it if it starts a token.

When doing this, I see that your key is matching the WhitespaceToken since that is
what you declared first in the state. So just move the YmlKeyToken def up above
WhitespaceToken and that resolves it. The dynamic lexer always accepts the first match.

Actipro Software Support

The latest build of this product (v24.1.0) was released 4 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.