Beispiel #1
0
        public void SetFromContents(string contents, bool removeComments, bool trimClassContents)
        {
            if (removeComments)
            {
                contents = ParsedClass.RemoveComments(contents);
            }

            int startOfNamespace = contents.IndexOf("namespace ");

            if (startOfNamespace != -1)
            {
                int i = GetClosingBracketOfNamespace(contents);

                try
                {
                    string parsedNamespaceString = contents.Substring(startOfNamespace, i - startOfNamespace + 1);

                    ParsedNamespace parsedNamespace = new ParsedNamespace(parsedNamespaceString, removeComments, trimClassContents);

                    mNamespaces.Add(parsedNamespace);
                }
                catch
                {
                    int m = 3;
                }
            }
        }
Beispiel #2
0
        private void ParseHeader(string line, out int index)
        {
            index = 0;
            while (true)
            {
                string word = ParsedClass.GetWord(line, ref index).Trim();

                if (word == "public")
                {
                    IsPublic = true;
                }
                else if (word == "private" || word == "internal" || word == "protected")
                {
                    // toss it
                }
                else if (word == "enum")
                {
                    // toss it
                }
                else
                {
                    this.Name = word;
                    return;
                }
            }
        }
        public void Test()
        {

            ParsedClass parsedClass = new ParsedClass(TestStringClassOnly, false);
            if (parsedClass.ParsedMethods.Count < 2)
            {
                throw new Exception("ParsedMethod count is too low");
            }


            ParsedFile parsedFile = new ParsedFile();
            parsedFile.SetFromContents(TestString, false, false);

            if (parsedFile.Namespaces.Count == 0)
            {
                throw new Exception("ParsedFile namespace is 0 when it shouldn't be");
            }

            if (parsedFile.Namespaces[0].Classes.Count == 0)
            {
                throw new Exception("ParsedNamespace class count is 0 when it shouldn't be");
            }

            if (parsedFile.Namespaces[0].Classes[0].ParsedMethods.Count < 2)
            {
                throw new Exception("ParsedMethod count is too low");
            }

        }
        public ParsedClass Clone()
        {
            ParsedClass newParsedClass = (ParsedClass)MemberwiseClone();

            newParsedClass.mParsedFields               = new List <ParsedField>();
            newParsedClass.mParsedProperties           = new List <ParsedProperty>();
            newParsedClass.mParsedMethods              = new List <ParsedMethod>();
            newParsedClass.mParentClassesAndInterfaces = new List <ParsedType>();
            newParsedClass.mCurrentBlock               = new List <string>();
            newParsedClass.mParsedEnums         = new List <ParsedEnum>();
            newParsedClass.mParentParsedClasses = new List <ParsedClass>();

            newParsedClass.Namespace       = Namespace;
            newParsedClass.mName           = mName;
            newParsedClass.mGetterContents = mGetterContents;
            newParsedClass.mSetterContents = mSetterContents;
            newParsedClass.mGenericTypes   = new List <ParsedType>();

            // do nothing with mCurrentBlock
            foreach (ParsedType parsedType in mParentClassesAndInterfaces)
            {
                newParsedClass.mParentClassesAndInterfaces.Add(parsedType.Clone());
            }

            foreach (ParsedField parsedField in mParsedFields)
            {
                newParsedClass.ParsedFields.Add(parsedField.Clone());
            }

            foreach (ParsedProperty parsedProperty in mParsedProperties)
            {
                newParsedClass.ParsedProperties.Add(parsedProperty.Clone());
            }

            foreach (ParsedMethod parsedMethod in mParsedMethods)
            {
                newParsedClass.ParsedMethods.Add(parsedMethod.Clone());
            }

            foreach (ParsedEnum parsedEnum in mParsedEnums)
            {
                newParsedClass.ParsedEnums.Add(parsedEnum.Clone());
            }

            foreach (ParsedClass parsedClass in mParentParsedClasses)
            {
                newParsedClass.mParentParsedClasses.Add(parsedClass.Clone());
            }

            foreach (ParsedType parsedType in mGenericTypes)
            {
                newParsedClass.mGenericTypes.Add(parsedType.Clone());
            }

            return(newParsedClass);
        }
        public static ParsedClass FromType(Type type)
        {
            ParsedClass toReturn = new ParsedClass();

            var fieldInfos = type.GetFields();

            foreach (var field in fieldInfos)
            {
                ParsedField parsedField = ParsedField.FromFieldInfo(field);

                toReturn.ParsedFields.Add(parsedField);
            }

            var propertyInfos = type.GetProperties();

            foreach (var property in propertyInfos)
            {
                ParsedProperty parsedProperty = ParsedProperty.FromPropertyInfo(property);

                toReturn.ParsedProperties.Add(parsedProperty);
            }

            return(toReturn);
        }
Beispiel #6
0
        public static bool InheritsFrom(string fileName, string baseClass, string nameToMatch)
        {
            fileName = FileManager.Standardize(fileName);

            if (FileManager.FileExists(fileName))
            {
                ParsedFile parsedFile = new ParsedFile(fileName);

                if (parsedFile.Namespaces.Count != 0)
                {
                    for (int i = 0; i < parsedFile.Namespaces[0].Classes.Count; i++)
                    {
                        ParsedClass parsedClass = parsedFile.Namespaces[0].Classes[i];

                        if (nameToMatch != null && parsedClass.Name != nameToMatch)
                        {
                            continue;
                        }

                        foreach (ParsedType parsedType in parsedClass.ParentClassesAndInterfaces)
                        {
                            if (parsedType.ToString() == baseClass)
                            {
                                return(true);
                            }
                        }
                    }
                }
                return(false);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Looking for the Game class.  The file  " + fileName + " is part of the project but couldn't find it on disk.");
                return(false);
            }
        }
        internal void FillArgumentList(ParsedMethod parsedMethod, List <string> currentBlock, int lineIndex, bool requireTypes)
        {
            int  wordIndex                 = 0;
            int  parenthesisDeep           = 0;
            bool hasFoundClosedParenthesis = false;

            ParsedField argumentToAddTo = null;

            while (!hasFoundClosedParenthesis)
            {
                string line = currentBlock[lineIndex];

                wordIndex = 0;

                string currentType = null;

                while (true)
                {
                    string word = ParsedClass.GetWord(line, ref wordIndex);

                    if (word == "(")
                    {
                        if (argumentToAddTo != null)
                        {
                            argumentToAddTo.Name += word;
                        }

                        parenthesisDeep++;
                    }
                    else if (word == ")")
                    {
                        parenthesisDeep--;


                        if (parenthesisDeep == 0)
                        {
                            hasFoundClosedParenthesis = true;
                            break;
                        }
                        else if (argumentToAddTo != null)
                        {
                            argumentToAddTo.Name += word;
                        }
                    }
                    else if (word == "")
                    {
                        lineIndex++;
                        break;
                    }
                    else if (parenthesisDeep == 0)
                    {
                        continue;
                    }
                    else if (word == ",")
                    {
                        argumentToAddTo = null;
                        continue;
                    }
                    else if (currentType == null && requireTypes)
                    {
                        currentType = word;
                    }
                    else
                    {
                        ParsedField parsedField = new ParsedField(Scope.Public, currentType, word);
                        currentType = null;

                        parsedMethod.ArgumentList.Add(parsedField);

                        argumentToAddTo = parsedField;
                    }
                }
            }
        }
        public static void GetLineInformation(
            string line,
            out Scope scope,
            out ParsedType type,
            out string variableName,
            out bool isConst,
            out bool isVirtual,
            out bool isOverride,
            out bool isStatic,
            out bool isNew,
            out bool isAsync,
            out string valueToAssignTo
            )
        {
            int index = 0;

            scope           = Scope.Private;
            type            = null;
            variableName    = null;
            isConst         = false;
            valueToAssignTo = null;
            isVirtual       = false;
            isOverride      = false;
            isStatic        = false;
            isNew           = false;
            isAsync         = false;

            bool hasHadOpenParenthesis = false;
            bool hasHadOpenQuotes      = false;
            bool hasEqualsBeenUsed     = false;

            string currentType = "";

            while (true)
            {
                string word = ParsedClass.GetWord(line, ref index);

                const string public1 = " public ";
                //const string startWithPublic = "public ";

                if (string.IsNullOrEmpty(word))
                {
                    break;
                }
                else if (word == ";")
                {
                    continue;
                }
                else if (word == "const")
                {
                    isConst = true;
                }
                else if (word == "public")
                {
                    scope = Scope.Public;
                }
                else if (word == "private")
                {
                    scope = Scope.Private;
                }
                else if (word == "protected")
                {
                    scope = Scope.Protected;
                }
                else if (word == "internal")
                {
                    scope = Scope.Internal;
                }
                else if (word == "virtual")
                {
                    isVirtual = true;
                }
                else if (word == "override")
                {
                    isOverride = true;
                }
                else if (word == "static")
                {
                    isStatic = true;
                }
                else if (word == "new")
                {
                    isNew = true;
                }
                else if (word == "async")
                {
                    isAsync = true;
                }
                else if (type == null)
                {
                    if (word.Contains("<") && !word.Contains(">"))
                    {
                        currentType += word;
                    }
                    else if (currentType != "")
                    {
                        currentType += word;
                        if (word.Contains(">"))
                        {
                            type = new ParsedType(currentType);

                            currentType = "";
                        }
                    }
                    else
                    {
                        // check for []
                        int    tempIndex     = index;
                        string nextWord      = ParsedClass.GetWord(line, ref tempIndex);
                        string wordAfterThat = ParsedClass.GetWord(line, ref tempIndex);

                        if (nextWord == "[" && wordAfterThat == "]")
                        {
                            type  = new ParsedType(word + "[]");
                            index = tempIndex;
                        }
                        else
                        {
                            type = new ParsedType(word);
                        }
                    }
                }
                else if (!hasEqualsBeenUsed && word == "(")
                {
                    hasHadOpenParenthesis = true;
                }
                else if (variableName == null && !hasHadOpenParenthesis)
                {
                    if (word.EndsWith(";"))
                    {
                        variableName = word.Substring(0, word.Length - 1);
                    }
                    else
                    {
                        variableName = word;
                    }
                }
                else if (word == "=")
                {
                    hasEqualsBeenUsed = true;
                }
                else if (hasEqualsBeenUsed)
                {
                    if (valueToAssignTo == null)
                    {
                        valueToAssignTo = word;

                        if (valueToAssignTo.StartsWith("\"") && !hasHadOpenQuotes)
                        {
                            if (!valueToAssignTo.EndsWith("\""))
                            {
                                hasHadOpenQuotes = true;

                                int indexOfClosingQuotes = line.IndexOf("\"", index) + 1; // add 1 to capture the quote

                                string extraStuffToAdd = line.Substring(index, indexOfClosingQuotes - index);

                                valueToAssignTo += extraStuffToAdd;
                                index            = indexOfClosingQuotes;
                            }
                        }
                    }
                    else
                    {
                        valueToAssignTo += " " + word;
                    }
                }
            }
        }
Beispiel #9
0
        public ParsedLine(string lineOfCode,  ParsedClass parsedClass, ParsedClass unchangedClass,
            List<ParsedField> localVariables)
        {
            existingFields.Clear();
            existingProperties.Clear();
            unchangedFields.Clear();
            unchangedProperties.Clear();

            if (parsedClass != null)
            {
                parsedClass.FillWithThisAndInheitedFields(existingFields);
                parsedClass.FillWithThisAndInheitedProperties(existingProperties);
            }

            if (unchangedClass != null)
            {
                unchangedClass.FillWithThisAndInheitedFields(unchangedFields);
                unchangedClass.FillWithThisAndInheitedProperties(unchangedProperties);
            }

            int index = 0;

            string lastWord = "";
            string word = "";

            bool isInString = false;

            string remainderFromWord = null;

            CodeItem lastCodeItem = null;

            string wordWithoutThis = "";

            bool hadBase = false;

            int numberOfSquareBracketsDeep;

            Stack<CodeItem> bracketStack = new Stack<CodeItem>();

            char separator;

            #region Loop through all words and try to figure out what they are

            while (true)
            {
                #region Determine word, wordWithoutThis, and lastCodeItem

                if (mCodeItems.Count != 0)
                {
                    lastCodeItem = mCodeItems[mCodeItems.Count - 1];
                }

                word = ParsedClass.GetWord(lineOfCode, ref index, out separator);

                if (word.StartsWith("this."))
                {
                    wordWithoutThis = word.Substring("this.".Length);
                }
                else
                {
                    wordWithoutThis = "";
                }

                #endregion
                
                #region Perform tests that modify the CodeItem, but don't fully determine its type

                hadBase = word.StartsWith("base.");

                if (word.StartsWith("base."))
                {
                    word = word.Substring("base.".Length);
                }

                int openingBracketCount = word.CountOf('[');
                int closingBracketCount = word.CountOf(']');

                int netValue = openingBracketCount - closingBracketCount;



                #endregion


                if (string.IsNullOrEmpty(word) && index == lineOfCode.Length)
                {
                    break;
                }

 

        //todo:  Add ability to take something like (variableName and split that up into two CodeItems

                else if (word == "if" || word == "else" || word == "while" || word == "for" || word == "foreach" || 
                    word == "case")
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.ConditionalKeyword;

                    mCodeItems.Add(codeItem);
                }
                // Handle = separately because it's an assignment
                else if (word == "=" || word == "+" || word == "-")
                {
                    bool combine = lastCodeItem != null && lastCodeItem.CodeType == CodeType.Operation;

                    if(combine && (word == "-" || word == "+"))
                    {
                        if(lastCodeItem.Code == "=" || lastCodeItem.Code == ")")
                        {
                            combine = false;
                        }
                    }

                    if (combine)
                    {
                        // This is something like += or -= and we don't want those separated by a space
                        mCodeItems[mCodeItems.Count - 1].Code += word;

                        if (mCodeItems.Count - 2 >= 0)
                        {
                            mCodeItems[mCodeItems.Count - 2].IsAssigned = true;
                        }
                    }
                    else
                    {
                        CodeItem codeItem = new CodeItem();
                        codeItem.Code = word;
                        codeItem.CodeType = CodeType.Operation;

                        mCodeItems.Add(codeItem);

                        if (word == "=")
                        {
                            lastCodeItem.IsAssigned = true;
                        }
                    }

                }
                else if (
                    word == "("  || word == ")"  || word == "==" || word == ";"  || word == "}"  || word == "{" ||
                    word == "*"  || word == "/"  || word == ","  || word == "<"  || word == ">"  || word == "||" ||
                    word == "&&" || word == "|=" || word == "!=" || word == "<=" || word == ">=" || word == "&" ||
                    word == "|"  || word == "["  || word == "]"  || word == "!"  || word == "!=" )
                {



                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.Operation;

                    mCodeItems.Add(codeItem);

                    if (word == "==" || word == "!=" || word == "<=" || word == "<=")
                    {
                        lastCodeItem.IsCompared = true;
                    }
                }
                else if (word == "float" || word == "int" || word == "double" || word == "string" ||
                    word == "long" || word == "uint" || word == "byte" || word == "char")
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.Primitive;

                    mCodeItems.Add(codeItem);
                }
                else if (word == "null" || word == "return" || word == "ref" || word == "out" ||
                         word == "throw" || word == "new" || word == "try" || word == "in" ||
                         word == "const" || word == "base" || word == "is" || word == "as")
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.Keyword;

                    mCodeItems.Add(codeItem);
                }
                else if (word == "true" || word == "false" || float.TryParse(word, out throwawayString) ||
                    (word.EndsWith("f") && float.TryParse(word.Substring(0, word.Length - 1), out throwawayString)))
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.Constant;

                    mCodeItems.Add(codeItem);
                }
                else if (lastCodeItem != null && lastCodeItem.CodeType == CodeType.Keyword && lastCodeItem.Code == "new")
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    codeItem.CodeType = CodeType.Constructor;

                    mCodeItems.Add(codeItem);
                }
                //else if(FlatRedBall.Utilities.StringFunctions.

                else
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = word;
                    bool justSetIsInString = false;

                    if (codeItem.Code.StartsWith("@\"") || codeItem.Code.StartsWith("\"") || isInString)
                    {
                        codeItem.CodeType = CodeType.Constant;

                        if (!isInString)
                        {
                            justSetIsInString = true;
                        }

                        if (!word.EndsWith("\""))
                        {
                            int indexToUse = index;

                            if (index > 0 && lineOfCode[index - 1] == ' ')
                            {
                                indexToUse--;
                            }

                            int nextQuote = lineOfCode.IndexOf('"', indexToUse) + 1;

                            string fullString = lineOfCode.Substring(indexToUse, nextQuote - indexToUse);

                            codeItem.Code += fullString;

                            isInString = false;

                            index = nextQuote;
                        }
                        else
                        {
                            isInString = true;
                        }
                    }
                    else if (lastCodeItem != null)
                    {
                        if (lastCodeItem.CodeType == CodeType.Primitive)
                        {
                            codeItem.CodeType = CodeType.VariableDeclaration;
                        }
                        else if (lastCodeItem.CodeType == CodeType.Unknown)
                        {
                            lastCodeItem.CodeType = CodeType.Type;
                            codeItem.CodeType = CodeType.VariableDeclaration;

                        }
                        else if (lastCodeItem.Code == "in")
                        {
                            codeItem.CodeType = CodeType.Variable;
                        }
                        else if (lastCodeItem.Code == "new")
                        {
                            codeItem.CodeType = CodeType.Type;
                        }
                    }
                    else
                    {
                        codeItem.CodeType = CodeType.Unknown;
                    }

                    if (codeItem.Code.EndsWith("\"") && (justSetIsInString == false || codeItem.Code.Length > 1))
                    {
                        isInString = false;
                    }

                    mCodeItems.Add(codeItem);

                }

                CodeItem lastCodeItemAdded = mCodeItems[mCodeItems.Count - 1];

                if (netValue > 0)
                {
                    // push this guy on the stack
                    for (int i = 0; i < netValue; i++)
                    {
                        bracketStack.Push(lastCodeItemAdded);
                    }
                }
                else if (netValue < 0)
                {
                    // pop off the stack and link the code items to this
                    for (int i = 0; i < (-netValue); i++)
                    {
                        CodeItem itemToPairWith = bracketStack.Pop();
                        itemToPairWith.PairedSquareBracket = lastCodeItemAdded;
                        lastCodeItemAdded.PairedSquareBracket = itemToPairWith;

                    }
                }

                if (separator != ' ')
                {
                    lastCodeItemAdded.HasSpaceAfter = false;
                }
                if (hadBase)
                {
                    CodeItem codeItem = new CodeItem();
                    codeItem.Code = "base";
                    codeItem.CodeType = CodeType.Keyword;
                    codeItem.ChildCodeItem = mCodeItems[mCodeItems.Count - 1];
                    mCodeItems[mCodeItems.Count - 1].ParentCodeItem = codeItem;


                    mCodeItems.Insert(mCodeItems.Count - 1, codeItem);
                }


            }

            #endregion


            CodeItem itemBefore = null;
            CodeItem itemAfter = null;
            // Do a final pass of all words and see if there are any unknowns and try to identify them
            for (int i = 0; i < mCodeItems.Count; i++)
            {
                CodeItem codeItem = mCodeItems[i];



                if (codeItem.CodeType == CodeType.Unknown)
                {
                    #region Determine information about this CodeItem

                    if (i > 0)
                    {
                        itemBefore = mCodeItems[i - 1];
                    }
                    if (i < mCodeItems.Count - 1)
                    {
                        itemAfter = mCodeItems[i + 1];
                    }
                    else
                    {
                        itemAfter = null;
                    }


                    bool canBeVariable = itemAfter == null || itemAfter.CodeType != CodeType.Unknown;

                    wordWithoutThis = "";

                    if (codeItem.Code.StartsWith("this."))
                    {
                        wordWithoutThis = codeItem.Code.Substring("this.".Length);
                    }

                    #endregion

                    if (itemAfter != null && (itemAfter.Code == "==" || itemAfter.Code == "!="))
                    {
                        CodeItem otherSideOfOperation = mCodeItems[i + 2];

                        if (otherSideOfOperation.CodeType == CodeType.Constant || otherSideOfOperation.CodeType == CodeType.Keyword)
                        {
                            codeItem.CodeType = CodeType.Variable;
                        }
                    }
                    
                    
                    if (itemAfter != null && itemAfter.Code == "(")
                    {
                        codeItem.CodeType = CodeType.MethodCall;
                    }
                    else if (canBeVariable &&
                        (DoesListOfFieldsContainsField(existingFields, codeItem.Code) || 
                        DoesListOfFieldsContainsField(existingFields, wordWithoutThis)
                    ))
                    {
                        codeItem.CodeType = CodeType.Variable;

                    }
                    else if (canBeVariable && 
                        (DoesListOfPropertiesContainProperty(existingProperties, codeItem.Code) ||
                        DoesListOfPropertiesContainProperty(existingProperties, wordWithoutThis)))
                    {
                        codeItem.CodeType = CodeType.Variable;
                    }


                    else if (canBeVariable && 
                        (DoesListOfFieldsContainsField(unchangedFields, codeItem.Code) ||
                        DoesListOfFieldsContainsField(unchangedFields, wordWithoutThis)))
                    {
                        codeItem.CodeType = CodeType.Variable;
                    }

                    else if(canBeVariable && 
                        (DoesListOfPropertiesContainProperty(unchangedProperties, codeItem.Code) ||
                        DoesListOfPropertiesContainProperty(unchangedProperties, wordWithoutThis)))
                    {
                        codeItem.CodeType = CodeType.Variable;
                    }

                    else if (canBeVariable && localVariables != null && DoesListOfFieldsContainsField(localVariables, codeItem.Code))
                    {
                        codeItem.CodeType = CodeType.Variable;
                    }

                }
            }

        }
Beispiel #10
0
        public static bool DoesListOfPropertiesContainProperty(ParsedClass parsedClass, string variableName)
        {
            if (DoesListOfPropertiesContainProperty(parsedClass.ParsedProperties, variableName))
            {
                return true;
            }

            foreach (ParsedClass parentClass in parsedClass.ParentParsedClasses)
            {
                if (DoesListOfPropertiesContainProperty(parentClass, variableName))
                {
                    return true;
                }
            }
            return false;

        }
Beispiel #11
0
        public static ParsedClass FromType(Type type)
        {
            ParsedClass toReturn = new ParsedClass();

            var fieldInfos = type.GetFields();
            foreach (var field in fieldInfos)
            {
                ParsedField parsedField = ParsedField.FromFieldInfo(field);

                toReturn.ParsedFields.Add(parsedField);
            }

            var propertyInfos = type.GetProperties();
            foreach (var property in propertyInfos)
            {
                ParsedProperty parsedProperty = ParsedProperty.FromPropertyInfo(property);

                toReturn.ParsedProperties.Add(parsedProperty);
            }

            return toReturn;
        }
Beispiel #12
0
        internal void Parse(string entireString, ref int startOfEnum)
        {
            int startBeforeParsing = startOfEnum;

            string line = ParsedClass.GetLine(entireString, ref startOfEnum);

            ParseHeader(line, out startOfEnum);

            startOfEnum += startBeforeParsing;

            int    bracketsDeep = 0;
            string untrimmed;

            while (true)
            {
                if (entireString.Contains("public  enum"))
                {
                    int m = 3;
                }

                int indexBeforeParsing = startOfEnum;
                untrimmed = ParsedClass.GetLine(entireString, ref startOfEnum);
                line      = untrimmed.Trim();

                if (line.StartsWith("["))
                {
                    continue;
                }
                else if (line.StartsWith("{"))
                {
                    bracketsDeep++;
                    startOfEnum = indexBeforeParsing + 1 + untrimmed.IndexOf('{');
                }
                else if (line == "")
                {
                    // do nothing, keep going
                }
                else if (line == "}" || line == "};" || line.StartsWith("}")) // the }; handles more cases.  Maybe we should be more robust for single-line enums
                {
                    bracketsDeep--;

                    if (bracketsDeep == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (line.EndsWith(","))
                    {
                        line = line.Substring(0, line.Length - 1);
                    }

                    if (line.Contains("="))
                    {
                        // it's got assignment
                        mHasAssignment = true;
                    }

                    if (line.Contains(","))
                    {
                        string[] values = line.Split(',');

                        foreach (string value in values)
                        {
                            if (value.Contains("}"))
                            {
                                mValues.Add(value.Substring(0, value.IndexOf("}")).Trim());
                            }
                            else
                            {
                                mValues.Add(value.Trim());
                            }
                        }

                        if (line.EndsWith("}") || line.EndsWith(";"))
                        {
                            bracketsDeep--;

                            if (bracketsDeep == 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        mValues.Add(line);
                    }
                }
            }
        }
Beispiel #13
0
        public ParsedNamespace(string namespaceContents, bool removeComments, bool trimParsedClassContents)
        {
            int indexIntoFile = 0;

            if (removeComments)
            {
                namespaceContents = ParsedClass.RemoveComments(namespaceContents);
            }

            namespaceContents = ParsedClass.RemovePreprocessoredOutCode(namespaceContents);


            #region Get the namespace name

            Name = FlatRedBall.Utilities.StringFunctions.GetWordAfter("namespace ", namespaceContents);


            #endregion


            while (true)
            {


                int startOfEnum = namespaceContents.IndexOf("enum ", indexIntoFile + 1);

                int startOfClass = namespaceContents.IndexOf("class ", indexIntoFile + 1);
                int startOfStruct = namespaceContents.IndexOf("struct ", indexIntoFile + 1);
                if (startOfStruct != -1 && startOfStruct < startOfClass || startOfClass == -1)
                {
                    startOfClass = startOfStruct;
                }


                int startOfInterface = namespaceContents.IndexOf("interface ", indexIntoFile + 1);

                if (startOfEnum == -1 && startOfClass == -1 && startOfInterface == -1)
                {
                    break;
                }

                if (startOfInterface != -1 && (startOfClass == -1 || startOfInterface < startOfClass))
                {
                    startOfClass = startOfInterface;
                }

                #region Get all enums


                if (startOfEnum != -1 && 
                    (startOfClass == -1 || startOfEnum < startOfClass))
                {

                    ParsedEnum parsedEnum = new ParsedEnum();

                    int startOfLine = 1 + namespaceContents.LastIndexOf('\n', startOfEnum - 1, startOfEnum);


                    parsedEnum.Parse(namespaceContents, ref startOfLine);
                    
                    startOfEnum = namespaceContents.IndexOf("enum ", startOfLine);

                    parsedEnum.Namespace = this.Name;
                    mEnums.Add(parsedEnum);
                    indexIntoFile = startOfLine;

                }

                #endregion

                #region Get all classes



                else if( startOfClass != -1)
                {
                    int i = GetEndOfClassIndex(namespaceContents, startOfClass);

                    indexIntoFile = i;

                    string substring = namespaceContents.Substring(startOfClass, i - startOfClass + 1);

                    ParsedClass parsedClass = new ParsedClass(substring, trimParsedClassContents);

                    parsedClass.Namespace = this.Name;
                    mClasses.Add(parsedClass);
                }
                #endregion

            }
		}
        public void TestCodeParser()
        {
            var parsedClass = new ParsedClass(mClassContents, false);

            Assert.AreEqual(parsedClass.ParsedMethods.Count, 1);

            var parsedMethod = parsedClass.ParsedMethods[0];

            var contents = parsedMethod.MethodContents;

            Assert.True(contents.Contains("this.HealthStatBar.InterpolateBetween(StatBar.VariableState.Empty, StatBar.VariableState.Full, 0.5f);"), 
                "The parsed method contents don't contain a call to InterpolateBetween - there is an error in parsing the method contents");
        }
Beispiel #15
0
        public ParsedNamespace(string namespaceContents, bool removeComments, bool trimParsedClassContents)
        {
            int indexIntoFile = 0;

            if (removeComments)
            {
                namespaceContents = ParsedClass.RemoveComments(namespaceContents);
            }

            namespaceContents = ParsedClass.RemovePreprocessoredOutCode(namespaceContents);


            #region Get the namespace name

            Name = FlatRedBall.Utilities.StringFunctions.GetWordAfter("namespace ", namespaceContents);


            #endregion


            while (true)
            {
                int startOfEnum = namespaceContents.IndexOf("enum ", indexIntoFile + 1);

                int startOfClass  = namespaceContents.IndexOf("class ", indexIntoFile + 1);
                int startOfStruct = namespaceContents.IndexOf("struct ", indexIntoFile + 1);
                if (startOfStruct != -1 && startOfStruct < startOfClass || startOfClass == -1)
                {
                    startOfClass = startOfStruct;
                }


                int startOfInterface = namespaceContents.IndexOf("interface ", indexIntoFile + 1);

                if (startOfEnum == -1 && startOfClass == -1 && startOfInterface == -1)
                {
                    break;
                }

                if (startOfInterface != -1 && (startOfClass == -1 || startOfInterface < startOfClass))
                {
                    startOfClass = startOfInterface;
                }

                #region Get all enums


                if (startOfEnum != -1 &&
                    (startOfClass == -1 || startOfEnum < startOfClass))
                {
                    ParsedEnum parsedEnum = new ParsedEnum();

                    int startOfLine = 1 + namespaceContents.LastIndexOf('\n', startOfEnum - 1, startOfEnum);


                    parsedEnum.Parse(namespaceContents, ref startOfLine);

                    startOfEnum = namespaceContents.IndexOf("enum ", startOfLine);

                    parsedEnum.Namespace = this.Name;
                    mEnums.Add(parsedEnum);
                    indexIntoFile = startOfLine;
                }

                #endregion

                #region Get all classes



                else if (startOfClass != -1)
                {
                    int i = GetEndOfClassIndex(namespaceContents, startOfClass);

                    indexIntoFile = i;

                    string substring = namespaceContents.Substring(startOfClass, i - startOfClass + 1);

                    ParsedClass parsedClass = new ParsedClass(substring, trimParsedClassContents);

                    parsedClass.Namespace = this.Name;
                    mClasses.Add(parsedClass);
                }
                #endregion
            }
        }