Skip to content

Instantly share code, notes, and snippets.

@mastersign
Created February 21, 2022 08:10
Show Gist options
  • Select an option

  • Save mastersign/a22344b6e563e8bad2dc2939b65ed3f5 to your computer and use it in GitHub Desktop.

Select an option

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 <dev@mastersign.de> -->
<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

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