public void WalkDeclarations(object tree, Context context)
        {
            var t = (CommonTree)tree;
            switch (t.Type)
            {
                case MessageContractsLexer.FragmentEntry:
                    var fragmentId = t.GetChild(0).Text;
                    var fragmentType = t.GetChild(1).Text;
                    var fragmentName = t.GetChild(2).Text;
                    context.Fragments[fragmentId] = new Fragment(fragmentType, fragmentName);
                    break;
                case MessageContractsLexer.ModifierDefinition:
                    Console.WriteLine(t);

                    var modifier = t.GetChild(1).Text;
                    var type = t.GetChild(0).Text;
                    context.CurrentEntity.Modifiers[modifier] = type;
                    break;

                case MessageContractsLexer.TypeToken:
                    var name = t.GetChild(0).Text;
                    var block = t.GetChild(1);

                    var modifiers = new List<Modifier>();
                    if (t.ChildCount>2)
                    {
                        var mod = t.GetChild(2).Text;
                        string typeName;
                        if (!context.CurrentEntity.Modifiers.TryGetValue(mod, out typeName))
                        {
                            var format = string.Format("Entity '{0}' does not have modifier reference: '{1}'", context.CurrentEntity.Name, mod);
                            throw new InvalidOperationException(format);
                        }
                        modifiers.Add(new Modifier(mod, typeName));
                    }

                    var message = new Message(name, modifiers);
                    if (modifiers.Any())
                    {
                        // only commands and events have modifiers
                        foreach (var member in context.Entities.Peek().FixedMembers)
                        {
                            message.Members.Add(member);
                        }
                    }

                    for (int i = 0; i < block.ChildCount; i++)
                    {
                        message.Members.AddRange(WalkContractMember(block.GetChild(i), context));
                    }

                    context.Contracts.Add(message);
                    context.CurrentEntity.Messages.Add(message);

                    break;

                case MessageContractsLexer.EntityDefinition:
                    var entityName = t.GetChild(0).Text;
                    var entityBlock = t.GetChild(1);

                    var entity = new Entity(entityName);
                    for (int i = 0; i < entityBlock.ChildCount; i++)
                    {
                        entity.FixedMembers.AddRange(WalkContractMember(entityBlock.GetChild(i), context));
                    }

                    context.Entities.Push(entity);

                    break;
                default:
                    throw new InvalidOperationException("Unexpected token: " + t.Text);
            }
        }
        public void WalkDeclarations(ITree tree, Context context)
        {
            var t = tree;
            switch (t.Type)
            {
                case MessageContractsLexer.FragmentEntry:
                    var fragmentId = t.GetChild(0).Text;
                    var fragmentType = t.GetChild(1).Text;
                    var fragmentName = t.GetChild(2).Text;
                    context.Fragments[fragmentId] = new Fragment(fragmentType, fragmentName);
                    break;
                case MessageContractsLexer.ModifierDefinition:
                    var modifier = t.GetChild(0).Text;
                    context.CurrentEntity.Modifiers.Remove(modifier);
                    for (int i = 1; i < t.ChildCount; i++)
                    {
                        context.CurrentEntity.Modifiers.Add(modifier, t.GetChild(i).Text);
                    }
                    break;
                case MessageContractsLexer.TypeToken:
                    var name = t.GetChild(0).Text;
                    var block = t.GetChild(1);

                    var modifiers = new List<Modifier>();
                    for (int i = 2; i < t.ChildCount; i++)
                    {
                        var mod = t.GetChild(i).Text;
                        var used = context.CurrentEntity.Modifiers.GetValues(mod);
                        if (null == used || used.Length == 0)
                        {
                            var format = string.Format("Entity '{0}' does not have modifier reference: '{1}'",
                                context.CurrentEntity.Name, mod);
                            throw new InvalidOperationException(format);
                        }
                        foreach (var s in used)
                        {
                            modifiers.Add(new Modifier(mod, s));
                        }
                    }

                    var message = new Message(name, modifiers);
                    if (modifiers.Any())
                    {
                        // only commands and events have modifiers
                        foreach (var member in context.Entities.Peek().FixedMembers)
                        {
                            message.Members.Add(member);
                        }
                    }

                    for (int i = 0; i < block.ChildCount; i++)
                    {
                        var members = WalkContractMember(block.GetChild(i), context).ToArray();
                        message.Members.AddRange(members.Where(m => m.Kind == Member.Kinds.Field));

                        var stringRepresentations =
                            members.Where(m => m.Kind == Member.Kinds.StringRepresentation).ToArray();
                        switch (stringRepresentations.Length)
                        {
                            case 0:
                                break;
                            case 1:
                                message.StringRepresentation = stringRepresentations[0].Name;
                                break;
                            default:
                                throw new InvalidOperationException("Only one string representation per message");
                        }
                    }

                    context.Contracts.Add(message);
                    context.CurrentEntity.Messages.Add(message);

                    break;

                case MessageContractsLexer.EntityDefinition:
                    var entityName = t.GetChild(0).Text;
                    var entityBlock = t.GetChild(1);

                    var entity = new Entity(entityName);
                    for (int i = 0; i < entityBlock.ChildCount; i++)
                    {
                        entity.FixedMembers.AddRange(WalkContractMember(entityBlock.GetChild(i), context));
                    }
                    context.PushEntity(entity);

                    for (int i = 2; i < t.ChildCount; i++)
                    {
                        WalkDeclarations(t.GetChild(i), context);
                    }
                    break;
                case MessageContractsLexer.NamespaceToken:
                    var ns = string.Join(".", t.Children().Select(s => s.Text));
                    context.CurrentNamespace = ns;
                    break;
                case MessageContractsLexer.ExternToken:
                    var text = t.GetChild(0).Text;
                    context.CurrentExtern = text;
                    break;
                case MessageContractsLexer.UsingToken:
                    var us = string.Join(".", t.Children().Select(s => s.Text));
                    context.Using.Add(us);
                    break;
                default:
                    throw new InvalidOperationException("Unexpected token: " + t.Text);
            }
        }
Exemplo n.º 3
0
 void WriteParameters(Message message, CodeWriter writer)
 {
     var first = true;
     foreach (var member in message.Members)
     {
         if (first)
         {
             first = false;
         }
         else
         {
             writer.Write(", ");
         }
         writer.Write("{0} {1}", member.Type, GeneratorUtil.ParameterCase(member.Name));
     }
 }
Exemplo n.º 4
0
 void WriteParametersForFactoryMethod(Message message, CodeWriter writer)
 {
     var first = true;
     foreach (var member in message.Members)
     {
         if (first)
         {
             first = false;
         }
         else
         {
             writer.Write(", ");
         }
         writer.Write("{0} {1}", member.Type.Replace(typeof(Guid).Name, typeof(string).Name), GeneratorUtil.ParameterCase(member.Name));
     }
 }
Exemplo n.º 5
0
        void WriteMembers(Message message, CodeWriter writer)
        {
            var idx = 1;
            foreach (var member in message.Members)
            {
                writer.WriteLine(MemberTemplate, idx, member.Type, GeneratorUtil.MemberCase(member.Name));

                idx += 1;
            }
        }
Exemplo n.º 6
0
        void WriteNewObjectArguments(Message message, CodeWriter writer)
        {
            var separator = "";

            foreach (var member in message.Members)
            {
                var toWrite = member.Type == typeof(Guid).Name
                    ? "Guid.Parse(" + GeneratorUtil.ParameterCase(member.Name) + ")"
                    :   GeneratorUtil.ParameterCase(member.Name);

                writer.Write("{0}{1}",separator, toWrite);
                separator = ", ";
            }
        }
Exemplo n.º 7
0
 void WriteInterfaceMembers(Message message, CodeWriter writer)
 {
     foreach (var member in message.Members)
     {
         writer.WriteLine("{0} {1} {{ get; }}", member.Type,GeneratorUtil.MemberCase(member.Name));
     }
 }
Exemplo n.º 8
0
        private void WriteInteropData(CodeWriter writer, Message contract)
        {
            writer.WriteLine("public partial interface IMessageFactory");
            writer.WriteLine("{");
            writer.Indent += 1;
            writer.Write("{0} Create{0} (", contract.Name);
            WriteParametersForFactoryMethod(contract, writer);
            writer.WriteLine(");");
            writer.Indent -= 1;
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("public partial class MessageFactory");
            writer.WriteLine("{");
            writer.Indent += 1;
            writer.Write("public {0} Create{0} (", contract.Name);
            WriteParametersForFactoryMethod(contract, writer);
            writer.WriteLine(")");
            writer.WriteLine("{");
            writer.Indent += 1;
            writer.Write("return new {0}(", contract.Name);
            WriteNewObjectArguments(contract, writer);
            writer.WriteLine(");");
            writer.Indent -= 1;
            writer.WriteLine("}");
            writer.Indent -= 1;
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("[ComVisible(true)]");
            writer.WriteLine(contract.InteropData.ComInterfaceGuidAttribute);
            writer.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]");
            writer.WriteLine("public partial interface I{0}", contract.Name);
            writer.WriteLine("{");
            writer.Indent += 1;
            if (contract.Members.Count > 0)
            {
                WriteInterfaceMembers(contract, writer);
            }
            writer.Indent -= 1;
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("[ComVisible(true)]");
            writer.WriteLine(contract.InteropData.ClassGuidAttribute);
            writer.WriteLine("[ClassInterface(ClassInterfaceType.None)]");
        }
Exemplo n.º 9
0
 void WriteAssignments(Message message, CodeWriter writer)
 {
     foreach (var member in message.Members)
     {
         writer.WriteLine("{0} = {1};", GeneratorUtil.MemberCase(member.Name), GeneratorUtil.ParameterCase(member.Name));
     }
 }
Exemplo n.º 10
0
        static void WriteToString(CodeWriter writer, Message contract)
        {
            if (string.IsNullOrWhiteSpace(contract.StringRepresentation))
                return;

            writer.WriteLine();
            writer.WriteLine("public override string ToString()");
            writer.WriteLine("{");
            writer.Indent += 1;

            var text = contract.StringRepresentation;
            var active = new List<string>();
            foreach (var member in contract.Members)
            {
                text = ReplaceAdd(text, "{" + member.DslName + ":", "{" + active.Count + ":", active, member);
                text = ReplaceAdd(text, "{" + member.DslName + "}", "{" + active.Count + "}", active, member);

                if (member.DslName != member.Name)
                {
                    text = ReplaceAdd(text, "{" + member.Name + ":", "{" + active.Count + ":", active, member);
                    text = ReplaceAdd(text, "{" + member.Name + "}", "{" + active.Count + "}", active, member);
                }
            }

            writer.Write("return string.Format(@{0}", text);

            foreach (var variable in active)
            {
                writer.Write(", " + GeneratorUtil.MemberCase(variable));
            }
            writer.WriteLine(");");
            writer.Indent -= 1;
            writer.WriteLine("}");
        }
Exemplo n.º 11
0
 static void WriteNotSoPrivateAnymoreCtor(CodeWriter writer, Message contract)
 {
     var arrays = contract.Members.Where(p => p.Type.EndsWith("[]")).ToArray();
     if (!arrays.Any())
     {
         writer.WriteLine(@"public {0} () {{}}", contract.Name);
     }
     else
     {
         writer.WriteLine(@"{0} ()
     {{", contract.Name);
         writer.Indent += 1;
         foreach (var array in arrays)
         {
             writer.WriteLine("{0} = new {1};",
                 GeneratorUtil.MemberCase(array.Name),
                 array.Type.Replace("[]", "[0]")
                 );
         }
         writer.Indent -= 1;
         writer.WriteLine("}");
     }
 }
Exemplo n.º 12
0
        void WriteMembers(Message message, CodeWriter writer)
        {
            var idx = 1;
            foreach (var member in message.Members)
            {
                var annotation = string.IsNullOrWhiteSpace(member.Annotation)
                    ? string.Empty
                    : string.Format("{0}{1}", member.Annotation, Environment.NewLine);

                var isRequired = string.Empty;
                if (annotation.IndexOf("required", StringComparison.InvariantCultureIgnoreCase) > 0 && annotation.IndexOf("RequiredIfNull", StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    isRequired = ", IsRequired=true";
                }
                writer.WriteLine(MemberTemplate, annotation, idx, member.Type, GeneratorUtil.MemberCase(member.Name), isRequired);

                idx += 1;
            }
        }
Exemplo n.º 13
0
 static void WriteSummary(CodeWriter writer, Message contract)
 {
     if (!string.IsNullOrWhiteSpace(contract.Representation) && contract.Representation.StartsWith("\""))
     {
         writer.WriteLine("/// <summary>");
         foreach (var line in contract.Representation.Trim('"').Split('\n'))
         {
             writer.WriteLine("/// " + line.Trim());
         }
         writer.WriteLine("/// </summary>");
     }
 }