|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using ColorCode.Common; |
| 7 | + |
| 8 | +namespace ColorCode.Compilation.Languages |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Leverage the regex from https://gist.github.com/goodmami/02f344e8c9a22fc9ac879230a9b9e071#version-2 |
| 12 | + /// for parsing the key, string value, number, and constant for a JSON code block. |
| 13 | + /// </summary> |
| 14 | + public class Json : ILanguage |
| 15 | + { |
| 16 | + private const string Regex_String = @"""[^""\\]*(?:\\[^\r\n]|[^""\\]*)*"""; |
| 17 | + private const string Regex_Number = @"-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?"; |
| 18 | + |
| 19 | + public string Id |
| 20 | + { |
| 21 | + get { return LanguageId.Json; } |
| 22 | + } |
| 23 | + |
| 24 | + public string Name |
| 25 | + { |
| 26 | + get { return "JSON"; } |
| 27 | + } |
| 28 | + |
| 29 | + public string CssClassName |
| 30 | + { |
| 31 | + get { return "json"; } |
| 32 | + } |
| 33 | + |
| 34 | + public string FirstLinePattern |
| 35 | + { |
| 36 | + get { return null; } |
| 37 | + } |
| 38 | + |
| 39 | + public IList<LanguageRule> Rules |
| 40 | + { |
| 41 | + get |
| 42 | + { |
| 43 | + return new List<LanguageRule> |
| 44 | + { |
| 45 | + new LanguageRule( |
| 46 | + $@"[,\{{]\s*({Regex_String})\s*:", |
| 47 | + new Dictionary<int, string> |
| 48 | + { |
| 49 | + {1, ScopeName.JsonKey} |
| 50 | + }), |
| 51 | + new LanguageRule( |
| 52 | + Regex_String, |
| 53 | + new Dictionary<int, string> |
| 54 | + { |
| 55 | + {0, ScopeName.JsonString} |
| 56 | + }), |
| 57 | + new LanguageRule( |
| 58 | + Regex_Number, |
| 59 | + new Dictionary<int, string> |
| 60 | + { |
| 61 | + {0, ScopeName.JsonNumber} |
| 62 | + }), |
| 63 | + new LanguageRule( |
| 64 | + @"\b(true|false|null)\b", |
| 65 | + new Dictionary<int, string> |
| 66 | + { |
| 67 | + {1, ScopeName.JsonConst} |
| 68 | + }), |
| 69 | + }; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public bool HasAlias(string lang) |
| 74 | + { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments