本文整理汇总了C#中ParseTreeNode.RequireChildren方法的典型用法代码示例。如果您正苦于以下问题:C# ParseTreeNode.RequireChildren方法的具体用法?C# ParseTreeNode.RequireChildren怎么用?C# ParseTreeNode.RequireChildren使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParseTreeNode
的用法示例。
在下文中一共展示了ParseTreeNode.RequireChildren方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrepareStringInstanceMethodCall
private static MethodInfo PrepareStringInstanceMethodCall(string methodName, ParseTreeNode root, CompilerState state, out Expression value, out Expression pattern)
{
root.RequireChildren(2);
MethodInfo method;
if (0 == StringComparer.OrdinalIgnoreCase.Compare(methodName, "StartsWith"))
{
method = ReflectionHelper.StringStartsWith;
}
else if (0 == StringComparer.OrdinalIgnoreCase.Compare(methodName, "EndsWith"))
{
method = ReflectionHelper.StringEndsWith;
}
else if (0 == StringComparer.OrdinalIgnoreCase.Compare(methodName, "IndexOf"))
{
method = ReflectionHelper.StringIndexOf;
}
else
{
throw new Exception("Could not find method " + methodName);
}
var arg1Node = root.RequireChild(null, 1, 0, 0);
value = state.ParentRuntime.Analyze(arg1Node, state);
value.RequireString(arg1Node);
var arg2Node = root.RequireChild(null, 1, 0, 1);
pattern = state.ParentRuntime.Analyze(arg2Node, state);
pattern.RequireString(arg2Node);
return method;
}
示例2: BuildBetweenExpression
private Expression BuildBetweenExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(6);
var variableNode = root.ChildNodes[0];
var notOpt = root.RequireChild("notOpt", 1);
root.RequireChild("between", 2);
root.RequireChild("and", 4);
var argument = Analyze(variableNode, state).RemoveNullability();
var leftNode = root.ChildNodes[3];
var leftExpr = Analyze(leftNode, state);
leftExpr.RequireNonVoid(leftNode);
leftExpr = ExpressionTreeExtensions.AdjustReturnType(leftNode, leftExpr, argument.Type);
var rightNode = root.ChildNodes[5];
var rightExpr = Analyze(rightNode, state);
rightExpr.RequireNonVoid(rightNode);
rightExpr = ExpressionTreeExtensions.AdjustReturnType(rightNode, rightExpr, argument.Type);
Expression lower, upper;
if (argument.IsString())
{
lower = ConstantHelper.TryEvalConst(leftNode, PrepareStringComparison(leftNode, leftExpr, argument), Expression.Constant(0), ExpressionType.LessThanOrEqual);
upper = ConstantHelper.TryEvalConst(rightNode, PrepareStringComparison(rightNode, rightExpr, argument), Expression.Constant(0), ExpressionType.GreaterThanOrEqual);
}
else
{
lower = ConstantHelper.TryEvalConst(leftNode, argument, leftExpr, ExpressionType.GreaterThanOrEqual);
upper = ConstantHelper.TryEvalConst(rightNode, argument, rightExpr, ExpressionType.LessThanOrEqual);
}
var result = ConstantHelper.TryEvalConst(root, lower, upper, ExpressionType.AndAlso);
if (notOpt.ChildNodes.Count > 0)
{
result = ConstantHelper.TryEvalConst(root, result, ExpressionType.Not);
}
return result;
}
示例3: BuildUnaryExpression
private Expression BuildUnaryExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(2, 4);
ParseTreeNode targetNode;
string op;
if (root.ChildNodes.Count == 2)
{
targetNode = root.ChildNodes[1];
op = root.ChildNodes[0].Token.ValueString;
}
else
{
// we should have IS NULL or IS NOT NULL operator here
targetNode = root.ChildNodes[0];
var k1 = root.RequireChild("is", 1).Term.Name;
var k2 = root.RequireChild(null, 2).Term.Name;
var k3 = root.ChildNodes.Count == 4 ? root.RequireChild(null, 3).Term.Name : null;
if (0 != StringComparer.OrdinalIgnoreCase.Compare(k1, "is")
|| (k3 == null && 0 != StringComparer.OrdinalIgnoreCase.Compare(k2, "null"))
|| (k3 != null && (0 != StringComparer.OrdinalIgnoreCase.Compare(k2, "not") || 0 != StringComparer.OrdinalIgnoreCase.Compare(k3, "null")))
)
{
throw new CompilationException("IS NULL or IS NOT NULL expected", root);
}
op = k3 == null ? "is null" : "is not null";
}
switch (op)
{
case "-":
{
var target = Analyze(targetNode, state).RemoveNullability();
target.RequireNumeric(targetNode);
return ConstantHelper.TryEvalConst(root, target, ExpressionType.Negate, target.Type);
}
case "+":
{
var target = Analyze(targetNode, state).RemoveNullability();
target.RequireNumeric(targetNode);
return target;
}
case "~":
{
var target = Analyze(targetNode, state).RemoveNullability();
target.RequireInteger(targetNode);
return ConstantHelper.TryEvalConst(root, target, ExpressionType.Not, target.Type);
}
case "is null":
return BuildIsNullPredicate(targetNode, state, true);
case "is not null":
return BuildIsNullPredicate(targetNode, state, false);
default:
{
var target = Analyze(targetNode, state).RemoveNullability();
if (0 == StringComparer.OrdinalIgnoreCase.Compare("not", op))
{
target.RequireBoolean(targetNode);
return ConstantHelper.TryEvalConst(root, target, ExpressionType.Not, target.Type);
}
throw new CompilationException(
string.Format(
"Unary operator {0} not supported for type {1}", op, target.Type.FullName), root);
}
}
}
示例4: BuildStringLiteralExpression
private Expression BuildStringLiteralExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(0);
var value = root.Token.ValueString;
var expr = string.IsNullOrEmpty(value)
? Expression.Constant(null, typeof (String)) : Expression.Constant(PreprocessStringLiteral(value, root));
return expr;
}
示例5: BuildNumericConstantExpression
private Expression BuildNumericConstantExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(0);
var objValue = root.Token.Value;
if (objValue != null && objValue.GetType().IsNumeric())
{
return Expression.Constant(objValue);
}
throw new CompilationException("Invalid numeric constant: " + root.Token.Text, root);
}
示例6: BuildInclusionExpression
private Expression BuildInclusionExpression(ParseTreeNode root, Expression leftExpr, string op, CompilerState state)
{
root.RequireChildren(3);
var rightNodeList = ExpressionTreeExtensions.UnwindTupleExprList(root.ChildNodes[2]);
if (rightNodeList.Term.Name == "Id")
{
throw new CompilationException("Parameterized IN statement is not yet supported, consider using function SetContains", rightNodeList);
}
if (rightNodeList.Term.Name != "exprList")
{
throw new CompilationException("Argument for IN operator must be a list of expressions", root);
}
// Expression text is not supposed to be used to pass tens and hundreds of thousands of IDs in plain text.
// Use parameters for large argument sets.
rightNodeList.RequireChildren(1, 1000);
leftExpr = leftExpr.RemoveNullability();
// compile a method to enumerate values in the argument set
var valueEnumerator = ReflectionHelper.EnumerateValues.MakeGenericMethod(leftExpr.Type);
// invoke enumerator, output is a hashset
object matchingSet;
try
{
matchingSet = valueEnumerator.Invoke(null, new object[] {this, rightNodeList, state});
}
catch (TargetInvocationException e)
{
if (e.InnerException == null) throw;
throw e.InnerException;
}
// how many items do we have there?
var countProperty = matchingSet.GetType().GetProperty("Count", BindingFlags.Instance | BindingFlags.Public);
var count = (int) (countProperty.GetValue(matchingSet));
Expression contains;
var leftArgConst = leftExpr as ConstantExpression;
if (leftArgConst != null)
{
// since list is constant and argument is constant, let's just evaluate it
var setContainsMethod = ReflectionHelper.GetOrAddMethod1(matchingSet.GetType(), "Contains", leftExpr.Type);
try
{
contains = Expression.Constant(setContainsMethod.Invoke(matchingSet, new[] {leftArgConst.Value}), typeof(bool));
}
catch (TargetInvocationException e)
{
if (e.InnerException == null) throw;
throw e.InnerException;
}
}
else
{
var threshold = leftExpr.IsInteger() ? 15 : 5;
// for small sets of values, just create a chain of IF/THEN/ELSE statements
if (count <= threshold)
{
var isString = leftExpr.IsString();
var enumeratorMethod = ReflectionHelper.GetOrAddMethod0(matchingSet.GetType(), "GetEnumerator");
IEnumerator enumerator;
try
{
enumerator = (IEnumerator)enumeratorMethod.Invoke(matchingSet, null);
}
catch (TargetInvocationException e)
{
if (e.InnerException == null) throw;
throw e.InnerException;
}
contains = null;
while (enumerator.MoveNext())
{
var next = isString
? PrepareStringEquality(rightNodeList, leftExpr, Expression.Constant(enumerator.Current, leftExpr.Type))
: Expression.Equal(leftExpr, Expression.Constant(enumerator.Current, leftExpr.Type));
contains = contains == null ? next : Expression.OrElse(contains, next);
}
}
else
{
// for larger sets, wire our matchingSet into this expression as constant reference
// it will be kept alive by garbage collector, and will be collected when expression is collected
var setContainsMethod = ReflectionHelper.GetOrAddMethod1(matchingSet.GetType(), "Contains", leftExpr.Type);
contains = Expression.Call(Expression.Constant(matchingSet), setContainsMethod, leftExpr);
}
}
if (op.StartsWith("not "))
{
contains = ConstantHelper.TryEvalConst(root, contains, ExpressionType.Not, typeof(bool));
}
return contains;
//.........这里部分代码省略.........
示例7: BuildIdentifierExpression
private Expression BuildIdentifierExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(1, 10);
// first, analyze the first identifier in the dotted sequence
var identifier = BuildIdentifierRootExpression(root, state);
// next, look for any next-level dotted fields and properties
// for these, we will not look into any other atoms or args - only into the reflected fields and properties of the object
for (var i = 1; i < root.ChildNodes.Count; i++)
{
var name = root.ChildNodes[i].Token.Text; // no need to lowercase this text
var next = TryGetFieldOrPropertyInfoFromContext(name, identifier);
if (next == null)
{
throw new CompilationException(string.Format("Could not find field or property {0} on type {1}",
name, identifier.Type));
}
identifier = next;
}
return identifier;
}
示例8: BuildCaseStatementExpression
private Expression BuildCaseStatementExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(4, 5);
root.RequireChild("CASE", 0);
var caseVariableNode = root.RequireChild("caseVariable", 1);
caseVariableNode.RequireChildren(0, 1);
ParseTreeNode caseDefaultValueNode = null;
if (root.ChildNodes.Count > 4)
{
var caseDefaultNode = root.RequireChild("caseDefault", 3);
if (caseDefaultNode.ChildNodes.Count > 0)
{
caseDefaultNode.RequireChildren(2);
caseDefaultNode.RequireChild("else", 0);
caseDefaultValueNode = caseDefaultNode.RequireChild(null, 1);
}
}
root.RequireChild("END", root.ChildNodes.Count - 1);
// first get the expression for ELSE
var caseDefault = caseDefaultValueNode != null ? Analyze(caseDefaultValueNode, state) : null;
var whenThenListNode = root.RequireChild("caseWhenList", 2);
whenThenListNode.RequireChildren(1, 100);
// now if we don't have an expression to be analyzed, just create a sequence of IIFs
if (caseVariableNode.ChildNodes.Count == 0)
{
return BuildCaseStatementExpression(state, whenThenListNode, caseDefault);
}
// but when there is a variable, type checking has different rules and output is SWITCH statement
return BuildSwitchStatementExpression(state, caseVariableNode, whenThenListNode, caseDefault);
}
示例9: BuildBinaryExpression
private Expression BuildBinaryExpression(ParseTreeNode root, CompilerState state)
{
root.RequireChildren(3);
var leftNode = root.ChildNodes[0];
var leftExpr = Analyze(leftNode, state);
var op = GetBinaryOperator(root.ChildNodes[1]);
if (op == "in" || op == "not in")
{
return BuildInclusionExpression(root, leftExpr, op, state);
}
var rightNode = root.ChildNodes[2];
var rightExpr = Analyze(rightNode, state);
if (!ExpressionTreeExtensions.TryAdjustVoid(ref leftExpr, ref rightExpr))
{
throw new CompilationException("This operation is not defined when both arguments are void", root);
}
Expression expr;
leftExpr = leftExpr.RemoveNullability();
rightExpr = rightExpr.RemoveNullability();
if (leftExpr.IsDateTime() && rightExpr.IsDateTime()
|| leftExpr.IsTimeSpan() && rightExpr.IsTimeSpan())
{
#region DateTime and DateTime, or TimeSpan and TimeSpan
switch (op)
{
case "+":
if (leftExpr.IsDateTime() && rightExpr.IsDateTime())
{
throw new CompilationException("Datetime values cannot be added to one another", root);
}
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.Add);
break;
case "-":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.Subtract);
break;
case "=":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.Equal);
break;
case "!=":
case "<>":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.NotEqual);
break;
case ">":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.GreaterThan);
break;
case "<":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.LessThan);
break;
case "<=":
case "!>":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.LessThanOrEqual);
break;
case ">=":
case "!<":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.GreaterThanOrEqual);
break;
default:
throw new CompilationException("Binary operator not supported for datetime values: " + op, root.ChildNodes[1]);
}
#endregion
}
else if (leftExpr.IsDateTime() && rightExpr.IsTimeSpan())
{
#region DateTime and TimeSpan or TimeSpan and DateTime
switch (op)
{
case "+":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.Add);
break;
case "-":
expr = ConstantHelper.TryEvalConst(root, leftExpr, rightExpr, ExpressionType.Subtract);
break;
default:
throw new CompilationException("Binary operator not supported for datetime and timespan: " + op, root.ChildNodes[1]);
}
#endregion
}
else if (leftExpr.IsTimeSpan() && rightExpr.IsDateTime())
{
#region TimeSpan and DateTime
switch (op)
{
case "+":
expr = ConstantHelper.TryEvalConst(root, rightExpr, leftExpr, ExpressionType.Add);
break;
default:
throw new CompilationException("Binary operator not supported for timespan and datetime: " + op, root.ChildNodes[1]);
}
#endregion
}
else if (leftExpr.IsString() && rightExpr.IsString())
{
#region String and String
//.........这里部分代码省略.........
示例10: GetBinaryOperator
private string GetBinaryOperator(ParseTreeNode opNode)
{
string text;
opNode.RequireChildren(1, 2);
if (opNode.ChildNodes.Count == 1)
{
text = opNode.ChildNodes[0].Token.ValueString;
return text.IsKeywordAffectedByCase() ? text.ToLower() : text;
}
// handle cases "NOT LIKE", "NOT IN"
text = opNode.ChildNodes[0].Token.ValueString + ' ' + opNode.ChildNodes[1].Token.ValueString;
return text.IsKeywordAffectedByCase() ? text.ToLower() : text;
}
示例11: TopLevelAtomExpressionGeneratorForParameters
private static Expression TopLevelAtomExpressionGeneratorForParameters(ParseTreeNode root, CompilerState compilerState)
{
root.RequireChildren(0);
var pqlCompilerState = (PqlCompilerState) compilerState;
var parsedRequest = pqlCompilerState.ParsedRequest;
var names = pqlCompilerState.RequestParameters.Names;
var paramName = root.Token.ValueString;
if (string.IsNullOrEmpty(paramName) || paramName[0] != '@')
{
// convention is for handlers to return nulls for unknown atoms
return null;
}
if (names != null)
{
for (var ordinal = 0; ordinal < names.Length; ordinal++)
{
if (0 == StringComparer.OrdinalIgnoreCase.Compare(names[ordinal], paramName))
{
return GetOrAddParameterRefToCompilationContext(parsedRequest, pqlCompilerState, ordinal);
}
}
}
throw new CompilationException("Undefined parameter: " + paramName, root);
}
示例12: TopLevelAtomExpressionGeneratorForFields
private static Expression TopLevelAtomExpressionGeneratorForFields(ParseTreeNode root, CompilerState compilerState)
{
root.RequireChildren(0);
var pqlCompilerState = (PqlCompilerState) compilerState;
var descriptor = pqlCompilerState.ContainerDescriptor;
var parsedRequest = pqlCompilerState.ParsedRequest;
var fieldName = root.Token.ValueString;
var field = descriptor.TryGetField(parsedRequest.TargetEntity.DocumentType, fieldName);
if (field == null)
{
// convention is for handlers to return nulls for unknown atoms
return null;
}
return GetOrAddFieldRefToCompilationContext(parsedRequest, pqlCompilerState, field);
}