Add translator to poyo
This commit is contained in:
@@ -4,28 +4,23 @@
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>default</LangVersion>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
<IncludeBuildOutput>false</IncludeBuildOutput>
|
||||
<IsRoslynComponent>true</IsRoslynComponent>
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
<IsRoslynComponent>true</IsRoslynComponent>
|
||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
|
||||
<PackageReference Include="PolySharp" Version="1.15.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Text.Json" Version="9.0.5" PrivateAssets="all" GeneratePathProperty="true" />
|
||||
</ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="dictionary.json" />
|
||||
<PackageReference Include="PolySharp" Version="1.15.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace PoyoLang.Translator.SourceGenerator;
|
||||
|
||||
@@ -15,12 +15,12 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
|
||||
// There will be only one of those but incremental generators work as pipelines
|
||||
var dictionaries = texts
|
||||
.Where(static text => text.Path.EndsWith("dictionary.json"))
|
||||
.Where(static text => text.Path.EndsWith("dictionary.txt"))
|
||||
.Select(static (text, _) => text.GetText());
|
||||
|
||||
var parsedDictionaries = dictionaries
|
||||
.Select(static (dictionary, _) =>
|
||||
JsonSerializer.Deserialize<Dictionary<string, string>>(dictionary!.ToString())
|
||||
ReadCustomDictionary(dictionary!)
|
||||
);
|
||||
|
||||
var formattedDictionaries = parsedDictionaries
|
||||
@@ -39,6 +39,28 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
});
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> ReadCustomDictionary(SourceText text)
|
||||
{
|
||||
var dictionary = new Dictionary<string, string>();
|
||||
|
||||
foreach (var line in text.ToString().Split('\n'))
|
||||
{
|
||||
var span = line.TrimEnd('\r').AsSpan();
|
||||
|
||||
// Reached end of file
|
||||
if (span.Length < 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var splitIndex = span.IndexOf('=');
|
||||
|
||||
dictionary[span[..splitIndex].ToString()] = span[(splitIndex + 1)..].ToString();
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
private static List<Node> BuildPrefixTree(Dictionary<string, string> dictionary)
|
||||
{
|
||||
var rootNodes = new List<Node>();
|
||||
@@ -93,6 +115,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
|
||||
namespace PoyoLang.Translator;
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -108,8 +131,9 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Next letter method definition
|
||||
source.Append(
|
||||
"""
|
||||
public void NextLetter(ref ReadOnlySpan<char> text, StringBuilder output)
|
||||
private void NextLetter(ref ReadOnlySpan<char> text, StringBuilder output)
|
||||
{
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -120,9 +144,10 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var isCaps = char.IsUpper(text[0]);
|
||||
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -131,11 +156,13 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Next letter method end
|
||||
source.Append(
|
||||
"""
|
||||
|
||||
// Punctuation/Unknown characters case
|
||||
text = text[1..];
|
||||
|
||||
output.Append(text[0]);
|
||||
|
||||
text = text[1..];
|
||||
}
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -158,6 +185,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
$$"""
|
||||
{{indent}}switch (text[{{depth}}])
|
||||
{{indent}}{
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -170,7 +199,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
source.Append(
|
||||
$$"""
|
||||
{{indent}} case '{{node.Letter}}' or '{{char.ToUpper(node.Letter)}}':
|
||||
{{indent}}
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -181,6 +211,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
$$"""
|
||||
{{indent}} if (text.Length > {{depth + 1}})
|
||||
{{indent}} {
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
@@ -190,6 +221,7 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
source.Append(
|
||||
$$"""
|
||||
{{indent}} }
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
@@ -203,6 +235,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
{{indent}} output.Append(isCaps ? "{{targetUpper}}" : "{{targetLower}}");
|
||||
{{indent}}
|
||||
{{indent}} return;
|
||||
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
@@ -210,8 +244,8 @@ public class PoyoLangTranslatorGenerator : IIncrementalGenerator
|
||||
// Switch-case end
|
||||
source.Append(
|
||||
$$"""
|
||||
|
||||
{{indent}}}
|
||||
|
||||
"""
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"PoyoLang.Translator.SourceGenerator": {
|
||||
"commandName": "DebugRoslynComponent",
|
||||
"targetProject": "../PoyoLang.Test/PoyoLang.Test.csproj"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user