Example #1
0
        public static Instance FromAstNode(InstanceValueDeclarationAst node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            var instance = new Instance
            {
                ClassName = node.TypeName.Name,
                Alias     = node?.Alias?.Name
            };

            foreach (var property in node.PropertyValues.PropertyValues)
            {
                var propertyValue = property.Value;
                if ((propertyValue as LiteralValueArrayAst) != null)
                {
                    var itemValues = ((LiteralValueArrayAst)propertyValue).Values
                                     .Select(Instance.GetLiteralValue)
                                     .ToArray();
                    instance.Properties.Add(property.Key, itemValues);
                }
                else if ((propertyValue as LiteralValueAst) != null)
                {
                    instance.Properties.Add(property.Key, Instance.GetLiteralValue((LiteralValueAst)propertyValue));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            return(instance);
        }
        public static string ConvertInstanceValueDeclarationAst(InstanceValueDeclarationAst node, MofQuirks quirks = MofQuirks.None, string indent = "")
        {
            // instance of myType as $Alias00000070
            // {
            //     Reference = TRUE;
            // };
            var source = new StringBuilder();

            // instance of myType as $Alias00000070
            source.Append(node.Instance.Extent.Text);
            source.Append(" ");
            source.Append(node.Of.Extent.Text);
            source.Append(" ");
            source.Append(node.TypeName.Name);
            if (node.Alias != null)
            {
                source.Append(" ");
                source.Append(node.As.Extent.Text);
                source.Append(" ");
                source.Append("$");
                source.Append(node.Alias.Name);
            }
            source.AppendLine();
            // {
            //     Reference = TRUE;
            // }
            source.Append(AstMofGenerator.ConvertPropertyValueListAst(node.PropertyValues, quirks, indent));
            // ;
            source.Append(node.StatementEnd.Extent.Text);
            return(source.ToString());
        }