Skip to content

Commit

Permalink
Add the language parser for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored and michael-hawker committed Jul 13, 2023
1 parent 7c21251 commit 34878ac
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions ColorCode.Core/Common/LanguageId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class LanguageId
public const string Html = "html";
public const string Java = "java";
public const string JavaScript = "javascript";
public const string Json = "json";
public const string TypeScript = "typescript";
public const string Php = "php";
public const string PowerShell = "powershell";
Expand Down
4 changes: 4 additions & 0 deletions ColorCode.Core/Common/ScopeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class ScopeName
public const string HtmlOperator = "HTML Operator";
public const string HtmlServerSideScript = "HTML Server-Side Script";
public const string HtmlTagDelimiter = "Html Tag Delimiter";
public const string JsonKey = "Json Key";
public const string JsonString = "Json String";
public const string JsonNumber = "Json Number";
public const string JsonConst = "Json Const";
public const string Keyword = "Keyword";
public const string LanguagePrefix = "&";
public const string PlainText = "Plain Text";
Expand Down
3 changes: 0 additions & 3 deletions ColorCode.Core/Compilation/Languages/JavaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ public bool HasAlias(string lang)
{
case "js":
return true;

case "json":
return true;

default:
return false;
Expand Down
78 changes: 78 additions & 0 deletions ColorCode.Core/Compilation/Languages/Json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using ColorCode.Common;

namespace ColorCode.Compilation.Languages
{
/// <summary>
/// Leverage the regex from https://gist.github.com/goodmami/02f344e8c9a22fc9ac879230a9b9e071#version-2
/// for parsing the key, string value, number, and constant for a JSON code block.
/// </summary>
public class Json : ILanguage
{
private const string Regex_String = @"""[^""\\]*(?:\\[^\r\n]|[^""\\]*)*""";
private const string Regex_Number = @"-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?";

public string Id
{
get { return LanguageId.Json; }
}

public string Name
{
get { return "JSON"; }
}

public string CssClassName
{
get { return "json"; }
}

public string FirstLinePattern
{
get { return null; }
}

public IList<LanguageRule> Rules
{
get
{
return new List<LanguageRule>
{
new LanguageRule(
$@"[,\{{]\s*({Regex_String})\s*:",
new Dictionary<int, string>
{
{1, ScopeName.JsonKey}
}),
new LanguageRule(
Regex_String,
new Dictionary<int, string>
{
{0, ScopeName.JsonString}
}),
new LanguageRule(
Regex_Number,
new Dictionary<int, string>
{
{0, ScopeName.JsonNumber}
}),
new LanguageRule(
@"\b(true|false|null)\b",
new Dictionary<int, string>
{
{1, ScopeName.JsonConst}
}),
};
}
}

public bool HasAlias(string lang)
{
return false;
}
}
}
1 change: 1 addition & 0 deletions ColorCode.Core/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static Languages()
Load<Css>();
Load<Cpp>();
Load<Java>();
Load<Json>();
Load<PowerShell>();
Load<Typescript>();
Load<FSharp>();
Expand Down
20 changes: 20 additions & 0 deletions ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ public static StyleDictionary DefaultDark
Foreground = Red,
ReferenceName = "htmlEntity"
},
new Style(ScopeName.JsonKey)
{
Foreground = DarkOrange,
ReferenceName = "jsonKey"
},
new Style(ScopeName.JsonString)
{
Foreground = DarkCyan,
ReferenceName = "jsonString"
},
new Style(ScopeName.JsonNumber)
{
Foreground = BrightGreen,
ReferenceName = "jsonNumber"
},
new Style(ScopeName.JsonConst)
{
Foreground = BrightPurple,
ReferenceName = "jsonConst"
},
new Style(ScopeName.XmlAttribute)
{
Foreground = VSDarkXMLAttribute,
Expand Down
20 changes: 20 additions & 0 deletions ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ public static StyleDictionary DefaultLight
Foreground = Red,
ReferenceName = "htmlEntity"
},
new Style(ScopeName.JsonKey)
{
Foreground = DarkOrange,
ReferenceName = "jsonKey"
},
new Style(ScopeName.JsonString)
{
Foreground = DarkCyan,
ReferenceName = "jsonString"
},
new Style(ScopeName.JsonNumber)
{
Foreground = BrightGreen,
ReferenceName = "jsonNumber"
},
new Style(ScopeName.JsonConst)
{
Foreground = BrightPurple,
ReferenceName = "jsonConst"
},
new Style(ScopeName.XmlAttribute)
{
Foreground = Red,
Expand Down
3 changes: 3 additions & 0 deletions ColorCode.Core/Styling/StyleDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ protected override string GetKeyForItem(Style item)
public const string OliveDrab = "#FF6B8E23";
public const string DarkOliveGreen = "#FF556B2F";
public const string DarkCyan = "#FF008B8B";
public const string DarkOrange = "#FFFF8700";
public const string BrightGreen = "#FF00d700";
public const string BrightPurple = "#FFaf87ff";
}
}

0 comments on commit 34878ac

Please sign in to comment.