Skip to content

Commit 42d6604

Browse files
committed
#170 - Structure is read-only list
1 parent 1022349 commit 42d6604

File tree

8 files changed

+21
-10
lines changed

8 files changed

+21
-10
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageVersion Include="System.IO.Abstractions" Version="22.0.16" />
1515
<PackageVersion Include="Visitor.NET" Version="4.2.0" />
1616
<PackageVersion Include="Visitor.NET.AutoVisitableGen" Version="1.5.2" />
17+
<PackageVersion Include="ZLinq" Version="1.5.2" />
1718
<PackageVersion Include="ZLogger" Version="2.5.10" />
1819
<PackageVersion Include="ZString" Version="2.6.0" />
1920
</ItemGroup>

src/Domain/HydraScript.Domain.FrontEnd/HydraScript.Domain.FrontEnd.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ItemGroup>
44
<PackageReference Include="Visitor.NET" />
55
<PackageReference Include="Visitor.NET.AutoVisitableGen" PrivateAssets="all" />
6+
<PackageReference Include="ZLinq" />
67
<PackageReference Include="ZString" />
78
</ItemGroup>
89

src/Domain/HydraScript.Domain.FrontEnd/Lexer/IStructure.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace HydraScript.Domain.FrontEnd.Lexer;
55

6-
public interface IStructure : IEnumerable<TokenType>
6+
public interface IStructure : IReadOnlyList<TokenType>
77
{
88
public Regex Regex { get; }
99

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using System.Collections.Frozen;
12
using HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
23

34
namespace HydraScript.Domain.FrontEnd.Lexer;
45

56
public interface ITokenTypesProvider
67
{
7-
IEnumerable<TokenType> GetTokenTypes();
8+
FrozenDictionary<string, TokenType> GetTokenTypes();
89
}

src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/RegexLexer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public IEnumerator<Token> GetEnumerator()
2424
{
2525
foreach (Match match in Structure.Regex.Matches(_text))
2626
{
27-
foreach (var type in Structure)
27+
for (var i = 0; i < Structure.Count; i++)
2828
{
29+
var type = Structure[i];
2930
var group = match.Groups[type.Tag];
3031

3132
if (!group.Success || type.CanIgnore()) continue;

src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/Structure.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ namespace HydraScript.Domain.FrontEnd.Lexer.Impl;
1010
public class Structure<TContainer>(ITokenTypesProvider provider) : IStructure
1111
where TContainer : IGeneratedRegexContainer
1212
{
13-
private FrozenDictionary<string, TokenType> Types { get; } = provider.GetTokenTypes()
14-
.Concat([new EndOfProgramType(), new ErrorType()])
15-
.ToFrozenDictionary(x => x.Tag);
13+
private FrozenDictionary<string, TokenType> Types { get; } = provider.GetTokenTypes();
1614

1715
public Regex Regex { get; } = TContainer.Regex;
1816

@@ -27,4 +25,8 @@ public IEnumerator<TokenType> GetEnumerator() =>
2725

2826
[ExcludeFromCodeCoverage]
2927
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
28+
29+
public int Count => Types.Values.Length;
30+
31+
public TokenType this[int index] => Types.Values[index];
3032
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
using System.Collections.Frozen;
12
using HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
3+
using ZLinq;
24

35
namespace HydraScript.Domain.FrontEnd.Lexer.Impl;
46

57
public class TokenTypesProvider : ITokenTypesProvider
68
{
7-
public IEnumerable<TokenType> GetTokenTypes() =>
8-
Constants.TokenTypes.Stream
9+
public FrozenDictionary<string, TokenType> GetTokenTypes() =>
10+
Constants.TokenTypes.Stream.AsValueEnumerable()
911
.OrderBy(x => x.Priority)
1012
.Select(x => x.CanIgnore
1113
? new IgnorableType(x.Tag)
12-
: new TokenType(x.Tag));
14+
: new TokenType(x.Tag))
15+
.Concat([new EndOfProgramType(), new ErrorType()])
16+
.ToFrozenDictionary(x => x.Tag);
1317
}

tests/HydraScript.UnitTests/Domain/FrontEnd/StructureTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Frozen;
12
using HydraScript.Domain.FrontEnd.Lexer;
23
using HydraScript.Domain.FrontEnd.Lexer.Impl;
34
using HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
@@ -14,7 +15,7 @@ public void ToStringCorrectTest()
1415
{
1516
new("MyToken"),
1617
new("OneToSeven")
17-
};
18+
}.ToFrozenDictionary(x => x.Tag);
1819
var provider = Substitute.For<ITokenTypesProvider>();
1920
provider.GetTokenTypes().Returns(tokenTypes);
2021
var structure = new Structure<GeneratedRegexContainer>(provider);

0 commit comments

Comments
 (0)