-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using VDF.Models; | ||
|
||
namespace VDF.Exceptions; | ||
|
||
public class VDFSyntaxException : Exception | ||
{ | ||
public VDFSyntaxException(VDFToken? unexpected, string[] expected, int index, int line, int character) : base($"Unexpected {(unexpected != null ? $"{unexpected?.Type} '{unexpected?.Value}'" : "EOF")} at position {index} (line {line + 1}, character {character + 1}). Expected {string.Join(" | ", expected)}.") | ||
{ | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace VDF.Models; | ||
|
||
public readonly record struct KeyValue | ||
{ | ||
public required string Key { get; init; } | ||
public required dynamic Value { get; init; } | ||
public required string? Conditional { get; init; } | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace VDF.Models; | ||
|
||
public class KeyValues : List<KeyValue> | ||
{ | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace VDF.Models; | ||
|
||
public readonly record struct VDFToken | ||
{ | ||
public required VDFTokenType Type { get; init; } | ||
public required string Value { get; init; } | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace VDF.Models; | ||
|
||
public enum VDFTokenType : byte | ||
{ | ||
String, | ||
Conditional, | ||
ControlCharacter, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System; | ||
using VDF.Exceptions; | ||
using VDF.Models; | ||
|
||
namespace VDF; | ||
|
||
public static class VDFSerializer | ||
{ | ||
public static KeyValues Deserialize(string str) | ||
{ | ||
VDFTokeniser tokeniser = new(str); | ||
|
||
static KeyValues ReadKeyValues(VDFTokeniser tokeniser, bool isObject) | ||
{ | ||
KeyValues keyValues = new(); | ||
|
||
while (true) | ||
{ | ||
KeyValue? keyValue = tokeniser.Read() switch | ||
{ | ||
{ Type: VDFTokenType.ControlCharacter, Value: "}" } when isObject => null, | ||
null when !isObject => null, | ||
null => throw new VDFSyntaxException( | ||
null, | ||
new[] { "key" }, | ||
tokeniser.Index, | ||
tokeniser.Line, | ||
tokeniser.Character | ||
), | ||
{ Type: VDFTokenType.String } token => ReadKeyValue(tokeniser, token.Value), | ||
VDFToken token => throw new VDFSyntaxException( | ||
token, | ||
new[] { "key" }, | ||
tokeniser.Index, | ||
tokeniser.Line, | ||
tokeniser.Character | ||
), | ||
}; | ||
|
||
if (keyValue == null) | ||
{ | ||
break; | ||
} | ||
|
||
keyValues.Add(keyValue.Value); | ||
} | ||
|
||
return keyValues; | ||
} | ||
|
||
static KeyValue ReadKeyValue(VDFTokeniser tokeniser, string key) | ||
{ | ||
VDFToken? valueToken = tokeniser.Read(); | ||
|
||
string? conditional; | ||
|
||
if (valueToken is { Type: VDFTokenType.Conditional }) | ||
{ | ||
conditional = valueToken.Value.Value; | ||
valueToken = tokeniser.Read(); | ||
} | ||
else | ||
{ | ||
conditional = null; | ||
} | ||
|
||
return valueToken switch | ||
{ | ||
{ Type: VDFTokenType.ControlCharacter, Value: "{" } => new KeyValue | ||
{ | ||
Key = key, | ||
Value = ReadKeyValues(tokeniser, true), | ||
Conditional = conditional, | ||
}, | ||
{ Type: VDFTokenType.String } token => new KeyValue | ||
{ | ||
Key = key, | ||
Value = token.Value, | ||
Conditional = ReadConditional(tokeniser) ?? conditional, | ||
}, | ||
VDFToken token => throw new VDFSyntaxException( | ||
token, | ||
new[] { "value", "{" }, | ||
tokeniser.Index, | ||
tokeniser.Line, | ||
tokeniser.Character | ||
), | ||
null => throw new VDFSyntaxException( | ||
null, | ||
new[] { "value", "{" }, | ||
tokeniser.Index, | ||
tokeniser.Line, | ||
tokeniser.Character | ||
), | ||
}; | ||
} | ||
|
||
static string? ReadConditional(VDFTokeniser tokeniser) | ||
{ | ||
if (tokeniser.Read(true) is { Type: VDFTokenType.Conditional } token) | ||
{ | ||
tokeniser.Read(); | ||
return token.Value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return ReadKeyValues(tokeniser, false); | ||
} | ||
|
||
public static string Serialize(KeyValues keyValues, int level = 0) | ||
{ | ||
string str = ""; | ||
|
||
foreach (KeyValue keyValue in keyValues) | ||
{ | ||
if (keyValue.Value is KeyValues kvs) | ||
{ | ||
str += $"{new string('\t', level)}\"{keyValue.Key}\"{(keyValue.Conditional != null ? $" {keyValue.Conditional}" : "")}\r\n"; | ||
str += $"{new string('\t', level)}{{\r\n"; | ||
str += $"{Serialize(kvs, level + 1)}{new string('\t', level)}}}\r\n"; | ||
} | ||
else if (keyValue.Value is string value) | ||
{ | ||
str += $"{new string('\t', level)}\"{keyValue.Key}\"\t\"{value}\"{(keyValue.Conditional != null ? $" {keyValue.Conditional}" : "")}\r\n"; | ||
} | ||
} | ||
|
||
return str; | ||
} | ||
} |
Oops, something went wrong.