Skip to content

Commit

Permalink
#57 - DI refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Aug 2, 2024
1 parent cf8c8f8 commit 2ebd542
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 83 deletions.
30 changes: 10 additions & 20 deletions src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,23 @@ namespace HydraScript.Domain.FrontEnd.Lexer.Impl;

public class Structure : IStructure
{
public Structure(List<TokenType> types)
public Structure(ITokenTypesProvider provider)
{
types.AddRange(new List<TokenType>
{
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<string, TokenType> Types { get; }

public Regex Regex { get; }

public TokenType FindByTag(string tag) =>
Expand All @@ -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<TokenType> GetEnumerator() =>
public IEnumerator<TokenType> GetEnumerator() =>
Types.Values.GetEnumerator();

[ExcludeFromCodeCoverage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddDomain(this IServiceCollection services)
{
services.AddSingleton<ITextCoordinateSystemComputer, TextCoordinateSystemComputer>();
services.AddSingleton(StructureInstance.Get);
services.AddSingleton<ITokenTypesProvider, TokenTypesProvider>();
services.AddSingleton<IStructure, Structure>();
services.AddSingleton<ILexer, RegexLexer>();
services.AddSingleton<IParser, TopDownParser>();

Expand Down
56 changes: 0 additions & 56 deletions src/Infrastructure/HydraScript.Infrastructure/StructureInstance.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public override IEnumerable<TokenType> 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()!;
Expand Down
2 changes: 1 addition & 1 deletion tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ITokenTypesProvider>();
provider.Setup(x => x.GetTokenTypes())
.Returns(tokenTypes);
var structure = new Structure(provider.Object);

var expectedText = string.Join('\n',
new List<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2ebd542

Please sign in to comment.