Skip to content

Commit 9bc9232

Browse files
committed
🔨 add syntax highlighting grammar
1 parent cf73c75 commit 9bc9232

File tree

9 files changed

+478
-15
lines changed

9 files changed

+478
-15
lines changed

‎Json5.Tests/ParserTests/Integers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ void Empty() =>
8585
Expecting: hexadecimal digit
8686
""");
8787

88+
[Fact]
89+
void FractionsNotSupported() =>
90+
Invoking(() => Parser.Parse("0x0.ABC"))
91+
.Should().Throw<Exception>().WithMessage("Format of the number literal 0x0.ABC is not supported.");
92+
93+
[Fact]
94+
void ExponentsNotSupported() =>
95+
Invoking(() => Parser.Parse("0x1p-3"))
96+
.Should().Throw<Exception>().WithMessage("Format of the number literal 0x1p-3 is not supported.");
97+
8898
[Fact] void MinInt() => Parser.Parse("-0x80000000").Should().Be(int.MinValue);
8999
[Fact] void MaxInt() => Parser.Parse("0x7fffffff").Should().Be(int.MaxValue);
90100
[Fact] void MaxUInt() => Parser.Parse("0xffffffff").Should().Be(uint.MaxValue);

‎Json5.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{58
2020
EndProject
2121
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicUsage", "examples\BasicUsage\BasicUsage.csproj", "{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}"
2222
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigurationBuilder", "examples\ConfigurationBuilder\ConfigurationBuilder.csproj", "{6A90617F-47D5-433D-A5CC-D5E2769A4C6D}"
24+
EndProject
25+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ide-integrations", "ide-integrations", "{D53C654D-DF88-4794-867E-093A5D3EBC1A}"
26+
ProjectSection(SolutionItems) = preProject
27+
ide-integrations\install.ps1 = ide-integrations\install.ps1
28+
ide-integrations\JSON5.tmLanguage = ide-integrations\JSON5.tmLanguage
29+
ide-integrations\language-configuration.json = ide-integrations\language-configuration.json
30+
EndProjectSection
31+
EndProject
2332
Global
2433
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2534
Debug|Any CPU = Debug|Any CPU
@@ -38,11 +47,16 @@ Global
3847
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
3948
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
4049
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{6A90617F-47D5-433D-A5CC-D5E2769A4C6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{6A90617F-47D5-433D-A5CC-D5E2769A4C6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{6A90617F-47D5-433D-A5CC-D5E2769A4C6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{6A90617F-47D5-433D-A5CC-D5E2769A4C6D}.Release|Any CPU.Build.0 = Release|Any CPU
4154
EndGlobalSection
4255
GlobalSection(SolutionProperties) = preSolution
4356
HideSolutionNode = FALSE
4457
EndGlobalSection
4558
GlobalSection(NestedProjects) = preSolution
4659
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B} = {58D535B0-AE32-4202-8D70-B9206667D248}
60+
{6A90617F-47D5-433D-A5CC-D5E2769A4C6D} = {58D535B0-AE32-4202-8D70-B9206667D248}
4761
EndGlobalSection
4862
EndGlobal

‎Json5/Internal/NumberParser.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static class NumberParser {
2424
public const NumberLiteralOptions NumLiteralOpts =
2525
AllowBinary | AllowHexadecimal
2626
| AllowMinusSign | AllowPlusSign
27-
| AllowFraction | AllowFractionWOIntegerPart | AllowExponent
27+
| AllowFraction | AllowFractionWOIntegerPart
28+
| AllowExponent
2829
| AllowInfinity | AllowNaN;
2930

3031
public static CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;
@@ -33,26 +34,26 @@ public static class NumberParser {
3334
{ IsInfinity: true, HasMinusSign: true } => '"' + double.NegativeInfinity.ToString(Culture) + '"',
3435
{ IsInfinity: true } => '"' + double.PositiveInfinity.ToString(Culture) + '"',
3536
{ IsNaN: true } => '"' + double.NaN.ToString(Culture) + '"',
36-
{ IsInteger: false } => nl.Normalize().String,
37-
{ IsDecimal: true } => nl.Normalize().String,
3837
{ IsHexadecimal: true } => ParseHexNum(nl).ToString(Culture),
3938
{ IsBinary: true } => ParseBinNum(nl).ToString(Culture),
40-
_ => throw new NotSupportedException($"Format of the number literal {nl.String} is not supported.")
39+
_ => nl.Normalize().String,
4140
};
4241

4342
public static string ParseHexNum(NumberLiteral nl) {
4443
var s = nl.HasMinusSign || nl.HasPlusSign ? nl.String[3..] : nl.String[2..];
4544
var sign = nl.HasMinusSign ? -1 : 1;
4645
// Prepend 0 so the most significant bit is never set and the result will always be positive.
47-
var val = BigInteger.Parse('0' + s, NumberStyles.AllowHexSpecifier, Culture) * sign;
48-
return val.ToString(Culture);
46+
return BigInteger.TryParse('0' + s, NumberStyles.AllowHexSpecifier, Culture, out var val)
47+
? (sign * val).ToString(Culture)
48+
: throw new NotSupportedException($"Format of the number literal {nl.String} is not supported.");
4949
}
5050

5151
public static string ParseBinNum(NumberLiteral nl) {
5252
var s = nl.HasMinusSign || nl.HasPlusSign ? nl.String[3..] : nl.String[2..];
5353
var sign = nl.HasMinusSign ? -1 : 1;
5454
// Prepend 0 so the most significant bit is never set and the result will always be positive.
55-
var val = BigInteger.Parse('0' + s, NumberStyles.AllowBinarySpecifier, Culture) * sign;
56-
return val.ToString(Culture);
55+
return BigInteger.TryParse('0' + s, NumberStyles.AllowBinarySpecifier, Culture, out var val)
56+
? (sign * val).ToString(Culture)
57+
: throw new NotSupportedException($"Format of the number literal {nl.String} is not supported.");
5758
}
5859
}

‎examples/BasicUsage/Program.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
2525
{Json5.Parser.Parse(json5)}
2626
27-
""");
28-
29-
// `JsonSerializerOptions.Web` makes property names case-insensitive and allows the
30-
// floating point literals "NaN" and "Infinity".
31-
Console.WriteLine(
32-
$"""
3327
Deserialized as record:
3428
35-
{Json5.Parser.Parse(json5).Deserialize<Foo>(JsonSerializerOptions.Web)}
29+
{
30+
// `JsonSerializerOptions.Web` makes property names case-insensitive and allows
31+
// floating point literals ("NaN", "Infinity").
32+
Json5.Parser.Parse(json5).Deserialize<Foo>(JsonSerializerOptions.Web)
33+
}
3634
""");
3735

3836
public record Foo(string Description, double[] Nums) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\Json5\Json5.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<None Update="appsettings.json5">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
var config = new ConfigurationBuilder()
4+
.AddJson5File("appsettings.json5")
5+
.Build();
6+
7+
Console.WriteLine(
8+
$"""
9+
Raw config entries:
10+
11+
{string.Join(Environment.NewLine, config.AsEnumerable())}
12+
13+
Deserialized config:
14+
15+
{config.Get<Config>()}
16+
""");
17+
18+
public record Config(string Description, double[] Nums) {
19+
public override string ToString()
20+
=> $"Config {{ Description = {Description[..18]}..., Nums = [{string.Join(", ", Nums)}] }}";
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
// hello world
3+
description: 'This example shows how to integrate your JSON5 settings file with \
4+
the `Microsoft.Extensions.Configuration.ConfigurationBuilder`.',
5+
nums: [.1, -Infinity, NaN, 2.e-3, 0b101, 0xBEEF]
6+
}

0 commit comments

Comments
 (0)