Skip to content

Commit 3c04b84

Browse files
authored
Упрощение создания экземпляра лексической структуры (#17)
* удаление лишних пакетов * миграция * new test * version
1 parent 147cc62 commit 3c04b84

File tree

23 files changed

+147
-205
lines changed

23 files changed

+147
-205
lines changed
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics.CodeAnalysis;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
45
using Interpreter.Lib.BackEnd.Values;
5-
using Newtonsoft.Json;
6-
using Newtonsoft.Json.Serialization;
76

87
namespace Interpreter.Lib.BackEnd.Instructions
98
{
@@ -17,18 +16,15 @@ public AsString(string left, IValue right, int number) :
1716
public override int Execute(VirtualMachine vm)
1817
{
1918
var frame = vm.Frames.Peek();
20-
frame[Left] = JsonConvert.SerializeObject(
19+
frame[Left] = JsonSerializer.Serialize(
2120
right.right.Get(frame),
22-
new JsonSerializerSettings
21+
new JsonSerializerOptions
2322
{
24-
Formatting = Formatting.Indented,
25-
FloatFormatHandling = FloatFormatHandling.Symbol,
26-
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
27-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
28-
Converters = new List<JsonConverter>
29-
{
30-
new DoubleValueConverter()
31-
}
23+
WriteIndented = true,
24+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
25+
ReferenceHandler = ReferenceHandler.IgnoreCycles,
26+
Converters = { new DoubleValueWriteConverter() },
27+
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals
3228
}
3329
);
3430

@@ -38,20 +34,21 @@ public override int Execute(VirtualMachine vm)
3834
protected override string ToStringRepresentation() => $"{Left} = {right.right} as string";
3935

4036
[ExcludeFromCodeCoverage]
41-
private class DoubleValueConverter : JsonConverter<double>
37+
private class DoubleValueWriteConverter : JsonConverter<double>
4238
{
43-
public override bool CanRead => false;
39+
public override double Read(ref Utf8JsonReader reader,
40+
Type typeToConvert, JsonSerializerOptions options) =>
41+
throw new NotImplementedException();
4442

45-
public override void WriteJson(JsonWriter writer, double value, JsonSerializer serializer) =>
43+
public override void Write(Utf8JsonWriter writer,
44+
double value, JsonSerializerOptions options)
45+
{
4646
// ReSharper disable once CompareOfFloatsByEqualityOperator
47-
writer.WriteRawValue(value == Math.Truncate(value)
48-
? JsonConvert.ToString(Convert.ToInt64(value))
49-
: JsonConvert.ToString(value));
50-
51-
public override double ReadJson(JsonReader reader, Type objectType,
52-
double existingValue, bool hasExistingValue,
53-
JsonSerializer serializer) =>
54-
throw new NotImplementedException("CanRead is false, so reading is unnecessary");
47+
if (value == Math.Truncate(value))
48+
writer.WriteNumberValue(Convert.ToInt64(value));
49+
else
50+
writer.WriteNumberValue(value);
51+
}
5552
}
5653
}
5754
}

Interpreter.Lib/FrontEnd/GetTokens/Data/Token.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override string ToString()
2020
{
2121
var displayValue = Value;
2222
if (displayValue != null) displayValue = Regex.Replace(Value, "\"", "\\\"");
23-
if (Type.WhiteSpace()) displayValue = "";
23+
if (Type.CanIgnore()) displayValue = "";
2424
return $"{Type} {Segment}: {displayValue}";
2525
}
2626
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Interpreter.Lib.FrontEnd.GetTokens.Data.TokenTypes
2+
{
3+
public record IgnorableType(string Tag = null, string Pattern = null, int Priority = 0)
4+
: TokenType(Tag, Pattern, Priority)
5+
{
6+
public override bool CanIgnore() => true;
7+
}
8+
}

Interpreter.Lib/FrontEnd/GetTokens/Data/TokenTypes/TokenType.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
{
33
public record TokenType(string Tag, string Pattern, int Priority)
44
{
5-
public TokenType() : this(null, null, 0)
6-
{
7-
}
8-
9-
public virtual bool WhiteSpace() => false;
5+
public virtual bool CanIgnore() => false;
106

117
public virtual bool EndOfProgram() => false;
128

Interpreter.Lib/FrontEnd/GetTokens/Data/TokenTypes/WhiteSpaceType.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

Interpreter.Lib/FrontEnd/GetTokens/Impl/Lexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public List<Token> GetTokens(string text)
3333
_lines.Add(match.Groups["NEWLINE"].Index);
3434
}
3535

36-
return this.Where(t => !t.Type.WhiteSpace()).ToList();
36+
return this.Where(t => !t.Type.CanIgnore()).ToList();
3737
}
3838

3939
public IEnumerator<Token> GetEnumerator()

Interpreter.Lib/Interpreter.Lib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
98
<PackageReference Include="Visitor.NET" Version="1.0.0" />
109
</ItemGroup>
1110

Interpreter.Tests/Stubs/MapperStub.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

Interpreter.Tests/Unit/FrontEnd/LexerTests.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Linq;
22
using Interpreter.Lib.FrontEnd.GetTokens;
3-
using Interpreter.Lib.FrontEnd.GetTokens.Data;
43
using Interpreter.Lib.FrontEnd.GetTokens.Impl;
5-
using Interpreter.Models;
6-
using Interpreter.Tests.Stubs;
4+
using Interpreter.Services.Providers.Impl.StructureProvider;
75
using Interpreter.Tests.TestData;
86
using Xunit;
97

@@ -15,8 +13,7 @@ public class LexerTests
1513

1614
public LexerTests()
1715
{
18-
var mapper = new MapperStub();
19-
_lexer = new Lexer(mapper.Map<StructureModel, Structure>(new()));
16+
_lexer = new Lexer(new StructureProvider().CreateStructure());
2017
}
2118

2219
[Theory]
@@ -41,5 +38,15 @@ public void LexerToStringCorrectTest()
4138
[Fact]
4239
public void EmptyTextTest() =>
4340
Assert.NotEmpty(_lexer.GetTokens(""));
41+
42+
[Fact]
43+
public void GetTokensSkipIgnorableTypesTest()
44+
{
45+
const string text = @"
46+
let x = 1 // int
47+
";
48+
var tokens = _lexer.GetTokens(text);
49+
Assert.DoesNotContain(_lexer.Structure.FindByTag("Comment"), tokens.Select(x => x.Type));
50+
}
4451
}
4552
}

Interpreter.Tests/Unit/FrontEnd/ParserTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using Interpreter.Lib.FrontEnd.GetTokens.Data;
21
using Interpreter.Lib.FrontEnd.GetTokens.Impl;
32
using Interpreter.Lib.FrontEnd.TopDownParse;
43
using Interpreter.Lib.FrontEnd.TopDownParse.Impl;
5-
using Interpreter.Models;
6-
using Interpreter.Tests.Stubs;
4+
using Interpreter.Services.Providers.Impl.StructureProvider;
75
using Interpreter.Tests.TestData;
86
using Xunit;
97

@@ -15,10 +13,9 @@ public class ParserTests
1513

1614
public ParserTests()
1715
{
18-
var mapper = new MapperStub();
19-
2016
_parser = new Parser(new Lexer(
21-
mapper.Map<StructureModel, Structure>(new())
17+
new StructureProvider()
18+
.CreateStructure()
2219
));
2320
}
2421

Interpreter.Tests/Unit/Infrastructure/ProvidersTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
23
using Interpreter.Lib.FrontEnd.GetTokens;
4+
using Interpreter.Lib.FrontEnd.GetTokens.Data;
5+
using Interpreter.Lib.FrontEnd.GetTokens.Data.TokenTypes;
36
using Interpreter.Lib.FrontEnd.GetTokens.Impl;
47
using Interpreter.Lib.FrontEnd.TopDownParse.Impl;
58
using Interpreter.Services.Providers;
69
using Interpreter.Services.Providers.Impl.LexerProvider;
710
using Interpreter.Services.Providers.Impl.ParserProvider;
8-
using Interpreter.Tests.Stubs;
911
using Microsoft.Extensions.Options;
1012
using Moq;
1113
using Xunit;
@@ -19,6 +21,10 @@ public class ProvidersTests
1921
[InlineData(typeof(LoggingLexer), true)]
2022
public void CertainLexerProvidedTest(Type lexerType, bool dump)
2123
{
24+
var structureProvider = new Mock<IStructureProvider>();
25+
structureProvider.Setup(x => x.CreateStructure())
26+
.Returns(new Structure(new List<TokenType>()));
27+
2228
var options = new Mock<IOptions<CommandLineSettings>>();
2329
options.Setup(x => x.Value)
2430
.Returns(new CommandLineSettings
@@ -27,7 +33,7 @@ public void CertainLexerProvidedTest(Type lexerType, bool dump)
2733
InputFilePath = "file.js"
2834
});
2935

30-
var lexerProvider = new LexerProvider(new MapperStub(), options.Object);
36+
var lexerProvider = new LexerProvider(structureProvider.Object, options.Object);
3137
var lexer = lexerProvider.CreateLexer();
3238

3339
Assert.IsType(lexerType, lexer);

Interpreter/CommandLineSettings.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.IO;
44
using CommandLine;
55
using CommandLine.Text;
6-
using Interpreter.Models;
76

87
namespace Interpreter
98
{
@@ -31,9 +30,8 @@ public static IEnumerable<Example> Examples
3130
}
3231
}
3332

34-
public string GetInputFileName() => InputFilePath.Split(".js")[0];
35-
36-
public StructureModel StructureModel { get; } = new();
33+
public string GetInputFileName() =>
34+
InputFilePath.Split(".js")[0];
3735

3836
public virtual string GetText() =>
3937
File.ReadAllText(InputFilePath);

Interpreter/Interpreter.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
6-
<Version>1.1.3</Version>
6+
<Version>1.1.4</Version>
77
</PropertyGroup>
88

99
<ItemGroup>
1010
<ProjectReference Include="..\Interpreter.Lib\Interpreter.Lib.csproj" />
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="AutoMapper" Version="11.0.1" />
15-
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
1614
<PackageReference Include="CommandLineParser" Version="2.9.1" />
1715
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
1817
<PackageReference Include="System.IO.Abstractions" Version="17.2.1" />
1918
</ItemGroup>
2019

Interpreter/MappingProfiles/StructureProfile.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

Interpreter/MappingProfiles/TokenTypeProfile.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

Interpreter/Models/StructureModel.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

Interpreter/Models/TokenTypeModel.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Interpreter/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using System.Diagnostics.CodeAnalysis;
33
using CommandLine;
44
using Microsoft.Extensions.DependencyInjection;
5-
using Interpreter.MappingProfiles;
65
using Interpreter.Services.Executor;
76
using Interpreter.Services.Executor.Impl;
87
using Interpreter.Services.Parsing;
98
using Interpreter.Services.Parsing.Impl;
109
using Interpreter.Services.Providers;
1110
using Interpreter.Services.Providers.Impl.LexerProvider;
1211
using Interpreter.Services.Providers.Impl.ParserProvider;
12+
using Interpreter.Services.Providers.Impl.StructureProvider;
1313
using Microsoft.Extensions.Options;
1414

1515
namespace Interpreter
@@ -34,12 +34,11 @@ private static void Main(string[] args) =>
3434

3535
private static void ConfigureServices(CommandLineSettings settings)
3636
{
37+
ServiceCollection.AddSingleton<IStructureProvider, StructureProvider>();
3738
ServiceCollection.AddSingleton<ILexerProvider, LexerProvider>();
3839
ServiceCollection.AddSingleton<IParserProvider, ParserProvider>();
3940
ServiceCollection.AddSingleton<IParsingService, ParsingService>();
4041

41-
ServiceCollection.AddAutoMapper(typeof(TokenTypeProfile), typeof(StructureProfile));
42-
4342
ServiceCollection.AddSingleton<IExecutor, Executor>();
4443

4544
ServiceCollection.AddSingleton(_ => Options.Create(settings));

0 commit comments

Comments
 (0)