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

implicit this reference #232. #233

Merged
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
2 changes: 2 additions & 0 deletions src/DynamicExpresso.Core/LanguageConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace DynamicExpresso
{
public static class LanguageConstants
{
public const string This = "this";

public static readonly ReferenceType[] PrimitiveTypes = {
new ReferenceType(typeof(object)),
new ReferenceType(typeof(bool)),
Expand Down
23 changes: 21 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,31 @@ private Expression ParseIdentifier()
{
return ParseTypeKeyword(knownType);
}


var token = _token;

try
{
if (_arguments.TryGetIdentifier(LanguageConstants.This, out var thisKeywordExpression))
{
return ParseMemberAccess(thisKeywordExpression);
}

if (_arguments.TryGetParameters(LanguageConstants.This, out var thisParameterExpression))
{
return ParseMemberAccess(thisParameterExpression);
}
}
catch(ParseException)
{
// ignore
}

// Working context implementation
//if (it != null)
// return ParseMemberAccess(null, it);

throw new UnknownIdentifierException(_token.text, _token.pos);
throw new UnknownIdentifierException(token.text, token.pos);
}

// Working context implementation
Expand Down
36 changes: 35 additions & 1 deletion test/DynamicExpresso.UnitTest/IdentifiersTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace DynamicExpresso.UnitTest
Expand Down Expand Up @@ -38,5 +39,38 @@ public void Getting_the_list_of_used_identifiers()
Assert.AreEqual("x", lambda.Identifiers.ElementAt(0).Name);
Assert.AreEqual("true", lambda.Identifiers.ElementAt(1).Name);
}

[Test]
public void This_identifier_variable()
{
const string Name = "John";
var context = new KeyValuePair<string, string>(nameof(Name), Name);

var interpreter = new Interpreter();

interpreter.SetVariable("this", context);

Assert.AreEqual(Name, interpreter.Eval("this.Value"));
Assert.AreEqual(nameof(Name), interpreter.Eval("this.Key"));

Assert.AreEqual(Name, interpreter.Eval("Value"));
Assert.AreEqual(nameof(Name), interpreter.Eval("Key"));
}

[Test]
public void This_identifier_parameter()
{
const string Name = "John";
var context = new KeyValuePair<string, string>(nameof(Name), Name);
var parameter = new Parameter("this", context.GetType());

var interpreter = new Interpreter();

Assert.AreEqual(Name, interpreter.Parse("this.Value", parameter).Invoke(context));
Assert.AreEqual(nameof(Name), interpreter.Parse("this.Key", parameter).Invoke(context));

Assert.AreEqual(Name, interpreter.Parse("Value", parameter).Invoke(context));
Assert.AreEqual(nameof(Name), interpreter.Parse("Key", parameter).Invoke(context));
}
}
}