This has probably been done before, but I couldn't find it with Google.
I have a script that takes a highlighted Excel formula and explodes it across multiple lines to show the nesting parentheses/formulas and to make the various arguments for a formula clear.
Regular formula (or "De-Analyzed"):
Exploded formula (or "Analyzed"):
I use this in conjunction with highlighting rules inspired by http://msmvps.com/blogs/xldynamic/archive/2010/09/22/formulas-made-easy.aspx and http://dailydoseofexcel.com/archives/2010/09/29/formula-editor-in-notepad/
My code makes use of a state machine that I made up on the spot. I'm sure that there are better ways to write a state machine in python, but this seemed like the obvious way to me (based on my experience with the J language.
Oh man, as soon as I posted this, I realized that I left out support for references to Table cells, such as TablePeriods[[#Headers],[YearMonth]]. I'll have to add that soon.
I just took care of the square brackets. I believe that states 3-5 handle square-brackets depths pretty well. Square brackets inside quotes will be ignored. Should be all good on that front.