public DynamicPincherProtocol(Protocol protocol)
        {
            _declarations = new Dictionary<string, Declaration>();

            foreach (Declaration declaration in protocol.Declarations)
            {
                _declarations[declaration.Identifier] = declaration;
            }
        }
        void CheckStructures(Protocol protocol)
        {
            foreach (Declaration declaration in protocol.Declarations)
            {
                if (!(declaration is Structure)) continue;

                Structure structure = declaration as Structure;

                foreach (StructureMember member in structure.Members)
                {
                    if (member.Modifier == FieldModifier.None && structure.StructureKind != StructureKind.Choice)
                    {
                        throw new SemanticException(string.Format(
                            "The member \"{0}\" in the structure or message \"{1}\" does not have the required \"required\" or \"optional\" " +
                            " qualifier.", member.Identifier, declaration.Identifier));
                    }

                    if (member.Modifier != FieldModifier.None && structure.StructureKind == StructureKind.Choice)
                    {
                        throw new SemanticException(string.Format(
                            "The member \"{0}\" in the choice \"{1}\" must not have a \"required\" or \"optional\" " +
                            " qualifier.", member.Identifier, declaration.Identifier));
                    }

                    if (member.Modifier == FieldModifier.Required && member.Versioning.RemovedInVersion.HasValue)
                    {
                        throw new SemanticException(string.Format(
                            "The member \"{0}\" in the structure or message \"{1}\" is marked as removed, but can not be " +
                            "removed because it is a \"required\" field.",
                            member.Identifier, declaration.Identifier));
                    }
                }
            }
        }
        void ResolveMembers(Protocol protocol, Structure structure)
        {
            Set<NamespaceName> references = new Set<NamespaceName>();

            foreach (StructureMember member in structure.Members)
            {
                NamespaceName name = member.FieldType;
                NamespaceName qualifiedName;

                if (name.IsQualified)
                {
                    qualifiedName = name;
                }
                else
                {
                    qualifiedName = new NamespaceName(protocol.Name, name.UnqualifiedName);
                }

                if (references.Contains(qualifiedName))
                {
                    throw new SemanticException(string.Format(
                        "The member \"{0}\" in the choice \"{1}\" in the protocol \"{2}\"; " +
                        "choices can only contain structures and messages.",
                        member.Identifier, structure.Identifier, protocol.Name));
                }

                if (_declarations.ContainsKey(qualifiedName))
                {
                    member.FieldTypeReference = new DeclarationTypeReference(_declarations[qualifiedName]);
                }
                else if (!name.IsQualified && _intrinsics.ContainsKey(name.UnqualifiedName))
                {
                    member.FieldTypeReference = _intrinsics[name.UnqualifiedName];

                    if (structure.StructureKind == StructureKind.Choice)
                    {
                        throw new SemanticException(string.Format(
                            "The member \"{0}\" in the choice \"{1}\" in the protocol \"{2}\"; " +
                            "choices can only contain structures and messages.",
                            member.Identifier, structure.Identifier, protocol.Name));

                    }
                }
                else
                {
                    throw new SemanticException(string.Format(
                        "The type \"{0}\" in \"{1}\" in the protocol \"{2}\" could not be resolved.",
                        name, member.Identifier, protocol.Name));
                }

                if (member.FieldContainer != null)
                {
                    if (!member.FieldContainer.IsQualified && _containers.ContainsKey(member.FieldContainer.UnqualifiedName))
                    {
                        member.FieldContainerReference = _containers[member.FieldContainer.UnqualifiedName];
                    }
                    else
                    {
                        throw new SemanticException(string.Format(
                            "The container \"{0}\" in \"{1}\" in the protocol \"{2}\" is not a valid container type.",
                            member.FieldContainer, member.Identifier, protocol.Name));
                    }
                }
            }
        }
        void CheckProtocolVersions(Protocol protocol)
        {
            // Check that the protocol version makes sense:
            int latestReferencedVersion = 0;

            foreach (Declaration declaration in protocol.Declarations)
            {
                if (declaration.Versioning.HasZeroVersionNumber)
                {
                    throw new InvalidOperationException(string.Format(
                        "The declaration for \"{0}\" has a zero version number, which is an invalid value.",
                        declaration.Identifier));
                }

                if (declaration.Versioning.WasRemovedBeforeAdded)
                {
                    throw new InvalidOperationException(string.Format(
                        "The declaration for \"{0}\" has a removal version number that is equal to or earlier than the " +
                        "initial add version number.", declaration.Identifier));
                }

                if (declaration.Versioning.LatestReferencedVersion > protocol.Version)
                {
                    throw new SemanticException(string.Format("The declaration for \"{0}\" references " +
                        "version {1}, but the protocol version is only version {2}.",
                        declaration.Identifier, declaration.Versioning.LatestReferencedVersion, protocol.Version));
                }

                latestReferencedVersion = Math.Max(latestReferencedVersion, declaration.Versioning.LatestReferencedVersion);

                foreach (DeclarationMember member in declaration.MemberBases)
                {
                    if (declaration.Versioning.HasZeroVersionNumber)
                    {
                        throw new InvalidOperationException(string.Format(
                            "The member \"{0}\" in declaration \"{1}\" has a zero version number, which is an invalid value.",
                            member.Identifier, declaration.Identifier));
                    }

                    if (declaration.Versioning.WasRemovedBeforeAdded)
                    {
                        throw new InvalidOperationException(string.Format(
                            "The member \"{0}\" in declaration \"{1}\" has a removal version number that is equal to or earlier than the " +
                            "initial add version number.",
                            member.Identifier, declaration.Identifier));
                    }

                    if (member.Versioning.LatestReferencedVersion > protocol.Version)
                    {
                        throw new SemanticException(string.Format("The member \"{0}\" within \"{1}\" references " +
                            "version {2}, but the protocol version is only version {3}.", member.Identifier,
                            declaration.Identifier, member.Versioning.LatestReferencedVersion, protocol.Version));
                    }

                    latestReferencedVersion = Math.Max(latestReferencedVersion, member.Versioning.LatestReferencedVersion);
                }
            }

            if (latestReferencedVersion < protocol.Version)
            {
                throw new SemanticException(string.Format(
                    "The most recent version \"{0}\" referenced in the protocol \"{1}\" " +
                    "is earlier than the version of the protocol (\"{1}\").",
                    latestReferencedVersion, protocol.Name, protocol.Version));
            }
        }
        void CheckNames(Protocol protocol)
        {
            Set<string> usedDeclarationNames = new Set<string>();

            foreach (Declaration declaration in protocol.Declarations)
            {
                if (usedDeclarationNames.Contains(declaration.Identifier))
                {
                    throw new SemanticException(string.Format("The name \"{0}\" is used in more than one declaration.",
                        declaration.Identifier));
                }

                usedDeclarationNames.UnionUpdate(declaration.Identifier);

                Set<string> usedMemberNames = new Set<string>();

                foreach (DeclarationMember member in declaration.MemberBases)
                {
                    if (usedMemberNames.Contains(member.Identifier))
                    {
                        throw new SemanticException(string.Format(
                            "The name \"{0}\" in the declaration \"{1}\" is used in more " +
                            " than once within the declaration.", member.Identifier, declaration.Identifier));
                    }
                }
            }
        }
Exemple #6
0
 public virtual object CreateProtocolImplementationHelper(Protocol protocol, PropertyDictionary options)
 {
     return null;
 }
 public override object CreateProtocolImplementationHelper(Protocol protocol, PropertyDictionary options)
 {
     return new JavaProtocol(protocol, options);
 }
 public PythonProtocol(Protocol protocol, PropertyDictionary options)
 {
     _protocol = protocol;
     _options = options;
 }