From e92b02a2080faf7278372b9fd3698370f78bc8d2 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 9 Mar 2024 21:44:37 +0800 Subject: [PATCH] add: DelimiterSemanticToken and tests --- Canon.Core/Enums/SemanticEnums.cs | 2 +- Canon.Core/LexicalParser/SemanticToken.cs | 35 +++++++++++++------ Canon.Tests/Canon.Tests.csproj | 29 +++++++++++++++ Canon.Tests/GlobalUsings.cs | 1 + .../LexicalParserTests/DelimiterTests.cs | 27 ++++++++++++++ Canon.Tests/Utils.cs | 16 +++++++++ Canon.sln | 6 ++++ 7 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 Canon.Tests/Canon.Tests.csproj create mode 100644 Canon.Tests/GlobalUsings.cs create mode 100644 Canon.Tests/LexicalParserTests/DelimiterTests.cs create mode 100644 Canon.Tests/Utils.cs diff --git a/Canon.Core/Enums/SemanticEnums.cs b/Canon.Core/Enums/SemanticEnums.cs index 9b3669c..b4c0427 100644 --- a/Canon.Core/Enums/SemanticEnums.cs +++ b/Canon.Core/Enums/SemanticEnums.cs @@ -19,7 +19,7 @@ public enum DelimiterType LeftParenthesis, RightParenthesis, LeftSquareBracket, - RightBracket + RightSquareBracket } public enum KeywordType diff --git a/Canon.Core/LexicalParser/SemanticToken.cs b/Canon.Core/LexicalParser/SemanticToken.cs index ded67ec..ae49933 100644 --- a/Canon.Core/LexicalParser/SemanticToken.cs +++ b/Canon.Core/LexicalParser/SemanticToken.cs @@ -33,19 +33,32 @@ public class DelimiterSemanticToken : SemanticToken public static bool TryParse(uint linePos, uint characterPos, LinkedListNode now, out DelimiterSemanticToken? token) { - switch (now.Value) + Dictionary delimiterMap = new() { - case ',': - token = new DelimiterSemanticToken - { - LinePos = linePos, CharacterPos = characterPos, - LiteralValue = ",", DelimiterType = DelimiterType.Comma - }; - return true; - default: - token = null; - return false; + { ',', DelimiterType.Comma }, + { '.', DelimiterType.Period }, + { ':', DelimiterType.Colon }, + { ';', DelimiterType.Semicolon }, + { '(', DelimiterType.LeftParenthesis }, + { ')', DelimiterType.RightParenthesis }, + { '[', DelimiterType.LeftSquareBracket }, + { ']', DelimiterType.RightSquareBracket } + }; + + if (!delimiterMap.TryGetValue(now.Value, out DelimiterType value)) + { + token = null; + return false; } + + token = new DelimiterSemanticToken + { + LinePos = linePos, + CharacterPos = characterPos, + LiteralValue = new string([now.Value]), + DelimiterType = value + }; + return true; } } diff --git a/Canon.Tests/Canon.Tests.csproj b/Canon.Tests/Canon.Tests.csproj new file mode 100644 index 0000000..bd9c5c3 --- /dev/null +++ b/Canon.Tests/Canon.Tests.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Canon.Tests/GlobalUsings.cs b/Canon.Tests/GlobalUsings.cs new file mode 100644 index 0000000..c802f44 --- /dev/null +++ b/Canon.Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; diff --git a/Canon.Tests/LexicalParserTests/DelimiterTests.cs b/Canon.Tests/LexicalParserTests/DelimiterTests.cs new file mode 100644 index 0000000..4c7e5a9 --- /dev/null +++ b/Canon.Tests/LexicalParserTests/DelimiterTests.cs @@ -0,0 +1,27 @@ +using Canon.Core.Enums; +using Canon.Core.LexicalParser; + +namespace Canon.Tests.LexicalParserTests; + + +public class DelimiterTests +{ + [Theory] + [InlineData(",123", DelimiterType.Comma)] + [InlineData(".123", DelimiterType.Period)] + [InlineData(":123", DelimiterType.Colon)] + [InlineData(";123", DelimiterType.Semicolon)] + [InlineData("(123)", DelimiterType.LeftParenthesis)] + [InlineData(").", DelimiterType.RightParenthesis)] + [InlineData("[asd", DelimiterType.LeftSquareBracket)] + [InlineData("]asd", DelimiterType.RightSquareBracket)] + public void SmokeTest(string input, DelimiterType type) + { + LinkedList content = Utils.GetLinkedList(input); + + Assert.True(DelimiterSemanticToken.TryParse(0, 0, content.First!, + out DelimiterSemanticToken? token)); + Assert.NotNull(token); + Assert.Equal(type, token.DelimiterType); + } +} diff --git a/Canon.Tests/Utils.cs b/Canon.Tests/Utils.cs new file mode 100644 index 0000000..9babb68 --- /dev/null +++ b/Canon.Tests/Utils.cs @@ -0,0 +1,16 @@ +namespace Canon.Tests; + +public static class Utils +{ + public static LinkedList GetLinkedList(string content) + { + LinkedList list = []; + + foreach(char c in content) + { + list.AddLast(c); + } + + return list; + } +} diff --git a/Canon.sln b/Canon.sln index 258ed3b..5225506 100644 --- a/Canon.sln +++ b/Canon.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{CA16 scripts\integration_test.py = scripts\integration_test.py EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Canon.Tests", "Canon.Tests\Canon.Tests.csproj", "{E5F2B97B-3766-466D-9309-BA361F0CE15E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.ActiveCfg = Release|Any CPU {3D1C0BA2-57F2-41B2-B024-7A0E54A91DA0}.Release|Any CPU.Build.0 = Release|Any CPU + {E5F2B97B-3766-466D-9309-BA361F0CE15E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5F2B97B-3766-466D-9309-BA361F0CE15E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5F2B97B-3766-466D-9309-BA361F0CE15E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E5F2B97B-3766-466D-9309-BA361F0CE15E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal