예제 #1
0
        /// <summary>
        /// Generates a struct definition with the specified name and fields.
        /// If the struct is to contain no fields pass null for the fields parameter.
        /// </summary>
        /// <param name="name">The name of the struct to be created.</param>
        /// <param name="fields">Optional list of fields.  Pass null if the struct has no fields.</param>
        /// <returns>Root node for this struct definition's AST.</returns>
        public static Node Generate(string name, IReadOnlyList <StructFieldDef> fields)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(nameof(name));
            }

            if (fields != null && fields.Count == 0)
            {
                throw new ArgumentException("pass null for no struct fields");
            }

            var typeDef = new TypeDef();

            typeDef.AddChild(new Identifier(name));

            var structDef = new StructDef();
            var defStart  = new OpenDelimiter(BinaryDelimiterType.Brace);

            if (fields != null)
            {
                foreach (var field in fields)
                {
                    var id = new Identifier(field.Name);
                    id.AddChild(new TypeName(field.TypeName));

                    if (field.Tag != null)
                    {
                        id.AddChild(new Tag(field.Tag));
                    }

                    defStart.AddChild(id);
                }
            }

            defStart.AddClosingDelimiter();
            structDef.AddChild(defStart);

            typeDef.AddChild(structDef);
            return(typeDef);
        }