-
-
Save mastersign/a22344b6e563e8bad2dc2939b65ed3f5 to your computer and use it in GitHub Desktop.
| <?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>(?<=\:)\s*[>|][+-]?\d*</Begin> | |
| <End>^$</End> | |
| <RuleSet> | |
| <Rule color="String">.+</Rule> | |
| </RuleSet> | |
| </Span> | |
| <Import ruleSet="Literals" /> | |
| <Span ruleSet="JsonExpression"> | |
| <Begin>(?<=\:)\s(?=\[|\{)</Begin> | |
| </Span> | |
| <Rule color="Array">^\s*-</Rule> | |
| <Rule color="MapKey">\s*(?:'.+?'|".+?"|\S+?)\s*(?=\:)</Rule> | |
| </RuleSet> | |
| <RuleSet name="Literals"> | |
| <Import ruleSet="Strings" /> | |
| <Rule color="Keyword">(?<=\:|-)\s+(true|false|yes|no|on|off|null|NaN)\s*$</Rule> | |
| <Rule color="Number">(?<=\:|-)\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>(?<=\:|-)\s*"</Begin> | |
| <End>"</End> | |
| <RuleSet> | |
| <Span begin="\\" end="." foreground="IndianRed" /> | |
| </RuleSet> | |
| </Span> | |
| <Span color="String"> | |
| <Begin>(?<=\:|-)\s*'</Begin> | |
| <End>'</End> | |
| <RuleSet> | |
| <Span begin="\\" end="." foreground="IndianRed" /> | |
| </RuleSet> | |
| </Span> | |
| </RuleSet> | |
| <RuleSet name="JsonStrings"> | |
| <Span color="Json"> | |
| <Begin>"</Begin> | |
| <End>"</End> | |
| <RuleSet> | |
| <Span begin="\\" end="." foreground="IndianRed" /> | |
| </RuleSet> | |
| </Span> | |
| <Span color="Json"> | |
| <Begin>'</Begin> | |
| <End>'</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> |
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.
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.
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.