Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tfms #107

Merged
merged 3 commits into from
May 18, 2024
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
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="FastExpressionCompiler.Internal.src" Version="4.2.0" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />

<!-- Benchmarks -->
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="Pidgin" Version="3.2.3" />
Expand All @@ -16,7 +16,8 @@

<!-- Global Package References -->
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />

<GlobalPackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />

<!-- Testing -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="xunit" Version="2.8.0" />
Expand Down
20 changes: 4 additions & 16 deletions src/Parlot/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public static TextSpan DecodeString(TextSpan span)
return span;
}

#if NETSTANDARD2_0
var result = CreateString(span.Length, span, static (chars, source) =>
#if NET6_0_OR_GREATER
var result = string.Create(span.Length, span, static (chars, source) =>
#else
var result = String.Create(span.Length, span, static (chars, source) =>
var result = "".Create(span.Length, span, static (chars, source) =>
#endif
{
// The asumption is that the new string will be shorter since escapes results are smaller than their source
// The assumption is that the new string will be shorter since escapes results are smaller than their source

var dataIndex = 0;
var buffer = source.Buffer;
Expand Down Expand Up @@ -153,17 +153,5 @@ public static TextSpan DecodeString(TextSpan span)
}

private static int HexValue(char ch) => HexConverter.FromChar(ch);

#if NETSTANDARD2_0
private delegate void SpanAction<T, in TArg>(T[] span, TArg arg);
private static string CreateString<TState>(int length, TState state, SpanAction<char, TState> action)
{
var array = new char[length];

action(array, state);

return new string(array);
}
#endif
}
}
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/DecimalLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public override bool Parse(ParseContext context, ref ParseResult<decimal> result
if (context.Scanner.ReadDecimal())
{
var end = context.Scanner.Cursor.Offset;
#if !SUPPORTS_SPAN_PARSE
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#else
#if NET6_0_OR_GREATER
var sourceToParse = context.Scanner.Buffer.AsSpan(start, end - start);
#else
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#endif

if (decimal.TryParse(sourceToParse, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var value))
Expand Down Expand Up @@ -94,14 +94,14 @@ public CompilationResult Compile(CompilationContext context)
//

var end = Expression.Variable(typeof(int), $"end{context.NextNumber}");
#if NETSTANDARD2_0
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(decimal).GetMethod(nameof(decimal.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(decimal).MakeByRefType()]);
#else
#if NET6_0_OR_GREATER
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(decimal).GetMethod(nameof(decimal.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(decimal).MakeByRefType()});
#else
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(decimal).GetMethod(nameof(decimal.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(decimal).MakeByRefType()]);
#endif

// TODO: NETSTANDARD2_1 code path
Expand Down
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/DoubleLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public override bool Parse(ParseContext context, ref ParseResult<double> result)
if (context.Scanner.ReadDecimal())
{
var end = context.Scanner.Cursor.Offset;
#if !SUPPORTS_SPAN_PARSE
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#else
#if NET6_0_OR_GREATER
var sourceToParse = context.Scanner.Buffer.AsSpan(start, end - start);
#else
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#endif

if (double.TryParse(sourceToParse, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var value))
Expand Down Expand Up @@ -94,14 +94,14 @@ public CompilationResult Compile(CompilationContext context)
//

var end = Expression.Variable(typeof(int), $"end{context.NextNumber}");
#if NETSTANDARD2_0
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(double).GetMethod(nameof(double.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(double).MakeByRefType()]);
#else
#if NET6_0_OR_GREATER
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(double).GetMethod(nameof(double.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(double).MakeByRefType()});
#else
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(double).GetMethod(nameof(double.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(double).MakeByRefType()]);
#endif

// TODO: NETSTANDARD2_1 code path
Expand Down
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/FloatLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public override bool Parse(ParseContext context, ref ParseResult<float> result)
if (context.Scanner.ReadDecimal())
{
var end = context.Scanner.Cursor.Offset;
#if !SUPPORTS_SPAN_PARSE
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#else
#if NET6_0_OR_GREATER
var sourceToParse = context.Scanner.Buffer.AsSpan(start, end - start);
#else
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#endif

if (float.TryParse(sourceToParse, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var value))
Expand Down Expand Up @@ -94,14 +94,14 @@ public CompilationResult Compile(CompilationContext context)
//

var end = Expression.Variable(typeof(int), $"end{context.NextNumber}");
#if NETSTANDARD2_0
#if NET6_0_OR_GREATER
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(float).GetMethod(nameof(float.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(float).MakeByRefType() });
#else
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(float).GetMethod(nameof(float.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(float).MakeByRefType()]);
#else
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(float).GetMethod(nameof(float.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(float).MakeByRefType()});
#endif

// TODO: NETSTANDARD2_1 code path
Expand Down
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/IntegerLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public override bool Parse(ParseContext context, ref ParseResult<long> result)
{
var end = context.Scanner.Cursor.Offset;

#if !SUPPORTS_SPAN_PARSE
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#else
#if NET6_0_OR_GREATER
var sourceToParse = context.Scanner.Buffer.AsSpan(start, end - start);
#else
var sourceToParse = context.Scanner.Buffer.Substring(start, end - start);
#endif

if (long.TryParse(sourceToParse, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var value))
Expand Down Expand Up @@ -92,14 +92,14 @@ public CompilationResult Compile(CompilationContext context)
//

var end = Expression.Variable(typeof(int), $"end{context.NextNumber}");
#if NETSTANDARD2_0
#if NET6_0_OR_GREATER
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(long).GetMethod(nameof(long.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() });
#else
var sourceToParse = Expression.Variable(typeof(string), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(context.Buffer(), typeof(string).GetMethod("Substring", [typeof(int), typeof(int)]), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(long).GetMethod(nameof(long.TryParse), [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType()]);
#else
var sourceToParse = Expression.Variable(typeof(ReadOnlySpan<char>), $"sourceToParse{context.NextNumber}");
var sliceExpression = Expression.Assign(sourceToParse, Expression.Call(typeof(MemoryExtensions).GetMethod("AsSpan", new[] { typeof(string), typeof(int), typeof(int) }), context.Buffer(), start, Expression.Subtract(end, start)));
var tryParseMethodInfo = typeof(long).GetMethod(nameof(long.TryParse), new[] { typeof(ReadOnlySpan<char>), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType()});
#endif

// TODO: NETSTANDARD2_1 code path
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/Parser.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private CompilationResult BuildAsNonCompilableParser(CompilationContext context)
// ParseResult parseResult;
//
// success = parser.Parse(context.ParseContext, ref parseResult)
// #if not DicardResult
// #if not DiscardResult
// if (success)
// {
// value = parseResult.Value;
Expand Down
4 changes: 0 additions & 4 deletions src/Parlot/IsExternalInit.cs

This file was deleted.

13 changes: 3 additions & 10 deletions src/Parlot/Parlot.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
<Title>Parser combinator for .NET</Title>
<Description>Parlot is a fast, lightweight and simple to use .NET parser combinator.</Description>
<PackageTags>parser;interpreter;</PackageTags>
Expand All @@ -14,16 +14,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Memory" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="System.Collections.Immutable" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="System.Memory" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))" />
<PackageReference Include="System.Collections.Immutable" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />
<PackageReference Include="FastExpressionCompiler.Internal.src" PrivateAssets="all" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<PackageReference Include="FastExpressionCompiler.Internal.src" PrivateAssets="all" />
<PackageReference Include="FastExpressionCompiler.Internal.src" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />

</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);SUPPORTS_READONLYSPAN</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1' ">
<DefineConstants>$(DefineConstants);SUPPORTS_SPAN_PARSE;SUPPORTS_CODENALYSIS</DefineConstants>
</PropertyGroup>
</Project>
28 changes: 28 additions & 0 deletions src/Parlot/Polyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if NET462
using System.Collections.Generic;
#endif

namespace Parlot;

internal static class Polyfill
{
#if NET462
internal static IEnumerable<T> Append<T>(this IEnumerable<T> source, T element)
{
var result = new List<T>(source);
result.Add(element);
return result;
}

internal delegate void SpanAction<T, in TArg>(T[] span, TArg arg);
internal static string Create<TState>(this string _, int length, TState state, SpanAction<char, TState> action)
{
var array = new char[length];

action(array, state);

return new string(array);
}

#endif
}
2 changes: 0 additions & 2 deletions src/Parlot/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#if !NET8_0_OR_GREATER
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be always "on" with pre-compiler directives.

should add to csproj:

<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />

then remove files IsExternalInit.cs and SkiptLocalsInitAttribute.cs

[module: System.Runtime.CompilerServices.SkipLocalsInit]
#endif
14 changes: 0 additions & 14 deletions src/Parlot/SkipLocalsInitAttribute.cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/Parlot/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace Parlot
{
#if SUPPORTS_CODENALYSIS
using System.Diagnostics.CodeAnalysis;
#endif

internal static class ThrowHelper
{
#if SUPPORTS_CODENALYSIS
#if NET6_0_OR_GREATER
[DoesNotReturn]
#endif
public static void ThrowArgumentNullException(string paramName)
Expand Down
6 changes: 3 additions & 3 deletions src/Samples/Calc/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ private decimal ParsePrimaryExpression()

if (_scanner.ReadDecimal(out var number))
{
#if NETCOREAPP2_1
return decimal.Parse(number.GetText(), provider: CultureInfo.InvariantCulture);
#else
#if NET6_0_OR_GREATER
return decimal.Parse(number.Span, provider: CultureInfo.InvariantCulture);
#else
return decimal.Parse(number.GetText(), provider: CultureInfo.InvariantCulture);
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions src/Samples/Calc/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ private Expression ParsePrimaryExpression()

if (_scanner.ReadDecimal(out var number))
{
#if NETCOREAPP2_1
return new Number(decimal.Parse(number.GetText(), provider: CultureInfo.InvariantCulture));
#else
#if NET6_0_OR_GREATER
return new Number(decimal.Parse(number.Span, provider: CultureInfo.InvariantCulture));
#else
return new Number(decimal.Parse(number.GetText(), provider: CultureInfo.InvariantCulture));
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion test/Parlot.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void TextSpanShoudNotThrow()
Assert.Equal(0, t2.Length);
Assert.Equal(0, t2.Offset);

#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
Assert.True(ReadOnlySpan<char>.Empty == t2.Span);
#endif
}
Expand Down