本文整理汇总了C#中ParseTreeNode.RequireChild方法的典型用法代码示例。如果您正苦于以下问题:C# ParseTreeNode.RequireChild方法的具体用法?C# ParseTreeNode.RequireChild怎么用?C# ParseTreeNode.RequireChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParseTreeNode
的用法示例。
在下文中一共展示了ParseTreeNode.RequireChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: BuildFunCallExpressionFromMethodInfo
private Expression BuildFunCallExpressionFromMethodInfo(AtomMetadata atom, ParseTreeNode root, CompilerState state)
{
// number of arguments must exactly match number of child nodes in the tree
var paramInfo = atom.MethodInfo.GetParameters();
var funArgs = root.RequireChild("exprList", 1, 0);
funArgs.RequireChildren(paramInfo.Length);
// types and order of arguments must match nodes in the tree
var args = new Expression[paramInfo.Length];
for (var i = 0; i < paramInfo.Length; i++)
{
var param = paramInfo[i];
var argNode = funArgs.ChildNodes[i];
var value = state.ParentRuntime.Analyze(argNode, state);
Expression adjusted;
if (!ExpressionTreeExtensions.TryAdjustReturnType(root, value, param.ParameterType, out adjusted))
{
throw new CompilationException(string.Format("Could not adjust parameter number {0} to invoke function {1}",
i, atom.Name), funArgs.ChildNodes[i]);
}
args[i] = adjusted;
}
return BuildFunctorInvokeExpression(atom, args);
}
示例4: BuildFunCallExpression
private Expression BuildFunCallExpression(ParseTreeNode root, CompilerState state)
{
// funCall -> Id
var idNode = root.RequireChild("Id", 0);
idNode.RequireChildren(1); // dotted identifiers are not supported for function calls
// funCall -> funArgs
root.RequireChild("funArgs", 1);
AtomMetadata atom;
var name = idNode.ChildNodes[0].Token.ValueString;
if (!m_atoms.TryGetValue(name, out atom) || atom.AtomType != AtomType.Function)
{
throw new CompilationException("Unknown function: " + name, root);
}
if (atom.MethodInfo != null)
{
return BuildFunCallExpressionFromMethodInfo(atom, root, state);
}
if (atom.ExpressionGenerator != null)
{
return atom.ExpressionGenerator(root, state);
}
// internal error, somebody screwed up with configuration of runtime
throw new Exception("ExpressionGenerator and MethodInfo are both null on atom: " + atom.Name);
}
示例5: BuildCaseStatementExpression
private Expression BuildCaseStatementExpression(CompilerState state, ParseTreeNode whenThenListNode, Expression caseDefault)
{
// now start building on top of the tail, right to left,
// also making sure that types are compatible
var tail = caseDefault;
for (var i = whenThenListNode.ChildNodes.Count - 1; i >= 0; i--)
{
var caseWhenThenNode = whenThenListNode.RequireChild("caseWhenThen", i);
caseWhenThenNode.RequireChildren(4);
var whenNode = caseWhenThenNode.RequireChild(null, 1);
var thenNode = caseWhenThenNode.RequireChild(null, 3);
if (whenNode.Term.Name == "tuple")
{
throw new CompilationException("When variable for CASE is not specified, you can only have one expression in each WHEN clause", whenNode);
}
// in this flavor of CASE, we are building a sequence of IIFs
// it requires that our "WHEN" clause is of non-nullable boolean type
var when = Analyze(whenNode, state).RemoveNullability();
when.RequireBoolean(whenNode);
var then = Analyze(thenNode, state);
// try to auto-adjust types of this "THEN" and current tail expression if needed
if (tail != null)
{
Expression adjusted;
if (ExpressionTreeExtensions.TryAdjustReturnType(thenNode, then, tail.Type, out adjusted))
{
then = adjusted;
}
else if (ExpressionTreeExtensions.TryAdjustReturnType(thenNode, tail, then.Type, out adjusted))
{
tail = adjusted;
}
else
{
throw new CompilationException(
string.Format(
"Incompatible types within CASE statement. Tail is of type {0}, and then is of type {1}",
tail.Type.FullName, then.Type.FullName), thenNode);
}
}
if (when is ConstantExpression)
{
if ((bool) ((ConstantExpression) when).Value)
{
tail = then;
}
}
else
{
tail = Expression.Condition(when, then, tail ?? ExpressionTreeExtensions.GetDefaultExpression(then.Type));
}
}
return tail;
}
示例6: ParseUpdateStatement
private void ParseUpdateStatement(ParsedRequest parsedRequest, ParseTreeNode updateStmt)
{
// get FROM entity name
var updateEntityClause = updateStmt.RequireChild("Id", 1);
parsedRequest.TargetEntity = GetTargetEntity(updateEntityClause);
// preprocess identifiers
m_preprocessor.ProcessIdentifierAliases(updateStmt, parsedRequest.TargetEntity);
var ctx = GetTreeIteratorContext(parsedRequest, m_containerDescriptor);
ctx.Functor = FieldExtractor;
var assignListClause = updateStmt.RequireChild("assignList", 3);
foreach (var assignClause in assignListClause.ChildNodes)
{
var field = TryGetFieldByIdentifierNode(assignClause.RequireChild("Id", 0), m_containerDescriptor, parsedRequest.TargetEntity.DocumentType);
if (field == null)
{
throw new CompilationException("Attempting to SET an unknown field", assignClause);
}
if (parsedRequest.Modify.ModifiedFields.Contains(field))
{
throw new CompilationException("A field can be assigned by UPDATE only once: " + field.Name, null);
}
var expressionClause = assignClause.RequireChild(null, 2);
parsedRequest.Modify.ModifiedFields.Add(field);
parsedRequest.Modify.InsertUpdateSetClauses.Add(expressionClause);
// iterator callback will place all referenced column IDs into "select" list
// later on, "select" list is used along with "where" list to create driver fetch list
ctx.Argument = 1;
IterateTree(expressionClause, 0, ctx);
}
// get field names for where clause
var whereClause = updateStmt.TryGetChild("whereClauseOpt", 4);
ParseWhereClause(whereClause, ctx);
// get field names for order clause
ctx.ParsedRequest.BaseDataset.OrderClause = updateStmt.TryGetChild("orderList", 5, 2);
ParseOrderClause(ctx);
}
示例7: CompileInt32ParamExtractorForPaging
private static Func<DriverRowData, int> CompileInt32ParamExtractorForPaging(ParsedRequest parsedRequest, ParseTreeNode parseTreeNode, int defaultValue)
{
var paramName = parseTreeNode.RequireChild("id_simple", 0).Token.ValueString;
if (string.IsNullOrEmpty(paramName) || paramName[0] != '@')
{
throw new CompilationException("Values for paging clause must be Int32 constants or Int32 parameters", parseTreeNode);
}
var names = parsedRequest.Params.Names;
if (names != null)
{
for (var paramOrdinal = 0; paramOrdinal < names.Length; paramOrdinal++)
{
if (StringComparer.OrdinalIgnoreCase.Equals(names[paramOrdinal], paramName))
{
if (parsedRequest.Params.DataTypes[paramOrdinal] != DbType.Int32)
{
throw new CompilationException(string.Format(
"Parameter {0} must be of type Int32 for use in paging options. Actual type: {1}",
paramName, parsedRequest.Params.DataTypes[paramOrdinal])
, parseTreeNode);
}
var localOrdinal = parsedRequest.Params.OrdinalToLocalOrdinal[paramOrdinal];
return data => BitVector.Get(data.NotNulls, localOrdinal) ? data.GetInt32(localOrdinal) : defaultValue;
}
}
}
throw new CompilationException("Unknown parameter: " + paramName, parseTreeNode);
}
示例8: ParseInsertStatement
private void ParseInsertStatement(ParsedRequest parsedRequest, ParseTreeNode insertStmt)
{
// get FROM entity name
var insertEntityClause = insertStmt.RequireChild("Id", 2);
parsedRequest.TargetEntity = GetTargetEntity(insertEntityClause);
// preprocess identifiers
m_preprocessor.ProcessIdentifierAliases(insertStmt, parsedRequest.TargetEntity);
var insertFieldsListClause = insertStmt.RequireChild("idList", 3, 0);
var valueListClause = insertStmt.RequireChild("insertTuplesList", 4, 1);
if (valueListClause.ChildNodes.Count != 1)
{
throw new CompilationException("Multi-tuple explicit list of values for INSERT is not yet supported");
}
valueListClause = valueListClause.ChildNodes[0];
if (insertFieldsListClause.ChildNodes.Count != valueListClause.ChildNodes.Count)
{
throw new CompilationException("Number of fields in INSERT clause must match number of expressions in VALUES clause", insertStmt);
}
parsedRequest.TargetEntityPkField = m_containerDescriptor.RequireField(parsedRequest.TargetEntity.DocumentType, parsedRequest.TargetEntity.PrimaryKeyFieldName);
for (var ordinal = 0; ordinal < insertFieldsListClause.ChildNodes.Count; ordinal++)
{
var insertFieldNode = insertFieldsListClause.ChildNodes[ordinal];
var field = TryGetFieldByIdentifierNode(insertFieldNode, m_containerDescriptor, parsedRequest.TargetEntity.DocumentType);
if (field == null)
{
throw new CompilationException("Attempting to INSERT into an unknown field", insertFieldNode);
}
if (ReferenceEquals(field, parsedRequest.TargetEntityPkField))
{
parsedRequest.OrdinalOfPrimaryKey = ordinal;
}
if (parsedRequest.Modify.ModifiedFields.Contains(field))
{
throw new CompilationException("A field can be assigned by INSERT only once: " + field.Name, insertFieldNode);
}
var expressionClause = valueListClause.ChildNodes[ordinal];
// update clauses list will hold value expressions
parsedRequest.Modify.ModifiedFields.Add(field);
parsedRequest.Modify.InsertUpdateSetClauses.Add(expressionClause);
}
if (parsedRequest.OrdinalOfPrimaryKey < 0)
{
throw new CompilationException("Value for primary key field must be specified: " + parsedRequest.TargetEntityPkField.Name);
}
}
示例9: ParseDeleteStatement
private void ParseDeleteStatement(ParsedRequest parsedRequest, ParseTreeNode deleteStmt)
{
// get FROM entity name
var deleteFromClause = deleteStmt.RequireChild("Id", 2);
parsedRequest.TargetEntity = GetTargetEntity(deleteFromClause);
// preprocess identifiers
m_preprocessor.ProcessIdentifierAliases(deleteStmt, parsedRequest.TargetEntity);
var ctx = GetTreeIteratorContext(parsedRequest, m_containerDescriptor);
ctx.Functor = FieldExtractor;
// get field names for where clause
var whereClause = deleteStmt.TryGetChild("whereClauseOpt", 3);
ParseWhereClause(whereClause, ctx);
// get field names for order clause
ctx.ParsedRequest.BaseDataset.OrderClause = deleteStmt.TryGetChild("orderList", 4, 2);
ParseOrderClause(ctx);
}
示例10: GetTargetEntity
private DocumentTypeDescriptor GetTargetEntity(ParseTreeNode parseTreeNode)
{
var targetEntityName = parseTreeNode.RequireChild("id_simple", 0).Token.ValueString;
try
{
var docType = m_containerDescriptor.RequireDocumentTypeName(targetEntityName);
return m_containerDescriptor.RequireDocumentType(docType);
}
catch (ArgumentException)
{
throw new CompilationException("Unknown entity: " + targetEntityName, parseTreeNode);
}
}
示例11: ReadPagingOptions
private static void ReadPagingOptions(ParseTreeNode selectStmt, ParsedRequest parsedRequest)
{
var clause = selectStmt.RequireChild("pageOffset", 10);
if (clause != null && clause.ChildNodes.Count > 0)
{
var param = clause.TryGetChild("Id", 1);
if (param != null)
{
parsedRequest.BaseDataset.Paging.Offset = CompileInt32ParamExtractorForPaging(parsedRequest, param, 0);
}
else
{
var number = clause.RequireChild("number", 1);
int value;
if (!int.TryParse(number.Token.ValueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)
|| value < 0)
{
throw new CompilationException("Invalid value for page offset", number);
}
parsedRequest.BaseDataset.Paging.Offset = x => value;
}
}
clause = selectStmt.RequireChild("pageSize", 9);
if (clause != null && clause.ChildNodes.Count > 0)
{
var param = clause.TryGetChild("Id", 1);
if (param != null)
{
parsedRequest.BaseDataset.Paging.PageSize = CompileInt32ParamExtractorForPaging(parsedRequest, param, Int32.MaxValue);
}
else
{
var number = clause.RequireChild("number", 1);
int value;
if (!int.TryParse(number.Token.ValueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)
|| value < -1)
{
throw new CompilationException("Invalid value for page size", number);
}
if (value != -1)
{
parsedRequest.BaseDataset.Paging.PageSize = x => value;
}
}
}
}
示例12: PredefinedAtom_RowNumberInPage
private static Expression PredefinedAtom_RowNumberInPage(ParseTreeNode root, CompilerState compilerState)
{
var funArgs = ExpressionTreeExtensions.UnwindTupleExprList(root.RequireChild("funArgs", 1));
funArgs.RequireChildren(0);
return Expression.Field(compilerState.Context, "RowNumberInOutput");
}
示例13: OrderFieldExtractor
private static bool OrderFieldExtractor(TreeIteratorContext ctx, ParseTreeNode node)
{
if (0 == StringComparer.Ordinal.Compare(node.Term.Name, "orderMember"))
{
var idsimple = node.RequireChild("Id", 0).RequireChild("id_simple", 0);
var fieldName = idsimple.Token.ValueString;
var field = ctx.ContainerDescriptor.TryGetField(ctx.ParsedRequest.TargetEntity.DocumentType, fieldName);
if (field == null)
{
throw new CompilationException("Unknown field: " + fieldName, node);
}
var descOpt = node.TryGetChild(null, 1, 0);
var descending = descOpt != null;
if (descending)
{
if (0 == StringComparer.Ordinal.Compare(descOpt.Term.Name, "ASC"))
{
descending = false;
}
else if (0 != StringComparer.Ordinal.Compare(descOpt.Term.Name, "DESC"))
{
throw new CompilationException("Invalid sort order option: " + descOpt.Token.Text, descOpt);
}
}
if (ctx.ParsedRequest.BaseDataset.OrderClauseFields.Count > 0)
{
throw new CompilationException("Cannot sort by more than one field at this time", node);
}
if (0 <= ctx.ParsedRequest.BaseDataset.OrderClauseFields.FindIndex(x => x.Item1 == field.FieldId))
{
throw new CompilationException("Duplicate order field: " + field.Name, node);
}
ctx.ParsedRequest.BaseDataset.OrderClauseFields.Add(new Tuple<int, bool>(field.FieldId, descending));
// no need to traverse deeper into the Id non-terminal
return false;
}
return true;
}
示例14: ParseSelectStatement
private void ParseSelectStatement(ParsedRequest parsedRequest, ParseTreeNode selectStmt)
{
// get FROM entity name
var fromClause = selectStmt.RequireChild("fromClauseOpt", 4);
var fromEntityNode = fromClause.RequireChild("Id", 1, 0);
parsedRequest.TargetEntity = GetTargetEntity(fromEntityNode);
var ctx = GetTreeIteratorContext(parsedRequest, m_containerDescriptor);
ctx.Functor = FieldExtractor;
// get field names for select clause
var selectColumnItemList = selectStmt.TryGetChild(null, 2, 0);
if (selectColumnItemList == null)
{
throw new CompilationException("Could not find select clause", selectStmt);
}
if (0 == StringComparer.Ordinal.Compare("columnItemList", selectColumnItemList.Term.Name))
{
// iterator callback will place all referenced column IDs into "select" list
// later on, "select" list is used along with "where" list to create driver fetch list
ctx.Argument = 1;
foreach (var columnItem in selectColumnItemList.ChildNodes)
{
// column or column with alias
columnItem.RequireChildren(1, 2);
var columnSource = columnItem.RequireChild("columnSource", 0);
// store a reference to every column item and its alias,
// since we may need to compile it into an expression or do something else later
parsedRequest.Select.SelectClauses.Add(columnItem);
// preprocess identifiers
m_preprocessor.ProcessIdentifierAliases(columnSource, parsedRequest.TargetEntity);
// extract field names from a regular select clause (but do not look into "as" alias)
IterateTree(columnSource, 0, ctx);
}
}
else if (selectColumnItemList.ChildNodes.Count == 0 && selectColumnItemList.Token != null && "*".Equals(selectColumnItemList.Token.Text))
{
// they ask for all fields (wildcard)
// let's extract only unpacked blobs
var docTypeDescriptor = parsedRequest.TargetEntity;
foreach (var field in m_containerDescriptor.EnumerateFields().Where(x => x.OwnerDocumentType == docTypeDescriptor.DocumentType))
{
ctx.ParsedRequest.Select.SelectFields.Add(field);
ctx.ParsedRequest.Select.SelectClauses.Add(m_simpleFieldAccessorNodes[field.FieldId]);
}
}
else
{
throw new CompilationException("Must have list of columns or an asterisk in select clause", selectColumnItemList);
}
// get field names for where clause
var whereClause = selectStmt.TryGetChild("whereClauseOpt", 5);
ParseWhereClause(whereClause, ctx);
// get field names for order clause
ctx.ParsedRequest.BaseDataset.OrderClause = selectStmt.TryGetChild("orderList", 8, 2);
ParseOrderClause(ctx);
ReadPagingOptions(selectStmt, ctx.ParsedRequest);
}
示例15: 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);
}
}
}