NArrange.VisualBasic.VBParser.TryParseElement C# (CSharp) Method

TryParseElement() private method

Tries to parse a code element.
private TryParseElement ( ICodeElement parentElement, StringBuilder elementBuilder, ReadOnlyCollection comments, ReadOnlyCollection attributes ) : ICodeElement
parentElement ICodeElement The parent element.
elementBuilder StringBuilder The element builder.
comments ReadOnlyCollection The comments.
attributes ReadOnlyCollection The attributes.
return ICodeElement
        private ICodeElement TryParseElement(
			ICodeElement parentElement,
			StringBuilder elementBuilder,
			ReadOnlyCollection<ICommentElement> comments,
			ReadOnlyCollection<AttributeElement> attributes)
        {
            CodeElement codeElement = null;

            string processedElementText =
                elementBuilder.ToString().Trim();

            switch (VBKeyword.Normalize(processedElementText))
            {
                case VBKeyword.Namespace:
                    codeElement = ParseNamespace();
                    break;

                case VBKeyword.Imports:
                    codeElement = ParseImport();
                    break;
            }

            if (codeElement == null)
            {
                string[] words = processedElementText.TrimEnd(
                    VBSymbol.Assignment,
                    VBSymbol.BeginParameterList).Split(
                        WhiteSpaceCharacters,
                        StringSplitOptions.RemoveEmptyEntries);

                if (words.Length > 0)
                {
                    string normalizedKeyWord = VBKeyword.Normalize(words[0]);

                    if (words.Length > 1 ||
                        normalizedKeyWord == VBKeyword.Class ||
                        normalizedKeyWord == VBKeyword.Structure ||
                        normalizedKeyWord == VBKeyword.Interface ||
                        normalizedKeyWord == VBKeyword.Enumeration ||
                        normalizedKeyWord == VBKeyword.Module ||
                        normalizedKeyWord == VBKeyword.Sub ||
                        normalizedKeyWord == VBKeyword.Function ||
                        normalizedKeyWord == VBKeyword.Property ||
                        normalizedKeyWord == VBKeyword.Delegate ||
                        normalizedKeyWord == VBKeyword.Event)
                    {
                        StringCollection wordList = new StringCollection();
                        wordList.AddRange(words);

                        StringCollection normalizedWordList = new StringCollection();
                        foreach (string word in wordList)
                        {
                            normalizedWordList.Add(VBKeyword.Normalize(word));
                        }

                        string name = string.Empty;
                        ElementType elementType;
                        CodeAccess access = CodeAccess.None;
                        MemberModifiers memberAttributes = MemberModifiers.None;
                        TypeElementType? typeElementType = null;

                        bool isAssignment = processedElementText[processedElementText.Length - 1] == VBSymbol.Assignment;
                        bool isField = normalizedWordList[normalizedWordList.Count - 1] == VBKeyword.As ||
                                       isAssignment;
                        if (isField)
                        {
                            elementType = ElementType.Field;
                        }
                        else
                        {
                            GetElementType(normalizedWordList, out elementType, out typeElementType);
                        }

                        if (elementType == ElementType.Method ||
                            elementType == ElementType.Property ||
                            elementType == ElementType.Event ||
                            elementType == ElementType.Delegate ||
                            elementType == ElementType.Type ||
                            elementType == ElementType.Field)
                        {
                            access = GetAccess(normalizedWordList);
                            memberAttributes = GetMemberAttributes(normalizedWordList);
                        }

                        TypeElement parentTypeElement = parentElement as TypeElement;
                        bool inInterface = parentTypeElement != null &&
                                           parentTypeElement.Type == TypeElementType.Interface;

                        switch (elementType)
                        {
                            case ElementType.Type:
                                TypeModifiers typeAttributes = (TypeModifiers) memberAttributes;
                                if (normalizedWordList.Contains(VBKeyword.Partial))
                                {
                                    typeAttributes |= TypeModifiers.Partial;
                                }

                                codeElement = ParseType(access, typeAttributes, typeElementType.Value);
                                break;

                            case ElementType.Event:
                                codeElement = ParseEvent(
                                    access, memberAttributes, normalizedWordList.Contains(VBKeyword.Custom));
                                break;

                            case ElementType.Field:
                                FieldElement fieldElement = ParseField(wordList, access, memberAttributes, isAssignment);
                                fieldElement[VBExtendedProperties.WithEvents] =
                                    normalizedWordList.Contains(VBKeyword.WithEvents);
                                fieldElement[VBExtendedProperties.Dim] =
                                    normalizedWordList.Contains(VBKeyword.Dim);
                                codeElement = fieldElement;
                                break;

                            case ElementType.Property:
                                string modifyAccess = null;
                                if (normalizedWordList.Contains(VBKeyword.ReadOnly))
                                {
                                    modifyAccess = VBKeyword.ReadOnly;
                                }
                                else if (normalizedWordList.Contains(VBKeyword.ReadWrite))
                                {
                                    modifyAccess = VBKeyword.ReadWrite;
                                }
                                else if (normalizedWordList.Contains(VBKeyword.WriteOnly))
                                {
                                    modifyAccess = VBKeyword.WriteOnly;
                                }

                                bool isDefault = normalizedWordList.Contains(VBKeyword.Default);
                                codeElement = ParseProperty(access, memberAttributes, isDefault, modifyAccess, inInterface);
                                break;

                            case ElementType.Delegate:
                                codeElement = ParseDelegate(access, memberAttributes);
                                break;

                            case ElementType.Method:
                                bool isOperator = normalizedWordList.Contains(VBKeyword.Operator);
                                OperatorType operatorType = OperatorType.None;
                                if (isOperator)
                                {
                                    operatorType = GetOperatorType(wordList);
                                }

                                bool external = false;
                                string externalModifier = null;
                                if (normalizedWordList.Contains(VBKeyword.Declare))
                                {
                                    external = true;
                                    if (normalizedWordList.Contains(VBKeyword.Ansi))
                                    {
                                        externalModifier = VBKeyword.Ansi;
                                    }
                                    else if (normalizedWordList.Contains(VBKeyword.Unicode))
                                    {
                                        externalModifier = VBKeyword.Unicode;
                                    }
                                    else if (normalizedWordList.Contains(VBKeyword.Auto))
                                    {
                                        externalModifier = VBKeyword.Auto;
                                    }
                                }

                                //
                                // Method
                                //
                                MethodElement methodElement = ParseMethod(
                                    access,
                                    memberAttributes,
                                    normalizedWordList.Contains(VBKeyword.Function),
                                    false,
                                    isOperator,
                                    operatorType,
                                    inInterface,
                                    external,
                                    externalModifier);
                                methodElement[VBExtendedProperties.Overloads] =
                                    normalizedWordList.Contains(VBKeyword.Overloads);
                                if (VBKeyword.Normalize(methodElement.Name) == VBKeyword.New)
                                {
                                    codeElement = CreateConstructor(methodElement);
                                }
                                else
                                {
                                    codeElement = methodElement;
                                }
                                break;
                        }

                        if (codeElement != null)
                        {
                            codeElement[VBExtendedProperties.Overloads] =
                                normalizedWordList.Contains(VBKeyword.Overloads);
                        }
                    }
                }
            }

            if (codeElement != null)
            {
                ApplyCommentsAndAttributes(codeElement, comments, attributes);
            }

            return codeElement;
        }