I am looking to create a method that will start a reader(likely StreamReader) on a c# .cs source file, and it will be able to parse through and locate all instances of "if" statements, sorting them by standalone if, or if with an else. It will then add it to its respective list(one for standalone if, one for if-else), with the line number as the unique identifier.
It would be quite easy to simply parse out all instances of "if(", "if (", and then add the line number to a list, but differentiating between an if with an else, and standalone is proving to be very difficult.
All that I can think of is to use the leading a trailing braces as points of reference. When an "if" statement is found, try and locate the trailing "}", then if the following reader.nextLine contains an else, it can be added to the else list. However, what makes this difficult are nested ifs that would require some kind of recursion or seperate background worker, as well as the possibility of there not being "{}" at all, since it is not syntactically required in C#.
In comments, @Filburt suggested
Have you considered using Roslyn IfStatement instead of going thru all the pain of parsing text?
This was the correct answer.