Example #1
0
        void CheckAttribute(FileDiagnostics diagnostics, Token attribute, MethodAttributeType type)
        {
            if (attribute == null)
            {
                return;
            }

            var newAttribute = new MethodAttributeContext(attribute, type);

            // If the attribute already exists, syntax error.
            bool wasCopy = false;

            for (int c = 0; c < ObtainedAttributes.Count; c++)
            {
                if (ObtainedAttributes[c].Type == newAttribute.Type)
                {
                    newAttribute.Copy(diagnostics);
                    wasCopy = true;
                    break;
                }
            }

            // Add the attribute.
            ObtainedAttributes.Add(newAttribute);

            // Additonal syntax errors. Only throw if the attribute is not a copy.
            if (!wasCopy)
            {
                ValidateAttribute(diagnostics, newAttribute);
                ApplyAttribute(newAttribute);
            }
        }
        public void GetAttributes(FileDiagnostics diagnostics)
        {
            // Get the name of the rule the method will be stored in.
            string subroutineName = GetSubroutineName();

            if (subroutineName != null)
            {
                ResultAppender.SetSubroutine(subroutineName);
            }

            // context will be null if there are no attributes.
            var context = GetAttributeContext();

            if (context == null)
            {
                return;
            }

            // Initialize the ObtainedAttributes array.
            int numberOfAttributes = context.Length;

            ObtainedAttributes = new MethodAttributeContext[numberOfAttributes];

            // Loop through all attributes.
            for (int i = 0; i < numberOfAttributes; i++)
            {
                var newAttribute = new MethodAttributeContext(context[i]);
                ObtainedAttributes[i] = newAttribute;

                // If the attribute already exists, syntax error.
                bool wasCopy = false;
                for (int c = i - 1; c >= 0; c--)
                {
                    if (ObtainedAttributes[c].Type == newAttribute.Type)
                    {
                        newAttribute.Copy(diagnostics);
                        wasCopy = true;
                        break;
                    }
                }

                // Additonal syntax errors. Only throw if the attribute is not a copy.
                if (!wasCopy)
                {
                    ValidateAttribute(diagnostics, newAttribute);
                    ApplyAttribute(newAttribute);
                }
            }
        }