Skip to content
Closed
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
Empty file removed Sources/Benchmarks/.gitignore
Empty file.
Empty file removed Sources/Examples/.gitignore
Empty file.
2 changes: 1 addition & 1 deletion Sources/Props/Package.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<None Include="$(SolutionDir)\..\.github\resources\YoakkeLogo.png" Visible="False">
<None Include="..\..\..\..\.github\resources\YoakkeLogo.png" Visible="False">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
Expand Down
8 changes: 4 additions & 4 deletions Sources/Props/SourceGenerator.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" />

<!-- Source-generator abstractions -->
<Compile Include="$(SolutionDir)/Shared/Libraries/SourceGenerator.Common/**/*.cs" Link="SourceGenerator.Common" Visible="False" />
<Compile Remove="$(SolutionDir)/Shared/Libraries/SourceGenerator.Common/obj/**/*.cs" />
<Compile Include="..\..\../Shared/Libraries/SourceGenerator.Common/**/*.cs" Link="SourceGenerator.Common" Visible="False" />
<Compile Remove="..\..\../Shared/Libraries/SourceGenerator.Common/obj/**/*.cs" />

<!-- Run-time polyfill libraries -->
<Compile Include="$(SolutionDir)/Shared/Libraries/Polyfill/**/*.cs" Link="Polyfill" Visible="False" />
<Compile Remove="$(SolutionDir)/Shared/Libraries/Polyfill/obj/**/*.cs" />
<Compile Include="..\..\../Shared/Libraries/Polyfill/**/*.cs" Link="Polyfill" Visible="False" />
<Compile Remove="..\..\../Shared/Libraries/Polyfill/obj/**/*.cs" />

<PackageReference Include="System.Memory" Version="4.5.4" PrivateAssets="all" GeneratePathProperty="true" />
<None Include="$(PkgSystem_Memory)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/Libraries/Collections/Collections.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
</ItemGroup>
</Project>
13 changes: 11 additions & 2 deletions Sources/Shared/Libraries/Collections/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;

namespace Yoakke.Collections;
Expand Down Expand Up @@ -57,13 +58,21 @@ public int Capacity
/// </summary>
public int Tail => (this.Head + this.Count) % this.Capacity;

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowOutOfRange()
{
throw new ArgumentOutOfRangeException("frog");
}

/// <inheritdoc/>
public T this[int index]
{
get
{
if (index < 0 || index >= this.Count) throw new ArgumentOutOfRangeException(nameof(index));
return this.storage[(this.Head + index) % this.Capacity];
if ((uint)index >= this.Count) ThrowOutOfRange();
var nd = Head + index;
if (nd >= this.Capacity) nd -= this.Capacity;
return this.storage[nd];
}

set
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/Libraries/Streams/Streams.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Collections\Collections.csproj" />
<ProjectReference Include="..\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\Collections\Collections.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Collections\Collections.csproj" />
<ProjectReference Include="..\..\Libraries\Collections\Collections.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Sources/Shared/Tests/Streams.Tests/Streams.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Streams\Streams.csproj" />
<ProjectReference Include="..\..\Libraries\Streams\Streams.csproj" />
</ItemGroup>
</Project>
Empty file.
71 changes: 71 additions & 0 deletions Sources/SynKit/Benchmarks/Parser.Benchmarks/Grammatics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using Yoakke.SynKit.Lexer;
using Yoakke.SynKit.Lexer.Attributes;
using Yoakke.SynKit.Parser.Attributes;

namespace Yoakke.SynKit.Parser.Benchmarks;

public enum TokenType
{
[Error] Error,
[End] End,
[Ignore] [Regex(Regexes.Whitespace)] Whitespace,

[Token("(")] OpenParen,
[Token(")")] CloseParen,

[Token("+")] Add,
[Token("-")] Sub,
[Token("*")] Mul,
[Token("/")] Div,
[Token("%")] Mod,
[Token("^")] Exp,

[Token(";")] Semicol,

[Regex(Regexes.IntLiteral)] IntLit,
}

[Lexer(typeof(TokenType))]
public partial class Lexer
{
public List<Token<TokenType>> LexAll()
{
var list = new List<Token<TokenType>>();
while (true)
{
var token = Next();
list.Add(token);
if (token.Kind == TokenType.End) break;
}
return list;
}
}

[Parser(typeof(TokenType))]
public partial class Parser
{
[Rule("program: expression ';'")]
public static int Program(int n, IToken _) => n;

[Right("^")]
[Left("*", "/", "%")]
[Left("+", "-")]
[Rule("expression")]
public static int BinOp(int a, IToken op, int b) => op.Text switch
{
"^" => (int)Math.Pow(a, b),
"*" => a * b,
"/" => a / b,
"%" => a % b,
"+" => a + b,
"-" => a - b,
_ => throw new NotImplementedException(),
};

[Rule("expression : '(' expression ')'")]
public static int Grouping(IToken _1, int n, IToken _2) => n;

[Rule("expression : IntLit")]
public static int IntLit(IToken token) => int.Parse(token.Text);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<!--
<PackageReference Include="Yoakke.Synkit.Lexer" Version="2022.6.14-8.42.51-nightly" />
<PackageReference Include="Yoakke.Synkit.Lexer.Generator" Version="2022.6.14-8.42.51-nightly" />
<PackageReference Include="Yoakke.Synkit.Parser" Version="2022.6.14-8.42.51-nightly" />
<PackageReference Include="Yoakke.Synkit.Parser.Generator" Version="2022.6.14-8.42.51-nightly" />
-->
<ProjectReference Include="..\..\Libraries\Parser\Parser.csproj" />
<ProjectReference Include="..\..\Libraries\Lexer.Generator\Lexer.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="False" />
<ProjectReference Include="..\..\Libraries\Parser.Generator\Parser.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="False" />
</ItemGroup>
</Project>
47 changes: 47 additions & 0 deletions Sources/SynKit/Benchmarks/Parser.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// See https://aka.ms/new-console-template for more information

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Yoakke.SynKit.Lexer;
using Yoakke.SynKit.Parser;
using Yoakke.SynKit.Parser.Benchmarks;

var a = new SimpleBench();
for (int i = 0; i < 10_000_000; i++)
a.Parse();
// BenchmarkRunner.Run<SimpleBench>();


// UNCOMMENT FOR DEBUG
/*
var result = new SimpleBench().Parse();
if (result.IsOk)
{
Console.WriteLine($" => {result.Ok.Value}");
}
else
{
var err = result.Error;
foreach (var element in err.Elements.Values)
{
Console.WriteLine($" expected {string.Join(" or ", element.Expected)} while parsing {element.Context}");
}
Console.WriteLine($" but got {(err.Got == null ? "end of input" : ((IToken<TokenType>)err.Got).Text)}");
}*/

public class SimpleBench
{
private static string source = "1+1/2+6^8-1-15*2;";

[Benchmark]
public ParseResult<int> Parse()
{
return new Parser(new Lexer(source)).ParseProgram();
}

[Benchmark]
public List<Token<TokenType>> Lex()
{
return new Lexer(source).LexAll();
}
}
Binary file added Sources/SynKit/Benchmarks/Parser.Benchmarks/key.snk
Binary file not shown.
4 changes: 2 additions & 2 deletions Sources/SynKit/Libraries/Automata/Automata.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Collections\Collections.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Collections\Collections.csproj" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Sources/SynKit/Libraries/C.Syntax/C.Syntax.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="$(SolutionDir)\SynKit\Libraries\Lexer\Lexer.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\Lexer\Lexer.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<Import Project="..\..\..\Props\SourceGenerator.Build.props" />

<ItemGroup>
<Compile Include="$(SolutionDir)/Shared\Libraries/Collections/**/*.cs" Link="Collections" Visible="False" />
<Compile Remove="$(SolutionDir)/Shared\Libraries/Collections/obj/**/*.cs" />
<Compile Include="..\..\../Shared\Libraries/Collections/**/*.cs" Link="Collections" Visible="False" />
<Compile Remove="..\..\../Shared\Libraries/Collections/obj/**/*.cs" />

<Compile Include="$(SolutionDir)/SynKit\Libraries/Automata/**/*.cs" Link="Automata" Visible="False" />
<Compile Remove="$(SolutionDir)/SynKit\Libraries/Automata/obj/**/*.cs" />
<Compile Include="../Automata/**/*.cs" Link="Automata" Visible="False" />
<Compile Remove="../Automata/obj/**/*.cs" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions Sources/SynKit/Libraries/Lexer/Lexer.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\SynKit\Libraries\Text\Text.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Collections\Collections.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Streams\Streams.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\Text\Text.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Collections\Collections.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Streams\Streams.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Polyfill\Polyfill.csproj" />
</ItemGroup>
</Project>
7 changes: 5 additions & 2 deletions Sources/SynKit/Libraries/Parser/ParseError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace Yoakke.SynKit.Parser;
/// </summary>
public class ParseError
{
private static readonly Dictionary<string, ParseErrorElement> plug = new();

/// <summary>
/// The error cases in different parse contexts.
/// </summary>
public IReadOnlyDictionary<string, ParseErrorElement> Elements { get; }
public IReadOnlyDictionary<string, ParseErrorElement> Elements { get; } = plug;

/// <summary>
/// The item that was found, if any.
Expand All @@ -37,7 +39,7 @@ public class ParseError
/// <param name="position">The position where the error occurred.</param>
/// <param name="context">The context in which the error occurred.</param>
public ParseError(object expected, object? got, IComparable position, string context)
: this(new Dictionary<string, ParseErrorElement> { { context, new ParseErrorElement(expected, context) } }, got, position)
: this(plug, got, position)
{
}

Expand All @@ -56,6 +58,7 @@ private ParseError(IReadOnlyDictionary<string, ParseErrorElement> elements, obje
/// <returns>The error that represents both of them properly.</returns>
public static ParseError? operator |(ParseError? first, ParseError? second)
{
return first;
// Check nullities
if (first is null && second is null) return null;
if (first is null) return second!;
Expand Down
22 changes: 14 additions & 8 deletions Sources/SynKit/Libraries/Parser/ParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@ namespace Yoakke.SynKit.Parser;
/// <typeparam name="T">The parsed value type.</typeparam>
public readonly struct ParseResult<T>
{
private readonly object value;
private readonly ParseOk<T> ok;
private readonly ParseError error;

/// <summary>
/// True, if the result is a success.
/// </summary>
public bool IsOk => this.value is ParseOk<T>;
public bool IsOk { get; }

/// <summary>
/// True, if the result is an error.
/// </summary>
public bool IsError => this.value is ParseError;
public bool IsError => !IsOk;

/// <summary>
/// Retrieves the parse as a success.
/// </summary>
public ParseOk<T> Ok => (ParseOk<T>)this.value;
public ParseOk<T> Ok => ok;

/// <summary>
/// Retrieves the parse as an error.
/// </summary>
public ParseError Error => (ParseError)this.value;
public ParseError Error => error;

/// <summary>
/// Retrieves the furthest error for this result.
/// </summary>
public ParseError? FurthestError => this.value is ParseOk<T> ok ? ok.FurthestError : (ParseError)this.value;
public ParseError? FurthestError => IsOk ? ok.FurthestError : error;

/// <summary>
/// Initializes a new instance of the <see cref="ParseResult{T}"/> struct.
/// </summary>
/// <param name="ok">The successful parse description.</param>
public ParseResult(ParseOk<T> ok)
{
this.value = ok;
this.ok = ok;
IsOk = true;
error = default!;
}

/// <summary>
Expand All @@ -52,7 +55,9 @@ public ParseResult(ParseOk<T> ok)
/// <param name="error">The error description.</param>
public ParseResult(ParseError error)
{
this.value = error;
this.error = error;
IsOk = false;
this.ok = default!;
}

/// <summary>
Expand All @@ -75,6 +80,7 @@ public ParseResult(ParseError error)
/// <returns>The <see cref="ParseResult{T}"/> constructed from the alternatives.</returns>
public static ParseResult<T> operator |(ParseResult<T> first, ParseResult<T> second)
{
return first.IsOk ? first.Ok : (second.IsOk ? second.Ok : second.Error);
if (first.IsOk && second.IsOk) return first.Ok | second.Ok;
if (first.IsOk) return first.Ok | second.Error;
if (second.IsOk) return second.Ok | first.Error;
Expand Down
2 changes: 1 addition & 1 deletion Sources/SynKit/Libraries/Parser/Parser.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\SynKit\Libraries\Lexer\Lexer.csproj" />
<ProjectReference Include="..\Lexer\Lexer.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Sources/SynKit/Libraries/Reporting/Reporting.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(SolutionDir)\SynKit\Libraries\Text\Text.csproj" />
<ProjectReference Include="$(SolutionDir)\Shared\Libraries\Polyfill\Polyfill.csproj" />
<ProjectReference Include="..\Text\Text.csproj" />
<ProjectReference Include="..\..\..\Shared\Libraries\Polyfill\Polyfill.csproj" />
</ItemGroup>
</Project>
Loading