Skip to content

Commit

Permalink
Small refactoring, added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi committed Oct 31, 2017
1 parent 4fb63bf commit b1d1217
Show file tree
Hide file tree
Showing 21 changed files with 427 additions and 381 deletions.
18 changes: 9 additions & 9 deletions src/DynamicExpresso.Core/Detector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace DynamicExpresso
{
internal class Detector
{
readonly ParserSettings _settings;
static readonly Regex IDENTIFIERS_DETECTION_REGEX = new Regex(@"([^\.]|^)\b(?<id>[a-zA-Z_]\w*)\b", RegexOptions.Compiled);
private readonly ParserSettings _settings;
private static readonly Regex IDENTIFIERS_DETECTION_REGEX = new Regex(@"([^\.]|^)\b(?<id>[a-zA-Z_]\w*)\b", RegexOptions.Compiled);

static readonly Regex STRING_DETECTION_REGEX = new Regex(@"(?<!\\)?"".*?(?<!\\)""", RegexOptions.Compiled);
static readonly Regex CHAR_DETECTION_REGEX = new Regex(@"(?<!\\)?'.{1,2}?(?<!\\)'", RegexOptions.Compiled);
private static readonly Regex STRING_DETECTION_REGEX = new Regex(@"(?<!\\)?"".*?(?<!\\)""", RegexOptions.Compiled);
private static readonly Regex CHAR_DETECTION_REGEX = new Regex(@"(?<!\\)?'.{1,2}?(?<!\\)'", RegexOptions.Compiled);

public Detector(ParserSettings settings)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ public IdentifiersInfo DetectIdentifiers(string expression)
return new IdentifiersInfo(unknownIdentifiers, knownIdentifiers, knownTypes);
}

string PrepareExpression(string expression)
private string PrepareExpression(string expression)
{
expression = expression ?? string.Empty;

Expand All @@ -60,22 +60,22 @@ string PrepareExpression(string expression)
return expression;
}

string RemoveStringLiterals(string expression)
private string RemoveStringLiterals(string expression)
{
return STRING_DETECTION_REGEX.Replace(expression, "");
}

string RemoveCharLiterals(string expression)
private string RemoveCharLiterals(string expression)
{
return CHAR_DETECTION_REGEX.Replace(expression, "");
}

bool IsReserverKeyword(string identifier)
private bool IsReserverKeyword(string identifier)
{
return ParserConstants.RESERVED_KEYWORDS.Contains(identifier, _settings.KeyComparer);
}

bool IsKnownIdentifier(string identifier)
private bool IsKnownIdentifier(string identifier)
{
return _settings.Identifiers.ContainsKey(identifier)
|| _settings.KnownTypes.ContainsKey(identifier);
Expand Down
9 changes: 5 additions & 4 deletions src/DynamicExpresso.Core/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace DynamicExpresso
/// </summary>
public class Interpreter
{
readonly ParserSettings _settings;
readonly ISet<ExpressionVisitor> _visitors = new HashSet<ExpressionVisitor>();
private readonly ParserSettings _settings;
private readonly ISet<ExpressionVisitor> _visitors = new HashSet<ExpressionVisitor>();

#region Constructors
/// <summary>
Expand Down Expand Up @@ -403,7 +403,8 @@ public IdentifiersInfo DetectIdentifiers(string expression)
#endregion

#region Private methods
Lambda ParseAsLambda(string expressionText, Type expressionType, Parameter[] parameters)

private Lambda ParseAsLambda(string expressionText, Type expressionType, Parameter[] parameters)
{
var arguments = new ParserArguments(
expressionText,
Expand All @@ -426,7 +427,7 @@ Lambda ParseAsLambda(string expressionText, Type expressionType, Parameter[] par
}

#if TEST_DetectIdentifiers
void AssertDetectIdentifiers(Lambda lambda)
private void AssertDetectIdentifiers(Lambda lambda)
{
var info = DetectIdentifiers(lambda.ExpressionText);

Expand Down
6 changes: 3 additions & 3 deletions src/DynamicExpresso.Core/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace DynamicExpresso
/// </summary>
public class Lambda
{
readonly Expression _expression;
readonly ParserArguments _parserArguments;
private readonly Expression _expression;
private readonly ParserArguments _parserArguments;

readonly Delegate _delegate;
private readonly Delegate _delegate;

internal Lambda(Expression expression, ParserArguments parserArguments)
{
Expand Down
30 changes: 15 additions & 15 deletions src/DynamicExpresso.Core/LanguageConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace DynamicExpresso
public static class LanguageConstants
{
public static readonly ReferenceType[] PrimitiveTypes = {
new ReferenceType(typeof(Object)),
new ReferenceType(typeof(Boolean)),
new ReferenceType(typeof(Char)),
new ReferenceType(typeof(String)),
new ReferenceType(typeof(SByte)),
new ReferenceType(typeof(Byte)),
new ReferenceType(typeof(Int16)),
new ReferenceType(typeof(UInt16)),
new ReferenceType(typeof(Int32)),
new ReferenceType(typeof(UInt32)),
new ReferenceType(typeof(Int64)),
new ReferenceType(typeof(UInt64)),
new ReferenceType(typeof(Single)),
new ReferenceType(typeof(Double)),
new ReferenceType(typeof(Decimal)),
new ReferenceType(typeof(object)),
new ReferenceType(typeof(bool)),
new ReferenceType(typeof(char)),
new ReferenceType(typeof(string)),
new ReferenceType(typeof(sbyte)),
new ReferenceType(typeof(byte)),
new ReferenceType(typeof(short)),
new ReferenceType(typeof(ushort)),
new ReferenceType(typeof(int)),
new ReferenceType(typeof(uint)),
new ReferenceType(typeof(long)),
new ReferenceType(typeof(ulong)),
new ReferenceType(typeof(float)),
new ReferenceType(typeof(double)),
new ReferenceType(typeof(decimal)),
new ReferenceType(typeof(DateTime)),
new ReferenceType(typeof(TimeSpan)),
new ReferenceType(typeof(Guid))
Expand Down
8 changes: 4 additions & 4 deletions src/DynamicExpresso.Core/ParserArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace DynamicExpresso
{
internal class ParserArguments
{
readonly Dictionary<string, Parameter> _declaredParameters;
private readonly Dictionary<string, Parameter> _declaredParameters;

readonly HashSet<Parameter> _usedParameters = new HashSet<Parameter>();
readonly HashSet<ReferenceType> _usedTypes = new HashSet<ReferenceType>();
readonly HashSet<Identifier> _usedIdentifiers = new HashSet<Identifier>();
private readonly HashSet<Parameter> _usedParameters = new HashSet<Parameter>();
private readonly HashSet<ReferenceType> _usedTypes = new HashSet<ReferenceType>();
private readonly HashSet<Identifier> _usedIdentifiers = new HashSet<Identifier>();

public ParserArguments(
string expressionText,
Expand Down
Loading

0 comments on commit b1d1217

Please sign in to comment.