This project is read-only.

Samples Code

Using QInjection

ProviderForInjector provider4injector = new ProviderForInjector();
provider4injector.ProvideData();

QInjector injector = new QInjector(@"x => x == 1");

injector = injector.Support(Node.SimpleLambdaExpression).As(Branch.Input)
    .Support(Node.BinaryExpression).As(Branch.Body)
    .Support(Node.IdentifierName).As(Branch.LeftMember)
    .Support(Node.LiteralExpression).As(Branch.RightMember)
    .Support(Node.Parameter).As(Branch.Parameter);

injector.Validate();

var genericdata = injector.WhereOn<Int32>(provider4injector);

Console.WriteLine("Check 2");
if (provider4injector.CountOnes() != genericdata.Count())
    Console.WriteLine("SQuery failed: eat my short!");
else
    Console.WriteLine("SQuery success: Woo Hoo!!!");

LPP - Samples by Use Cases

Literal Expression

x => 1

{{
injector = injector.Support(Node.LiteralExpression).As(Branch.Body);
}]

Indentifier Expression

x => x

injector = injector.Support(Node.IdentifierName).As(Branch.Body);

Unary Expression

x => !x

injector = injector.Support(Node.PrefixUnaryExpression).As(Branch.Body)
    .WithOperator(Operator.LogicalNot);
injector = injector.Support(Node.IdentifierName).As(Branch.Expression);

Binary Expression

x => x == 1

injector = injector.Support(Node.BinaryExpression).As(Branch.Body)
    .WithOperator(Operator.Equals);
injector = injector.Support(Node.IdentifierName).As(Branch.LeftMember);
injector = injector.Support(Node.LiteralExpression).As(Branch.RightMember);

Conditional Expression (ternary)

x => x ? 1 : 0

injector = injector.Support(Node.ConditionalExpression).As(Branch.Body);
injector = injector.Support(Node.IdentifierName).As(Branch.Condition);
injector = injector.Support(Node.LiteralExpression).As(Branch.WhenFalse)
    .As(Branch.WhenTrue);

Member Access Expression

x => x.item == 0

injector = injector.Support(Node.MemberAccessExpression).As(Branch.Body)
    .WithName("item");
injector = injector.Support(Node.IdentifierName).As(Branch.Expression);

Invocation Expression

x => x.Check(x,x)

injector = injector.Support(Node.InvocationExpression).As(Branch.Body)
    .WithName("Check").WithArity(2);
injector = injector.Support(Node.Argument).As(Branch.Argument);
injector = injector.Support(Node.IdentifierName).As(Branch.Expression);

Nested Binary Expression

x => x + 1 == 1

injector = injector.Support(Node.BinaryExpression).As(Branch.Body)
    .WithOperator(Operator.Equals).WithOperator(Operator.Plus);
injector = injector.Support(Node.LiteralExpression).As(Branch.RightMember);
injector = injector.Support(Node.BinaryExpression).As(Branch.LeftMember);
injector = injector.Support(Node.IdentifierName).As(Branch.LeftMember);

Parenthesized Expression

x => (x == 1)

injector = injector.Support(Node.ParenthesizedExpression).As(Branch.Body);
injector = injector.Support(Node.BinaryExpression).As(Branch.Expression)
    .WithOperator(Operator.Equals);
injector = injector.Support(Node.LiteralExpression).As(Branch.RightMember);
injector = injector.Support(Node.IdentifierName).As(Branch.LeftMember);

Nested Expression

x => x.Check(x ? x : 1, x + 1) && !x && (x + 1 == 2)

injector = injector.Support(Node.BinaryExpression).As(Branch.Body);

injector = injector.Support(Node.InvocationExpression).As(Branch.LeftMember);
injector = injector.Support(Node.PrefixUnaryExpression).As(Branch.RightMember);
injector = injector.Support(Node.ParenthesizedExpression).As(Branch.RightMember);

injector = injector.Support(Node.BinaryExpression).As(Branch.LeftMember);

injector = injector.Support(Node.LiteralExpression).As(Branch.RightMember);
injector = injector.Support(Node.IdentifierName).As(Branch.LeftMember);

injector = injector.Support(Node.IdentifierName).As(Branch.Expression);

injector = injector.Support(Node.Argument).As(Branch.Argument);
injector = injector.Support(Node.BinaryExpression).As(Branch.Expression);
injector = injector.Support(Node.ConditionalExpression).As(Branch.Expression);

injector = injector.Support(Node.IdentifierName).As(Branch.Condition);
injector = injector.Support(Node.IdentifierName).As(Branch.WhenTrue);
injector = injector.Support(Node.LiteralExpression).As(Branch.WhenFalse);


!!HPP - A generic validator
ProviderForInjector provider4injector = new ProviderForInjector();
provider4injector.ProvideData();

QInjector injector = new QInjector();

injector.Begin();

// Define basic elements (mandatory)
injector = injector.Accept(Element.BinaryExpression).AsBranch();
injector = injector.Accept(Element.UnaryExpression).AsBranch();
injector = injector.Accept(Element.ConditionalExpression).AsBranch();
injector = injector.Accept(Element.IdentifierName).AsBranch();
injector = injector.Accept(Element.LiteralExpression).AsBranch();
injector = injector.Accept(Element.MemberAccessExpression).AsBranch();
injector = injector.Accept(Element.InvocationExpression).AsBranch();
injector = injector.Accept(Element.ParenthesizedExpression).AsBranch();

// Constraints
injector = injector.Support(Node.Argument)
    .RefuseReference(Reference.Reference);

injector = injector.Accept(Element.UnaryExpression)
    .WithOperators(OperatorCategory.Unary);

List<Operator> oplist = new List<Operator>();
foreach (Operator op in Enum.GetValues(typeof(Operator)))
{
    oplist.Add(op);
}
injector = injector.Accept(Element.BinaryExpression)
    .WithOperators(oplist);

injector = injector.Accept(Element.InvocationExpression)
    .WithName("Check")
    .WithArity(2)
    .WithNameRegex(@"^[\w]{1,5}");

injector = injector.Accept(Element.MemberAccessExpression)
    .WithName("item")
    .WithNameRegex(@"^[\w]{1,5}");

injector.End();


while(true)
{
	var expr = Console.Readline();
    try
    {
        injector.Validate(expr);
        Console.WriteLine("PASSED");
    }
    catch (Exception ex)
    {
        Console.WriteLine("KO " + ex.Message);
    }
}


Last edited Jan 8, 2014 at 8:54 PM by SamNium, version 1