Esempio n. 1
0
        public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
        {
            var name = node.Identifier.Text;

            cb.Append("enum class ").AppendLine(name)
            .Scope(() =>
            {
                base.VisitEnumDeclaration(node);
            });

            // now let's generate ostream& operator <<
            cb.AppendLine("std::ostream& operator<<(std::ostream& os, const " + name + " obj)")
            .Scope(() =>
            {
                cb.AppendLineWithIndent("switch (obj)")
                .Scope(() =>
                {
                    foreach (var enumCase in node.Members)
                    {
                        string caseName = enumCase.Identifier.Text;
                        // todo: string output needs to parse camelCase => "camel case"
                        cb.AppendWithIndent("case " + name + "::" + caseName + ": ")
                        .AppendLine("os << \"" + Regex.Replace(caseName, @"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1").ToLowerInvariant() + "\"; break;");
                    }
                });
                cb.AppendLineWithIndent("return os;");
            });
        }
Esempio n. 2
0
        public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            cb.AppendIndent()
            .Append(node.Identifier.ToString())
            .Append("::")
            .Append(node.Identifier.ToString())
            .Append("(");

            if (node.ParameterList != null)
            {
                var pars = node.ParameterList.Parameters.ToList();
                for (int i = 0; i < pars.Count; ++i)
                {
                    var p = pars[i];
                    var z = model.GetDeclaredSymbol(p);
                    cb.Append(ArgumentTypeFor(z.Type))
                    .Append(" ")
                    .Append(p.Identifier.Text);
                    if (i + 1 != pars.Count)
                    {
                        cb.Append(", ");
                    }
                }
            }

            cb.Append(")");

            var parent = node.Parent as ClassDeclarationSyntax;

            if (parent != null && parent.HasInitializableMembers(model))
            {
                cb.AppendLine(" :");
                cb.Indent(() => AppendFieldInitializers(parent));
            }
            else
            {
                cb.AppendLine();
            }

            cb.AppendLineWithIndent("{");
            cb.Indent(() => base.VisitConstructorDeclaration(node));
            cb.AppendLineWithIndent("}");
        }