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: 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++) { 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); } }
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)); } }
void WriteAssignments(Message message, CodeWriter writer) { foreach (var member in message.Members) { writer.WriteLine("{0} = {1};", GeneratorUtil.MemberCase(member.Name), GeneratorUtil.ParameterCase(member.Name)); } }
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; } }
static void WritePrivateCtor(CodeWriter writer, Message contract) { var arrays = contract.Members.Where(p => p.Type.EndsWith("[]")).ToArray(); if (!arrays.Any()) { writer.WriteLine(@"{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("}"); } }