Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CSharpSourceBuilder.TestApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// See https://aka.ms/new-console-template for more information

using RhoMicro.CodeAnalysis;
using RhoMicro.CodeAnalysis.Lyra;

using var builder = new CSharpSourceBuilder();

Expand Down
5 changes: 3 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>All</AnalysisMode>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SolutionName>RhoMicro.CodeAnalysis</SolutionName>
<RootNamespace>$(SolutionName)</RootNamespace>
<AssemblyName>$(SolutionName).$(MSBuildProjectName)</AssemblyName>
</PropertyGroup>
Expand All @@ -17,7 +18,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MPL-2.0</PackageLicenseExpression>
<Authors>Paul Braetz</Authors>
<Copyright>2024</Copyright>
<Copyright>$([System.DateTime]::Now.ToString('yyyy'))</Copyright>
<Company>RhoMicro</Company>
<PackageId>$(SolutionName).$(MSBuildProjectName)</PackageId>
<RepositoryUrl>https://github.com/SleepWellPupper/RhoMicro.CodeAnalysis/tree/master/$(MSBuildProjectName)</RepositoryUrl>
Expand Down
5 changes: 3 additions & 2 deletions DslGenerator.TestApp/DslGenerator.TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

<ItemGroup>
<!--<ProjectReference Include="..\DslGenerator\DslGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />-->
<ProjectReference Include="..\UnionsGenerator\UnionsGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Janus.Analyzers\Janus.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Janus.CodeFixes\Janus.CodeFixes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="/" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project>
</Project>
108 changes: 56 additions & 52 deletions DslGenerator/DslGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
<Project Sdk="Microsoft.NET.Sdk">
<!--AutoUpdateAssemblyName-->

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IncludeBuildOutput>false</IncludeBuildOutput>
<developmentDependency>true</developmentDependency>
<NoPackageAnalysis>true</NoPackageAnalysis>
<IsRoslynComponent>true</IsRoslynComponent>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="RhoMicro.CodeAnalysis.DslGenerator.Tests"/>
</ItemGroup>

<PropertyGroup>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Generates utilities for lexing and parsing domain specific languages, little languages etc.</Description>
<PackageTags>Source Generator</PackageTags>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);DSL_GENERATOR</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Update="NETStandard.Library" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="15.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<ProjectReference Include="..\UnionsGenerator\UnionsGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>

<ItemGroup>
<None Remove="bin\Debug\netstandard2.0\\RhoMicro.CodeAnalysis.DslGenerator.13.0.0.568.dll"/>
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
<None Include="README.md" Pack="true" PackagePath="/" CopyToOutputDirectory="Always"/>
</ItemGroup>
<!--AutoUpdateAssemblyName-->

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IncludeBuildOutput>false</IncludeBuildOutput>
<developmentDependency>true</developmentDependency>
<NoPackageAnalysis>true</NoPackageAnalysis>
<IsRoslynComponent>true</IsRoslynComponent>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="RhoMicro.CodeAnalysis.DslGenerator.Tests"/>
</ItemGroup>

<PropertyGroup>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Generates utilities for lexing and parsing domain specific languages, little languages etc.</Description>
<PackageTags>Source Generator</PackageTags>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);DSL_GENERATOR</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Update="NETStandard.Library" PrivateAssets="all"/>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="15.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<ProjectReference Include="..\Janus.Analyzers\Janus.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\Janus.CodeFixes\Janus.CodeFixes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\Janus.Library\Janus.Library.csproj"/>
</ItemGroup>

<ItemGroup>
<None Remove="bin\Debug\netstandard2.0\\RhoMicro.CodeAnalysis.DslGenerator.13.0.0.568.dll"/>
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
<None Include="README.md" Pack="true" PackagePath="/" CopyToOutputDirectory="Always"/>
</ItemGroup>

</Project>
49 changes: 25 additions & 24 deletions DslGenerator/Lexing/Lexeme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,37 @@ namespace RhoMicro.CodeAnalysis.DslGenerator.Lexing;
[IncludeFile]
#endif
[UnionType<String, Char, StringSlice>]
[UnionTypeSettings(
ToStringSetting = ToStringSetting.Simple,
Miscellaneous = MiscellaneousSettings.Default | MiscellaneousSettings.EmitGeneratedSourceCode)]
[UnionTypeSettings(ToStringSetting = ToStringSetting.Simple)]
internal readonly partial struct Lexeme : IEquatable<String>, IEquatable<Char>, IEquatable<StringSlice>
{
public Int32 Length => Match(
s => s.Length,
s => 1,
s => s.Length);
public Int32 Length => Switch(
onString: s => s.Length,
onChar: s => 1,
onStringSlice: s => s.Length);

public static Lexeme Empty { get; } = String.Empty;
public Boolean Equals(Lexeme other) =>
Match(other.Equals, other.Equals, other.Equals);
public override Int32 GetHashCode() =>
Match(v => v.GetHashCode(), v => v.GetHashCode(), v => v.GetHashCode());

public Boolean Equals(Lexeme other) => Switch(other.Equals, other.Equals, other.Equals);

public Boolean Equals(Char c) =>
Match(
s => s.Length == 1 && s[0] == c,
thisChar => thisChar == c,
s => s.Equals(c));
Switch(
onString: s => s.Length == 1 && s[0] == c,
onChar: thisChar => thisChar == c,
onStringSlice: s => s.Equals(c));

public Boolean Equals(String s) =>
Match(
thisString => thisString == s,
c => s.Length == 1 && s[0] == c,
slice => slice.Equals(s));
Switch(
onString: thisString => thisString == s,
onChar: c => s.Length == 1 && s[0] == c,
onStringSlice: slice => slice.Equals(s));

public Boolean Equals(StringSlice s) =>
Match(s.Equals, s.Equals, s.Equals);
Switch(s.Equals, s.Equals, s.Equals);

public String ToEscapedString() =>
ToString()?
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t")
?? String.Empty;
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t")
?? String.Empty;
}
12 changes: 7 additions & 5 deletions DslGenerator/Lexing/SourceText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace RhoMicro.CodeAnalysis.DslGenerator.Lexing;
internal readonly partial struct SourceText : IDisposable
{
public String ToString(CancellationToken cancellationToken) =>
Match(
s => s,
s =>
Switch(
onString: s => s,
onStream: s =>
{
cancellationToken.ThrowIfCancellationRequested();
var reader = new StreamReader(s);
var resultBuilder = new StringBuilder();
var line = reader.ReadLine();
while(line != null)
while (line != null)
{
cancellationToken.ThrowIfCancellationRequested();
_ = resultBuilder.AppendLine(line);
Expand All @@ -31,10 +31,12 @@ public String ToString(CancellationToken cancellationToken) =>

return result;
});

public static SourceText Empty { get; } = String.Empty;

public void Dispose()
{
if(TryAsStream(out var s))
if (TryCastToStream(out var s))
s.Dispose();
}
}
Loading
Loading