示例#1
0
        /// <summary>
        /// Main entry point. Convert a CodeGeneratorRequest to a list of EsiTypes.
        /// </summary>
        /// <param name="cgr"></param>
        /// <returns></returns>
        protected EsiSystem Read(CodeGeneratorRequest.READER cgr)
        {
            ulong CapnpSchemaID = cgr.RequestedFiles.FirstOrDefault().Id;

            // First pass: get all the filenames
            var IDtoFile = new Dictionary <ulong, string>();

            cgr.RequestedFiles.Iterate(file => IDtoFile[file.Id] = file.Filename);

            // Second pass: get all the node names
            cgr.Nodes
            .SelectMany(fileNode => fileNode.NestedNodes.Select(nested => (nested, fileNode)))
            .ForEach(n =>
                     IDtoNames[n.nested.Id] =
                         new EsiCapnpLocation {
                Id       = n.nested.Id,
                NodeName = n.nested.Name,
                File     = IDtoFile.GetValueOrDefault(n.fileNode.Id)
            });

            // Third pass: get references to each node
            cgr.Nodes.ForEach(n => {
                var loc         = GetLocation(n.Id);
                loc.Node        = n;
                loc.DisplayName = n.DisplayName;
            });

            // Fourth pass: Do the actual conversion
            var esiObjects = cgr.Nodes.Select(
                node => ConvertNode(node) switch {
                _ when(ESIAnnotations.Contains(node.Id)) => null,
                EsiReferenceType stRef => stRef.Reference,
                EsiObject o => o,
                null => null,
            }).Where(t => t != null).ToList();
        private bool TypeWriteTraversePre(EsiObject obj, ref ushort codeOrder)
        {
            switch (obj)
            {
            case EsiStruct st:
                if (indent == 0)
                {
                    WL($"struct {st.Name} {{");
                }
                else
                {
                    Writer.WriteLine($"group {{");
                }
                indent++;
                break;

            case EsiReferenceType refTy when refTy.Reference is EsiStruct stRef:
                Writer.WriteLine($"{stRef.Name};");
                break;

            case EsiStruct.StructField f when f.Type is EsiStruct || f.Type is EsiArray:
                W($"{f.Name} :");
                break;

            case EsiStruct.StructField f:
                W($"{f.Name} @{codeOrder++} :");
                break;

            case EsiArray array:
                WriteArray(array, ref codeOrder);
                break;

            case EsiContainerType containerType when containerType.Inner is EsiNamedType:
                containerType = containerType.WithInner(new EsiReferenceType(containerType.Inner));
                Writer.Write($"{GetSimpleKeyword(containerType)} {GetAnnotation(containerType)} $ESI.inline()");
                Writer.WriteLine(";");
                break;

            case EsiContainerType containerType when containerType.Inner is EsiReferenceType refType:
                if (refType is EsiNamedType named)
                {
                    Writer.Write($"{named.Name} $ESI.inline()");
                    Writer.WriteLine(";");
                }
                else
                {
                    C.Log.Error("References to unnamed types are not supported by CapnProto schemas!");
                }
                break;

            case EsiValueType valueType:
                Writer.Write($"{GetSimpleKeyword(valueType)} {GetAnnotation(valueType)}");
                Writer.WriteLine(";");
                break;
            }
            return(!(obj is EsiContainerType));
        }
 private void TypeWriteTraversePost(EsiObject obj)
 {
     switch (obj)
     {
     case EsiStruct st:
         indent--;
         WL("}");
         break;
     }
 }