Skip to content

Commit 34878ac

Browse files
daxian-dbwmichael-hawker
authored andcommitted
Add the language parser for JSON
1 parent 7c21251 commit 34878ac

File tree

8 files changed

+127
-3
lines changed

8 files changed

+127
-3
lines changed

ColorCode.Core/Common/LanguageId.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class LanguageId
1818
public const string Html = "html";
1919
public const string Java = "java";
2020
public const string JavaScript = "javascript";
21+
public const string Json = "json";
2122
public const string TypeScript = "typescript";
2223
public const string Php = "php";
2324
public const string PowerShell = "powershell";

ColorCode.Core/Common/ScopeName.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class ScopeName
1919
public const string HtmlOperator = "HTML Operator";
2020
public const string HtmlServerSideScript = "HTML Server-Side Script";
2121
public const string HtmlTagDelimiter = "Html Tag Delimiter";
22+
public const string JsonKey = "Json Key";
23+
public const string JsonString = "Json String";
24+
public const string JsonNumber = "Json Number";
25+
public const string JsonConst = "Json Const";
2226
public const string Keyword = "Keyword";
2327
public const string LanguagePrefix = "&";
2428
public const string PlainText = "Plain Text";

ColorCode.Core/Compilation/Languages/JavaScript.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ public bool HasAlias(string lang)
7878
{
7979
case "js":
8080
return true;
81-
82-
case "json":
83-
return true;
8481

8582
default:
8683
return false;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

ColorCode.Core/Languages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static Languages()
4242
Load<Css>();
4343
Load<Cpp>();
4444
Load<Java>();
45+
Load<Json>();
4546
Load<PowerShell>();
4647
Load<Typescript>();
4748
Load<FSharp>();

ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ public static StyleDictionary DefaultDark
117117
Foreground = Red,
118118
ReferenceName = "htmlEntity"
119119
},
120+
new Style(ScopeName.JsonKey)
121+
{
122+
Foreground = DarkOrange,
123+
ReferenceName = "jsonKey"
124+
},
125+
new Style(ScopeName.JsonString)
126+
{
127+
Foreground = DarkCyan,
128+
ReferenceName = "jsonString"
129+
},
130+
new Style(ScopeName.JsonNumber)
131+
{
132+
Foreground = BrightGreen,
133+
ReferenceName = "jsonNumber"
134+
},
135+
new Style(ScopeName.JsonConst)
136+
{
137+
Foreground = BrightPurple,
138+
ReferenceName = "jsonConst"
139+
},
120140
new Style(ScopeName.XmlAttribute)
121141
{
122142
Foreground = VSDarkXMLAttribute,

ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ public static StyleDictionary DefaultLight
101101
Foreground = Red,
102102
ReferenceName = "htmlEntity"
103103
},
104+
new Style(ScopeName.JsonKey)
105+
{
106+
Foreground = DarkOrange,
107+
ReferenceName = "jsonKey"
108+
},
109+
new Style(ScopeName.JsonString)
110+
{
111+
Foreground = DarkCyan,
112+
ReferenceName = "jsonString"
113+
},
114+
new Style(ScopeName.JsonNumber)
115+
{
116+
Foreground = BrightGreen,
117+
ReferenceName = "jsonNumber"
118+
},
119+
new Style(ScopeName.JsonConst)
120+
{
121+
Foreground = BrightPurple,
122+
ReferenceName = "jsonConst"
123+
},
104124
new Style(ScopeName.XmlAttribute)
105125
{
106126
Foreground = Red,

ColorCode.Core/Styling/StyleDictionary.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ protected override string GetKeyForItem(Style item)
3939
public const string OliveDrab = "#FF6B8E23";
4040
public const string DarkOliveGreen = "#FF556B2F";
4141
public const string DarkCyan = "#FF008B8B";
42+
public const string DarkOrange = "#FFFF8700";
43+
public const string BrightGreen = "#FF00d700";
44+
public const string BrightPurple = "#FFaf87ff";
4245
}
4346
}

0 commit comments

Comments
 (0)