Created
February 21, 2022 08:10
-
-
Save mastersign/a22344b6e563e8bad2dc2939b65ed3f5 to your computer and use it in GitHub Desktop.
YAML syntax grammar for AvalonEdit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.