private void SetPropertyMethodName(ILUnit ilUnit, string ilCode, int startIndex, int offset, string endPhrase)
 {
     var name = GetMethodPropertyName(ilCode, endPhrase, startIndex);
     ilUnit.NameStartIndex = offset + name.Item1;
     ilUnit.Name = name.Item2;
 }
        private void SetEventName(ILUnit ilUnit, string ilCode, int startIndex, int offset)
        {
            int newLineIndex = ilCode.IndexOf(Environment.NewLine, startIndex);
            var nameStartIndex = LastIndexOf(ilCode, ' ', startIndex, newLineIndex);

            ilUnit.NameStartIndex = offset + nameStartIndex;
            ilUnit.Name = ilCode.Substring(nameStartIndex, newLineIndex - nameStartIndex);
        }
        private void SetFieldName(ILUnit ilUnit, string ilCode, int startIndex, int offset)
        {
            int newLineIndex = ilCode.IndexOf(Environment.NewLine, startIndex);
            var line = ilCode.Substring(startIndex, newLineIndex - startIndex);
            var name = ExtractFieldName(line);

            ilUnit.NameStartIndex = offset + startIndex + name.Item1;
            ilUnit.Name = name.Item2;
        }
        private void ParseClasses(Assembly assembly, IClassContainer classContainer, string ilCode, int offset = 0)
        {
            bool newLine = false;
            bool comment = false;
            var length = ilCode.Length;
            var fms = new ILFMS();
            ILClass currentClass = null;

            for (int i = 0; i < length; i++)
            {
                fms.GoToNextState(ilCode[i]);
                if (fms.CurrentState.IsFinal)
                {
                    var ilUnit = new ILUnit() { ParentAssembly = assembly, ParentClass = currentClass };

                    if (fms.CurrentState.StateId == ILFMS.StateIdentifier.Class)
                    {
                        int newLineIndex = ilCode.IndexOf(Environment.NewLine, i);
                        var name = ExtractClassName(ilCode, i, newLineIndex);
                        currentClass = new ILClass()
                        {
                            StartIndex = offset + i - 5,
                            NameStartIndex = offset + name.Item1,
                            Name = name.Item2,
                            ParentAssembly = assembly,
                            ParentClass = currentClass
                        };

                        // append class to the parent
                        if (currentClass.ParentClass != null)
                        {
                            currentClass.ParentClass.Classes.AddLast(currentClass);
                        }
                        else
                        {
                            classContainer.Classes.AddLast(currentClass);
                        }
                    }
                    else if (fms.CurrentState.StateId == ILFMS.StateIdentifier.Method)
                    {
                        SetPropertyMethodName(ilUnit, ilCode, i, offset, MethodNameEndToken);
                        currentClass.Methods.AddLast(ilUnit);
                    }
                    else if (fms.CurrentState.StateId == ILFMS.StateIdentifier.Property)
                    {
                        SetPropertyMethodName(ilUnit, ilCode, i, offset, PropertyNameEndToken);
                        currentClass.Properties.AddLast(ilUnit);
                    }
                    else if (fms.CurrentState.StateId == ILFMS.StateIdentifier.Field)
                    {
                        SetFieldName(ilUnit, ilCode, i, offset);
                        currentClass.Fields.AddLast(ilUnit);
                    }
                    else if (fms.CurrentState.StateId == ILFMS.StateIdentifier.Event)
                    {
                        SetEventName(ilUnit, ilCode, i, offset);
                        currentClass.Events.AddLast(ilUnit);
                    }

                    fms.Restart();
                }
                else if (i + 1 < length && ilCode[i] == '/' && ilCode[i + 1] == '/') //to skip comments next to custom attributes
                {
                    comment = true;
                }
                else if (comment && ilCode[i] == '\n')
                {
                    comment = false;
                }
                else if (!comment && currentClass != null)
                {
                    // look for class end
                    if (ilCode[i] == '\n')
                    {
                        newLine = true;
                    }
                    else if (ilCode[i] == '{')
                    {
                        currentClass.BracketsCounter++;
                        newLine = false;
                    }
                    else if (ilCode[i] == '}')
                    {
                        if (--currentClass.BracketsCounter == 0 && newLine) //closing bracket should be in another line then the opening one
                        {
                            currentClass.EndIndex = offset + i + 1;
                            currentClass = currentClass.ParentClass;
                        }
                    }
                }
            }
        }