diff --git a/src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs b/src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs index aa85bb3..c9c886c 100644 --- a/src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs +++ b/src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs @@ -8,33 +8,23 @@ namespace HydraScript.Domain.FrontEnd.Lexer.Impl; public class Structure : IStructure { - public Structure(List types) + public Structure(ITokenTypesProvider provider) { - types.AddRange(new List - { - new EndOfProgramType(), - new ErrorType() - }); - types = types + Types = provider.GetTokenTypes() + .Concat([new EndOfProgramType(), new ErrorType()]) .OrderBy(t => t.Priority) - .ToList(); - - Types = types - .ToDictionary(x => x.Tag, x => x); + .ToDictionary(x => x.Tag); Regex = new Regex( string.Join( '|', - types - .Where(t => !t.EndOfProgram()) + this.Where(t => !t.EndOfProgram()) .Select(t => t.GetNamedRegex()) - .ToList() - ) - ); + .ToList())); } private Dictionary Types { get; } - + public Regex Regex { get; } public TokenType FindByTag(string tag) => @@ -43,10 +33,10 @@ public TokenType FindByTag(string tag) => public override string ToString() => new StringBuilder() .AppendJoin('\n', - Types.Select(x => $"{x.Key} {x.Value.Pattern}") - ).ToString(); + Types.Select(x => $"{x.Key} {x.Value.Pattern}")) + .ToString(); - public IEnumerator GetEnumerator() => + public IEnumerator GetEnumerator() => Types.Values.GetEnumerator(); [ExcludeFromCodeCoverage] diff --git a/src/Infrastructure/HydraScript.Infrastructure/ServiceCollectionExtensions.cs b/src/Infrastructure/HydraScript.Infrastructure/ServiceCollectionExtensions.cs index dc285de..457fe69 100644 --- a/src/Infrastructure/HydraScript.Infrastructure/ServiceCollectionExtensions.cs +++ b/src/Infrastructure/HydraScript.Infrastructure/ServiceCollectionExtensions.cs @@ -18,7 +18,8 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddDomain(this IServiceCollection services) { services.AddSingleton(); - services.AddSingleton(StructureInstance.Get); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Infrastructure/HydraScript.Infrastructure/StructureInstance.cs b/src/Infrastructure/HydraScript.Infrastructure/StructureInstance.cs deleted file mode 100644 index 3923c8b..0000000 --- a/src/Infrastructure/HydraScript.Infrastructure/StructureInstance.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text.Json; -using System.Text.Json.Serialization; -using HydraScript.Domain.FrontEnd.Lexer; -using HydraScript.Domain.FrontEnd.Lexer.Impl; -using HydraScript.Domain.FrontEnd.Lexer.TokenTypes; - -namespace HydraScript.Infrastructure; - -internal static class StructureInstance -{ - private static readonly JsonSerializerOptions JsonSerializerOptions = new() - { - Converters = { new StructureReadConverter() } - }; - - private static Structure? _instance; - public static IStructure Get - { - get - { - _instance ??= JsonSerializer.Deserialize( - TokenTypesJson.String, - JsonSerializerOptions)!; - return _instance; - } - } - - [ExcludeFromCodeCoverage] - private class StructureReadConverter : JsonConverter - { - public override Structure Read(ref Utf8JsonReader reader, - Type typeToConvert, JsonSerializerOptions options) - { - using var jsonDocument = JsonDocument.ParseValue(ref reader); - var tokenTypes = jsonDocument.RootElement - .EnumerateArray().Select(element => - { - var tag = element.GetProperty("tag").GetString()!; - var pattern = element.GetProperty("pattern").GetString()!; - var priority = element.GetProperty("priority").GetInt32(); - - var ignorable = element.TryGetProperty("canIgnore", out var canIgnore); - - return ignorable && canIgnore.GetBoolean() - ? new IgnorableType(tag, pattern, priority) - : new TokenType(tag, pattern, priority); - }).ToList(); - return new Structure(tokenTypes); - } - - public override void Write(Utf8JsonWriter writer, - Structure value, JsonSerializerOptions options) => - throw new NotSupportedException(); - } -} \ No newline at end of file diff --git a/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs b/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs index 6ab2884..37640af 100644 --- a/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs +++ b/src/Infrastructure/HydraScript.Infrastructure/TokenTypesProvider.cs @@ -26,9 +26,8 @@ public override IEnumerable Read( Type typeToConvert, JsonSerializerOptions options) { - using var jsonDocument = JsonDocument.ParseValue(ref reader); - var tokenTypes = jsonDocument.RootElement - .EnumerateArray() + var root = JsonElement.ParseValue(ref reader); + var tokenTypes = root.EnumerateArray() .Select(element => { var tag = element.GetProperty("tag").GetString()!; diff --git a/tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs b/tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs index 76d6c12..f35f981 100644 --- a/tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs +++ b/tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs @@ -9,7 +9,7 @@ namespace HydraScript.Tests.Unit.FrontEnd; public class RegexLexerTests { private readonly RegexLexer _regexLexer = new( - StructureInstance.Get, + new Structure(new TokenTypesProvider()), new TextCoordinateSystemComputer()); [Theory] diff --git a/tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs b/tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs index 5dc98ef..667fa16 100644 --- a/tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs +++ b/tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs @@ -1,5 +1,7 @@ +using HydraScript.Domain.FrontEnd.Lexer; using HydraScript.Domain.FrontEnd.Lexer.Impl; using HydraScript.Domain.FrontEnd.Lexer.TokenTypes; +using Moq; using Xunit; namespace HydraScript.Tests.Unit.FrontEnd; @@ -14,7 +16,10 @@ public void ToStringCorrectTest() new ("MyToken", "[m|M][y|Y]", 2), new ("OneToSeven", "[1-7]", 1) }; - var structure = new Structure(tokenTypes); + var provider = new Mock(); + provider.Setup(x => x.GetTokenTypes()) + .Returns(tokenTypes); + var structure = new Structure(provider.Object); var expectedText = string.Join('\n', new List diff --git a/tests/HydraScript.Tests/Unit/FrontEnd/TopDownParserTests.cs b/tests/HydraScript.Tests/Unit/FrontEnd/TopDownParserTests.cs index c27055f..b6104d3 100644 --- a/tests/HydraScript.Tests/Unit/FrontEnd/TopDownParserTests.cs +++ b/tests/HydraScript.Tests/Unit/FrontEnd/TopDownParserTests.cs @@ -10,7 +10,7 @@ namespace HydraScript.Tests.Unit.FrontEnd; public class TopDownParserTests { private readonly IParser _parser = new TopDownParser(new RegexLexer( - StructureInstance.Get, + new Structure(new TokenTypesProvider()), new TextCoordinateSystemComputer())); [Theory]