Skip to content

Commit

Permalink
#57 - убрал лишнее из TokenType
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Aug 3, 2024
1 parent f087277 commit 8a42fd7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Structure<TContainer>(ITokenTypesProvider provider) : IStructure
{
private Dictionary<string, TokenType> Types { get; } = provider.GetTokenTypes()
.Concat([new EndOfProgramType(), new ErrorType()])
.OrderBy(t => t.Priority)
.ToDictionary(x => x.Tag);

public Regex Regex { get; } = TContainer.GetRegex();
Expand All @@ -21,8 +20,7 @@ public TokenType FindByTag(string tag) =>

public override string ToString() =>
new StringBuilder()
.AppendJoin('\n',
Types.Select(x => $"{x.Key} {x.Value.Pattern}"))
.AppendJoin('\n', this)
.ToString();

public IEnumerator<TokenType> GetEnumerator() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;

internal record EndOfProgramType() : TokenType("EOP", "", int.MaxValue - 1)
internal record EndOfProgramType() : TokenType(EopTag)
{
public override bool EndOfProgram() => true;
public const string EopTag = "EOP";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;

internal record ErrorType() : TokenType("ERROR", @"\S+", int.MaxValue)
internal record ErrorType() : TokenType("ERROR")
{
public override bool Error() => true;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;

public record IgnorableType(string Tag, string Pattern, int Priority)
: TokenType(Tag, Pattern, Priority)
public record IgnorableType(string Tag) : TokenType(Tag)
{
public override bool CanIgnore() => true;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;

public record TokenType(string Tag, string Pattern, int Priority)
public record TokenType(string Tag)
{
public virtual bool CanIgnore() => false;

public virtual bool EndOfProgram() => false;

public virtual bool Error() => false;

public string GetNamedRegex() => $"(?<{Tag}>{Pattern})";

public override string ToString() => Tag;
public sealed override string ToString() => Tag;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Globalization;
using System.Text.RegularExpressions;
using HydraScript.Domain.FrontEnd.Lexer;
using HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes;
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Declarations;
Expand All @@ -26,7 +27,7 @@ public IAbstractSyntaxTree Parse(string text)
_tokens = _lexer.GetTokens(text);

var root = Script();
Expect("EOP");
Expect(EndOfProgramType.EopTag);
return new AbstractSyntaxTree(root);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ public override IEnumerable<TokenType> Read(
.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);
var tokenType = ignorable && canIgnore.GetBoolean()
? new IgnorableType(tag)
: new TokenType(tag);
return new PrioritizedTokenType(tokenType, priority);
})
.OrderBy(x => x.Priority);
.OrderBy(x => x.Priority)
.Select(x => x.TokenType);
return tokenTypes;
}

public override void Write(
Utf8JsonWriter writer,
IEnumerable<TokenType> value,
JsonSerializerOptions options) => throw new NotSupportedException();

private record PrioritizedTokenType(TokenType TokenType, int Priority);
}
}
23 changes: 11 additions & 12 deletions tests/HydraScript.Tests/Unit/FrontEnd/StructureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ public void ToStringCorrectTest()
{
var tokenTypes = new List<TokenType>
{
new ("MyToken", "[m|M][y|Y]", 2),
new ("OneToSeven", "[1-7]", 1)
new("MyToken"),
new("OneToSeven")
};
var provider = new Mock<ITokenTypesProvider>();
provider.Setup(x => x.GetTokenTypes())
.Returns(tokenTypes);
var structure = new Structure<GeneratedRegexContainer>(provider.Object);

var expectedText = string.Join('\n',
new List<string>
{
"OneToSeven [1-7]",
"MyToken [m|M][y|Y]",
"EOP ",
"ERROR \\S+"
}
);
Assert.Equal(expectedText,structure.ToString());
var expectedText = string.Join(
'\n',
[
"MyToken",
"OneToSeven",
"EOP",
"ERROR"
]);
Assert.Equal(expectedText, structure.ToString());
}
}

0 comments on commit 8a42fd7

Please sign in to comment.