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

No longer try to parse a generic type if there's no possible candidates #315

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions src/DynamicExpresso.Core/ParserArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq.Expressions;
using System.Reflection;
using DynamicExpresso.Exceptions;
using System.Text.RegularExpressions;

namespace DynamicExpresso
{
Expand Down Expand Up @@ -75,6 +76,15 @@ public bool TryGetKnownType(string name, out Type type)
return false;
}

/// <summary>
/// Returns true if the known types contain a generic type definition with the given name + any arity (e.g. name`1).
/// </summary>
internal bool HasKnownGenericTypeDefinition(string name)
{
var regex = new Regex("^" + name + "`\\d+$");
return Settings.KnownTypes.Values.Any(refType => regex.IsMatch(refType.Name) && refType.Type.IsGenericTypeDefinition);
}

public bool TryGetIdentifier(string name, out Expression expression)
{
Identifier identifier;
Expand Down
9 changes: 7 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,9 +1440,14 @@ private bool TryParseKnownType(string name, out Type type)
{
// if the type is unknown, we need to restart parsing
var originalPos = _token.pos;
_arguments.TryGetKnownType(name, out type);

type = ParseKnownGenericType(name, type);
// the name might reference a generic type, with an aliased name (e.g. List<T> = MyList instead of List`1)
// it can also reference a generic type for which we don't know the arity yet (and therefore the name doesn't contain the `n suffix)
if (_arguments.TryGetKnownType(name, out type) || _arguments.HasKnownGenericTypeDefinition(name))
{
type = ParseKnownGenericType(name, type);
}

type = ParseTypeModifiers(type);

if (type == null)
Expand Down
12 changes: 12 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,18 @@ public void GitHub_Issue_305()
}

#endregion

[Test]
public void GitHub_Issue_314()
{
var interpreter = new Interpreter();

var exception1 = Assert.Throws<UnknownIdentifierException>(() => interpreter.Eval("b < 1"));
Assert.AreEqual("b", exception1.Identifier);

var exception2 = Assert.Throws<UnknownIdentifierException>(() => interpreter.Eval("b > 1"));
Assert.AreEqual("b", exception2.Identifier);
}
}

internal static class GithubIssuesTestExtensionsMethods
Expand Down
Loading