Skip to content

Instantly share code, notes, and snippets.

@mastersign
Created February 21, 2022 08:10
Show Gist options
  • Save mastersign/a22344b6e563e8bad2dc2939b65ed3f5 to your computer and use it in GitHub Desktop.
Save mastersign/a22344b6e563e8bad2dc2939b65ed3f5 to your computer and use it in GitHub Desktop.
YAML syntax grammar for AvalonEdit
<?xml version="1.0"?>
<!-- Imperfect syntax grammar for YAML by Tobias Kiertscher <[email protected]> -->
<SyntaxDefinition xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008"
name="YAML" extensions=".yaml,.yml">
<Color name="Comment" foreground="LightSlateGray" fontStyle="italic" />
<Color name="String" foreground="DarkGreen" fontWeight="normal" />
<Color name="Array" foreground="DarkMagenta" fontWeight="bold" />
<Color name="MultilineStringIndicator" foreground="DarkMagenta" fontWeight="bold" />
<Color name="MapKey" foreground="Firebrick" />
<Color name="Keyword" foreground="DarkCyan" />
<Color name="Number" foreground="RoyalBlue" />
<Color name="Json" foreground="SlateBlue" />
<Color name="Punctuation" foreground="MediumSlateBlue" />
<RuleSet>
<Span color="Comment" begin="#" />
<Span color="MultilineStringIndicator" multiline="true">
<Begin>(?&lt;=\:)\s*[&gt;|][+-]?\d*</Begin>
<End>^$</End>
<RuleSet>
<Rule color="String">.+</Rule>
</RuleSet>
</Span>
<Import ruleSet="Literals" />
<Span ruleSet="JsonExpression">
<Begin>(?&lt;=\:)\s(?=\[|\{)</Begin>
</Span>
<Rule color="Array">^\s*-</Rule>
<Rule color="MapKey">\s*(?:&apos;.+?&apos;|&quot;.+?&quot;|\S+?)\s*(?=\:)</Rule>
</RuleSet>
<RuleSet name="Literals">
<Import ruleSet="Strings" />
<Rule color="Keyword">(?&lt;=\:|-)\s+(true|false|yes|no|on|off|null|NaN)\s*$</Rule>
<Rule color="Number">(?&lt;=\:|-)\s*(\b0[xX][0-9a-fA-F]+|(\b\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?)\s*$</Rule>
</RuleSet>
<RuleSet name="Strings">
<Span color="String">
<Begin>(?&lt;=\:|-)\s*&quot;</Begin>
<End>&quot;</End>
<RuleSet>
<Span begin="\\" end="." foreground="IndianRed" />
</RuleSet>
</Span>
<Span color="String">
<Begin>(?&lt;=\:|-)\s*&apos;</Begin>
<End>&apos;</End>
<RuleSet>
<Span begin="\\" end="." foreground="IndianRed" />
</RuleSet>
</Span>
</RuleSet>
<RuleSet name="JsonStrings">
<Span color="Json">
<Begin>&quot;</Begin>
<End>&quot;</End>
<RuleSet>
<Span begin="\\" end="." foreground="IndianRed" />
</RuleSet>
</Span>
<Span color="Json">
<Begin>&apos;</Begin>
<End>&apos;</End>
<RuleSet>
<Span begin="\\" end="." foreground="IndianRed" />
</RuleSet>
</Span>
</RuleSet>
<RuleSet name="JsonObject">
<Import ruleSet="JsonExpression" />
<Rule color="Punctuation">,</Rule>
</RuleSet>
<RuleSet name="JsonArray">
<Import ruleSet="JsonExpression"/>
<Rule color="Punctuation">,</Rule>
</RuleSet>
<RuleSet name="JsonExpression">
<Span color="Comment" begin="#" />
<Span color="Json" ruleSet="JsonObject" multiline="true">
<Begin>\{</Begin>
<End>\}</End>
</Span>
<Span color="Json" ruleSet="JsonArray" multiline="true">
<Begin>\[</Begin>
<End>\]</End>
</Span>
<Import ruleSet="JsonStrings" />
</RuleSet>
</SyntaxDefinition>
@DavidBerg-MSFT
Copy link

This looks very useful, thank you for posting it, I'm using AvalonEdit for an internal project and need YAML syntax highlighting. Are you the original author? What license is it under? Have you thought about submitting it back to AvlonEdit as a 3rd party SyntaxHighlighting: https://github.com/icsharpcode/AvalonEdit/tree/master/ThirdParty-Highlightings? Thanks for your time!

@mastersign
Copy link
Author

I have no affiliation with AvalonEdit. This syntax definition was an attempt to create a comfortable config editor for WPF based GUI. Unfortunately I was not entirely happy with the result, hence the "Imperfect" in the description up top. I was ending up an external text editor for my purposes.
If this is of any use for you anyway, feel free to use it or improve up on it any way you like. An MIT-style mentioning of my name is appreciated, but not necessary.

@mastersign
Copy link
Author

A warning though: After spending some time developing this syntax, I came to believe the grammar of the syntax engine for AvalonEdit is not fully capable of describing the YAML syntax correctly. I may be wrong.

@DavidBerg-MSFT
Copy link

Thanks, much appreciated! I suspect you're right that it might not be possible to properly define YAML with RegEx's (I'm not sure exactly how to build a regex for 'must match # of spaces in line above' and other YAML concepts...). If it helps, YAML was designed to be 'easy to read and write at the expense of being hard to parse' (as opposed to JSON where easy parsing was prioritized above ease of reading and writing), and I often see the YAML syntax highlighting in VS differ from how the YAML parser actually reads the file - so you're not the only one to struggle with it.

At the moment, I think anything is better than nothing. I'll try it out (probably be a little while before I can get to it) and let you know how it goes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment