Beispiel #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);
            }
        }
Beispiel #2
0
        private void ApplyAttribute(MethodAttributeContext newAttribute)
        {
            // Apply the attribute.
            switch (newAttribute.Type)
            {
            // Accessors
            case MethodAttributeType.Public: ResultAppender.SetAccessLevel(AccessLevel.Public); break;

            case MethodAttributeType.Protected: ResultAppender.SetAccessLevel(AccessLevel.Protected); break;

            case MethodAttributeType.Private: ResultAppender.SetAccessLevel(AccessLevel.Private); break;

            // Apply static
            case MethodAttributeType.Static: ResultAppender.SetStatic(); break;

            // Apply virtual
            case MethodAttributeType.Virtual: ResultAppender.SetVirtual(); break;

            // Apply override
            case MethodAttributeType.Override: ResultAppender.SetOverride(); break;

            // Apply Recursive
            case MethodAttributeType.Recursive: ResultAppender.SetRecursive(); break;

            // Apply Variables

            default: throw new NotImplementedException();
            }
        }
        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);
                }
            }
        }
        private void ApplyAttribute(MethodAttributeContext newAttribute)
        {
            // Apply the attribute.
            switch (newAttribute.Type)
            {
            // Apply accessor
            case MethodAttributeType.Accessor: ResultAppender.SetAccessLevel(newAttribute.AttributeContext.accessor().GetAccessLevel()); break;

            // Apply static
            case MethodAttributeType.Static: ResultAppender.SetStatic(); break;

            // Apply virtual
            case MethodAttributeType.Virtual: ResultAppender.SetVirtual(); break;

            // Apply override
            case MethodAttributeType.Override: ResultAppender.SetOverride(); break;

            // Apply Recursive
            case MethodAttributeType.Recursive: ResultAppender.SetRecursive(); break;
            }
        }
Beispiel #5
0
        private void ValidateAttribute(FileDiagnostics diagnostics, MethodAttributeContext newAttribute)
        {
            // The attribute is not allowed.
            if (DisallowAttributes().Contains(newAttribute.Type))
            {
                diagnostics.Error("The '" + newAttribute.Type.ToString().ToLower() + "' attribute is not allowed.", newAttribute.Range);
            }
            else
            {
                // Virtual attribute on a static method (static attribute was first.)
                if (ResultAppender.IsStatic() && newAttribute.Type == MethodAttributeType.Virtual)
                {
                    diagnostics.Error("Static methods cannot be virtual.", newAttribute.Range);
                }

                // Static attribute on a virtual method (virtual attribute was first.)
                if (ResultAppender.IsVirtual() && newAttribute.Type == MethodAttributeType.Static)
                {
                    diagnostics.Error("Virtual methods cannot be static.", newAttribute.Range);
                }
            }
        }