Hand parsing gets messy, even for relatively trivial cases.
Have you considered a Lexer/Parser, such as Flex/Bison? I highly recommend Antlr - and get AntlrWorks.
A picture is worth a thousand words, so this will tell you why - http://www.antlr.org/works/screenshots/editor.jpg