/// <summary> /// propertyName parameter must be of type generic list /// </summary> public ISyntaxNode Create(ISyntaxNode concept, string propertyName, Type child) { ISyntaxNode item = (ISyntaxNode)Activator.CreateInstance(child); item.Parent = concept; IList list; PropertyInfo property = concept.GetType().GetProperty(propertyName); if (property.IsOptional()) { IOptional optional = (IOptional)property.GetValue(concept); list = (IList)optional.Value; if (list == null) { Type listType = property.PropertyType.GetProperty("Value").PropertyType; list = (IList)Activator.CreateInstance(listType); optional.Value = list; } } else { list = (IList)property.GetValue(concept); if (list == null) { Type listType = property.PropertyType; list = (IList)Activator.CreateInstance(listType); property.SetValue(concept, list); } } list.Add(item); return(item); }
private IAssemblyConcept SelectAssemblyReference(ISyntaxNode concept, string propertyName, Visual control) { // get scope provider IScopeProvider scopeProvider = SyntaxTreeManager.GetScopeProvider(concept.GetType()); if (scopeProvider == null) { return(null); } // get references in the scope IEnumerable <ISyntaxNode> scope = scopeProvider.Scope(concept, propertyName); if (scope == null || scope.Count() == 0) { return(null); } // build tree view TreeNodeViewModel viewModel = SyntaxNodeExtensions.BuildAssemblySelectorTree(scope); // open dialog window PopupWindow dialog = new PopupWindow(control, viewModel); _ = dialog.ShowDialog(); if (dialog.Result == null) { return(null); } // return selected reference return(dialog.Result.NodePayload as IAssemblyConcept); }
public static PropertyInfo GetPropertyInfo(this ISyntaxNode @this, string propertyName) { Type metadata = @this.GetType(); PropertyInfo property = metadata.GetProperty(propertyName); return(property); }
private static VisitorAction Leave( ISyntaxNodeVisitor visitor, ISyntaxNode node, ISyntaxNode parent, IReadOnlyList <object> path, IReadOnlyList <ISyntaxNode> ancestors) { if (_leaveVisitors.TryGetValue(node.GetType(), out IntVisitorFn v)) { return(v.Invoke(visitor, node, parent, path, ancestors)); } return(VisitorAction.Default); }
private IConceptLayout GetLayout(ISyntaxNode concept) { if (concept == null) { throw new ArgumentNullException(nameof(concept)); } Type type = concept.GetType(); if (_layouts.TryGetValue(type, out IConceptLayout layout)) { return(layout); } return(null); }
public IdentifierNodeViewModel(ISyntaxNodeViewModel owner, ISyntaxNode model) : base(owner, model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } Type metadata = model.GetType(); if (!metadata.GetInterfaces().Contains(typeof(IIdentifiable))) { throw new InvalidOperationException("Interface \"IIdentifiable\" is not found!"); } _identifiable = (IIdentifiable)model; }
public static ISyntaxNode Ancestor <T>(this ISyntaxNode @this) { Type ancestorType = typeof(T); ISyntaxNode ancestor = @this.Parent; while (ancestor != null) { if (ancestor.GetType() != ancestorType) { ancestor = ancestor.Parent; } else { break; } } return(ancestor); }
private static (object, FunnyType) ParseSyntaxNode(ISyntaxNode syntaxNode) { if (syntaxNode == null) { throw new ArgumentException(); } if (syntaxNode is ConstantSyntaxNode constant) { return(ParseConstant(constant)); } if (syntaxNode is GenericIntSyntaxNode intGeneric) { return(ParseGenericIntConstant(intGeneric)); } if (syntaxNode is ArraySyntaxNode array) { var items = new List <object>(array.Expressions.Count); FunnyType?unifiedType = null; foreach (var child in array.Expressions) { var(value, childVarType) = ParseSyntaxNode(child); if (!unifiedType.HasValue) { unifiedType = childVarType; } else if (unifiedType != childVarType) { unifiedType = FunnyType.Anything; } items.Add(value); } if (!items.Any()) { return(new object[0], FunnyType.ArrayOf(FunnyType.Anything)); } return(items.ToArray(), FunnyType.ArrayOf(unifiedType.Value)); } throw new NotSupportedException($"syntax node {syntaxNode.GetType().Name} is not supported"); }
public static TypeConstraint GetTypeConstraints(IEnumerable <LanguageConcept> languages, ISyntaxNode concept, string propertyName) { if (concept == null) { throw new ArgumentNullException(nameof(concept)); } if (string.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentNullException(nameof(propertyName)); } Type metadata = concept.GetType(); PropertyInfo property = metadata.GetProperty(propertyName); if (property == null) { throw new ArgumentOutOfRangeException(nameof(property)); } return(GetTypeConstraints(languages, property)); }
protected override ISyntaxVisitorAction Enter( ISyntaxNode node, ISyntaxVisitorContext context) { switch (node.Kind) { case SyntaxKind.Name: return(Enter((NameNode)node, context)); case SyntaxKind.Document: return(Enter((DocumentNode)node, context)); case SyntaxKind.OperationDefinition: return(Enter((OperationDefinitionNode)node, context)); case SyntaxKind.VariableDefinition: return(Enter((VariableDefinitionNode)node, context)); case SyntaxKind.Variable: return(Enter((VariableNode)node, context)); case SyntaxKind.SelectionSet: return(Enter((SelectionSetNode)node, context)); case SyntaxKind.Field: return(Enter((FieldNode)node, context)); case SyntaxKind.Argument: return(Enter((ArgumentNode)node, context)); case SyntaxKind.FragmentSpread: return(Enter((FragmentSpreadNode)node, context)); case SyntaxKind.InlineFragment: return(Enter((InlineFragmentNode)node, context)); case SyntaxKind.FragmentDefinition: return(Enter((FragmentDefinitionNode)node, context)); case SyntaxKind.Directive: return(Enter((DirectiveNode)node, context)); case SyntaxKind.NamedType: return(Enter((NamedTypeNode)node, context)); case SyntaxKind.ListType: return(Enter((ListTypeNode)node, context)); case SyntaxKind.NonNullType: return(Enter((NonNullTypeNode)node, context)); case SyntaxKind.ListValue: return(Enter((ListValueNode)node, context)); case SyntaxKind.ObjectValue: return(Enter((ObjectValueNode)node, context)); case SyntaxKind.ObjectField: return(Enter((ObjectFieldNode)node, context)); case SyntaxKind.BooleanValue: case SyntaxKind.EnumValue: case SyntaxKind.FloatValue: case SyntaxKind.IntValue: case SyntaxKind.NullValue: case SyntaxKind.StringValue: return(Enter((IValueNode)node, context)); case SyntaxKind.SchemaDefinition: return(Enter((SchemaDefinitionNode)node, context)); case SyntaxKind.OperationTypeDefinition: return(Enter((OperationTypeDefinitionNode)node, context)); case SyntaxKind.ScalarTypeDefinition: return(Enter((ScalarTypeDefinitionNode)node, context)); case SyntaxKind.ObjectTypeDefinition: return(Enter((ObjectTypeDefinitionNode)node, context)); case SyntaxKind.FieldDefinition: return(Enter((FieldDefinitionNode)node, context)); case SyntaxKind.InputValueDefinition: return(Enter((InputValueDefinitionNode)node, context)); case SyntaxKind.InterfaceTypeDefinition: return(Enter((InterfaceTypeDefinitionNode)node, context)); case SyntaxKind.UnionTypeDefinition: return(Enter((UnionTypeDefinitionNode)node, context)); case SyntaxKind.EnumTypeDefinition: return(Enter((EnumTypeDefinitionNode)node, context)); case SyntaxKind.EnumValueDefinition: return(Enter((EnumValueDefinitionNode)node, context)); case SyntaxKind.InputObjectTypeDefinition: return(Enter((InputObjectTypeDefinitionNode)node, context)); case SyntaxKind.DirectiveDefinition: return(Enter((DirectiveDefinitionNode)node, context)); case SyntaxKind.SchemaExtension: return(Enter((SchemaExtensionNode)node, context)); case SyntaxKind.ScalarTypeExtension: return(Enter((ScalarTypeExtensionNode)node, context)); case SyntaxKind.ObjectTypeExtension: return(Enter((ObjectTypeExtensionNode)node, context)); case SyntaxKind.InterfaceTypeExtension: return(Enter((InterfaceTypeExtensionNode)node, context)); case SyntaxKind.UnionTypeExtension: return(Enter((UnionTypeExtensionNode)node, context)); case SyntaxKind.EnumTypeExtension: return(Enter((EnumTypeExtensionNode)node, context)); case SyntaxKind.InputObjectTypeExtension: return(Enter((InputObjectTypeExtensionNode)node, context)); default: throw new NotSupportedException(node.GetType().FullName); } }
/// <summary> /// Gets property type constraints for the given concept's property (derived from SyntaxNode only) /// </summary> public static IEnumerable <ISyntaxNode> GetPropertyTypeConstraints(ISyntaxNode concept, string propertyName) { List <ISyntaxNode> constraints = new List <ISyntaxNode>(); Type metadata = concept.GetType(); PropertyInfo property = metadata.GetProperty(propertyName); TypeConstraintAttribute typeConstraint = property.GetCustomAttribute <TypeConstraintAttribute>(); SimpleTypeConstraintAttribute simpleTypeConstraint = property.GetCustomAttribute <SimpleTypeConstraintAttribute>(); if (simpleTypeConstraint != null) { constraints.AddRange(SimpleTypes.References); } if (typeConstraint != null) { foreach (Type type in typeConstraint.Types) { if (type.IsAbstract) { foreach (Type subclass in GetSubclassesOfType(type)) { constraints.Add((ISyntaxNode)Activator.CreateInstance(subclass)); } } else { if (type.IsClass && type.IsSubclassOf(typeof(SyntaxNode))) { constraints.Add((ISyntaxNode)Activator.CreateInstance(type)); } } } } if (property.IsOptional()) { property = property.PropertyType.GetProperty("Value"); } Type propertyType; if (property.IsList()) // looking for List<T> { propertyType = property.PropertyType.GenericTypeArguments[0]; } else { propertyType = property.PropertyType; } if (propertyType.IsAbstract) { foreach (Type subclass in GetSubclassesOfType(propertyType)) { constraints.Add((ISyntaxNode)Activator.CreateInstance(subclass)); } } else { if (propertyType.IsClass && propertyType.IsSubclassOf(typeof(SyntaxNode))) { constraints.Add((ISyntaxNode)Activator.CreateInstance(propertyType)); } } return(constraints); }
public static void SetConceptProperty(ISyntaxNode concept, string propertyName, object value) { PropertyInfo property = concept.GetPropertyInfo(propertyName); if (property == null) { throw new NullReferenceException($"Property \"{propertyName}\" of the \"{concept.GetType()}\" type is not found!"); } if (property.IsRepeatable()) { throw new InvalidOperationException($"Property \"{propertyName}\" can not be repeatable!"); } if (property.IsOptional()) { IOptional optional = (IOptional)property.GetValue(concept); optional.Value = value; } else { property.SetValue(concept, value); } }