Example #1
0
 public void AddExtend(TypeName target, FieldNode field)
 {
     if (!Extends.ContainsKey(target))
     {
         Extends[target] = new ExtendNode(target);
     }
     Extends[target].Fields.Add(field);
 }
Example #2
0
        public override AbstractNode VisitExtend([NotNull] GiraphParser.ExtendContext context)
        {
            ExtendNode ENode = new ExtendNode(context.Start.Line, context.Start.Column);

            ENode.ExtensionName = context.variable(0).GetText();
            if (context.variable().Length == 2)
            {
                ENode.ExtensionShortName = context.variable(1).GetText();
            }
            ENode.IsCollection   = context.allTypeWithColl().COLLECTION() != null;
            ENode.ExtendWithType = context.allTypeWithColl().allType().GetText();
            ENode.ClassToExtend  = context.objects().GetText();
            return(ENode);
        }
 public override void Visit(ExtendNode node)
 {
     Debug.Print("ExtendNode");
     ProgramCode.Append("EXTEND ");
     ProgramCode.Append($"{node.ClassToExtend} {node.ExtendWithType} ");
     ProgramCode.Append($"'{node.ExtensionName}'");
     if (node.ExtensionShortName != null)
     {
         ProgramCode.Append($":'{node.ExtensionShortName}'");
     }
     if (node.ExtensionDefaultValue != null)
     {
         ProgramCode.Append($"= {node.ExtensionDefaultValue}");
     }
     ProgramCode.Append(");\n");
 }
        public override void Visit(ExtendNode node)
        {
            SymbolTable.SetCurrentNode(node);

            string longAttributeName = node.ExtensionName;

            // If there is a shortname AND a long name, create 2 entries in the class table
            if (node.ExtensionShortName != null && node.ExtensionShortName.Length > 0)
            {
                string shortAttributeName = node.ExtensionShortName;
                SymbolTable.ExtendClass(node.ExtendWithType_enum, longAttributeName, shortAttributeName, node.ClassToExtend_enum, node.IsCollection);
            }
            else
            {
                // If only a long name is used, ignore the shortAttribute
                SymbolTable.ExtendClass(node.ExtendWithType_enum, longAttributeName, node.ClassToExtend_enum, node.IsCollection);
            }
        }
Example #5
0
 public ExtendTable(ExtendNode node, Program program) : base(node, program)
 {
 }
Example #6
0
 public override void Visit(ExtendNode node)
 {
     _symbolTable.SetCurrentNode(node);
     VisitChildren(node);
 }
Example #7
0
    public void ProcessTypes(IEnumerable <TypeDefinition> types, string typesMapFile)
    {
        if (types.Any(x => x.Name == "IProtoBuf" || x.Name == "ServiceDescriptor"))
        {
            Console.WriteLine("detected SilentOrbit protos");
            processor = new SilentOrbitTypeProcessor();
        }
        else
        {
            // We could detect based on subclassing GeneratedMessage*, but no
            // point unless we have more than 2 types of protogens.
            Console.WriteLine("assuming Google protos");
            processor = new GoogleTypeProcessor();
        }

        foreach (var type in types)
        {
            processor.Process(type);
        }
        processor.Complete();

        // A map from typename to message:
        var allMessages = new Dictionary <string, MessageNode>();

        // Add child nodes to their parents:
        foreach (var nodeList in processor.PackageNodes.Values)
        {
            var children = new List <ILanguageNode>();
            var messages = nodeList.Where(x => x is MessageNode).Select(x => x as MessageNode);
            foreach (var message in messages)
            {
                allMessages.Add(message.Name.Text, message);
            }
            var enums = nodeList.Where(x => x is EnumNode).Select(x => x as EnumNode);
            foreach (var node in messages.Where(n => n.Name.Name.Contains(".")))
            {
                var parentName = node.Name.Name;
                parentName = parentName.Substring(0, parentName.LastIndexOf('.'));
                var parent = messages.First(m => m.Name.Name == parentName);
                parent.Messages.Add(node);
                children.Add(node);
            }
            foreach (var node in enums.Where(n => n.Name.Name.Contains(".")))
            {
                var parentName = node.Name.Name;
                parentName = parentName.Substring(0, parentName.LastIndexOf('.'));
                var parent = messages.First(m => m.Name.Name == parentName);
                parent.Enums.Add(node);
                children.Add(node);
            }
            foreach (var node in children)
            {
                nodeList.Remove(node);
            }
        }

        // Move extensions to their sources:
        var blacklistedExtensionSources = new[] {
            // These types aren't allowed to have extensions, because they were
            // made by an intern or something:
            ".PegasusShared.ScenarioDbRecord"
        };

        foreach (var nodeList in processor.PackageNodes.Values)
        {
            var messages = nodeList
                           .Where(x => x is MessageNode)
                           .Select(x => x as MessageNode)
                           .Where(x => !blacklistedExtensionSources.Contains(x.Name.Text));
            foreach (var message in messages)
            {
                var extensions = message.Fields
                                 .Where(x => x.Label != FieldLabel.Required &&
                                        x.Tag >= 100 &&
                                        !blacklistedExtensionSources.Contains(x.TypeName.Text))
                                 .ToList();
                foreach (var extField in extensions)
                {
                    message.Fields.Remove(extField);
                    message.AcceptsExtensions = true;
                    message.ExtendLowerBound  = Math.Min(message.ExtendLowerBound, extField.Tag);
                    message.ExtendUpperBound  = Math.Max(message.ExtendUpperBound, extField.Tag);

                    var target = message.Name;
                    var source = extField.TypeName;
                    // Extensions which are primitive types
                    if (String.IsNullOrEmpty(source.Package))
                    {
                        message.AddExtend(target, extField);
                    }
                    // Extensions which are messages in other files
                    else
                    {
                        var sourceNode = allMessages[source.Text];
                        sourceNode.AddExtend(target, extField);
                    }
                }
            }
        }

        // A map from type name to its file
        var typesMap = new Dictionary <string, string>();

        using (var typesMapReader = new StreamReader(typesMapFile)) {
            while (!typesMapReader.EndOfStream)
            {
                var line = typesMapReader.ReadLine().Trim();
                if (line.Length == 0)
                {
                    continue;
                }
                if (line[0] == '#')
                {
                    continue;
                }
                var parts = line.Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                typesMap[parts[1]] = parts[0];
            }
        }

        foreach (var nodeList in processor.PackageNodes.Values)
        {
            string   fileName = null;
            FileNode fileNode = null;
            foreach (var node in nodeList)
            {
                var type = default(TypeName);
                if (node is MessageNode)
                {
                    type = (node as MessageNode).Name;
                }
                if (node is ServiceNode)
                {
                    type = (node as ServiceNode).Name;
                }
                if (node is EnumNode)
                {
                    type = (node as EnumNode).Name;
                }
                var typeName = type.Text;
                if (typesMap.ContainsKey(typeName))
                {
                    fileName = typesMap[typeName];
                    if (fileNodes.ContainsKey(fileName))
                    {
                        fileNode = fileNodes[fileName];
                    }
                    else
                    {
                        fileNode            = new FileNode(fileName, type.Package);
                        fileNodes[fileName] = fileNode;
                    }
                }
                else if (fileNode == null)
                {
                    throw new Exception(String.Format(
                                            "Couldn't find the first type of a package in the type map: {0}", typeName));
                }
                fileNode.Types.Add(node);
                messageFile[type.Text] = fileName;
            }
        }

        // Define "method_id" extension for identifying RPC methods.
        var methodIdExtension = new ExtendNode(new TypeName(
                                                   "google.protobuf", "MethodOptions"));

        methodIdExtension.Fields.Add(new FieldNode(
                                         "method_id", FieldLabel.Optional, FieldType.UInt32, 50000));
        fileNodes["bnet/rpc"].Types.Add(methodIdExtension);
    }
Example #8
0
 public abstract void Visit(ExtendNode node);