Skip to content

Commit

Permalink
Merge branch 'master' into fix_netstandard
Browse files Browse the repository at this point in the history
  • Loading branch information
metoule committed Aug 19, 2024
2 parents 5e70ae3 + e4589fb commit 9d2095f
Show file tree
Hide file tree
Showing 19 changed files with 1,268 additions and 1,157 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ on:

jobs:
test-linux:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: '0' # all
- name: Setup .NET Core 3.1
uses: actions/setup-dotnet@v3
- name: Setup .NET 8.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '3.1.x'
- name: Setup .NET 7.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'
- name: Setup gitversion
run: dotnet tool install --global GitVersion.Tool
- name: Calculate version
Expand All @@ -32,18 +28,20 @@ jobs:
run: dotnet restore DynamicExpresso.sln
- name: Build
run: dotnet build DynamicExpresso.sln --no-restore -c Release /p:Version=${{steps.calc_version.outputs.PROJECT_VERSION}}
- name: Test .net core 3.1
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f netcoreapp3.1
- name: Test .net core 7.0
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f net7.0
- name: Test .net core 8.0
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f net8.0
test-win:
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET 7.0
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'
- name: Setup .NET 8.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Restore packages
run: dotnet restore DynamicExpresso.sln
- name: Build
Expand All @@ -52,3 +50,5 @@ jobs:
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f net461
- name: Test .net 4.5
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f net45
- name: Test .net core 8.0
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release --verbosity normal -f net8.0
16 changes: 7 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ on:
default: 'true'
jobs:
publish-nuget:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: "${{ github.event.inputs.ref }}"
fetch-depth: '0' # all
- name: Setup .NET Core 7.0
uses: actions/setup-dotnet@v3
- name: Setup .NET Core 8.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'
- name: Setup gitversion
run: dotnet tool install --global GitVersion.Tool
- name: Calculate version
Expand All @@ -35,10 +35,8 @@ jobs:
run: dotnet restore DynamicExpresso.sln
- name: Build
run: dotnet build DynamicExpresso.sln --no-restore -c Release /p:Version=${{steps.calc_version.outputs.PROJECT_VERSION}}
- name: Test .net core 3.1
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release /p:Version=${{steps.calc_version.outputs.PROJECT_VERSION}} --verbosity normal -f netcoreapp3.1
- name: Test .net core 7.0
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release /p:Version=${{steps.calc_version.outputs.PROJECT_VERSION}} --verbosity normal -f net7.0
- name: Test .net core 8.0
run: dotnet test DynamicExpresso.sln --no-build --no-restore -c Release /p:Version=${{steps.calc_version.outputs.PROJECT_VERSION}} --verbosity normal -f net8.0
- name: Setup nuget sources
run: dotnet nuget add source --name github "https://nuget.pkg.github.com/dynamicexpresso/index.json"
- name: Pack
Expand Down
15 changes: 12 additions & 3 deletions src/DynamicExpresso.Core/Detector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ public IdentifiersInfo DetectIdentifiers(string expression)
if (IsReservedKeyword(identifier))
continue;

// don't consider member accesses as identifiers (e.g. "x.Length" will only return x but not Length)
if (idGroup.Index > 0 && expression[idGroup.Index - 1] == '.')
continue;
if (idGroup.Index > 0)
{
var previousChar = expression[idGroup.Index - 1];

// don't consider member accesses as identifiers (e.g. "x.Length" will only return x but not Length)
if (previousChar == '.')
continue;

// don't consider number literals as identifiers
if (char.IsDigit(previousChar))
continue;
}

if (_settings.Identifiers.TryGetValue(identifier, out Identifier knownIdentifier))
knownIdentifiers.Add(knownIdentifier);
Expand Down
5 changes: 5 additions & 0 deletions src/DynamicExpresso.Core/Exceptions/ParseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public ParseException(string message, int position, Exception innerException)

public int Position { get; private set; }

public static ParseException Create(int pos, string format, params object[] args)
{
return new ParseException(string.Format(format, args), pos);
}

protected ParseException(
SerializationInfo info,
StreamingContext context)
Expand Down
16 changes: 15 additions & 1 deletion src/DynamicExpresso.Core/Parameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq.Expressions;

namespace DynamicExpresso
Expand Down Expand Up @@ -49,4 +49,18 @@ public static Parameter Create<T>(string name, T value)

public ParameterExpression Expression { get; private set; }
}

/// <summary>
/// Parameter with its position in the expression.
/// </summary>
internal class ParameterWithPosition : Parameter
{
public ParameterWithPosition(int pos, string name, Type type)
: base(name, type)
{
Position = pos;
}

public int Position { get; }
}
}
76 changes: 76 additions & 0 deletions src/DynamicExpresso.Core/Parsing/InterpreterExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using DynamicExpresso.Exceptions;
using DynamicExpresso.Reflection;

namespace DynamicExpresso.Parsing
{
internal class InterpreterExpression : Expression
{
private readonly Interpreter _interpreter;
private readonly string _expressionText;
private readonly IList<Parameter> _parameters;
private Type _type;

public InterpreterExpression(ParserArguments parserArguments, string expressionText, params ParameterWithPosition[] parameters)
{
var settings = parserArguments.Settings.Clone();
_interpreter = new Interpreter(settings);
_expressionText = expressionText;
_parameters = parameters;

// Take the parent expression's parameters and set them as an identifier that
// can be accessed by any lower call
// note: this doesn't impact the initial settings, because they're cloned
foreach (var dp in parserArguments.DeclaredParameters)
{
// Have to mark the parameter as "Used" otherwise we can get a compilation error.
parserArguments.TryGetParameters(dp.Name, out var pe);
_interpreter.SetIdentifier(new Identifier(dp.Name, pe));
}

foreach (var myParameter in parameters)
{
if (settings.Identifiers.ContainsKey(myParameter.Name))
{
throw new ParseException($"A local or parameter named '{myParameter.Name}' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter", myParameter.Position);
}
}

// prior to evaluation, we don't know the generic arguments types
_type = ReflectionExtensions.GetFuncType(parameters.Length);
}

public IList<Parameter> Parameters
{
get { return _parameters; }
}

public override Type Type
{
get { return _type; }
}

internal LambdaExpression EvalAs(Type delegateType)
{
if (!IsCompatibleWithDelegate(delegateType))
return null;

var lambdaExpr = _interpreter.ParseAsExpression(delegateType, _expressionText, _parameters.Select(p => p.Name).ToArray());
_type = lambdaExpr.Type;
return lambdaExpr;
}

internal bool IsCompatibleWithDelegate(Type target)
{
if (!target.IsGenericType || target.BaseType != typeof(MulticastDelegate))
return false;

var genericTypeDefinition = target.GetGenericTypeDefinition();
return genericTypeDefinition == ReflectionExtensions.GetFuncType(_parameters.Count)
|| genericTypeDefinition == ReflectionExtensions.GetActionType(_parameters.Count);
}
}
}
Loading

0 comments on commit 9d2095f

Please sign in to comment.