CodeTranslator.Parsers.ParsedLine.GetMatchingBracketForBracketAtIndex C# (CSharp) Method

GetMatchingBracketForBracketAtIndex() public method

public GetMatchingBracketForBracketAtIndex ( int codeItemIndex ) : int
codeItemIndex int
return int
        public int GetMatchingBracketForBracketAtIndex(int codeItemIndex)
        {
            if (CodeItems[codeItemIndex].Code != "(")
            {
                throw new ArgumentException("The item at index " + codeItemIndex + " must be a '('.");
            }

            int parenCount = 0;
            for (int i = codeItemIndex; i < this.CodeItems.Count; i++)
            {
                if (CodeItems[i].Code == "(")
                {
                    parenCount++;
                }
                if (CodeItems[i].Code == ")")
                {
                    parenCount--;
                    if (parenCount == 0)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

Usage Example

        public void TestParsedLine()
        {
            var parsedLine = new ParsedLine("this.DoSomething(firstArgument, secondArgument)");

            var match = parsedLine.GetMatchingBracketForBracketAtIndex(1);

            if (match != 5)
            {
                throw new Exception("The matching closing bracket code item index should be 5, but it's " + match);
            }

            parsedLine.ConsolidateMethodContents();

            if (parsedLine.CodeItems.Count != 2)
            {
                throw new Exception("There should only be 2 code items, but there are " + parsedLine.CodeItems.Count);
            }


            parsedLine = new ParsedLine("new Vector3()");

            if (parsedLine[1].CodeType == CodeType.MethodCall)
            {
                throw new Exception("The type should be a constructor, not method call");
            }

            parsedLine = new ParsedLine("x() + y");
            if (parsedLine.CodeItems.Count != 5)
            {
                throw new Exception("There should be 5 CodeItems in the parsed line");
            }

            parsedLine = new ParsedLine("x");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 1)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("x.y.z");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 1)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("x.y.z + a.b.c");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 3)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("x()");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 1)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("x().y");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 1)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("x() + y");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 3)
            {
                throw new Exception("Error combining to expressions");
            }


            parsedLine = new ParsedLine("x(3+4) + y");
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 3)
            {
                throw new Exception("Error combining to expressions");
            }

            parsedLine = new ParsedLine("ToolEmitters.FindByName().Emit;");
            parsedLine.ConsolidateMethodContents();
            parsedLine.CombineToExpressions();
            if (parsedLine.Count != 2)
            {
                throw new Exception("Error combining to expressions");
            }
        }